Skip to content

Commit e1e096b

Browse files
authored
Collapsed IAM resources + parent for multiple resources check (#15973)
1 parent a47faa1 commit e1e096b

File tree

3 files changed

+104
-7
lines changed

3 files changed

+104
-7
lines changed

.ci/magician/cmd/generate_comment.go

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ type diffCommentData struct {
8989
MissingServiceLabels []string
9090
MissingTests map[string]*MissingTestInfo
9191
MissingDocs *MissingDocsSummary
92-
AddedResources []string
92+
MultipleResources []string
9393
Errors []Errors
9494
}
9595

@@ -377,7 +377,8 @@ func execGenerateComment(prNumber int, ghTokenMagicModules, buildId, buildStep,
377377

378378
// Check if multiple resources were added.
379379
multipleResourcesState := "success"
380-
if len(uniqueAddedResources) > 1 {
380+
data.MultipleResources = multipleResources(maps.Keys(uniqueAddedResources))
381+
if len(data.MultipleResources) > 1 {
381382
multipleResourcesState = "failure"
382383
for _, label := range pullRequest.Labels {
383384
if label.Name == allowMultipleResourcesLabel {
@@ -391,8 +392,6 @@ func execGenerateComment(prNumber int, ghTokenMagicModules, buildId, buildStep,
391392
fmt.Printf("Error posting terraform-provider-multiple-resources build status for pr %d commit %s: %v\n", prNumber, commitSha, err)
392393
errors["Other"] = append(errors["Other"], "Failed to update missing-service-labels status check with state: "+multipleResourcesState)
393394
}
394-
data.AddedResources = maps.Keys(uniqueAddedResources)
395-
slices.Sort(data.AddedResources)
396395

397396
// Compute affected resources based on changed files
398397
changedFilesAffectedResources := map[string]struct{}{}
@@ -642,6 +641,38 @@ func formatDiffComment(data diffCommentData) (string, error) {
642641
return sb.String(), nil
643642
}
644643

644+
// addedMultipleResources returns a sorted slice of resource names that are considered "separate" resources.
645+
// In particular, IAM resources are merged with the parent resource as part of this check.
646+
func multipleResources(resources []string) []string {
647+
if len(resources) == 0 {
648+
return nil
649+
}
650+
iam := map[string]struct{}{}
651+
final := map[string]struct{}{}
652+
653+
for _, r := range resources {
654+
if k, found := strings.CutSuffix(r, "_iam_member"); found {
655+
iam[k] = struct{}{}
656+
} else if k, found := strings.CutSuffix(r, "_iam_binding"); found {
657+
iam[k] = struct{}{}
658+
} else if k, found := strings.CutSuffix(r, "_iam_policy"); found {
659+
iam[k] = struct{}{}
660+
} else {
661+
final[k] = struct{}{}
662+
}
663+
}
664+
665+
for r, _ := range iam {
666+
if _, ok := final[r]; !ok {
667+
final[r+"_iam_*"] = struct{}{}
668+
}
669+
}
670+
671+
ret := maps.Keys(final)
672+
slices.Sort(ret)
673+
return ret
674+
}
675+
645676
var resourceFileRegexp = regexp.MustCompile(`^.*/services/[^/]+/(?:data_source_|resource_|iam_)(.*?)(?:_test|_sweeper|_iam_test|_generated_test|_internal_test)?.go`)
646677
var resourceDocsRegexp = regexp.MustCompile(`^.*website/docs/(?:r|d)/(.*).html.markdown`)
647678

.ci/magician/cmd/generate_comment_test.go

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ func TestFormatDiffComment(t *testing.T) {
246246
},
247247
"multiple resources are displayed": {
248248
data: diffCommentData{
249-
AddedResources: []string{"google_redis_instance", "google_alloydb_cluster"},
249+
MultipleResources: []string{"google_redis_instance", "google_alloydb_cluster"},
250250
},
251251
expectedStrings: []string{
252252
"## Diff report",
@@ -352,6 +352,72 @@ func TestFormatDiffComment(t *testing.T) {
352352
}
353353
}
354354

355+
func TestMultipleResources(t *testing.T) {
356+
cases := []struct {
357+
name string
358+
resources []string
359+
want []string
360+
}{
361+
{
362+
name: "no resources",
363+
},
364+
{
365+
name: "single non-iam",
366+
resources: []string{"google_redis_instance"},
367+
want: []string{"google_redis_instance"},
368+
},
369+
{
370+
name: "multiple non-iam",
371+
resources: []string{"google_redis_instance", "google_alloydb_cluster"},
372+
want: []string{"google_alloydb_cluster", "google_redis_instance"},
373+
},
374+
{
375+
name: "single iam only",
376+
resources: []string{"google_redis_instance_iam_member", "google_redis_instance_iam_policy", "google_redis_instance_iam_binding"},
377+
want: []string{"google_redis_instance_iam_*"},
378+
},
379+
{
380+
name: "single iam with parent",
381+
resources: []string{"google_redis_instance_iam_member", "google_redis_instance_iam_policy", "google_redis_instance_iam_binding", "google_redis_instance"},
382+
want: []string{"google_redis_instance"},
383+
},
384+
{
385+
name: "multiple iam",
386+
resources: []string{
387+
"google_redis_instance_iam_member",
388+
"google_redis_instance_iam_policy",
389+
"google_redis_instance_iam_binding",
390+
"google_alloydb_cluster_iam_member",
391+
"google_alloydb_cluster_iam_policy",
392+
"google_alloydb_cluster_iam_binding",
393+
},
394+
want: []string{"google_alloydb_cluster_iam_*", "google_redis_instance_iam_*"},
395+
},
396+
{
397+
name: "multiple iam with parent",
398+
resources: []string{
399+
"google_redis_instance_iam_member",
400+
"google_redis_instance_iam_policy",
401+
"google_redis_instance_iam_binding",
402+
"google_alloydb_cluster_iam_member",
403+
"google_alloydb_cluster_iam_policy",
404+
"google_alloydb_cluster_iam_binding",
405+
"google_redis_instance",
406+
},
407+
want: []string{"google_alloydb_cluster_iam_*", "google_redis_instance"},
408+
},
409+
}
410+
411+
for _, tc := range cases {
412+
t.Run(tc.name, func(t *testing.T) {
413+
t.Parallel()
414+
415+
got := multipleResources(tc.resources)
416+
assert.Equal(t, tc.want, got)
417+
})
418+
}
419+
}
420+
355421
func TestFileToResource(t *testing.T) {
356422
cases := map[string]struct {
357423
path string

.ci/magician/cmd/templates/DIFF_COMMENT.md.tmpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ If you believe this detection to be incorrect please raise the concern with your
5151
An `override-missing-service-label` label can be added to allow merging.
5252
{{end}}
5353

54-
{{- if gt (len .AddedResources) 1 }}
54+
{{- if gt (len .MultipleResources) 1 }}
5555
## Multiple resources added
5656

57-
This PR adds multiple new resources: {{range $i, $resource := .AddedResources}}{{ if gt $i 0}}, {{end}}`{{$resource}}`{{end}}. This makes review significantly more difficult. Please split it into multiple PRs, one per resource.
57+
This PR adds multiple new resources: {{range $i, $resource := .MultipleResources}}{{ if gt $i 0}}, {{end}}`{{$resource}}`{{end}}. This makes review significantly more difficult. Please split it into multiple PRs, one per resource.
5858
An `override-multiple-resources` label can be added to allow merging.
5959
{{end}}
6060

0 commit comments

Comments
 (0)