Skip to content

Commit 6f26e8f

Browse files
Fix duplicate CAI asset name path segments for node pools (#16442) (#11613)
[upstream:cbc1ed41059d7b1e4f44011defd77360857ca3b9] Signed-off-by: Modular Magician <magic-modules@google.com>
1 parent 288d464 commit 6f26e8f

File tree

2 files changed

+125
-20
lines changed

2 files changed

+125
-20
lines changed

google-beta/acctest/tgc_utils.go

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,16 @@ func CollectAllTgcMetadata(tgcPayload TgcMetadataPayload) resource.TestCheckFunc
157157
caiAssetNameFormat := ""
158158
if len(yamlMetadata.CaiAssetNameFormats) == 1 {
159159
caiAssetNameFormat = yamlMetadata.CaiAssetNameFormats[0]
160+
caiAssetName := formatCaiAssetName(caiAssetNameFormat, rState.Primary.Attributes)
161+
if _, ok := serviceWithProjectNumber[metadata.Service]; ok {
162+
caiAssetName = strings.Replace(caiAssetName, projectId, projectNumber, 1)
163+
}
164+
metadata.CaiAssetNames = []string{caiAssetName}
160165
} else {
161166
if strings.HasPrefix(yamlMetadata.Resource, "google_container_") {
162-
caiAssetNameFormat = resolveContainerCaiAssetNameFormat(yamlMetadata.CaiAssetNameFormats, rState.Primary.Attributes["location"])
167+
metadata.CaiAssetNames = resolveContainerCaiAssetName(yamlMetadata.CaiAssetNameFormats, rState.Primary.ID, rState.Primary.Attributes["location"])
163168
}
164169
}
165-
166-
caiAssetName := formatCaiAssetName(caiAssetNameFormat, rState.Primary.Attributes)
167-
if _, ok := serviceWithProjectNumber[metadata.Service]; ok {
168-
caiAssetName = strings.Replace(caiAssetName, projectId, projectNumber, 1)
169-
}
170-
metadata.CaiAssetNames = []string{caiAssetName}
171170
}
172171
}
173172

@@ -196,23 +195,23 @@ func CollectAllTgcMetadata(tgcPayload TgcMetadataPayload) resource.TestCheckFunc
196195
}
197196
}
198197

199-
// resolveContainerCaiAssetNameFormat determines the correct CAI asset name format
198+
// resolveContainerCaiAssetName determines the correct CAI asset name
200199
// for GKE resources based on whether the resource location is a zone or a region.
201-
func resolveContainerCaiAssetNameFormat(caiAssetNameFormats []string, location string) string {
200+
func resolveContainerCaiAssetName(caiAssetNameFormats []string, rName, location string) []string {
201+
serviceDomain := getServiceDomain(caiAssetNameFormats[0])
202202
if tpgresource.IsZone(location) {
203-
for _, nameFormat := range caiAssetNameFormats {
204-
if strings.Contains(nameFormat, "/zones/") {
205-
return nameFormat
206-
}
207-
}
208-
} else {
209-
for _, nameFormat := range caiAssetNameFormats {
210-
if strings.Contains(nameFormat, "/locations/") {
211-
return nameFormat
212-
}
213-
}
203+
rName = strings.Replace(rName, "/locations/", "/zones/", 1)
214204
}
215205

206+
return []string{fmt.Sprintf("//%s/%s", serviceDomain, rName)}
207+
}
208+
209+
func getServiceDomain(caiAssetName string) string {
210+
// caiAssetName format: //container.googleapis.com/projects/...
211+
parts := strings.Split(caiAssetName, "/")
212+
if len(parts) >= 3 {
213+
return parts[2]
214+
}
216215
return ""
217216
}
218217

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Copyright (c) HashiCorp, Inc.
2+
// SPDX-License-Identifier: MPL-2.0
3+
// ----------------------------------------------------------------------------
4+
//
5+
// *** AUTO GENERATED CODE *** Type: Handwritten ***
6+
//
7+
// ----------------------------------------------------------------------------
8+
//
9+
// This code is generated by Magic Modules using the following:
10+
//
11+
// Source file: https://github.com/GoogleCloudPlatform/magic-modules/tree/main/mmv1/third_party/terraform/acctest/tgc_utils_test.go
12+
//
13+
// DO NOT EDIT this file directly. Any changes made to this file will be
14+
// overwritten during the next generation cycle.
15+
//
16+
// ----------------------------------------------------------------------------
17+
package acctest
18+
19+
import (
20+
"reflect"
21+
"testing"
22+
)
23+
24+
func TestGetServiceDomain(t *testing.T) {
25+
tests := []struct {
26+
name string
27+
cai string
28+
want string
29+
}{
30+
{
31+
name: "valid cai",
32+
cai: "//container.googleapis.com/projects/foo",
33+
want: "container.googleapis.com",
34+
},
35+
{
36+
name: "valid cai with deeper path",
37+
cai: "//storage.googleapis.com/projects/foo/buckets/bar",
38+
want: "storage.googleapis.com",
39+
},
40+
{
41+
name: "invalid cai - too short",
42+
cai: "foo",
43+
want: "",
44+
},
45+
{
46+
name: "empty",
47+
cai: "",
48+
want: "",
49+
},
50+
}
51+
52+
for _, tt := range tests {
53+
t.Run(tt.name, func(t *testing.T) {
54+
if got := getServiceDomain(tt.cai); got != tt.want {
55+
t.Errorf("getServiceDomain() = %v, want %v", got, tt.want)
56+
}
57+
})
58+
}
59+
}
60+
61+
func TestResolveContainerCaiAssetName(t *testing.T) {
62+
formats := []string{
63+
"//container.googleapis.com/projects/{{project}}/zones/{{location}}/clusters/{{cluster}}",
64+
}
65+
66+
tests := []struct {
67+
name string
68+
rName string
69+
location string
70+
want []string
71+
}{
72+
{
73+
name: "zonal location replacement",
74+
rName: "projects/p/locations/us-central1-a/clusters/c",
75+
location: "us-central1-a",
76+
want: []string{
77+
"//container.googleapis.com/projects/p/zones/us-central1-a/clusters/c",
78+
},
79+
},
80+
{
81+
name: "regional location no change",
82+
rName: "projects/p/locations/us-central1/clusters/c",
83+
location: "us-central1",
84+
want: []string{
85+
"//container.googleapis.com/projects/p/locations/us-central1/clusters/c",
86+
},
87+
},
88+
{
89+
name: "unknown location type defaults to no change",
90+
rName: "projects/p/locations/global/clusters/c",
91+
location: "global",
92+
want: []string{
93+
"//container.googleapis.com/projects/p/locations/global/clusters/c",
94+
},
95+
},
96+
}
97+
98+
for _, tt := range tests {
99+
t.Run(tt.name, func(t *testing.T) {
100+
got := resolveContainerCaiAssetName(formats, tt.rName, tt.location)
101+
if !reflect.DeepEqual(got, tt.want) {
102+
t.Errorf("resolveContainerCaiAssetName() = %v, want %v", got, tt.want)
103+
}
104+
})
105+
}
106+
}

0 commit comments

Comments
 (0)