diff --git a/builtin/providers/google/resource_container_cluster.go b/builtin/providers/google/resource_container_cluster.go index 4f1870b30..203a990b8 100644 --- a/builtin/providers/google/resource_container_cluster.go +++ b/builtin/providers/google/resource_container_cluster.go @@ -522,27 +522,11 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro d.Set("subnetwork", cluster.Subnetwork) d.Set("node_config", flattenClusterNodeConfig(cluster.NodeConfig)) - // container engine's API currently mistakenly returns the instance group manager's - // URL instead of the instance group's URL in its responses. This shim detects that - // error, and corrects it, by fetching the instance group manager URL and retrieving - // the instance group manager, then using that to look up the instance group URL, which - // is then substituted. - // - // This should be removed when the API response is fixed. - instanceGroupURLs := make([]string, 0, len(cluster.InstanceGroupUrls)) - for _, u := range cluster.InstanceGroupUrls { - if !instanceGroupManagerURL.MatchString(u) { - instanceGroupURLs = append(instanceGroupURLs, u) - continue - } - matches := instanceGroupManagerURL.FindStringSubmatch(u) - instanceGroupManager, err := config.clientCompute.InstanceGroupManagers.Get(matches[1], matches[2], matches[3]).Do() - if err != nil { - return fmt.Errorf("Error reading instance group manager returned as an instance group URL: %s", err) - } - instanceGroupURLs = append(instanceGroupURLs, instanceGroupManager.InstanceGroup) + if igUrls, err := getInstanceGroupUrlsFromManagerUrls(config, cluster.InstanceGroupUrls); err != nil { + return err + } else { + d.Set("instance_group_urls", igUrls) } - d.Set("instance_group_urls", instanceGroupURLs) return nil } @@ -613,6 +597,30 @@ func resourceContainerClusterDelete(d *schema.ResourceData, meta interface{}) er return nil } +// container engine's API currently mistakenly returns the instance group manager's +// URL instead of the instance group's URL in its responses. This shim detects that +// error, and corrects it, by fetching the instance group manager URL and retrieving +// the instance group manager, then using that to look up the instance group URL, which +// is then substituted. +// +// This should be removed when the API response is fixed. +func getInstanceGroupUrlsFromManagerUrls(config *Config, igmUrls []string) ([]string, error) { + instanceGroupURLs := make([]string, 0, len(igmUrls)) + for _, u := range igmUrls { + if !instanceGroupManagerURL.MatchString(u) { + instanceGroupURLs = append(instanceGroupURLs, u) + continue + } + matches := instanceGroupManagerURL.FindStringSubmatch(u) + instanceGroupManager, err := config.clientCompute.InstanceGroupManagers.Get(matches[1], matches[2], matches[3]).Do() + if err != nil { + return nil, fmt.Errorf("Error reading instance group manager returned as an instance group URL: %s", err) + } + instanceGroupURLs = append(instanceGroupURLs, instanceGroupManager.InstanceGroup) + } + return instanceGroupURLs, nil +} + func flattenClusterNodeConfig(c *container.NodeConfig) []map[string]interface{} { config := []map[string]interface{}{ map[string]interface{}{ diff --git a/builtin/providers/google/resource_container_cluster_test.go b/builtin/providers/google/resource_container_cluster_test.go index 09fad2d3d..f0723dcb1 100644 --- a/builtin/providers/google/resource_container_cluster_test.go +++ b/builtin/providers/google/resource_container_cluster_test.go @@ -174,6 +174,10 @@ func testAccCheckContainerCluster(n string) resource.TestCheckFunc { gcp_attr interface{} } + var igUrls []string + if igUrls, err = getInstanceGroupUrlsFromManagerUrls(config, cluster.InstanceGroupUrls); err != nil { + return err + } clusterTests := []clusterTestField{ {"initial_node_count", strconv.FormatInt(cluster.InitialNodeCount, 10)}, {"master_auth.0.client_certificate", cluster.MasterAuth.ClientCertificate}, @@ -185,7 +189,7 @@ func testAccCheckContainerCluster(n string) resource.TestCheckFunc { {"cluster_ipv4_cidr", cluster.ClusterIpv4Cidr}, {"description", cluster.Description}, {"endpoint", cluster.Endpoint}, - {"instance_group_urls", cluster.InstanceGroupUrls}, + {"instance_group_urls", igUrls}, {"logging_service", cluster.LoggingService}, {"monitoring_service", cluster.MonitoringService}, {"subnetwork", cluster.Subnetwork},