diff --git a/google/resource_container_cluster.go b/google/resource_container_cluster.go index aa88f57d..6a567c87 100644 --- a/google/resource_container_cluster.go +++ b/google/resource_container_cluster.go @@ -1468,16 +1468,17 @@ func expandMasterAuthorizedNetworksConfig(configured interface{}) *containerBeta result := &containerBeta.MasterAuthorizedNetworksConfig{} if len(configured.([]interface{})) > 0 { result.Enabled = true - config := configured.([]interface{})[0].(map[string]interface{}) - if _, ok := config["cidr_blocks"]; ok { - cidrBlocks := config["cidr_blocks"].(*schema.Set).List() - result.CidrBlocks = make([]*containerBeta.CidrBlock, 0) - for _, v := range cidrBlocks { - cidrBlock := v.(map[string]interface{}) - result.CidrBlocks = append(result.CidrBlocks, &containerBeta.CidrBlock{ - CidrBlock: cidrBlock["cidr_block"].(string), - DisplayName: cidrBlock["display_name"].(string), - }) + if config, ok := configured.([]interface{})[0].(map[string]interface{}); ok { + if _, ok := config["cidr_blocks"]; ok { + cidrBlocks := config["cidr_blocks"].(*schema.Set).List() + result.CidrBlocks = make([]*containerBeta.CidrBlock, 0) + for _, v := range cidrBlocks { + cidrBlock := v.(map[string]interface{}) + result.CidrBlocks = append(result.CidrBlocks, &containerBeta.CidrBlock{ + CidrBlock: cidrBlock["cidr_block"].(string), + DisplayName: cidrBlock["display_name"].(string), + }) + } } } } @@ -1629,9 +1630,6 @@ func flattenMasterAuthorizedNetworksConfig(c *containerBeta.MasterAuthorizedNetw if c == nil { return nil } - if len(c.CidrBlocks) == 0 { - return nil - } result := make(map[string]interface{}) if c.Enabled { cidrBlocks := make([]interface{}, 0, len(c.CidrBlocks)) diff --git a/google/resource_container_cluster_test.go b/google/resource_container_cluster_test.go index cd9c1de0..3566fb9f 100644 --- a/google/resource_container_cluster_test.go +++ b/google/resource_container_cluster_test.go @@ -276,7 +276,16 @@ func TestAccContainerCluster_withMasterAuthorizedNetworksConfig(t *testing.T) { CheckDestroy: testAccCheckContainerClusterDestroy, Steps: []resource.TestStep{ { - Config: testAccContainerCluster_withMasterAuthorizedNetworksConfig(clusterName, []string{"8.8.8.8/32"}), + Config: testAccContainerCluster_withMasterAuthorizedNetworksConfig(clusterName, []string{}, "cidr_blocks = []"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("google_container_cluster.with_master_authorized_networks", + "master_authorized_networks_config.#", "1"), + resource.TestCheckResourceAttr("google_container_cluster.with_master_authorized_networks", + "master_authorized_networks_config.0.cidr_blocks.#", "0"), + ), + }, + { + Config: testAccContainerCluster_withMasterAuthorizedNetworksConfig(clusterName, []string{"8.8.8.8/32"}, ""), Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr("google_container_cluster.with_master_authorized_networks", "master_authorized_networks_config.0.cidr_blocks.#", "1"), @@ -289,7 +298,7 @@ func TestAccContainerCluster_withMasterAuthorizedNetworksConfig(t *testing.T) { ImportStateIdPrefix: "us-central1-a/", }, { - Config: testAccContainerCluster_withMasterAuthorizedNetworksConfig(clusterName, []string{"10.0.0.0/8", "8.8.8.8/32"}), + Config: testAccContainerCluster_withMasterAuthorizedNetworksConfig(clusterName, []string{"10.0.0.0/8", "8.8.8.8/32"}, ""), }, { ResourceName: "google_container_cluster.with_master_authorized_networks", @@ -298,7 +307,7 @@ func TestAccContainerCluster_withMasterAuthorizedNetworksConfig(t *testing.T) { ImportStateIdPrefix: "us-central1-a/", }, { - Config: testAccContainerCluster_withMasterAuthorizedNetworksConfig(clusterName, []string{}), + Config: testAccContainerCluster_withMasterAuthorizedNetworksConfig(clusterName, []string{}, ""), Check: resource.ComposeTestCheckFunc( resource.TestCheckNoResourceAttr("google_container_cluster.with_master_authorized_networks", "master_authorized_networks_config.0.cidr_blocks"), @@ -1591,9 +1600,9 @@ resource "google_container_cluster" "with_network_policy_enabled" { }`, clusterName) } -func testAccContainerCluster_withMasterAuthorizedNetworksConfig(clusterName string, cidrs []string) string { +func testAccContainerCluster_withMasterAuthorizedNetworksConfig(clusterName string, cidrs []string, emptyValue string) string { - cidrBlocks := "" + cidrBlocks := emptyValue if len(cidrs) > 0 { var buf bytes.Buffer buf.WriteString("cidr_blocks = [")