Skip to content

Commit d821ba2

Browse files
Self-contained SLO package (#1396)
* Self-contained SLO package 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 * Format
1 parent 2689148 commit d821ba2

File tree

4 files changed

+47
-28
lines changed

4 files changed

+47
-28
lines changed

internal/provider/legacy_provider.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ func Provider(version string) *schema.Provider {
6969
"grafana_service_account_permission": grafana.ResourceServiceAccountPermission(),
7070
"grafana_sso_settings": grafana.ResourceSSOSettings(),
7171
"grafana_user": grafana.ResourceUser(),
72-
73-
// SLO
74-
"grafana_slo": slo.ResourceSlo(),
7572
})
7673

7774
// Resources that require the Synthetic Monitoring client to exist.
@@ -109,9 +106,6 @@ func Provider(version string) *schema.Provider {
109106
"grafana_team": grafana.DatasourceTeam(),
110107
"grafana_organization": grafana.DatasourceOrganization(),
111108
"grafana_organization_preferences": grafana.DatasourceOrganizationPreferences(),
112-
113-
// SLO
114-
"grafana_slos": slo.DatasourceSlo(),
115109
})
116110

117111
// Datasources that require the Synthetic Monitoring client to exist.
@@ -253,6 +247,7 @@ func Provider(version string) *schema.Provider {
253247
ResourcesMap: mergeResourceMaps(
254248
grafanaClientResources,
255249
machinelearning.ResourcesMap,
250+
slo.ResourcesMap,
256251
smClientResources,
257252
onCallClientResources,
258253
cloud.ResourcesMap,
@@ -261,6 +256,7 @@ func Provider(version string) *schema.Provider {
261256
DataSourcesMap: mergeResourceMaps(
262257
grafanaClientDatasources,
263258
machinelearning.DatasourcesMap,
259+
slo.DatasourcesMap,
264260
smClientDatasources,
265261
onCallClientDatasources,
266262
cloud.DatasourcesMap,

internal/resources/slo/data_source_slo.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1010
)
1111

12-
func DatasourceSlo() *schema.Resource {
12+
func datasourceSlo() *schema.Resource {
1313
return &schema.Resource{
1414
Description: `
1515
Datasource for retrieving all SLOs.
@@ -18,14 +18,14 @@ Datasource for retrieving all SLOs.
1818
* [API documentation](https://grafana.com/docs/grafana-cloud/alerting-and-irm/slo/api/)
1919
* [Additional Information On Alerting Rule Annotations and Labels](https://prometheus.io/docs/prometheus/latest/configuration/alerting_rules/#templating/)
2020
`,
21-
ReadContext: datasourceSloRead,
21+
ReadContext: withClient[schema.ReadContextFunc](datasourceSloRead),
2222
Schema: map[string]*schema.Schema{
2323
"slos": {
2424
Type: schema.TypeList,
2525
Computed: true,
2626
Description: `Returns a list of all SLOs"`,
2727
Elem: &schema.Resource{
28-
Schema: common.CloneResourceSchemaForDatasource(ResourceSlo(), map[string]*schema.Schema{
28+
Schema: common.CloneResourceSchemaForDatasource(resourceSlo(), map[string]*schema.Schema{
2929
"uuid": {
3030
Type: schema.TypeString,
3131
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.`,
@@ -40,10 +40,9 @@ Datasource for retrieving all SLOs.
4040

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

46-
client := m.(*common.Client).SLOClient
4746
req := client.DefaultAPI.V1SloGet(ctx)
4847
apiSlos, _, err := req.Execute()
4948
if err != nil {

internal/resources/slo/resource_slo.go

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const (
2020
QueryTypeThreshold string = "threshold"
2121
)
2222

23-
func ResourceSlo() *schema.Resource {
23+
func resourceSlo() *schema.Resource {
2424
return &schema.Resource{
2525
Description: `
2626
Resource manages Grafana SLOs.
@@ -29,10 +29,10 @@ Resource manages Grafana SLOs.
2929
* [API documentation](https://grafana.com/docs/grafana-cloud/alerting-and-irm/slo/api/)
3030
* [Additional Information On Alerting Rule Annotations and Labels](https://prometheus.io/docs/prometheus/latest/configuration/alerting_rules/#templating/)
3131
`,
32-
CreateContext: resourceSloCreate,
33-
ReadContext: resourceSloRead,
34-
UpdateContext: resourceSloUpdate,
35-
DeleteContext: resourceSloDelete,
32+
CreateContext: withClient[schema.CreateContextFunc](resourceSloCreate),
33+
ReadContext: withClient[schema.ReadContextFunc](resourceSloRead),
34+
UpdateContext: withClient[schema.UpdateContextFunc](resourceSloUpdate),
35+
DeleteContext: withClient[schema.DeleteContextFunc](resourceSloDelete),
3636
Importer: &schema.ResourceImporter{
3737
StateContext: schema.ImportStatePassthroughContext,
3838
},
@@ -234,7 +234,7 @@ var keyvalueSchema = &schema.Resource{
234234
},
235235
}
236236

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

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

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

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

258257
d.SetId(response.Uuid)
259-
resourceSloRead(ctx, d, m)
260258

261-
return resourceSloRead(ctx, d, m)
259+
return resourceSloRead(ctx, d, client)
262260
}
263261

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

268266
sloID := d.Id()
269267

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

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

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

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

298-
client := m.(*common.Client).SLOClient
299-
300295
req := client.DefaultAPI.V1SloIdPut(ctx, sloID).Slo(slo)
301296
if _, err := req.Execute(); err != nil {
302297
return apiError("Unable to Update SLO - API", err)
303298
}
304299
}
305300

306-
return resourceSloRead(ctx, d, m)
301+
return resourceSloRead(ctx, d, client)
307302
}
308303

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

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package slo
2+
3+
import (
4+
"context"
5+
6+
slo "github.com/grafana/slo-openapi-client/go"
7+
"github.com/grafana/terraform-provider-grafana/internal/common"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
type crudWithClientFunc func(ctx context.Context, d *schema.ResourceData, client *slo.APIClient) diag.Diagnostics
13+
14+
func withClient[T schema.CreateContextFunc | schema.UpdateContextFunc | schema.ReadContextFunc | schema.DeleteContextFunc](f crudWithClientFunc) T {
15+
return func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
16+
client := meta.(*common.Client).SLOClient
17+
if client == nil {
18+
return diag.Errorf("the SLO API client is required for this resource. Set the url and auth provider attributes")
19+
}
20+
return f(ctx, d, client)
21+
}
22+
}
23+
24+
var DatasourcesMap = map[string]*schema.Resource{
25+
"grafana_slos": datasourceSlo(),
26+
}
27+
28+
var ResourcesMap = map[string]*schema.Resource{
29+
"grafana_slo": resourceSlo(),
30+
}

0 commit comments

Comments
 (0)