From 13fe0fca91497c95e9e4b5a597a524f3898fef3a Mon Sep 17 00:00:00 2001 From: Dana Hoffman Date: Fri, 17 May 2019 20:10:56 +0000 Subject: [PATCH] use the cluster subnet to look up the node cidr block Signed-off-by: Modular Magician --- google-beta/resource_container_cluster.go | 31 ++++++++++--------- .../resource_container_cluster_test.go | 9 +----- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/google-beta/resource_container_cluster.go b/google-beta/resource_container_cluster.go index 8d0fc6835f9..9490c44ed9e 100644 --- a/google-beta/resource_container_cluster.go +++ b/google-beta/resource_container_cluster.go @@ -976,7 +976,7 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro return err } - if err := d.Set("ip_allocation_policy", flattenIPAllocationPolicy(cluster.IpAllocationPolicy, d, config)); err != nil { + if err := d.Set("ip_allocation_policy", flattenIPAllocationPolicy(cluster, d, config)); err != nil { return err } @@ -2045,35 +2045,36 @@ func flattenPrivateClusterConfig(c *containerBeta.PrivateClusterConfig) []map[st } } -func flattenIPAllocationPolicy(c *containerBeta.IPAllocationPolicy, d *schema.ResourceData, config *Config) []map[string]interface{} { - if c == nil { +func flattenIPAllocationPolicy(c *containerBeta.Cluster, d *schema.ResourceData, config *Config) []map[string]interface{} { + if c == nil || c.IpAllocationPolicy == nil { return nil } - node_cidr_block := "" - if c.SubnetworkName != "" { - subnetwork, err := ParseSubnetworkFieldValue(c.SubnetworkName, d, config) + nodeCidrBlock := "" + if c.Subnetwork != "" { + subnetwork, err := ParseSubnetworkFieldValue(c.Subnetwork, d, config) if err == nil { sn, err := config.clientCompute.Subnetworks.Get(subnetwork.Project, subnetwork.Region, subnetwork.Name).Do() if err == nil { - node_cidr_block = sn.IpCidrRange + nodeCidrBlock = sn.IpCidrRange } } else { log.Printf("[WARN] Unable to parse subnetwork name, got error while trying to get new subnetwork: %s", err) } } + p := c.IpAllocationPolicy return []map[string]interface{}{ { - "use_ip_aliases": c.UseIpAliases, + "use_ip_aliases": p.UseIpAliases, - "create_subnetwork": c.CreateSubnetwork, - "subnetwork_name": c.SubnetworkName, + "create_subnetwork": p.CreateSubnetwork, + "subnetwork_name": p.SubnetworkName, - "cluster_ipv4_cidr_block": c.ClusterIpv4CidrBlock, - "services_ipv4_cidr_block": c.ServicesIpv4CidrBlock, - "node_ipv4_cidr_block": node_cidr_block, + "cluster_ipv4_cidr_block": p.ClusterIpv4CidrBlock, + "services_ipv4_cidr_block": p.ServicesIpv4CidrBlock, + "node_ipv4_cidr_block": nodeCidrBlock, - "cluster_secondary_range_name": c.ClusterSecondaryRangeName, - "services_secondary_range_name": c.ServicesSecondaryRangeName, + "cluster_secondary_range_name": p.ClusterSecondaryRangeName, + "services_secondary_range_name": p.ServicesSecondaryRangeName, }, } } diff --git a/google-beta/resource_container_cluster_test.go b/google-beta/resource_container_cluster_test.go index d375da07509..9417c50b48b 100644 --- a/google-beta/resource_container_cluster_test.go +++ b/google-beta/resource_container_cluster_test.go @@ -2823,22 +2823,15 @@ resource "google_compute_network" "container_network" { auto_create_subnetworks = false } -resource "google_compute_subnetwork" "container_subnetwork" { - name = "${google_compute_network.container_network.name}" - network = "${google_compute_network.container_network.name}" - ip_cidr_range = "10.128.0.0/9" - region = "us-central1" -} - resource "google_container_cluster" "with_ip_allocation_policy" { name = "%s" zone = "us-central1-a" network = "${google_compute_network.container_network.name}" - subnetwork = "${google_compute_subnetwork.container_subnetwork.name}" initial_node_count = 1 ip_allocation_policy { use_ip_aliases = true + create_subnetwork = true cluster_ipv4_cidr_block = "10.0.0.0/16" services_ipv4_cidr_block = "10.1.0.0/16" node_ipv4_cidr_block = "10.2.0.0/16"