Skip to content

Commit 3982562

Browse files
santihernandezcheliapb
authored andcommitted
Alerting: Allow duplicated alert rule names (grafana#2323)
* Alerting: Allow duplicated alert rule names * update test * update Grafana version in test Signed-off-by: Hélia Barroso <[email protected]>
1 parent ae91b09 commit 3982562

File tree

103 files changed

+832
-883
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+832
-883
lines changed

cmd/generate/validation.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"slices"
56

67
"github.com/urfave/cli/v2"
78
)
@@ -15,10 +16,8 @@ func newFlagValidations() flagValidations {
1516

1617
func (v flagValidations) atLeastOne(flags ...string) flagValidations {
1718
validateFunc := func(ctx *cli.Context) error {
18-
for _, flag := range flags {
19-
if ctx.IsSet(flag) {
20-
return nil
21-
}
19+
if slices.ContainsFunc(flags, ctx.IsSet) {
20+
return nil
2221
}
2322
return fmt.Errorf("at least one of flags %s must be specified", flags)
2423
}

internal/common/adapters.go

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

7-
func ListToStringSlice(src []interface{}) []string {
7+
func ListToStringSlice(src []any) []string {
88
dst := make([]string, 0, len(src))
99
for _, s := range src {
1010
val, ok := s.(string)
@@ -20,7 +20,7 @@ func SetToStringSlice(src *schema.Set) []string {
2020
return ListToStringSlice(src.List())
2121
}
2222

23-
func ListToIntSlice[T int | int64](src []interface{}) []T {
23+
func ListToIntSlice[T int | int64](src []any) []T {
2424
dst := make([]T, 0, len(src))
2525
for _, s := range src {
2626
val, ok := s.(int)
@@ -36,8 +36,8 @@ func SetToIntSlice[T int | int64](src *schema.Set) []T {
3636
return ListToIntSlice[T](src.List())
3737
}
3838

39-
func StringSliceToList(list []string) []interface{} {
40-
vs := make([]interface{}, 0, len(list))
39+
func StringSliceToList(list []string) []any {
40+
vs := make([]any, 0, len(list))
4141
for _, v := range list {
4242
vs = append(vs, v)
4343
}
@@ -48,8 +48,8 @@ func StringSliceToSet(src []string) *schema.Set {
4848
return schema.NewSet(schema.HashString, StringSliceToList(src))
4949
}
5050

51-
func Int32SliceToIntList(list []int32) []interface{} {
52-
vs := make([]interface{}, 0, len(list))
51+
func Int32SliceToIntList(list []int32) []any {
52+
vs := make([]any, 0, len(list))
5353
for _, v := range list {
5454
vs = append(vs, int(v))
5555
}
@@ -60,17 +60,17 @@ func Int32SliceToSet(src []int32) *schema.Set {
6060
return schema.NewSet(schema.HashInt, Int32SliceToIntList(src))
6161
}
6262

63-
func ListOfSetsToStringSlice(listSet []interface{}) [][]string {
63+
func ListOfSetsToStringSlice(listSet []any) [][]string {
6464
ret := make([][]string, 0, len(listSet))
6565
for _, set := range listSet {
6666
ret = append(ret, SetToStringSlice(set.(*schema.Set)))
6767
}
6868
return ret
6969
}
7070

71-
func UnpackMap[T any](m interface{}) map[string]T {
71+
func UnpackMap[T any](m any) map[string]T {
7272
ret := make(map[string]T)
73-
for k, v := range m.(map[string]interface{}) {
73+
for k, v := range m.(map[string]any) {
7474
ret[k] = v.(T)
7575
}
7676
return ret

internal/common/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type Client struct {
5555

5656
// WithAlertingMutex is a helper function that wraps a CRUD Terraform function with a mutex.
5757
func WithAlertingMutex[T schema.CreateContextFunc | schema.ReadContextFunc | schema.UpdateContextFunc | schema.DeleteContextFunc](f T) T {
58-
return func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
58+
return func(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
5959
lock := &meta.(*Client).alertingMutex
6060
lock.Lock()
6161
defer lock.Unlock()

internal/common/json.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ func SuppressEquivalentJSONDiffs(k, old, new string, d *schema.ResourceData) boo
2323
}
2424

2525
func JSONBytesEqual(b1, b2 []byte) bool {
26-
var o1 interface{}
26+
var o1 any
2727
if err := json.Unmarshal(b1, &o1); err != nil {
2828
return false
2929
}
3030

31-
var o2 interface{}
31+
var o2 any
3232
if err := json.Unmarshal(b2, &o2); err != nil {
3333
return false
3434
}

internal/common/schema.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ func AllowedValuesDescription(description string, allowedValues []string) string
5757
return fmt.Sprintf("%s. Allowed values: `%s`.", description, strings.Join(allowedValues, "`, `"))
5858
}
5959

60-
func ValidateDuration(i interface{}, p cty.Path) diag.Diagnostics {
60+
func ValidateDuration(i any, p cty.Path) diag.Diagnostics {
6161
v := i.(string)
6262
_, err := time.ParseDuration(v)
6363
if err != nil {
@@ -66,7 +66,7 @@ func ValidateDuration(i interface{}, p cty.Path) diag.Diagnostics {
6666
return nil
6767
}
6868

69-
func ValidateDurationWithDays(i interface{}, p cty.Path) diag.Diagnostics {
69+
func ValidateDurationWithDays(i any, p cty.Path) diag.Diagnostics {
7070
v := i.(string)
7171
_, err := strfmt.ParseDuration(v)
7272
if err != nil {

internal/resources/asserts/resource_alert_config.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func makeResourceAlertConfig() *common.Resource {
7070
).WithLister(assertsListerFunction(listAlertConfigs))
7171
}
7272

73-
func resourceAlertConfigCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
73+
func resourceAlertConfigCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
7474
client := meta.(*common.Client).AssertsAPIClient
7575
if client == nil {
7676
return diag.Errorf("Asserts API client is not configured")
@@ -85,13 +85,13 @@ func resourceAlertConfigCreate(ctx context.Context, d *schema.ResourceData, meta
8585
alertLabels := make(map[string]string)
8686

8787
if v, ok := d.GetOk("match_labels"); ok {
88-
for k, val := range v.(map[string]interface{}) {
88+
for k, val := range v.(map[string]any) {
8989
matchLabels[k] = val.(string)
9090
}
9191
}
9292

9393
if v, ok := d.GetOk("alert_labels"); ok {
94-
for k, val := range v.(map[string]interface{}) {
94+
for k, val := range v.(map[string]any) {
9595
alertLabels[k] = val.(string)
9696
}
9797
}
@@ -136,7 +136,7 @@ func resourceAlertConfigCreate(ctx context.Context, d *schema.ResourceData, meta
136136
return resourceAlertConfigRead(ctx, d, meta)
137137
}
138138

139-
func resourceAlertConfigRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
139+
func resourceAlertConfigRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
140140
client := meta.(*common.Client).AssertsAPIClient
141141
if client == nil {
142142
return diag.Errorf("Asserts API client is not configured")
@@ -229,7 +229,7 @@ func resourceAlertConfigRead(ctx context.Context, d *schema.ResourceData, meta i
229229
return nil
230230
}
231231

232-
func resourceAlertConfigUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
232+
func resourceAlertConfigUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
233233
client := meta.(*common.Client).AssertsAPIClient
234234
if client == nil {
235235
return diag.Errorf("Asserts API client is not configured")
@@ -245,13 +245,13 @@ func resourceAlertConfigUpdate(ctx context.Context, d *schema.ResourceData, meta
245245
alertLabels := make(map[string]string)
246246

247247
if v, ok := d.GetOk("match_labels"); ok {
248-
for k, val := range v.(map[string]interface{}) {
248+
for k, val := range v.(map[string]any) {
249249
matchLabels[k] = val.(string)
250250
}
251251
}
252252

253253
if v, ok := d.GetOk("alert_labels"); ok {
254-
for k, val := range v.(map[string]interface{}) {
254+
for k, val := range v.(map[string]any) {
255255
alertLabels[k] = val.(string)
256256
}
257257
}
@@ -294,7 +294,7 @@ func resourceAlertConfigUpdate(ctx context.Context, d *schema.ResourceData, meta
294294
return resourceAlertConfigRead(ctx, d, meta)
295295
}
296296

297-
func resourceAlertConfigDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
297+
func resourceAlertConfigDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
298298
client := meta.(*common.Client).AssertsAPIClient
299299
if client == nil {
300300
return diag.Errorf("Asserts API client is not configured")

internal/resources/asserts/resource_disabled_alert_config.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func makeResourceDisabledAlertConfig() *common.Resource {
5252
).WithLister(assertsListerFunction(listDisabledAlertConfigs))
5353
}
5454

55-
func resourceDisabledAlertConfigCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
55+
func resourceDisabledAlertConfigCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
5656
client := meta.(*common.Client).AssertsAPIClient
5757
if client == nil {
5858
return diag.Errorf("Asserts API client is not configured")
@@ -66,7 +66,7 @@ func resourceDisabledAlertConfigCreate(ctx context.Context, d *schema.ResourceDa
6666
matchLabels := make(map[string]string)
6767

6868
if v, ok := d.GetOk("match_labels"); ok {
69-
for k, val := range v.(map[string]interface{}) {
69+
for k, val := range v.(map[string]any) {
7070
matchLabels[k] = val.(string)
7171
}
7272
}
@@ -96,7 +96,7 @@ func resourceDisabledAlertConfigCreate(ctx context.Context, d *schema.ResourceDa
9696
return resourceDisabledAlertConfigRead(ctx, d, meta)
9797
}
9898

99-
func resourceDisabledAlertConfigRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
99+
func resourceDisabledAlertConfigRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
100100
client := meta.(*common.Client).AssertsAPIClient
101101
if client == nil {
102102
return diag.Errorf("Asserts API client is not configured")
@@ -174,7 +174,7 @@ func resourceDisabledAlertConfigRead(ctx context.Context, d *schema.ResourceData
174174
return nil
175175
}
176176

177-
func resourceDisabledAlertConfigUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
177+
func resourceDisabledAlertConfigUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
178178
client := meta.(*common.Client).AssertsAPIClient
179179
if client == nil {
180180
return diag.Errorf("Asserts API client is not configured")
@@ -189,7 +189,7 @@ func resourceDisabledAlertConfigUpdate(ctx context.Context, d *schema.ResourceDa
189189
matchLabels := make(map[string]string)
190190

191191
if v, ok := d.GetOk("match_labels"); ok {
192-
for k, val := range v.(map[string]interface{}) {
192+
for k, val := range v.(map[string]any) {
193193
matchLabels[k] = val.(string)
194194
}
195195
}
@@ -218,7 +218,7 @@ func resourceDisabledAlertConfigUpdate(ctx context.Context, d *schema.ResourceDa
218218
return resourceDisabledAlertConfigRead(ctx, d, meta)
219219
}
220220

221-
func resourceDisabledAlertConfigDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
221+
func resourceDisabledAlertConfigDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
222222
client := meta.(*common.Client).AssertsAPIClient
223223
if client == nil {
224224
return diag.Errorf("Asserts API client is not configured")

internal/resources/cloud/common.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func ClientRequestID() string {
2424
type crudWithClientFunc func(ctx context.Context, d *schema.ResourceData, client *gcom.APIClient) diag.Diagnostics
2525

2626
func withClient[T schema.CreateContextFunc | schema.UpdateContextFunc | schema.ReadContextFunc | schema.DeleteContextFunc](f crudWithClientFunc) T {
27-
return func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
27+
return func(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
2828
client := meta.(*common.Client).GrafanaCloudAPI
2929
if client == nil {
3030
return diag.Errorf("the Cloud API client is required for this resource. Set the cloud_access_policy_token provider attribute")

internal/resources/cloud/data_source_cloud_ips.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func datasourceIPs() *common.DataSource {
6161
return common.NewLegacySDKDataSource(common.CategoryCloud, "grafana_cloud_ips", schema)
6262
}
6363

64-
func datasourceIPsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
64+
func datasourceIPsRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
6565
d.SetId("cloud_ips")
6666
for attr, dataURL := range map[string]string{
6767
"hosted_alerts": "https://grafana.com/api/hosted-alerts/source-ips.txt",

internal/resources/cloud/resource_cloud_access_policy.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -281,34 +281,34 @@ func deleteCloudAccessPolicy(ctx context.Context, d *schema.ResourceData, client
281281
return apiError(err)
282282
}
283283

284-
func validateCloudAccessPolicyScope(v interface{}, path cty.Path) diag.Diagnostics {
284+
func validateCloudAccessPolicyScope(v any, path cty.Path) diag.Diagnostics {
285285
if strings.Count(v.(string), ":") != 1 {
286286
return diag.Errorf("invalid scope: %s. Should be in the `service:permission` format", v.(string))
287287
}
288288

289289
return nil
290290
}
291291

292-
func validateCloudAccessPolicyAllowedSubnets(v interface{}, path cty.Path) diag.Diagnostics {
292+
func validateCloudAccessPolicyAllowedSubnets(v any, path cty.Path) diag.Diagnostics {
293293
_, _, err := net.ParseCIDR(v.(string))
294294
if err == nil {
295295
return nil
296296
}
297297
return diag.Errorf("Invalid IP CIDR : %s.", v.(string))
298298
}
299299

300-
func flattenCloudAccessPolicyRealm(realm []gcom.AuthAccessPolicyRealmsInner) []interface{} {
301-
var result []interface{}
300+
func flattenCloudAccessPolicyRealm(realm []gcom.AuthAccessPolicyRealmsInner) []any {
301+
var result []any
302302

303303
for _, r := range realm {
304-
labelPolicy := []interface{}{}
304+
labelPolicy := []any{}
305305
for _, lp := range r.LabelPolicies {
306-
labelPolicy = append(labelPolicy, map[string]interface{}{
306+
labelPolicy = append(labelPolicy, map[string]any{
307307
"selector": lp.Selector,
308308
})
309309
}
310310

311-
result = append(result, map[string]interface{}{
311+
result = append(result, map[string]any{
312312
"type": r.Type,
313313
"identifier": r.Identifier,
314314
"label_policy": labelPolicy,
@@ -318,29 +318,29 @@ func flattenCloudAccessPolicyRealm(realm []gcom.AuthAccessPolicyRealmsInner) []i
318318
return result
319319
}
320320

321-
func flattenCloudAccessPolicyConditions(condition *gcom.AuthAccessPolicyConditions) []interface{} {
321+
func flattenCloudAccessPolicyConditions(condition *gcom.AuthAccessPolicyConditions) []any {
322322
if condition == nil || len(condition.GetAllowedSubnets()) == 0 {
323323
return nil
324324
}
325-
var result []interface{}
325+
var result []any
326326
var allowedSubnets []string
327327

328328
for _, sn := range condition.GetAllowedSubnets() {
329329
allowedSubnets = append(allowedSubnets, *sn.String)
330330
}
331331

332-
result = append(result, map[string]interface{}{
332+
result = append(result, map[string]any{
333333
"allowed_subnets": allowedSubnets,
334334
})
335335

336336
return result
337337
}
338338

339-
func expandCloudAccessPolicyConditions(condition []interface{}) gcom.NullablePostAccessPoliciesRequestConditions {
339+
func expandCloudAccessPolicyConditions(condition []any) gcom.NullablePostAccessPoliciesRequestConditions {
340340
var result gcom.PostAccessPoliciesRequestConditions
341341

342342
for _, c := range condition {
343-
c := c.(map[string]interface{})
343+
c := c.(map[string]any)
344344
for _, as := range c["allowed_subnets"].(*schema.Set).List() {
345345
result.AllowedSubnets = append(result.AllowedSubnets, as.(string))
346346
}
@@ -349,14 +349,14 @@ func expandCloudAccessPolicyConditions(condition []interface{}) gcom.NullablePos
349349
return *gcom.NewNullablePostAccessPoliciesRequestConditions(&result)
350350
}
351351

352-
func expandCloudAccessPolicyRealm(realm []interface{}) []gcom.PostAccessPoliciesRequestRealmsInner {
352+
func expandCloudAccessPolicyRealm(realm []any) []gcom.PostAccessPoliciesRequestRealmsInner {
353353
var result []gcom.PostAccessPoliciesRequestRealmsInner
354354

355355
for _, r := range realm {
356-
r := r.(map[string]interface{})
356+
r := r.(map[string]any)
357357
labelPolicy := []gcom.PostAccessPoliciesRequestRealmsInnerLabelPoliciesInner{}
358358
for _, lp := range r["label_policy"].(*schema.Set).List() {
359-
lp := lp.(map[string]interface{})
359+
lp := lp.(map[string]any)
360360
labelPolicy = append(labelPolicy, gcom.PostAccessPoliciesRequestRealmsInnerLabelPoliciesInner{
361361
Selector: lp["selector"].(string),
362362
})

0 commit comments

Comments
 (0)