Skip to content

Commit

Permalink
(Backport) Fix SLO client (#1716)
Browse files Browse the repository at this point in the history
v2 of #1697
  • Loading branch information
julienduchesne committed Jul 25, 2024
1 parent 55cc616 commit 5740b7e
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 54 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
22 changes: 11 additions & 11 deletions internal/resources/slo/data_source_slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
}
Expand All @@ -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 {
Expand All @@ -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{})

Expand All @@ -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{})
Expand Down
2 changes: 1 addition & 1 deletion internal/resources/slo/data_source_slo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
70 changes: 35 additions & 35 deletions internal/resources/slo/resource_slo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -340,19 +340,19 @@ 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)
tfdescription := d.Get("description").(string)
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{})
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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,
}

Expand All @@ -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,
Expand All @@ -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),
}
Expand All @@ -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),
}
Expand All @@ -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 {
Expand All @@ -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,
Expand All @@ -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{})
Expand All @@ -547,15 +547,15 @@ func packAlertMetadata(metadata []interface{}) slo.AlertingMetadata {
}
}

apiMetadata := slo.AlertingMetadata{
apiMetadata := slo.SloV00AlertingMetadata{
Labels: tflabels,
Annotations: tfannots,
}

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)

Expand Down
8 changes: 4 additions & 4 deletions internal/resources/slo/resource_slo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit 5740b7e

Please sign in to comment.