Skip to content

Commit be773b1

Browse files
4777:adding managed by for Asserts (#2408)
* 4777:adding managed by, wait til asserts is updated before release * 4777:adding managed by to common and making threshold use retry logic * 4777: adding go client update * 4777:adding managed by to common and making threshold use retry logic * 4777: terraform * 4914: updating test * 4777: updating test 4914: updating test * 4777: updating test * 4777: updating fmt --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent e2a2984 commit be773b1

14 files changed

+151
-42
lines changed

docs/resources/asserts_log_config.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ resource "grafana_asserts_log_config" "production" {
2222
2323
match {
2424
property = "asserts_entity_type"
25-
op = "EQUALS"
25+
op = "="
2626
values = ["Service"]
2727
}
2828
2929
match {
3030
property = "environment"
31-
op = "EQUALS"
31+
op = "="
3232
values = ["production", "staging"]
3333
}
3434
3535
match {
3636
property = "site"
37-
op = "EQUALS"
37+
op = "="
3838
values = ["us-east-1", "us-west-2"]
3939
}
4040
@@ -58,25 +58,25 @@ resource "grafana_asserts_log_config" "development" {
5858
5959
match {
6060
property = "asserts_entity_type"
61-
op = "EQUALS"
61+
op = "="
6262
values = ["Service"]
6363
}
6464
6565
match {
6666
property = "environment"
67-
op = "EQUALS"
67+
op = "="
6868
values = ["development", "testing"]
6969
}
7070
7171
match {
7272
property = "site"
73-
op = "EQUALS"
73+
op = "="
7474
values = ["us-east-1"]
7575
}
7676
7777
match {
7878
property = "service"
79-
op = "EQUALS"
79+
op = "="
8080
values = ["api"]
8181
}
8282
@@ -100,7 +100,7 @@ resource "grafana_asserts_log_config" "minimal" {
100100
101101
match {
102102
property = "asserts_entity_type"
103-
op = "IS_NOT_NULL"
103+
op = "IS NOT NULL"
104104
values = []
105105
}
106106
}
@@ -134,7 +134,7 @@ resource "grafana_asserts_log_config" "minimal" {
134134

135135
Required:
136136

137-
- `op` (String) Operation to use for matching. One of: EQUALS, NOT_EQUALS, CONTAINS, DOES_NOT_CONTAIN, IS_NULL, IS_NOT_NULL.
137+
- `op` (String) Operation to use for matching. One of: =, <>, <, >, <=, >=, IS NULL, IS NOT NULL, STARTS WITH, CONTAINS.
138138
- `property` (String) Entity property to match.
139139
- `values` (List of String) Values to match against.
140140

examples/resources/grafana_asserts_log_config/resource.tf

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ resource "grafana_asserts_log_config" "production" {
77

88
match {
99
property = "asserts_entity_type"
10-
op = "EQUALS"
10+
op = "="
1111
values = ["Service"]
1212
}
1313

1414
match {
1515
property = "environment"
16-
op = "EQUALS"
16+
op = "="
1717
values = ["production", "staging"]
1818
}
1919

2020
match {
2121
property = "site"
22-
op = "EQUALS"
22+
op = "="
2323
values = ["us-east-1", "us-west-2"]
2424
}
2525

@@ -43,25 +43,25 @@ resource "grafana_asserts_log_config" "development" {
4343

4444
match {
4545
property = "asserts_entity_type"
46-
op = "EQUALS"
46+
op = "="
4747
values = ["Service"]
4848
}
4949

5050
match {
5151
property = "environment"
52-
op = "EQUALS"
52+
op = "="
5353
values = ["development", "testing"]
5454
}
5555

5656
match {
5757
property = "site"
58-
op = "EQUALS"
58+
op = "="
5959
values = ["us-east-1"]
6060
}
6161

6262
match {
6363
property = "service"
64-
op = "EQUALS"
64+
op = "="
6565
values = ["api"]
6666
}
6767

@@ -85,7 +85,7 @@ resource "grafana_asserts_log_config" "minimal" {
8585

8686
match {
8787
property = "asserts_entity_type"
88-
op = "IS_NOT_NULL"
88+
op = "IS NOT NULL"
8989
values = []
9090
}
9191
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/grafana/authlib/claims v0.0.0-20250120084028-e3328c576437
1313
github.com/grafana/fleet-management-api v1.0.0
1414
github.com/grafana/grafana-app-sdk v0.48.1
15-
github.com/grafana/grafana-asserts-public-clients/go/gcom v0.0.0-20251014094449-86b1bd3db812
15+
github.com/grafana/grafana-asserts-public-clients/go/gcom v0.0.0-20251113191110-a4819b8e1224
1616
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20250526074454-7ec66e02e4bb
1717
github.com/grafana/grafana-openapi-client-go v0.0.0-20250617151817-c0f8cbb88d5c
1818
github.com/grafana/grafana/apps/alerting/alertenrichment v0.0.0-20250925121631-89b988ca553e

go.sum

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,6 @@ github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e h1:JKmoR8x90Iww1
171171
github.com/gopherjs/gopherjs v0.0.0-20181103185306-d547d1d9531e/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
172172
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
173173
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
174-
github.com/grafana/amixr-api-go-client v0.0.25 h1:tAQeJRuq9ihHotxq6/oEB6lIhuAdM+MUP0uPkNn1I3A=
175-
github.com/grafana/amixr-api-go-client v0.0.25/go.mod h1:ihgLhTVimmjASuZ06y/mQxPcYH3toAIuUVGK6flHsMU=
176174
github.com/grafana/amixr-api-go-client v0.0.27 h1:tmw7JcbX3D0N52mK60kYvAdRNFKH0aOb+9FKPaye2sc=
177175
github.com/grafana/amixr-api-go-client v0.0.27/go.mod h1:ihgLhTVimmjASuZ06y/mQxPcYH3toAIuUVGK6flHsMU=
178176
github.com/grafana/authlib/claims v0.0.0-20250120084028-e3328c576437 h1:OlwbIVFcYgMjnQhpbZwRPVNrvZKTodvPMqwb8yEqVW0=
@@ -183,8 +181,8 @@ github.com/grafana/grafana-app-sdk v0.48.1 h1:bKJadWH18WCpJ+Zk8AezRFXCcZgGredRv+
183181
github.com/grafana/grafana-app-sdk v0.48.1/go.mod h1:5LljCz+wvmGfkQ8ZKTOfserhtXNEF0cSFthoWShvN6c=
184182
github.com/grafana/grafana-app-sdk/logging v0.48.1 h1:veM0X5LAPyN3KsDLglWjIofndbGuf7MqnrDuDN+F/Ng=
185183
github.com/grafana/grafana-app-sdk/logging v0.48.1/go.mod h1:Gh/nBWnspK3oDNWtiM5qUF/fardHzOIEez+SPI3JeHA=
186-
github.com/grafana/grafana-asserts-public-clients/go/gcom v0.0.0-20251014094449-86b1bd3db812 h1:U09VZk8e7XLmrgUr6/AAmWhs8yexWQxPh+EZ3Gfv318=
187-
github.com/grafana/grafana-asserts-public-clients/go/gcom v0.0.0-20251014094449-86b1bd3db812/go.mod h1:piOOzeZUPg0tP4sfe1y+MFp5+jGVPri4WOVV4oSJS30=
184+
github.com/grafana/grafana-asserts-public-clients/go/gcom v0.0.0-20251113191110-a4819b8e1224 h1:/D6r4sf/NHRJQxDGUKMkZrp4fAJqfFUXbaMvDAJj+js=
185+
github.com/grafana/grafana-asserts-public-clients/go/gcom v0.0.0-20251113191110-a4819b8e1224/go.mod h1:piOOzeZUPg0tP4sfe1y+MFp5+jGVPri4WOVV4oSJS30=
188186
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20250526074454-7ec66e02e4bb h1:rmYEnCXHNQbRsuzc5jCX5qkBqFF37c5RCHlyqAAPJZo=
189187
github.com/grafana/grafana-com-public-clients/go/gcom v0.0.0-20250526074454-7ec66e02e4bb/go.mod h1:sYWkB3NhyirQJfy3wtNQ29UYjoHbRlJlYhqN1jNsC5g=
190188
github.com/grafana/grafana-openapi-client-go v0.0.0-20250617151817-c0f8cbb88d5c h1:jox7J0BnJmcZJp8lp631u4gjDEoIfpi6O3yrpiXNTtg=

internal/resources/asserts/common.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,26 @@ import (
1414
"github.com/grafana/terraform-provider-grafana/v4/internal/common"
1515
)
1616

17+
// TerraformManagedBy is the value used to mark resources as managed by Terraform.
18+
// This constant is exported for use in tests.
19+
const TerraformManagedBy = "terraform"
20+
21+
// getManagedByTerraform returns a pointer to the Terraform managed-by string.
22+
// This is used to set the managedBy field on Asserts resources to indicate
23+
// they are managed by Terraform (as opposed to the UI or other sources).
24+
// Use this for DTOs that expect *string (e.g., AlertConfigDto, DisabledAlertConfigDto).
25+
func getManagedByTerraform() *string {
26+
s := TerraformManagedBy
27+
return &s
28+
}
29+
30+
// getManagedByTerraformValue returns the Terraform managed-by string value.
31+
// Use this for DTOs with SetManagedBy methods that expect string values
32+
// (e.g., LogDrilldownConfigDto, threshold DTOs).
33+
func getManagedByTerraformValue() string {
34+
return TerraformManagedBy
35+
}
36+
1737
// validateAssertsClient checks if the Asserts API client is properly configured
1838
func validateAssertsClient(meta interface{}) (*assertsapi.APIClient, int64, diag.Diagnostics) {
1939
client := meta.(*common.Client).AssertsAPIClient

internal/resources/asserts/resource_alert_config.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,9 @@ func resourceAlertConfigCreate(ctx context.Context, d *schema.ResourceData, meta
9494
// Create AlertConfigDto using the generated client models
9595
// Only include non-empty maps to avoid validation issues
9696
alertConfig := assertsapi.AlertConfigDto{
97-
Name: &name,
98-
Silenced: &silenced,
97+
Name: &name,
98+
Silenced: &silenced,
99+
ManagedBy: getManagedByTerraform(),
99100
}
100101

101102
// Only set matchLabels if not empty
@@ -229,8 +230,9 @@ func resourceAlertConfigUpdate(ctx context.Context, d *schema.ResourceData, meta
229230
// Create AlertConfigDto using the generated client models
230231
// Only include non-empty maps to avoid validation issues
231232
alertConfig := assertsapi.AlertConfigDto{
232-
Name: &name,
233-
Silenced: &silenced,
233+
Name: &name,
234+
Silenced: &silenced,
235+
ManagedBy: getManagedByTerraform(),
234236
}
235237

236238
// Only set matchLabels if not empty

internal/resources/asserts/resource_alert_config_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"time"
1010

1111
"github.com/grafana/terraform-provider-grafana/v4/internal/common"
12+
"github.com/grafana/terraform-provider-grafana/v4/internal/resources/asserts"
1213
"github.com/grafana/terraform-provider-grafana/v4/internal/testutils"
1314
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
1415
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -106,6 +107,10 @@ func testAccAssertsAlertConfigCheckExists(rn string, stackID int64, name string)
106107
// Find our specific config
107108
for _, config := range alertConfigs.AlertConfigs {
108109
if config.Name != nil && *config.Name == name {
110+
// Verify managedBy field is set to terraform
111+
if config.ManagedBy == nil || *config.ManagedBy != asserts.TerraformManagedBy {
112+
return fmt.Errorf("alert config %s has invalid managedBy field (expected '%s', got %v)", name, asserts.TerraformManagedBy, config.ManagedBy)
113+
}
109114
return nil // Found it
110115
}
111116
}

internal/resources/asserts/resource_custom_model_rules.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,7 @@ func resourceCustomModelRulesCreate(ctx context.Context, d *schema.ResourceData,
369369
}
370370

371371
rules.Name = &name
372+
rules.SetManagedBy(getManagedByTerraformValue())
372373

373374
req := client.CustomModelRulesConfigurationAPI.PutModelRules(ctx).ModelRulesDto(*rules).XScopeOrgID(fmt.Sprintf("%d", stackID))
374375
_, err = req.Execute()
@@ -451,6 +452,7 @@ func resourceCustomModelRulesUpdate(ctx context.Context, d *schema.ResourceData,
451452
}
452453

453454
rules.Name = &name
455+
rules.SetManagedBy(getManagedByTerraformValue())
454456

455457
req := client.CustomModelRulesConfigurationAPI.PutModelRules(ctx).ModelRulesDto(*rules).XScopeOrgID(fmt.Sprintf("%d", stackID))
456458
_, err = req.Execute()

internal/resources/asserts/resource_disabled_alert_config.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ func resourceDisabledAlertConfigCreate(ctx context.Context, d *schema.ResourceDa
6565

6666
// Create DisabledAlertConfigDto using the generated client models
6767
disabledAlertConfig := assertsapi.DisabledAlertConfigDto{
68-
Name: &name,
68+
Name: &name,
69+
ManagedBy: getManagedByTerraform(),
6970
}
7071

7172
// Only set matchLabels if not empty
@@ -163,7 +164,8 @@ func resourceDisabledAlertConfigUpdate(ctx context.Context, d *schema.ResourceDa
163164

164165
// Create DisabledAlertConfigDto using the generated client models
165166
disabledAlertConfig := assertsapi.DisabledAlertConfigDto{
166-
Name: &name,
167+
Name: &name,
168+
ManagedBy: getManagedByTerraform(),
167169
}
168170

169171
// Only set matchLabels if not empty

internal/resources/asserts/resource_disabled_alert_config_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/grafana/terraform-provider-grafana/v4/internal/common"
10+
"github.com/grafana/terraform-provider-grafana/v4/internal/resources/asserts"
1011
"github.com/grafana/terraform-provider-grafana/v4/internal/testutils"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
1213
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
@@ -100,6 +101,10 @@ func testAccAssertsDisabledAlertConfigCheckExists(rn string, stackID int64, name
100101
// Find our specific config
101102
for _, config := range disabledAlertConfigs.DisabledAlertConfigs {
102103
if config.Name != nil && *config.Name == name {
104+
// Verify managedBy field is set to terraform
105+
if config.ManagedBy == nil || *config.ManagedBy != asserts.TerraformManagedBy {
106+
return fmt.Errorf("disabled alert config %s has invalid managedBy field (expected '%s', got %v)", name, asserts.TerraformManagedBy, config.ManagedBy)
107+
}
103108
return nil // Found it
104109
}
105110
}

0 commit comments

Comments
 (0)