From b3ff0e5a9e5a3f0c435b3360180ea89dd39b4399 Mon Sep 17 00:00:00 2001 From: Ashish Amarnath Date: Fri, 23 Mar 2018 15:14:20 -0700 Subject: [PATCH] * implement a method to generate the update function and refactor --- google/resource_container_cluster.go | 73 +++++++++++++++------------- 1 file changed, 38 insertions(+), 35 deletions(-) diff --git a/google/resource_container_cluster.go b/google/resource_container_cluster.go index d63e3d5bc2c..fff93fca51e 100644 --- a/google/resource_container_cluster.go +++ b/google/resource_container_cluster.go @@ -784,6 +784,30 @@ func resourceContainerClusterRead(d *schema.ResourceData, meta interface{}) erro return nil } +func getUpdateFunc(req *container.UpdateClusterRequest, config *Config, apiVersion ApiVersion, project, location, clusterName, updateDescription string, timeoutInMinutes int) func() error { + return func() error { + var err error + var op interface{} + switch apiVersion { + case v1: + op, err = config.clientContainer.Projects.Zones.Clusters.Update(project, location, clusterName, req).Do() + case v1beta1: + reqV1Beta := &containerBeta.UpdateClusterRequest{} + err = Convert(req, reqV1Beta) + if err != nil { + return err + } + name := fmt.Sprintf("projects/%s/locations/%s/clusters/%s", project, location, clusterName) + op, err = config.clientContainerBeta.Projects.Locations.Clusters.Update(name, reqV1Beta).Do() + } + if err != nil { + return err + } + // Wait until it's updated + return containerSharedOperationWait(config, op, project, location, updateDescription, timeoutInMinutes, 2) + } +} + func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) error { containerAPIVersion := getContainerApiVersion(d, ContainerClusterBaseApiVersion, ContainerClusterVersionedFeatures) config := meta.(*Config) @@ -814,29 +838,6 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er // The ClusterUpdate object that we use for most of these updates only allows updating one field at a time, // so we have to make separate calls for each field that we want to update. The order here is fairly arbitrary- // if the order of updating fields does matter, it is called out explicitly. - var req *container.UpdateClusterRequest - var updateDescription string - updateF := func() error { - var op interface{} - switch containerAPIVersion { - case v1: - op, err = config.clientContainer.Projects.Zones.Clusters.Update( - project, location, clusterName, req).Do() - case v1beta1: - reqV1Beta := &containerBeta.UpdateClusterRequest{} - err = Convert(req, reqV1Beta) - if err != nil { - return err - } - name := fmt.Sprintf("projects/%s/locations/%s/clusters/%s", project, location, clusterName) - op, err = config.clientContainerBeta.Projects.Locations.Clusters.Update(name, reqV1Beta).Do() - } - if err != nil { - return err - } - // Wait until it's updated - return containerSharedOperationWait(config, op, project, location, updateDescription, timeoutInMinutes, 2) - } if d.HasChange("master_authorized_networks_config") { c := d.Get("master_authorized_networks_config") @@ -845,12 +846,13 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er if err != nil { return err } - req = &container.UpdateClusterRequest{ + req := &container.UpdateClusterRequest{ Update: &container.ClusterUpdate{ DesiredMasterAuthorizedNetworksConfig: conf, }, } - updateDescription = "updating GKE cluster master authorized networks" + + updateF := getUpdateFunc(req, config, containerAPIVersion, project, location, clusterName, "updating GKE cluster master authorized networks", timeoutInMinutes) if err := lockedCall(lockKey, updateF); err != nil { return err } @@ -874,13 +876,13 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er // Only upgrade the master if the current version is lower than the desired version if cur.LessThan(des) { - req = &container.UpdateClusterRequest{ + req := &container.UpdateClusterRequest{ Update: &container.ClusterUpdate{ DesiredMasterVersion: desiredMasterVersion, }, } - updateDescription = "updating GKE master version" + updateF := getUpdateFunc(req, config, containerAPIVersion, project, location, clusterName, "updating GKE master version", timeoutInMinutes) // Call update serially. if err := lockedCall(lockKey, updateF); err != nil { return err @@ -892,13 +894,13 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er if d.HasChange("node_version") { desiredNodeVersion := d.Get("node_version").(string) - req = &container.UpdateClusterRequest{ + req := &container.UpdateClusterRequest{ Update: &container.ClusterUpdate{ DesiredNodeVersion: desiredNodeVersion, }, } - updateDescription = "updating GKE node version" + updateF := getUpdateFunc(req, config, containerAPIVersion, project, location, clusterName, "updating GKE node version", timeoutInMinutes) // Call update serially. if err := lockedCall(lockKey, updateF); err != nil { return err @@ -916,13 +918,13 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er if err != nil { return err } - req = &container.UpdateClusterRequest{ + req := &container.UpdateClusterRequest{ Update: &container.ClusterUpdate{ DesiredAddonsConfig: conf, }, } - updateDescription = "updating GKE cluster addons" + updateF := getUpdateFunc(req, config, containerAPIVersion, project, location, clusterName, "updating GKE cluster addons", timeoutInMinutes) // Call update serially. if err := lockedCall(lockKey, updateF); err != nil { return err @@ -992,13 +994,13 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er } azs := convertStringArr(azSet.List()) locations := append(azs, location) - req = &container.UpdateClusterRequest{ + req := &container.UpdateClusterRequest{ Update: &container.ClusterUpdate{ DesiredLocations: locations, }, } - updateDescription = "updating GKE cluster locations" + updateF := getUpdateFunc(req, config, containerAPIVersion, project, location, clusterName, "updating GKE cluster locations", timeoutInMinutes) // Call update serially. if err := lockedCall(lockKey, updateF); err != nil { return err @@ -1055,12 +1057,13 @@ func resourceContainerClusterUpdate(d *schema.ResourceData, meta interface{}) er if d.HasChange("monitoring_service") { desiredMonitoringService := d.Get("monitoring_service").(string) - req = &container.UpdateClusterRequest{ + req := &container.UpdateClusterRequest{ Update: &container.ClusterUpdate{ DesiredMonitoringService: desiredMonitoringService, }, } - updateDescription = "updating GKE cluster monitoring service" + + updateF := getUpdateFunc(req, config, containerAPIVersion, project, location, clusterName, "updating GKE cluster monitoring service", timeoutInMinutes) // Call update serially. if err := lockedCall(lockKey, updateF); err != nil { return err