@@ -142,6 +142,26 @@ func TestAccResourceSlo(t *testing.T) {
142142 ImportState : true ,
143143 ImportStateVerify : true ,
144144 },
145+ {
146+ // Tests Asserts Integration
147+ Config : testutils .TestAccExampleWithReplace (t , "resources/grafana_slo/resource_asserts.tf" , map [string ]string {
148+ "Asserts SLO Example" : randomName + " - Asserts" ,
149+ }),
150+ Check : resource .ComposeTestCheckFunc (
151+ testAccSloCheckExistsWithProvenance ("grafana_slo.asserts_example" , & slo , "asserts" ),
152+ resource .TestCheckResourceAttrSet ("grafana_slo.asserts_example" , "id" ),
153+ resource .TestCheckResourceAttr ("grafana_slo.asserts_example" , "name" , randomName + " - Asserts" ),
154+ // Verify Asserts integration labels
155+ resource .TestCheckResourceAttr ("grafana_slo.asserts_example" , "label.0.key" , "grafana_slo_provenance" ),
156+ resource .TestCheckResourceAttr ("grafana_slo.asserts_example" , "label.0.value" , "asserts" ),
157+ resource .TestCheckResourceAttr ("grafana_slo.asserts_example" , "label.1.key" , "service_name" ),
158+ resource .TestCheckResourceAttr ("grafana_slo.asserts_example" , "label.2.key" , "team_name" ),
159+ // Verify search expression
160+ resource .TestCheckResourceAttr ("grafana_slo.asserts_example" , "search_expression" , "service=my-service" ),
161+ // Verify the SLO has the correct Asserts provenance
162+ testAccSloCheckAssertsProvenance ("grafana_slo.asserts_example" ),
163+ ),
164+ },
145165 },
146166 })
147167}
@@ -211,6 +231,10 @@ func TestAccSLO_recreate(t *testing.T) {
211231}
212232
213233func testAccSloCheckExists (rn string , slo * slo.SloV00Slo ) resource.TestCheckFunc {
234+ return testAccSloCheckExistsWithProvenance (rn , slo , "terraform" )
235+ }
236+
237+ func testAccSloCheckExistsWithProvenance (rn string , slo * slo.SloV00Slo , expectedProvenance string ) resource.TestCheckFunc {
214238 return func (s * terraform.State ) error {
215239 rs , ok := s .RootModule ().Resources [rn ]
216240 if ! ok {
@@ -228,8 +252,8 @@ func testAccSloCheckExists(rn string, slo *slo.SloV00Slo) resource.TestCheckFunc
228252 return fmt .Errorf ("error getting SLO: %s" , err )
229253 }
230254
231- if * gotSlo .ReadOnly .Provenance != "terraform" {
232- return fmt .Errorf ("provenance header missing - verify within the Grafana Terraform Provider that the 'Grafana-Terraform-Provider' request header is set to 'true'" )
255+ if * gotSlo .ReadOnly .Provenance != expectedProvenance {
256+ return fmt .Errorf ("expected provenance to be '%s', got '%s'" , expectedProvenance , * gotSlo . ReadOnly . Provenance )
233257 }
234258
235259 * slo = * gotSlo
@@ -721,3 +745,48 @@ resource "grafana_slo" "custom_uuid_test" {
721745}
722746` , uuid )
723747}
748+
749+ // testAccSloCheckAssertsProvenance verifies that the SLO has the correct Asserts provenance
750+ func testAccSloCheckAssertsProvenance (rn string ) resource.TestCheckFunc {
751+ return func (s * terraform.State ) error {
752+ rs , ok := s .RootModule ().Resources [rn ]
753+ if ! ok {
754+ return fmt .Errorf ("resource not found: %s" , rn )
755+ }
756+
757+ if rs .Primary .ID == "" {
758+ return fmt .Errorf ("resource id not set" )
759+ }
760+
761+ client := testutils .Provider .Meta ().(* common.Client ).SLOClient
762+ req := client .DefaultAPI .V1SloIdGet (context .Background (), rs .Primary .ID )
763+ gotSlo , _ , err := req .Execute ()
764+ if err != nil {
765+ return fmt .Errorf ("error getting SLO: %s" , err )
766+ }
767+
768+ // Check that the SLO has the correct provenance
769+ if gotSlo .ReadOnly == nil || gotSlo .ReadOnly .Provenance == nil {
770+ return fmt .Errorf ("SLO provenance is not set" )
771+ }
772+
773+ if * gotSlo .ReadOnly .Provenance != "asserts" {
774+ return fmt .Errorf ("expected SLO provenance to be 'asserts', got '%s'" , * gotSlo .ReadOnly .Provenance )
775+ }
776+
777+ // Verify the SLO has the Asserts provenance label
778+ hasAssertsLabel := false
779+ for _ , label := range gotSlo .Labels {
780+ if label .Key == "grafana_slo_provenance" && label .Value == "asserts" {
781+ hasAssertsLabel = true
782+ break
783+ }
784+ }
785+
786+ if ! hasAssertsLabel {
787+ return fmt .Errorf ("SLO does not have the grafana_slo_provenance=asserts label" )
788+ }
789+
790+ return nil
791+ }
792+ }
0 commit comments