-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate: Moving detector to new definiton (#532)
* Adding current work on detector * Adding further tests * Added create tests * Finishing up tests * Fixing up detector defintition
- Loading branch information
1 parent
1b7f771
commit f121683
Showing
8 changed files
with
1,277 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
// Copyright Splunk, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package detector | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/signalfx/signalfx-go/detector" | ||
|
||
pmeta "github.com/splunk-terraform/terraform-provider-signalfx/internal/providermeta" | ||
tfext "github.com/splunk-terraform/terraform-provider-signalfx/internal/tfextension" | ||
) | ||
|
||
const ( | ||
ResourceName = "signalfx_detector" | ||
AppPath = "/detector/v2" | ||
) | ||
|
||
func NewResource() *schema.Resource { | ||
return &schema.Resource{ | ||
SchemaFunc: newSchema, | ||
CreateContext: resourceCreate, | ||
ReadContext: resourceRead, | ||
UpdateContext: resourceUpdate, | ||
DeleteContext: resourceDelete, | ||
Importer: &schema.ResourceImporter{ | ||
StateContext: schema.ImportStatePassthroughContext, | ||
}, | ||
StateUpgraders: []schema.StateUpgrader{ | ||
{Type: v0state().CoreConfigSchema().ImpliedType(), Upgrade: v0stateMigration, Version: 0}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceCreate(ctx context.Context, data *schema.ResourceData, meta any) (issues diag.Diagnostics) { | ||
client, err := pmeta.LoadClient(ctx, meta) | ||
if err != nil { | ||
return tfext.AsErrorDiagnostics(err) | ||
} | ||
dt, err := decodeTerraform(data) | ||
if err != nil { | ||
return tfext.AsErrorDiagnostics(err) | ||
} | ||
tflog.Debug(ctx, "Creating new detector", tfext.NewLogFields().JSON("detector", dt)) | ||
|
||
resp, err := client.CreateDetector(ctx, &detector.CreateUpdateDetectorRequest{ | ||
Name: dt.Name, | ||
AuthorizedWriters: dt.AuthorizedWriters, | ||
Description: dt.Description, | ||
TimeZone: dt.TimeZone, | ||
MaxDelay: dt.MaxDelay, | ||
MinDelay: dt.MinDelay, | ||
ProgramText: dt.ProgramText, | ||
Rules: dt.Rules, | ||
Tags: dt.Tags, | ||
Teams: dt.Teams, | ||
VisualizationOptions: dt.VisualizationOptions, | ||
ParentDetectorId: dt.ParentDetectorId, | ||
DetectorOrigin: dt.DetectorOrigin, | ||
}) | ||
if err != nil { | ||
return tfext.AsErrorDiagnostics(err) | ||
} | ||
|
||
issues = tfext.AppendDiagnostics(issues, | ||
tfext.AsErrorDiagnostics( | ||
data.Set("url", | ||
pmeta.LoadApplicationURL(ctx, meta, AppPath, resp.Id, "edit"), | ||
), | ||
)..., | ||
) | ||
|
||
return tfext.AppendDiagnostics( | ||
issues, | ||
tfext.AsErrorDiagnostics(encodeTerraform(resp, data))..., | ||
) | ||
} | ||
|
||
func resourceRead(ctx context.Context, data *schema.ResourceData, meta any) (issues diag.Diagnostics) { | ||
client, err := pmeta.LoadClient(ctx, meta) | ||
if err != nil { | ||
return tfext.AsErrorDiagnostics(err) | ||
} | ||
|
||
dt, err := client.GetDetector(ctx, data.Id()) | ||
if err != nil { | ||
return tfext.AsErrorDiagnostics(err) | ||
} | ||
|
||
tflog.Debug(ctx, "Read detector details", tfext.NewLogFields().JSON("detector", dt)) | ||
|
||
if dt.OverMTSLimit { | ||
issues = tfext.AppendDiagnostics(issues, tfext.AsWarnDiagnostics(fmt.Errorf("detector is over mts limit"))...) | ||
} | ||
|
||
return tfext.AppendDiagnostics( | ||
issues, | ||
tfext.AsErrorDiagnostics(encodeTerraform(dt, data))..., | ||
) | ||
} | ||
|
||
func resourceUpdate(ctx context.Context, data *schema.ResourceData, meta any) (issues diag.Diagnostics) { | ||
client, err := pmeta.LoadClient(ctx, meta) | ||
if err != nil { | ||
return tfext.AsErrorDiagnostics(err) | ||
} | ||
dt, err := decodeTerraform(data) | ||
if err != nil { | ||
return tfext.AsErrorDiagnostics(err) | ||
} | ||
tflog.Debug(ctx, "Updating detector", tfext.NewLogFields(). | ||
JSON("detector", dt). | ||
Field("id", data.Id()), | ||
) | ||
|
||
resp, err := client.UpdateDetector(ctx, data.Id(), &detector.CreateUpdateDetectorRequest{ | ||
Name: dt.Name, | ||
AuthorizedWriters: dt.AuthorizedWriters, | ||
Description: dt.Description, | ||
TimeZone: dt.TimeZone, | ||
MaxDelay: dt.MaxDelay, | ||
MinDelay: dt.MinDelay, | ||
ProgramText: dt.ProgramText, | ||
Rules: dt.Rules, | ||
Tags: dt.Tags, | ||
Teams: dt.Teams, | ||
VisualizationOptions: dt.VisualizationOptions, | ||
ParentDetectorId: dt.ParentDetectorId, | ||
DetectorOrigin: dt.DetectorOrigin, | ||
}) | ||
if err != nil { | ||
return tfext.AsErrorDiagnostics(err) | ||
} | ||
|
||
issues = tfext.AppendDiagnostics(issues, | ||
tfext.AsErrorDiagnostics( | ||
data.Set("url", pmeta.LoadApplicationURL(ctx, meta, AppPath, resp.Id, "edit")), | ||
)..., | ||
) | ||
|
||
return tfext.AppendDiagnostics( | ||
issues, | ||
tfext.AsErrorDiagnostics(encodeTerraform(resp, data))..., | ||
) | ||
} | ||
|
||
func resourceDelete(ctx context.Context, data *schema.ResourceData, meta any) diag.Diagnostics { | ||
client, err := pmeta.LoadClient(ctx, meta) | ||
if err != nil { | ||
return tfext.AsErrorDiagnostics(err) | ||
} | ||
return tfext.AsErrorDiagnostics(client.DeleteDetector(ctx, data.Id())) | ||
} |
Oops, something went wrong.