Skip to content

Commit f7a618e

Browse files
Fix crash in containerattached when removing admin_groups or admin_users. (#9647) (#6786)
* Fix crash in containerattached when removing admin_groups or admin_users. * Format test file. [upstream:1bb86d9a9272c4b2ab46186dd35fe76e9c3a2296] Signed-off-by: Modular Magician <[email protected]>
1 parent 9db20ba commit f7a618e

File tree

3 files changed

+133
-15
lines changed

3 files changed

+133
-15
lines changed

.changelog/9647.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
containerattached: fixed crash when updating a cluster to remove `admin_users` or `admin_groups`
3+
```

google-beta/services/containerattached/resource_container_attached_cluster.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,23 +1167,27 @@ func flattenContainerAttachedClusterErrorsMessage(v interface{}, d *schema.Resou
11671167
// ],
11681168
// }
11691169
func flattenContainerAttachedClusterAuthorization(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
1170-
if v == nil {
1170+
if v == nil || len(v.(map[string]interface{})) == 0 {
11711171
return nil
11721172
}
11731173

1174-
orig := v.(map[string]interface{})["adminUsers"].([]interface{})
11751174
transformed := make(map[string][]string)
1176-
transformed["admin_users"] = make([]string, len(orig))
1177-
for i, u := range orig {
1178-
if u != nil {
1179-
transformed["admin_users"][i] = u.(map[string]interface{})["username"].(string)
1175+
if v.(map[string]interface{})["adminUsers"] != nil {
1176+
orig := v.(map[string]interface{})["adminUsers"].([]interface{})
1177+
transformed["admin_users"] = make([]string, len(orig))
1178+
for i, u := range orig {
1179+
if u != nil {
1180+
transformed["admin_users"][i] = u.(map[string]interface{})["username"].(string)
1181+
}
11801182
}
11811183
}
1182-
orig = v.(map[string]interface{})["adminGroups"].([]interface{})
1183-
transformed["admin_groups"] = make([]string, len(orig))
1184-
for i, u := range orig {
1185-
if u != nil {
1186-
transformed["admin_groups"][i] = u.(map[string]interface{})["group"].(string)
1184+
if v.(map[string]interface{})["adminGroups"] != nil {
1185+
orig := v.(map[string]interface{})["adminGroups"].([]interface{})
1186+
transformed["admin_groups"] = make([]string, len(orig))
1187+
for i, u := range orig {
1188+
if u != nil {
1189+
transformed["admin_groups"][i] = u.(map[string]interface{})["group"].(string)
1190+
}
11871191
}
11881192
}
11891193

google-beta/services/containerattached/resource_container_attached_cluster_update_test.go

Lines changed: 115 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,24 @@ func TestAccContainerAttachedCluster_update(t *testing.T) {
3939
ImportStateVerify: true,
4040
ImportStateVerifyIgnore: []string{"location", "annotations"},
4141
},
42+
{
43+
Config: testAccContainerAttachedCluster_containerAttachedCluster_removeAuthorizationUsers(context),
44+
},
45+
{
46+
ResourceName: "google_container_attached_cluster.primary",
47+
ImportState: true,
48+
ImportStateVerify: true,
49+
ImportStateVerifyIgnore: []string{"location", "annotations"},
50+
},
51+
{
52+
Config: testAccContainerAttachedCluster_containerAttachedCluster_removeAuthorizationGroups(context),
53+
},
54+
{
55+
ResourceName: "google_container_attached_cluster.primary",
56+
ImportState: true,
57+
ImportStateVerify: true,
58+
ImportStateVerifyIgnore: []string{"location", "annotations"},
59+
},
4260
{
4361
Config: testAccContainerAttachedCluster_containerAttachedCluster_destroy(context),
4462
},
@@ -157,9 +175,7 @@ resource "google_container_attached_cluster" "primary" {
157175
`, context)
158176
}
159177

