Skip to content

Commit

Permalink
Fix crash in containerattached when removing admin_groups or admin_us…
Browse files Browse the repository at this point in the history
…ers. (#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]>
  • Loading branch information
modular-magician authored Dec 20, 2023
1 parent 9db20ba commit f7a618e
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 15 deletions.
3 changes: 3 additions & 0 deletions .changelog/9647.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
containerattached: fixed crash when updating a cluster to remove `admin_users` or `admin_groups`
```
Original file line number Diff line number Diff line change
Expand Up @@ -1167,23 +1167,27 @@ func flattenContainerAttachedClusterErrorsMessage(v interface{}, d *schema.Resou
// ],
// }
func flattenContainerAttachedClusterAuthorization(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
if v == nil {
if v == nil || len(v.(map[string]interface{})) == 0 {
return nil
}

orig := v.(map[string]interface{})["adminUsers"].([]interface{})
transformed := make(map[string][]string)
transformed["admin_users"] = make([]string, len(orig))
for i, u := range orig {
if u != nil {
transformed["admin_users"][i] = u.(map[string]interface{})["username"].(string)
if v.(map[string]interface{})["adminUsers"] != nil {
orig := v.(map[string]interface{})["adminUsers"].([]interface{})
transformed["admin_users"] = make([]string, len(orig))
for i, u := range orig {
if u != nil {
transformed["admin_users"][i] = u.(map[string]interface{})["username"].(string)
}
}
}
orig = v.(map[string]interface{})["adminGroups"].([]interface{})
transformed["admin_groups"] = make([]string, len(orig))
for i, u := range orig {
if u != nil {
transformed["admin_groups"][i] = u.(map[string]interface{})["group"].(string)
if v.(map[string]interface{})["adminGroups"] != nil {
orig := v.(map[string]interface{})["adminGroups"].([]interface{})
transformed["admin_groups"] = make([]string, len(orig))
for i, u := range orig {
if u != nil {
transformed["admin_groups"][i] = u.(map[string]interface{})["group"].(string)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ func TestAccContainerAttachedCluster_update(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"location", "annotations"},
},
{
Config: testAccContainerAttachedCluster_containerAttachedCluster_removeAuthorizationUsers(context),
},
{
ResourceName: "google_container_attached_cluster.primary",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"location", "annotations"},
},
{
Config: testAccContainerAttachedCluster_containerAttachedCluster_removeAuthorizationGroups(context),
},
{
ResourceName: "google_container_attached_cluster.primary",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"location", "annotations"},
},
{
Config: testAccContainerAttachedCluster_containerAttachedCluster_destroy(context),
},
Expand Down Expand Up @@ -157,9 +175,7 @@ resource "google_container_attached_cluster" "primary" {
`, context)
}

// Duplicate of testAccContainerAttachedCluster_containerAttachedCluster_update without lifecycle.prevent_destroy set
// so the test can clean up the resource after the update.
func testAccContainerAttachedCluster_containerAttachedCluster_destroy(context map[string]interface{}) string {
func testAccContainerAttachedCluster_containerAttachedCluster_removeAuthorizationUsers(context map[string]interface{}) string {
return acctest.Nprintf(`
data "google_project" "project" {
}
Expand All @@ -180,7 +196,6 @@ resource "google_container_attached_cluster" "primary" {
label-two = "value-two"
}
authorization {
admin_users = [ "[email protected]", "[email protected]"]
admin_groups = [ "[email protected]"]
}
oidc_config {
Expand All @@ -203,6 +218,102 @@ resource "google_container_attached_cluster" "primary" {
namespace = "custom-ns"
}
}
lifecycle {
prevent_destroy = true
}
}
`, context)
}

func testAccContainerAttachedCluster_containerAttachedCluster_removeAuthorizationGroups(context map[string]interface{}) string {
return acctest.Nprintf(`
data "google_project" "project" {
}
data "google_container_attached_versions" "versions" {
location = "us-west1"
project = data.google_project.project.project_id
}
resource "google_container_attached_cluster" "primary" {
name = "update%{random_suffix}"
project = data.google_project.project.project_id
location = "us-west1"
description = "Test cluster updated"
distribution = "aks"
annotations = {
label-one = "value-one"
label-two = "value-two"
}
oidc_config {
issuer_url = "https://oidc.issuer.url"
jwks = base64encode("{\"keys\":[{\"use\":\"sig\",\"kty\":\"RSA\",\"kid\":\"testid\",\"alg\":\"RS256\",\"n\":\"somedata\",\"e\":\"AQAB\"}]}")
}
platform_version = data.google_container_attached_versions.versions.valid_versions[0]
fleet {
project = "projects/${data.google_project.project.number}"
}
monitoring_config {
managed_prometheus_config {}
}
binary_authorization {
evaluation_mode = "DISABLED"
}
proxy_config {
kubernetes_secret {
name = "new-proxy-config"
namespace = "custom-ns"
}
}
lifecycle {
prevent_destroy = true
}
}
`, context)
}

// Duplicate of testAccContainerAttachedCluster_containerAttachedCluster_update without lifecycle.prevent_destroy set
// so the test can clean up the resource after the update.
func testAccContainerAttachedCluster_containerAttachedCluster_destroy(context map[string]interface{}) string {
return acctest.Nprintf(`
data "google_project" "project" {
}
data "google_container_attached_versions" "versions" {
location = "us-west1"
project = data.google_project.project.project_id
}
resource "google_container_attached_cluster" "primary" {
name = "update%{random_suffix}"
project = data.google_project.project.project_id
location = "us-west1"
description = "Test cluster updated"
distribution = "aks"
annotations = {
label-one = "value-one"
label-two = "value-two"
}
oidc_config {
issuer_url = "https://oidc.issuer.url"
jwks = base64encode("{\"keys\":[{\"use\":\"sig\",\"kty\":\"RSA\",\"kid\":\"testid\",\"alg\":\"RS256\",\"n\":\"somedata\",\"e\":\"AQAB\"}]}")
}
platform_version = data.google_container_attached_versions.versions.valid_versions[0]
fleet {
project = "projects/${data.google_project.project.number}"
}
monitoring_config {
managed_prometheus_config {}
}
binary_authorization {
evaluation_mode = "DISABLED"
}
proxy_config {
kubernetes_secret {
name = "new-proxy-config"
namespace = "custom-ns"
}
}
}
`, context)
}

0 comments on commit f7a618e

Please sign in to comment.