Skip to content

Commit

Permalink
Self-contained SM package (#1419)
Browse files Browse the repository at this point in the history
* Self-contained SM package
Just like what was done for cloud, slo and ml packages
This moves the mapping of resources and client validation to the SM package. This will make it easier to support TF code generation + upgrade to the new TF plugin framework

* Remove unused function
  • Loading branch information
julienduchesne authored Mar 14, 2024
1 parent 29cab68 commit 272030b
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 143 deletions.
16 changes: 2 additions & 14 deletions internal/provider/legacy_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,6 @@ func Provider(version string) *schema.Provider {
"grafana_user": grafana.ResourceUser(),
})

// Resources that require the Synthetic Monitoring client to exist.
smClientResources = addResourcesMetadataValidation(smClientPresent, map[string]*schema.Resource{
"grafana_synthetic_monitoring_check": syntheticmonitoring.ResourceCheck(),
"grafana_synthetic_monitoring_probe": syntheticmonitoring.ResourceProbe(),
})

// Datasources that require the Grafana client to exist.
grafanaClientDatasources = addCreateReadResourcesMetadataValidation(
readGrafanaClientValidation,
Expand All @@ -96,12 +90,6 @@ func Provider(version string) *schema.Provider {
"grafana_organization": grafana.DatasourceOrganization(),
"grafana_organization_preferences": grafana.DatasourceOrganizationPreferences(),
})

// Datasources that require the Synthetic Monitoring client to exist.
smClientDatasources = addResourcesMetadataValidation(smClientPresent, map[string]*schema.Resource{
"grafana_synthetic_monitoring_probe": syntheticmonitoring.DataSourceProbe(),
"grafana_synthetic_monitoring_probes": syntheticmonitoring.DataSourceProbes(),
})
)