160-
// Duplicate of testAccContainerAttachedCluster_containerAttachedCluster_update without lifecycle.prevent_destroy set
161-
// so the test can clean up the resource after the update.
162-
func testAccContainerAttachedCluster_containerAttachedCluster_destroy(context map[string]interface{}) string {
178+
func testAccContainerAttachedCluster_containerAttachedCluster_removeAuthorizationUsers(context map[string]interface{}) string {
163179
return acctest.Nprintf(`
164180
data "google_project" "project" {
165181
}
@@ -180,7 +196,6 @@ resource "google_container_attached_cluster" "primary" {
180196
label-two = "value-two"
181197
}
182198
authorization {
183-
admin_users = [ "[email protected]", "[email protected]"]
184199
admin_groups = [ "[email protected]"]
185200
}
186201
oidc_config {
@@ -203,6 +218,102 @@ resource "google_container_attached_cluster" "primary" {
203218
namespace = "custom-ns"
204219
}
205220
}
221+
lifecycle {
222+
prevent_destroy = true
223+
}
224+
}
225+
`, context)
226+
}
227+
228+
func testAccContainerAttachedCluster_containerAttachedCluster_removeAuthorizationGroups(context map[string]interface{}) string {
229+
return acctest.Nprintf(`
230+
data "google_project" "project" {
231+
}
232+
233+
data "google_container_attached_versions" "versions" {
234+
location = "us-west1"
235+
project = data.google_project.project.project_id
236+
}
237+
238+
resource "google_container_attached_cluster" "primary" {
239+
name = "update%{random_suffix}"
240+
project = data.google_project.project.project_id
241+
location = "us-west1"
242+
description = "Test cluster updated"
243+
distribution = "aks"
244+
annotations = {
245+
label-one = "value-one"
246+
label-two = "value-two"
247+
}
248+
oidc_config {
249+
issuer_url = "https://oidc.issuer.url"
250+
jwks = base64encode("{\"keys\":[{\"use\":\"sig\",\"kty\":\"RSA\",\"kid\":\"testid\",\"alg\":\"RS256\",\"n\":\"somedata\",\"e\":\"AQAB\"}]}")
251+
}
252+
platform_version = data.google_container_attached_versions.versions.valid_versions[0]
253+
fleet {
254+
project = "projects/${data.google_project.project.number}"
255+
}
256+
monitoring_config {
257+
managed_prometheus_config {}
258+
}
259+
binary_authorization {
260+
evaluation_mode = "DISABLED"
261+
}
262+
proxy_config {
263+
kubernetes_secret {
264+
name = "new-proxy-config"
265+
namespace = "custom-ns"
266+
}
267+
}
268+
lifecycle {
269+
prevent_destroy = true
270+
}
271+
}
272+
`, context)
273+
}
274+
275+
// Duplicate of testAccContainerAttachedCluster_containerAttachedCluster_update without lifecycle.prevent_destroy set
276+
// so the test can clean up the resource after the update.
277+
func testAccContainerAttachedCluster_containerAttachedCluster_destroy(context map[string]interface{}) string {
278+
return acctest.Nprintf(`
279+
data "google_project" "project" {
280+
}
281+
282+
data "google_container_attached_versions" "versions" {
283+
location = "us-west1"
284+
project = data.google_project.project.project_id
285+
}
286+
287+
resource "google_container_attached_cluster" "primary" {
288+
name = "update%{random_suffix}"
289+
project = data.google_project.project.project_id
290+
location = "us-west1"
291+
description = "Test cluster updated"
292+
distribution = "aks"
293+
annotations = {
294+
label-one = "value-one"
295+
label-two = "value-two"
296+
}
297+
oidc_config {
298+
issuer_url = "https://oidc.issuer.url"
299+
jwks = base64encode("{\"keys\":[{\"use\":\"sig\",\"kty\":\"RSA\",\"kid\":\"testid\",\"alg\":\"RS256\",\"n\":\"somedata\",\"e\":\"AQAB\"}]}")
300+
}
301+
platform_version = data.google_container_attached_versions.versions.valid_versions[0]
302+
fleet {
303+
project = "projects/${data.google_project.project.number}"
304+
}
305+
monitoring_config {
306+
managed_prometheus_config {}
307+
}
308+
binary_authorization {
309+
evaluation_mode = "DISABLED"
310+
}
311+
proxy_config {
312+
kubernetes_secret {
313+
name = "new-proxy-config"
314+
namespace = "custom-ns"
315+
}
316+
}
206317
}
207318
`, context)
208319
}

0 commit comments

Comments
 (0)