Skip to content

Commit

Permalink
Self-contained SLO package
Browse files Browse the repository at this point in the history
Just like #1393

- Makes the resource functions private (exposed via a single map attribute) rather than each resource individually
- Puts the client validation in the SLO package
  • Loading branch information
julienduchesne committed Mar 3, 2024
1 parent 226d3f5 commit 03a6644
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 28 deletions.
8 changes: 2 additions & 6 deletions internal/provider/legacy_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ func Provider(version string) *schema.Provider {
"grafana_machine_learning_job": machinelearning.ResourceJob(),
"grafana_machine_learning_holiday": machinelearning.ResourceHoliday(),
"grafana_machine_learning_outlier_detector": machinelearning.ResourceOutlierDetector(),

// SLO
"grafana_slo": slo.ResourceSlo(),
})

// Resources that require the Synthetic Monitoring client to exist.
Expand Down Expand Up @@ -127,9 +124,6 @@ func Provider(version string) *schema.Provider {
"grafana_team": grafana.DatasourceTeam(),
"grafana_organization": grafana.DatasourceOrganization(),
"grafana_organization_preferences": grafana.DatasourceOrganizationPreferences(),

// SLO
"grafana_slos": slo.DatasourceSlo(),
})

// Datasources that require the Synthetic Monitoring client to exist.
Expand Down Expand Up @@ -277,13 +271,15 @@ func Provider(version string) *schema.Provider {

ResourcesMap: mergeResourceMaps(
grafanaClientResources,
slo.ResourcesMap,
smClientResources,
onCallClientResources,
cloudClientResources,
),

DataSourcesMap: mergeResourceMaps(
grafanaClientDatasources,
slo.DatasourcesMap,
smClientDatasources,
onCallClientDatasources,
cloudClientDatasources,
Expand Down
9 changes: 4 additions & 5 deletions internal/resources/slo/data_source_slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func DatasourceSlo() *schema.Resource {
func datasourceSlo() *schema.Resource {
return &schema.Resource{
Description: `
Datasource for retrieving all SLOs.
Expand All @@ -18,14 +18,14 @@ Datasource for retrieving all SLOs.
* [API documentation](https://grafana.com/docs/grafana-cloud/alerting-and-irm/slo/api/)
* [Additional Information On Alerting Rule Annotations and Labels](https://prometheus.io/docs/prometheus/latest/configuration/alerting_rules/#templating/)
`,
ReadContext: datasourceSloRead,
ReadContext: withClient[schema.ReadContextFunc](datasourceSloRead),
Schema: map[string]*schema.Schema{
"slos": {
Type: schema.TypeList,
Computed: true,
Description: `Returns a list of all SLOs"`,
Elem: &schema.Resource{
Schema: common.CloneResourceSchemaForDatasource(ResourceSlo(), map[string]*schema.Schema{
Schema: common.CloneResourceSchemaForDatasource(resourceSlo(), map[string]*schema.Schema{
"uuid": {
Type: schema.TypeString,
Description: `A unique, random identifier. This value will also be the name of the resource stored in the API server. This value is read-only.`,
Expand All @@ -40,10 +40,9 @@ Datasource for retrieving all SLOs.

// Function sends a GET request to the SLO API Endpoint which returns a list of all SLOs
// Maps the API Response body to the Terraform Schema and displays as a READ in the terminal
func datasourceSloRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
func datasourceSloRead(ctx context.Context, d *schema.ResourceData, client *slo.APIClient) diag.Diagnostics {
var diags diag.Diagnostics

client := m.(*common.Client).SLOClient
req := client.DefaultAPI.V1SloGet(ctx)
apiSlos, _, err := req.Execute()
if err != nil {
Expand Down
28 changes: 11 additions & 17 deletions internal/resources/slo/resource_slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
QueryTypeThreshold string = "threshold"
)

func ResourceSlo() *schema.Resource {
func resourceSlo() *schema.Resource {
return &schema.Resource{
Description: `
Resource manages Grafana SLOs.
Expand All @@ -29,10 +29,10 @@ Resource manages Grafana SLOs.
* [API documentation](https://grafana.com/docs/grafana-cloud/alerting-and-irm/slo/api/)
* [Additional Information On Alerting Rule Annotations and Labels](https://prometheus.io/docs/prometheus/latest/configuration/alerting_rules/#templating/)
`,
CreateContext: resourceSloCreate,
ReadContext: resourceSloRead,
UpdateContext: resourceSloUpdate,
DeleteContext: resourceSloDelete,
CreateContext: withClient[schema.CreateContextFunc](resourceSloCreate),
ReadContext: withClient[schema.ReadContextFunc](resourceSloRead),
UpdateContext: withClient[schema.UpdateContextFunc](resourceSloUpdate),
DeleteContext: withClient[schema.DeleteContextFunc](resourceSloDelete),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Expand Down Expand Up @@ -234,7 +234,7 @@ var keyvalueSchema = &schema.Resource{
},
}

func resourceSloCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
func resourceSloCreate(ctx context.Context, d *schema.ResourceData, client *slo.APIClient) diag.Diagnostics {
var diags diag.Diagnostics

sloModel, err := packSloResource(d)
Expand All @@ -247,7 +247,6 @@ func resourceSloCreate(ctx context.Context, d *schema.ResourceData, m interface{
return diags
}

client := m.(*common.Client).SLOClient
req := client.DefaultAPI.V1SloPost(ctx).Slo(sloModel)
response, _, err := req.Execute()

Expand All @@ -256,18 +255,16 @@ func resourceSloCreate(ctx context.Context, d *schema.ResourceData, m interface{
}

d.SetId(response.Uuid)
resourceSloRead(ctx, d, m)

return resourceSloRead(ctx, d, m)
return resourceSloRead(ctx, d, client)
}

// resourceSloRead - sends a GET Request to the single SLO Endpoint
func resourceSloRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
func resourceSloRead(ctx context.Context, d *schema.ResourceData, client *slo.APIClient) diag.Diagnostics {
var diags diag.Diagnostics

sloID := d.Id()

client := m.(*common.Client).SLOClient
req := client.DefaultAPI.V1SloIdGet(ctx, sloID)
slo, _, err := req.Execute()

Expand All @@ -280,7 +277,7 @@ func resourceSloRead(ctx context.Context, d *schema.ResourceData, m interface{})
return diags
}

func resourceSloUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
func resourceSloUpdate(ctx context.Context, d *schema.ResourceData, client *slo.APIClient) diag.Diagnostics {
var diags diag.Diagnostics
sloID := d.Id()

Expand All @@ -295,21 +292,18 @@ func resourceSloUpdate(ctx context.Context, d *schema.ResourceData, m interface{
return diags
}

client := m.(*common.Client).SLOClient

req := client.DefaultAPI.V1SloIdPut(ctx, sloID).Slo(slo)
if _, err := req.Execute(); err != nil {
return apiError("Unable to Update SLO - API", err)
}
}

return resourceSloRead(ctx, d, m)
return resourceSloRead(ctx, d, client)
}

func resourceSloDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
func resourceSloDelete(ctx context.Context, d *schema.ResourceData, client *slo.APIClient) diag.Diagnostics {
sloID := d.Id()

client := m.(*common.Client).SLOClient
req := client.DefaultAPI.V1SloIdDelete(ctx, sloID)
_, err := req.Execute()

Expand Down
30 changes: 30 additions & 0 deletions internal/resources/slo/resources.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package slo

import (
"context"

slo "github.com/grafana/slo-openapi-client/go"
"github.com/grafana/terraform-provider-grafana/internal/common"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

type crudWithClientFunc func(ctx context.Context, d *schema.ResourceData, client *slo.APIClient) diag.Diagnostics

func withClient[T schema.CreateContextFunc | schema.UpdateContextFunc | schema.ReadContextFunc | schema.DeleteContextFunc](f crudWithClientFunc) T {
return func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*common.Client).SLOClient
if client == nil {
return diag.Errorf("the SLO API client is required for this resource. Set the url and auth provider attributes")
}
return f(ctx, d, client)
}
}

var DatasourcesMap = map[string]*schema.Resource{
"grafana_slos": datasourceSlo(),
}

var ResourcesMap = map[string]*schema.Resource{
"grafana_slo": resourceSlo(),
}

0 comments on commit 03a6644

Please sign in to comment.