Skip to content

Commit

Permalink
* implement a method to generate the update function and refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ashish-amarnath committed Mar 23, 2018
1 parent e339451 commit b3ff0e5
Showing 1 changed file with 38 additions and 35 deletions.
73 changes: 38 additions & 35 deletions google/resource_container_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b3ff0e5

Please sign in to comment.