p := &schema.Provider{
Expand Down Expand Up @@ -225,7 +213,7 @@ func Provider(version string) *schema.Provider {
grafanaClientResources,
machinelearning.ResourcesMap,
slo.ResourcesMap,
smClientResources,
syntheticmonitoring.ResourcesMap,
oncall.ResourcesMap,
cloud.ResourcesMap,
),
Expand All @@ -234,7 +222,7 @@ func Provider(version string) *schema.Provider {
grafanaClientDatasources,
machinelearning.DatasourcesMap,
slo.DatasourcesMap,
smClientDatasources,
syntheticmonitoring.DatasourcesMap,
oncall.DatasourcesMap,
cloud.DatasourcesMap,
),
Expand Down
11 changes: 0 additions & 11 deletions internal/provider/legacy_provider_validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,6 @@ func createGrafanaClientValidation(resourceName string, d *schema.ResourceData,
return nil
}

func smClientPresent(resourceName string, d *schema.ResourceData, m interface{}) error {
if m.(*common.Client).SMAPI == nil {
return fmt.Errorf("the Synthetic Monitoring client is required for `%s`. Set the sm_access_token provider attribute", resourceName)
}
return nil
}

func addResourcesMetadataValidation(validateFunc metadataValidation, resources map[string]*schema.Resource) map[string]*schema.Resource {
return addCreateReadResourcesMetadataValidation(validateFunc, validateFunc, resources)
}

func addCreateReadResourcesMetadataValidation(readValidateFunc, createValidateFunc metadataValidation, resources map[string]*schema.Resource) map[string]*schema.Resource {
for name, r := range resources {
name := name
Expand Down
10 changes: 5 additions & 5 deletions internal/resources/syntheticmonitoring/data_source_probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
smapi "github.com/grafana/synthetic-monitoring-api-go-client"
"github.com/grafana/terraform-provider-grafana/internal/common"
)

func DataSourceProbe() *schema.Resource {
func dataSourceProbe() *schema.Resource {
return &schema.Resource{
Description: "Data source for retrieving a single probe by name.",
ReadContext: DataSourceProbeRead,
Schema: common.CloneResourceSchemaForDatasource(ResourceProbe(), map[string]*schema.Schema{
ReadContext: withClient[schema.ReadContextFunc](dataSourceProbeRead),
Schema: common.CloneResourceSchemaForDatasource(resourceProbe(), map[string]*schema.Schema{
"name": {
Description: "Name of the probe.",
Type: schema.TypeString,
Expand All @@ -26,8 +27,7 @@ func DataSourceProbe() *schema.Resource {
}
}

func DataSourceProbeRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*common.Client).SMAPI
func dataSourceProbeRead(ctx context.Context, d *schema.ResourceData, c *smapi.Client) diag.Diagnostics {
var diags diag.Diagnostics
prbs, err := c.ListProbes(ctx)
if err != nil {
Expand Down
9 changes: 4 additions & 5 deletions internal/resources/syntheticmonitoring/data_source_probes.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package syntheticmonitoring
import (
"context"

"github.com/grafana/terraform-provider-grafana/internal/common"
smapi "github.com/grafana/synthetic-monitoring-api-go-client"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DataSourceProbes() *schema.Resource {
func dataSourceProbes() *schema.Resource {
return &schema.Resource{
Description: "Data source for retrieving all probes.",
ReadContext: DataSourceProbesRead,
ReadContext: withClient[schema.ReadContextFunc](dataSourceProbesRead),
Schema: map[string]*schema.Schema{
"filter_deprecated": {
Type: schema.TypeBool,
Expand All @@ -31,8 +31,7 @@ func DataSourceProbes() *schema.Resource {
}
}

func DataSourceProbesRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*common.Client).SMAPI
func dataSourceProbesRead(ctx context.Context, d *schema.ResourceData, c *smapi.Client) diag.Diagnostics {
var diags diag.Diagnostics
prbs, err := c.ListProbes(ctx)
if err != nil {
Expand Down
31 changes: 14 additions & 17 deletions internal/resources/syntheticmonitoring/resource_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
smapi "github.com/grafana/synthetic-monitoring-api-go-client"
"github.com/grafana/terraform-provider-grafana/internal/common"
)

Expand Down Expand Up @@ -624,7 +625,7 @@ var (
}
)

func ResourceCheck() *schema.Resource {
func resourceCheck() *schema.Resource {
return &schema.Resource{

Description: `
Expand All @@ -637,14 +638,14 @@ multiple checks for a single endpoint to check different capabilities.
* [Official documentation](https://grafana.com/docs/grafana-cloud/monitor-public-endpoints/checks/)
`,

CreateContext: ResourceCheckCreate,
ReadContext: ResourceCheckRead,
UpdateContext: ResourceCheckUpdate,
DeleteContext: ResourceCheckDelete,
CreateContext: withClient[schema.CreateContextFunc](resourceCheckCreate),
ReadContext: withClient[schema.ReadContextFunc](resourceCheckRead),
UpdateContext: withClient[schema.UpdateContextFunc](resourceCheckUpdate),
DeleteContext: withClient[schema.DeleteContextFunc](resourceCheckDelete),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
CustomizeDiff: ResourceCheckCustomizeDiff,
CustomizeDiff: resourceCheckCustomizeDiff,

Schema: map[string]*schema.Schema{
"id": {
Expand Down Expand Up @@ -742,8 +743,7 @@ multiple checks for a single endpoint to check different capabilities.
}
}

func ResourceCheckCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*common.Client).SMAPI
func resourceCheckCreate(ctx context.Context, d *schema.ResourceData, c *smapi.Client) diag.Diagnostics {
chk, err := makeCheck(d)
if err != nil {
return diag.FromErr(err)
Expand All @@ -754,11 +754,10 @@ func ResourceCheckCreate(ctx context.Context, d *schema.ResourceData, meta inter
}
d.SetId(strconv.FormatInt(res.Id, 10))
d.Set("tenant_id", res.TenantId)
return ResourceCheckRead(ctx, d, meta)
return resourceCheckRead(ctx, d, c)
}

func ResourceCheckRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*common.Client).SMAPI
func resourceCheckRead(ctx context.Context, d *schema.ResourceData, c *smapi.Client) diag.Diagnostics {
id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -1058,8 +1057,7 @@ func ResourceCheckRead(ctx context.Context, d *schema.ResourceData, meta interfa
return nil
}

func ResourceCheckUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*common.Client).SMAPI
func resourceCheckUpdate(ctx context.Context, d *schema.ResourceData, c *smapi.Client) diag.Diagnostics {
chk, err := makeCheck(d)
if err != nil {
return diag.FromErr(err)
Expand All @@ -1068,11 +1066,10 @@ func ResourceCheckUpdate(ctx context.Context, d *schema.ResourceData, meta inter
if err != nil {
return diag.FromErr(err)
}
return ResourceCheckRead(ctx, d, meta)
return resourceCheckRead(ctx, d, c)
}

func ResourceCheckDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*common.Client).SMAPI
func resourceCheckDelete(ctx context.Context, d *schema.ResourceData, c *smapi.Client) diag.Diagnostics {
var diags diag.Diagnostics
id, _ := strconv.ParseInt(d.Id(), 10, 64)
err := c.DeleteCheck(ctx, id)
Expand Down Expand Up @@ -1477,7 +1474,7 @@ func makeCheckSettings(settings map[string]interface{}) (sm.CheckSettings, error
// Ideally, we'd use `ExactlyOneOf` here but it doesn't support TypeSet.
// Also, TypeSet doesn't support ValidateFunc.
// To maintain backwards compatibility, we do a custom validation in the CustomizeDiff function.
func ResourceCheckCustomizeDiff(ctx context.Context, diff *schema.ResourceDiff, meta interface{}) error {
func resourceCheckCustomizeDiff(ctx context.Context, diff *schema.ResourceDiff, meta interface{}) error {
settingsList := diff.Get("settings").(*schema.Set).List()
if len(settingsList) == 0 {
return fmt.Errorf("at least one check setting must be defined")
Expand Down
27 changes: 12 additions & 15 deletions internal/resources/syntheticmonitoring/resource_probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
smapi "github.com/grafana/synthetic-monitoring-api-go-client"
"github.com/grafana/terraform-provider-grafana/internal/common"
)

func ResourceProbe() *schema.Resource {
func resourceProbe() *schema.Resource {
return &schema.Resource{

Description: `
Expand All @@ -28,10 +29,10 @@ Grafana Synthetic Monitoring Agent.
* [Official documentation](https://grafana.com/docs/grafana-cloud/monitor-public-endpoints/private-probes/)
`,

CreateContext: ResourceProbeCreate,
ReadContext: ResourceProbeRead,
UpdateContext: ResourceProbeUpdate,
DeleteContext: ResourceProbeDelete,
CreateContext: withClient[schema.CreateContextFunc](resourceProbeCreate),
ReadContext: withClient[schema.ReadContextFunc](resourceProbeRead),
UpdateContext: withClient[schema.UpdateContextFunc](resourceProbeUpdate),
DeleteContext: withClient[schema.DeleteContextFunc](resourceProbeDelete),
Importer: &schema.ResourceImporter{
StateContext: ImportProbeStateWithToken,
},
Expand Down Expand Up @@ -103,8 +104,7 @@ Grafana Synthetic Monitoring Agent.
}
}

func ResourceProbeCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*common.Client).SMAPI
func resourceProbeCreate(ctx context.Context, d *schema.ResourceData, c *smapi.Client) diag.Diagnostics {
p := makeProbe(d)
res, token, err := c.AddProbe(ctx, *p)
if err != nil {
Expand All @@ -113,11 +113,10 @@ func ResourceProbeCreate(ctx context.Context, d *schema.ResourceData, meta inter
d.SetId(strconv.FormatInt(res.Id, 10))
d.Set("tenant_id", res.TenantId)
d.Set("auth_token", base64.StdEncoding.EncodeToString(token))
return ResourceProbeRead(ctx, d, meta)
return resourceProbeRead(ctx, d, c)
}

func ResourceProbeRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*common.Client).SMAPI
func resourceProbeRead(ctx context.Context, d *schema.ResourceData, c *smapi.Client) diag.Diagnostics {
id, err := strconv.ParseInt(d.Id(), 10, 64)
if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -151,18 +150,16 @@ func ResourceProbeRead(ctx context.Context, d *schema.ResourceData, meta interfa
return nil
}

func ResourceProbeUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*common.Client).SMAPI
func resourceProbeUpdate(ctx context.Context, d *schema.ResourceData, c *smapi.Client) diag.Diagnostics {
p := makeProbe(d)
_, err := c.UpdateProbe(ctx, *p)
if err != nil {
return diag.FromErr(err)
}
return ResourceProbeRead(ctx, d, meta)
return resourceProbeRead(ctx, d, c)
}

func ResourceProbeDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*common.Client).SMAPI
func resourceProbeDelete(ctx context.Context, d *schema.ResourceData, c *smapi.Client) diag.Diagnostics {
id, _ := strconv.ParseInt(d.Id(), 10, 64)

// Remove the probe from any checks that use it.
Expand Down

This file was deleted.

Loading

0 comments on commit 272030b

Please sign in to comment.