From 5740b7e9704edf817ff2e1ce106da80a17db08b0 Mon Sep 17 00:00:00 2001 From: Julien Duchesne Date: Thu, 25 Jul 2024 11:06:18 -0400 Subject: [PATCH] (Backport) Fix SLO client (#1716) v2 of https://github.com/grafana/terraform-provider-grafana/pull/1697 --- go.mod | 2 +- go.sum | 4 +- internal/resources/slo/data_source_slo.go | 22 +++--- .../resources/slo/data_source_slo_test.go | 2 +- internal/resources/slo/resource_slo.go | 70 +++++++++---------- internal/resources/slo/resource_slo_test.go | 8 +-- 6 files changed, 54 insertions(+), 54 deletions(-) diff --git a/go.mod b/go.mod index e328e3b7d..1a6075278 100644 --- a/go.mod +++ b/go.mod @@ -12,7 +12,7 @@ require ( github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20240322153219-42c6a1d2bcab github.com/grafana/grafana-openapi-client-go v0.0.0-20240430202104-3ad0f7e4ee52 github.com/grafana/machine-learning-go-client v0.5.0 - github.com/grafana/slo-openapi-client/go v0.0.0-20240112175006-de02e75b9d73 + github.com/grafana/slo-openapi-client/go v0.0.0-20240717162314-26344962b4c5 github.com/grafana/synthetic-monitoring-agent v0.24.1 github.com/grafana/synthetic-monitoring-api-go-client v0.8.0 github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320 diff --git a/go.sum b/go.sum index 484b6149d..61309f2cb 100644 --- a/go.sum +++ b/go.sum @@ -106,8 +106,8 @@ github.com/grafana/grafana-openapi-client-go v0.0.0-20240430202104-3ad0f7e4ee52 github.com/grafana/grafana-openapi-client-go v0.0.0-20240430202104-3ad0f7e4ee52/go.mod h1:hiZnMmXc9KXNUlvkV2BKFsiWuIFF/fF4wGgYWEjBitI= github.com/grafana/machine-learning-go-client v0.5.0 h1:Q1K+MPSy8vfMm2jsk3WQ7O77cGr2fM5hxwtPSoPc5NU= github.com/grafana/machine-learning-go-client v0.5.0/go.mod h1:QFfZz8NkqVF8++skjkKQXJEZfpCYd8S0yTWJUpsLLTA= -github.com/grafana/slo-openapi-client/go v0.0.0-20240112175006-de02e75b9d73 h1:E5vAeB5q1H3BVeNhtd1dI8RubucJdPwpx/ElNtKD3ls= -github.com/grafana/slo-openapi-client/go v0.0.0-20240112175006-de02e75b9d73/go.mod h1:EvM3pcxqS+avXd0G8VMyo/kITr9QsN1CwZTNgYtQ9lI= +github.com/grafana/slo-openapi-client/go v0.0.0-20240717162314-26344962b4c5 h1:qUb8ZPVC/QQDh8uCH9ZObZnsdT9S7iX3qe3q73XLj/o= +github.com/grafana/slo-openapi-client/go v0.0.0-20240717162314-26344962b4c5/go.mod h1:HgbbeH2gFfCk2XZCrCly39DB13WkwWyQ+Ww+HTxePCs= github.com/grafana/synthetic-monitoring-agent v0.24.1 h1:BVruIcSLM5EpNtN5Z8ApdkoVSoFuUoR1YLC1pu44Lng= github.com/grafana/synthetic-monitoring-agent v0.24.1/go.mod h1:izlTwdDryrndATx2rSAtyjplvccPdIg+Zm331+Oy9hU= github.com/grafana/synthetic-monitoring-api-go-client v0.8.0 h1:Tm4MtwwYmPNInGfnj66l6j6KOshMkNV4emIVKJdlXMg= diff --git a/internal/resources/slo/data_source_slo.go b/internal/resources/slo/data_source_slo.go index 74123857a..2e68a97cc 100644 --- a/internal/resources/slo/data_source_slo.go +++ b/internal/resources/slo/data_source_slo.go @@ -68,7 +68,7 @@ func datasourceSloRead(ctx context.Context, d *schema.ResourceData, client *slo. return diags } -func convertDatasourceSlo(slo slo.Slo) map[string]interface{} { +func convertDatasourceSlo(slo slo.SloV00Slo) map[string]interface{} { ret := make(map[string]interface{}) ret["uuid"] = slo.Uuid @@ -91,7 +91,7 @@ func convertDatasourceSlo(slo slo.Slo) map[string]interface{} { return ret } -func unpackQuery(apiquery slo.Query) []map[string]interface{} { +func unpackQuery(apiquery slo.SloV00Query) []map[string]interface{} { retQuery := []map[string]interface{}{} if apiquery.Type == QueryTypeFreeform { @@ -124,7 +124,7 @@ func unpackQuery(apiquery slo.Query) []map[string]interface{} { return retQuery } -func unpackObjectives(objectives []slo.Objective) []map[string]interface{} { +func unpackObjectives(objectives []slo.SloV00Objective) []map[string]interface{} { retObjectives := []map[string]interface{}{} for _, objective := range objectives { @@ -140,22 +140,22 @@ func unpackObjectives(objectives []slo.Objective) []map[string]interface{} { func unpackLabels(labelsInterface interface{}) []map[string]interface{} { retLabels := []map[string]interface{}{} - var labels []slo.Label + var labels []slo.SloV00Label switch v := labelsInterface.(type) { - case *[]slo.Label: + case *[]slo.SloV00Label: labels = *v - case []slo.Label: + case []slo.SloV00Label: labels = v case []interface{}: for _, labelInterface := range v { switch v := labelInterface.(type) { case map[string]interface{}: - label := slo.Label{ + label := slo.SloV00Label{ Key: v["key"].(string), Value: v["value"].(string), } labels = append(labels, label) - case slo.Label: + case slo.SloV00Label: labels = append(labels, v) } } @@ -170,7 +170,7 @@ func unpackLabels(labelsInterface interface{}) []map[string]interface{} { return retLabels } -func unpackAlerting(alertData *slo.Alerting) []map[string]interface{} { +func unpackAlerting(alertData *slo.SloV00Alerting) []map[string]interface{} { retAlertData := []map[string]interface{}{} if alertData == nil { @@ -193,7 +193,7 @@ func unpackAlerting(alertData *slo.Alerting) []map[string]interface{} { return retAlertData } -func unpackAlertingMetadata(metaData slo.AlertingMetadata) []map[string]interface{} { +func unpackAlertingMetadata(metaData slo.SloV00AlertingMetadata) []map[string]interface{} { retAlertMetaData := []map[string]interface{}{} labelsAnnotsStruct := make(map[string]interface{}) @@ -211,7 +211,7 @@ func unpackAlertingMetadata(metaData slo.AlertingMetadata) []map[string]interfac return retAlertMetaData } -func unpackDestinationDatasource(destinationDatasource *slo.DestinationDatasource) []map[string]interface{} { +func unpackDestinationDatasource(destinationDatasource *slo.SloV00DestinationDatasource) []map[string]interface{} { retDestinationDatasources := []map[string]interface{}{} retDestinationDatasource := make(map[string]interface{}) diff --git a/internal/resources/slo/data_source_slo_test.go b/internal/resources/slo/data_source_slo_test.go index 231750a8e..3bb2d6977 100644 --- a/internal/resources/slo/data_source_slo_test.go +++ b/internal/resources/slo/data_source_slo_test.go @@ -14,7 +14,7 @@ func TestAccDataSourceSlo(t *testing.T) { randomName := acctest.RandomWithPrefix("SLO Terraform Testing") - var slo slo.Slo + var slo slo.SloV00Slo resource.ParallelTest(t, resource.TestCase{ ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories, CheckDestroy: testAccSloCheckDestroy(&slo), diff --git a/internal/resources/slo/resource_slo.go b/internal/resources/slo/resource_slo.go index 2bff90590..807bcd348 100644 --- a/internal/resources/slo/resource_slo.go +++ b/internal/resources/slo/resource_slo.go @@ -275,7 +275,7 @@ func resourceSloCreate(ctx context.Context, d *schema.ResourceData, client *slo. return diags } - req := client.DefaultAPI.V1SloPost(ctx).Slo(sloModel) + req := client.DefaultAPI.V1SloPost(ctx).SloV00Slo(sloModel) response, _, err := req.Execute() if err != nil { @@ -320,7 +320,7 @@ func resourceSloUpdate(ctx context.Context, d *schema.ResourceData, client *slo. return diags } - req := client.DefaultAPI.V1SloIdPut(ctx, sloID).Slo(slo) + req := client.DefaultAPI.V1SloIdPut(ctx, sloID).SloV00Slo(slo) if _, err := req.Execute(); err != nil { return apiError("Unable to Update SLO - API", err) } @@ -340,11 +340,11 @@ func resourceSloDelete(ctx context.Context, d *schema.ResourceData, client *slo. // Fetches all the Properties defined on the Terraform SLO State Object and converts it // to a Slo so that it can be converted to JSON and sent to the API -func packSloResource(d *schema.ResourceData) (slo.Slo, error) { +func packSloResource(d *schema.ResourceData) (slo.SloV00Slo, error) { var ( - tfalerting slo.Alerting - tflabels []slo.Label - tfdestinationdatasource slo.DestinationDatasource + tfalerting slo.SloV00Alerting + tflabels []slo.SloV00Label + tfdestinationdatasource slo.SloV00DestinationDatasource ) tfname := d.Get("name").(string) @@ -352,7 +352,7 @@ func packSloResource(d *schema.ResourceData) (slo.Slo, error) { query := d.Get("query").([]interface{})[0].(map[string]interface{}) tfquery, err := packQuery(query) if err != nil { - return slo.Slo{}, err + return slo.SloV00Slo{}, err } objectives := d.Get("objectives").([]interface{}) @@ -363,7 +363,7 @@ func packSloResource(d *schema.ResourceData) (slo.Slo, error) { tflabels = packLabels(labels) } - slo := slo.Slo{ + slo := slo.SloV00Slo{ Uuid: d.Id(), Name: tfname, Description: tfdescription, @@ -402,8 +402,8 @@ func packSloResource(d *schema.ResourceData) (slo.Slo, error) { return slo, nil } -func packDestinationDatasource(destinationdatasource map[string]interface{}) (slo.DestinationDatasource, error) { - packedDestinationDatasource := slo.DestinationDatasource{} +func packDestinationDatasource(destinationdatasource map[string]interface{}) (slo.SloV00DestinationDatasource, error) { + packedDestinationDatasource := slo.SloV00DestinationDatasource{} if destinationdatasource["uid"].(string) != "" { datasourceUID := destinationdatasource["uid"].(string) @@ -413,13 +413,13 @@ func packDestinationDatasource(destinationdatasource map[string]interface{}) (sl return packedDestinationDatasource, nil } -func packQuery(query map[string]interface{}) (slo.Query, error) { +func packQuery(query map[string]interface{}) (slo.SloV00Query, error) { if query["type"] == "freeform" { freeformquery := query["freeform"].([]interface{})[0].(map[string]interface{}) querystring := freeformquery["query"].(string) - sloQuery := slo.Query{ - Freeform: &slo.FreeformQuery{Query: querystring}, + sloQuery := slo.SloV00Query{ + Freeform: &slo.SloV00FreeformQuery{Query: querystring}, Type: QueryTypeFreeform, } @@ -442,12 +442,12 @@ func packQuery(query map[string]interface{}) (slo.Query, error) { labels = append(labels, groupByLabels[ind].(string)) } - sloQuery := slo.Query{ - Ratio: &slo.RatioQuery{ - SuccessMetric: slo.MetricDef{ + sloQuery := slo.SloV00Query{ + Ratio: &slo.SloV00RatioQuery{ + SuccessMetric: slo.SloV00MetricDef{ PrometheusMetric: successMetric, }, - TotalMetric: slo.MetricDef{ + TotalMetric: slo.SloV00MetricDef{ PrometheusMetric: totalMetric, }, GroupByLabels: labels, @@ -458,15 +458,15 @@ func packQuery(query map[string]interface{}) (slo.Query, error) { return sloQuery, nil } - return slo.Query{}, fmt.Errorf("%s query type not implemented", query["type"]) + return slo.SloV00Query{}, fmt.Errorf("%s query type not implemented", query["type"]) } -func packObjectives(tfobjectives []interface{}) []slo.Objective { - objectives := []slo.Objective{} +func packObjectives(tfobjectives []interface{}) []slo.SloV00Objective { + objectives := []slo.SloV00Objective{} for ind := range tfobjectives { tfobjective := tfobjectives[ind].(map[string]interface{}) - objective := slo.Objective{ + objective := slo.SloV00Objective{ Value: tfobjective["value"].(float64), Window: tfobjective["window"].(string), } @@ -476,12 +476,12 @@ func packObjectives(tfobjectives []interface{}) []slo.Objective { return objectives } -func packLabels(tfLabels []interface{}) []slo.Label { - labelSlice := []slo.Label{} +func packLabels(tfLabels []interface{}) []slo.SloV00Label { + labelSlice := []slo.SloV00Label{} for ind := range tfLabels { currLabel := tfLabels[ind].(map[string]interface{}) - curr := slo.Label{ + curr := slo.SloV00Label{ Key: currLabel["key"].(string), Value: currLabel["value"].(string), } @@ -492,11 +492,11 @@ func packLabels(tfLabels []interface{}) []slo.Label { return labelSlice } -func packAlerting(tfAlerting map[string]interface{}) slo.Alerting { - var tfAnnots []slo.Label - var tfLabels []slo.Label - var tfFastBurn slo.AlertingMetadata - var tfSlowBurn slo.AlertingMetadata +func packAlerting(tfAlerting map[string]interface{}) slo.SloV00Alerting { + var tfAnnots []slo.SloV00Label + var tfLabels []slo.SloV00Label + var tfFastBurn slo.SloV00AlertingMetadata + var tfSlowBurn slo.SloV00AlertingMetadata annots, ok := tfAlerting["annotation"].([]interface{}) if ok { @@ -518,7 +518,7 @@ func packAlerting(tfAlerting map[string]interface{}) slo.Alerting { tfSlowBurn = packAlertMetadata(slowBurn) } - alerting := slo.Alerting{ + alerting := slo.SloV00Alerting{ Annotations: tfAnnots, Labels: tfLabels, FastBurn: &tfFastBurn, @@ -528,9 +528,9 @@ func packAlerting(tfAlerting map[string]interface{}) slo.Alerting { return alerting } -func packAlertMetadata(metadata []interface{}) slo.AlertingMetadata { - var tflabels []slo.Label - var tfannots []slo.Label +func packAlertMetadata(metadata []interface{}) slo.SloV00AlertingMetadata { + var tflabels []slo.SloV00Label + var tfannots []slo.SloV00Label if len(metadata) > 0 { meta, ok := metadata[0].(map[string]interface{}) @@ -547,7 +547,7 @@ func packAlertMetadata(metadata []interface{}) slo.AlertingMetadata { } } - apiMetadata := slo.AlertingMetadata{ + apiMetadata := slo.SloV00AlertingMetadata{ Labels: tflabels, Annotations: tfannots, } @@ -555,7 +555,7 @@ func packAlertMetadata(metadata []interface{}) slo.AlertingMetadata { return apiMetadata } -func setTerraformState(d *schema.ResourceData, slo slo.Slo) { +func setTerraformState(d *schema.ResourceData, slo slo.SloV00Slo) { d.Set("name", slo.Name) d.Set("description", slo.Description) diff --git a/internal/resources/slo/resource_slo_test.go b/internal/resources/slo/resource_slo_test.go index 72d8d79a1..bcb25f138 100644 --- a/internal/resources/slo/resource_slo_test.go +++ b/internal/resources/slo/resource_slo_test.go @@ -19,7 +19,7 @@ func TestAccResourceSlo(t *testing.T) { randomName := acctest.RandomWithPrefix("SLO Terraform Testing") - var slo slo.Slo + var slo slo.SloV00Slo resource.ParallelTest(t, resource.TestCase{ ProtoV5ProviderFactories: testutils.ProtoV5ProviderFactories, // Implicitly tests destroy @@ -96,7 +96,7 @@ func TestAccResourceSlo(t *testing.T) { }) } -func testAccSloCheckExists(rn string, slo *slo.Slo) resource.TestCheckFunc { +func testAccSloCheckExists(rn string, slo *slo.SloV00Slo) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[rn] if !ok { @@ -121,7 +121,7 @@ func testAccSloCheckExists(rn string, slo *slo.Slo) resource.TestCheckFunc { } } -func testAlertingExists(expectation bool, rn string, slo *slo.Slo) resource.TestCheckFunc { +func testAlertingExists(expectation bool, rn string, slo *slo.SloV00Slo) resource.TestCheckFunc { return func(s *terraform.State) error { rs := s.RootModule().Resources[rn] client := testutils.Provider.Meta().(*common.Client).SLOClient @@ -145,7 +145,7 @@ func testAlertingExists(expectation bool, rn string, slo *slo.Slo) resource.Test } } -func testAccSloCheckDestroy(slo *slo.Slo) resource.TestCheckFunc { +func testAccSloCheckDestroy(slo *slo.SloV00Slo) resource.TestCheckFunc { return func(s *terraform.State) error { client := testutils.Provider.Meta().(*common.Client).SLOClient req := client.DefaultAPI.V1SloIdGet(context.Background(), slo.Uuid)