func resourceAliCloudAlbServerGroupUpdate()

in alicloud/resource_alicloud_alb_server_group.go [670:1033]


func resourceAliCloudAlbServerGroupUpdate(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*connectivity.AliyunClient)
	var request map[string]interface{}
	var response map[string]interface{}
	var query map[string]interface{}
	update := false
	d.Partial(true)

	var err error
	action := "UpdateServerGroupAttribute"
	request = make(map[string]interface{})
	query = make(map[string]interface{})
	request["ServerGroupId"] = d.Id()

	request["ClientToken"] = buildClientToken(action)
	if !d.IsNewResource() && d.HasChange("sticky_session_config") {
		update = true
		objectDataLocalMap := make(map[string]interface{})

		if v := d.Get("sticky_session_config"); v != nil {
			cookieTimeout1, _ := jsonpath.Get("$[0].cookie_timeout", v)
			if cookieTimeout1 != nil && (d.HasChange("sticky_session_config.0.cookie_timeout") || cookieTimeout1 != "") && cookieTimeout1.(int) > 0 {
				objectDataLocalMap["CookieTimeout"] = cookieTimeout1
			}
			cookie1, _ := jsonpath.Get("$[0].cookie", v)
			if cookie1 != nil && (d.HasChange("sticky_session_config.0.cookie") || cookie1 != "") {
				objectDataLocalMap["Cookie"] = cookie1
			}
			stickySessionEnabled1, _ := jsonpath.Get("$[0].sticky_session_enabled", v)
			if stickySessionEnabled1 != nil && (d.HasChange("sticky_session_config.0.sticky_session_enabled") || stickySessionEnabled1 != "") {
				objectDataLocalMap["StickySessionEnabled"] = stickySessionEnabled1
			}
			stickySessionType1, _ := jsonpath.Get("$[0].sticky_session_type", v)
			if stickySessionType1 != nil && (d.HasChange("sticky_session_config.0.sticky_session_type") || stickySessionType1 != "") {
				objectDataLocalMap["StickySessionType"] = stickySessionType1
			}

			request["StickySessionConfig"] = objectDataLocalMap
		}
	}

	if !d.IsNewResource() && d.HasChange("health_check_config") {
		update = true
	}
	objectDataLocalMap1 := make(map[string]interface{})

	if v := d.Get("health_check_config"); v != nil {
		healthCheckCodes1, _ := jsonpath.Get("$[0].health_check_codes", d.Get("health_check_config"))
		if healthCheckCodes1 != nil && (d.HasChange("health_check_config.0.health_check_codes") || healthCheckCodes1 != "") {
			objectDataLocalMap1["HealthCheckCodes"] = healthCheckCodes1
		}
		healthCheckHost1, _ := jsonpath.Get("$[0].health_check_host", v)
		if healthCheckHost1 != nil && (d.HasChange("health_check_config.0.health_check_host") || healthCheckHost1 != "") {
			objectDataLocalMap1["HealthCheckHost"] = healthCheckHost1
		}
		healthCheckPath1, _ := jsonpath.Get("$[0].health_check_path", v)
		if healthCheckPath1 != nil && (d.HasChange("health_check_config.0.health_check_path") || healthCheckPath1 != "") {
			objectDataLocalMap1["HealthCheckPath"] = healthCheckPath1
		}
		healthCheckEnabled1, _ := jsonpath.Get("$[0].health_check_enabled", v)
		if healthCheckEnabled1 != nil && (d.HasChange("health_check_config.0.health_check_enabled") || healthCheckEnabled1 != "") {
			objectDataLocalMap1["HealthCheckEnabled"] = healthCheckEnabled1
		}
		healthCheckTimeout1, _ := jsonpath.Get("$[0].health_check_timeout", v)
		if healthCheckTimeout1 != nil && (d.HasChange("health_check_config.0.health_check_timeout") || healthCheckTimeout1 != "") && healthCheckTimeout1.(int) > 0 {
			objectDataLocalMap1["HealthCheckTimeout"] = healthCheckTimeout1
		}
		unhealthyThreshold1, _ := jsonpath.Get("$[0].unhealthy_threshold", v)
		if unhealthyThreshold1 != nil && (d.HasChange("health_check_config.0.unhealthy_threshold") || unhealthyThreshold1 != "") && unhealthyThreshold1.(int) > 0 {
			objectDataLocalMap1["UnhealthyThreshold"] = unhealthyThreshold1
		}
		healthCheckInterval1, _ := jsonpath.Get("$[0].health_check_interval", v)
		if healthCheckInterval1 != nil && (d.HasChange("health_check_config.0.health_check_interval") || healthCheckInterval1 != "") && healthCheckInterval1.(int) > 0 {
			objectDataLocalMap1["HealthCheckInterval"] = healthCheckInterval1
		}
		healthCheckConnectPort1, _ := jsonpath.Get("$[0].health_check_connect_port", v)
		if healthCheckConnectPort1 != nil && (d.HasChange("health_check_config.0.health_check_connect_port") || healthCheckConnectPort1 != "") {
			objectDataLocalMap1["HealthCheckConnectPort"] = healthCheckConnectPort1
		}
		healthCheckHttpVersion1, _ := jsonpath.Get("$[0].health_check_http_version", v)
		if healthCheckHttpVersion1 != nil && (d.HasChange("health_check_config.0.health_check_http_version") || healthCheckHttpVersion1 != "") {
			objectDataLocalMap1["HealthCheckHttpVersion"] = healthCheckHttpVersion1
		}
		healthCheckMethod1, _ := jsonpath.Get("$[0].health_check_method", v)
		if healthCheckMethod1 != nil && (d.HasChange("health_check_config.0.health_check_method") || healthCheckMethod1 != "") {
			objectDataLocalMap1["HealthCheckMethod"] = healthCheckMethod1
		}
		healthCheckProtocol1, _ := jsonpath.Get("$[0].health_check_protocol", v)
		if healthCheckProtocol1 != nil && (d.HasChange("health_check_config.0.health_check_protocol") || healthCheckProtocol1 != "") {
			objectDataLocalMap1["HealthCheckProtocol"] = healthCheckProtocol1
		}
		healthyThreshold1, _ := jsonpath.Get("$[0].healthy_threshold", v)
		if healthyThreshold1 != nil && (d.HasChange("health_check_config.0.healthy_threshold") || healthyThreshold1 != "") && healthyThreshold1.(int) > 0 {
			objectDataLocalMap1["HealthyThreshold"] = healthyThreshold1
		}

		request["HealthCheckConfig"] = objectDataLocalMap1
	}

	if !d.IsNewResource() && d.HasChange("slow_start_config") {
		update = true
		objectDataLocalMap2 := make(map[string]interface{})

		if v := d.Get("slow_start_config"); v != nil {
			slowStartEnabled1, _ := jsonpath.Get("$[0].slow_start_enabled", v)
			if slowStartEnabled1 != nil && (d.HasChange("slow_start_config.0.slow_start_enabled") || slowStartEnabled1 != "") {
				objectDataLocalMap2["SlowStartEnabled"] = slowStartEnabled1
			}
			slowStartDuration1, _ := jsonpath.Get("$[0].slow_start_duration", v)
			if slowStartDuration1 != nil && (d.HasChange("slow_start_config.0.slow_start_duration") || slowStartDuration1 != "") {
				objectDataLocalMap2["SlowStartDuration"] = slowStartDuration1
			}

			request["SlowStartConfig"] = objectDataLocalMap2
		}
	}

	if !d.IsNewResource() && d.HasChange("server_group_name") {
		update = true
	}
	request["ServerGroupName"] = d.Get("server_group_name")
	if !d.IsNewResource() && d.HasChange("connection_drain_config") {
		update = true
		objectDataLocalMap3 := make(map[string]interface{})

		if v := d.Get("connection_drain_config"); v != nil {
			connectionDrainEnabled1, _ := jsonpath.Get("$[0].connection_drain_enabled", v)
			if connectionDrainEnabled1 != nil && (d.HasChange("connection_drain_config.0.connection_drain_enabled") || connectionDrainEnabled1 != "") {
				objectDataLocalMap3["ConnectionDrainEnabled"] = connectionDrainEnabled1
			}
			connectionDrainTimeout1, _ := jsonpath.Get("$[0].connection_drain_timeout", v)
			if connectionDrainTimeout1 != nil && (d.HasChange("connection_drain_config.0.connection_drain_timeout") || connectionDrainTimeout1 != "") {
				objectDataLocalMap3["ConnectionDrainTimeout"] = connectionDrainTimeout1
			}

			request["ConnectionDrainConfig"] = objectDataLocalMap3
		}
	}

	if !d.IsNewResource() && d.HasChange("uch_config") {
		update = true
		objectDataLocalMap4 := make(map[string]interface{})
		if v := d.Get("uch_config"); v != nil {
			value1, _ := jsonpath.Get("$[0].value", v)
			if value1 != nil && (d.HasChange("uch_config.0.value") || value1 != "") {
				objectDataLocalMap4["Value"] = value1
			}
			type1, _ := jsonpath.Get("$[0].type", v)
			if type1 != nil && (d.HasChange("uch_config.0.type") || type1 != "") {
				objectDataLocalMap4["Type"] = type1
			}

			request["UchConfig"] = objectDataLocalMap4
		}
	}

	if !d.IsNewResource() && d.HasChange("scheduler") {
		update = true
		request["Scheduler"] = d.Get("scheduler")
	}

	if !d.IsNewResource() && d.HasChange("upstream_keepalive_enabled") {
		update = true
		request["UpstreamKeepaliveEnabled"] = d.Get("upstream_keepalive_enabled")
	}

	if !d.IsNewResource() && d.HasChange("cross_zone_enabled") {
		update = true
		request["CrossZoneEnabled"] = d.Get("cross_zone_enabled")
	}

	if update {
		wait := incrementalWait(3*time.Second, 5*time.Second)
		err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
			response, err = client.RpcPost("Alb", "2020-06-16", action, query, request, true)
			if err != nil {
				if IsExpectedErrors(err, []string{"SystemBusy", "IdempotenceProcessing", "IncorrectStatus.ServerGroup"}) || NeedRetry(err) {
					wait()
					return resource.RetryableError(err)
				}
				return resource.NonRetryableError(err)
			}
			return nil
		})
		addDebug(action, response, request)
		if err != nil {
			return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
		}
		albServiceV2 := AlbServiceV2{client}
		stateConf := BuildStateConf([]string{}, []string{"Available"}, d.Timeout(schema.TimeoutUpdate), 5*time.Second, albServiceV2.AlbServerGroupStateRefreshFunc(d.Id(), "ServerGroupStatus", []string{}))
		if _, err := stateConf.WaitForState(); err != nil {
			return WrapErrorf(err, IdMsg, d.Id())
		}
	}
	update = false
	action = "MoveResourceGroup"
	request = make(map[string]interface{})
	query = make(map[string]interface{})
	request["ResourceId"] = d.Id()

	if _, ok := d.GetOk("resource_group_id"); ok && !d.IsNewResource() && d.HasChange("resource_group_id") {
		update = true
	}
	request["NewResourceGroupId"] = d.Get("resource_group_id")
	request["ResourceType"] = "servergroup"
	if update {
		wait := incrementalWait(3*time.Second, 5*time.Second)
		err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
			response, err = client.RpcPost("Alb", "2020-06-16", action, query, request, true)
			if err != nil {
				if IsExpectedErrors(err, []string{"NotExist.ResourceGroup"}) || NeedRetry(err) {
					wait()
					return resource.RetryableError(err)
				}
				return resource.NonRetryableError(err)
			}
			return nil
		})
		addDebug(action, response, request)
		if err != nil {
			return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
		}
	}
	update = false
	action = "ApplyHealthCheckTemplateToServerGroup"
	request = make(map[string]interface{})
	query = make(map[string]interface{})
	request["ServerGroupId"] = d.Id()

	request["ClientToken"] = buildClientToken(action)
	request["HealthCheckTemplateId"] = d.Get("health_check_template_id")
	if update {
		wait := incrementalWait(3*time.Second, 5*time.Second)
		err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
			response, err = client.RpcPost("Alb", "2020-06-16", action, query, request, true)
			if err != nil {
				if NeedRetry(err) {
					wait()
					return resource.RetryableError(err)
				}
				return resource.NonRetryableError(err)
			}
			return nil
		})
		addDebug(action, response, request)
		if err != nil {
			return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
		}
	}

	if d.HasChange("servers") {
		oldEntry, newEntry := d.GetChange("servers")
		oldEntrySet := oldEntry.(*schema.Set)
		newEntrySet := newEntry.(*schema.Set)
		removed := oldEntrySet.Difference(newEntrySet)
		added := newEntrySet.Difference(oldEntrySet)

		if removed.Len() > 0 {
			action := "RemoveServersFromServerGroup"
			request = make(map[string]interface{})
			query = make(map[string]interface{})
			request["ServerGroupId"] = d.Id()

			request["ClientToken"] = buildClientToken(action)
			localData := removed.List()
			serversMapsArray := make([]interface{}, 0)
			for _, dataLoop := range localData {
				dataLoopTmp := dataLoop.(map[string]interface{})
				dataLoopMap := make(map[string]interface{})
				if dataLoopTmp["port"].(int) > 0 {
					dataLoopMap["Port"] = dataLoopTmp["port"]
				}
				dataLoopMap["ServerId"] = dataLoopTmp["server_id"]
				dataLoopMap["ServerIp"] = dataLoopTmp["server_ip"]
				dataLoopMap["ServerType"] = dataLoopTmp["server_type"]
				serversMapsArray = append(serversMapsArray, dataLoopMap)
			}
			request["Servers"] = serversMapsArray

			wait := incrementalWait(3*time.Second, 5*time.Second)
			err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
				response, err = client.RpcPost("Alb", "2020-06-16", action, query, request, true)
				if err != nil {
					if IsExpectedErrors(err, []string{"SystemBusy", "IdempotenceProcessing", "IncorrectStatus.ServerGroup"}) || NeedRetry(err) {
						wait()
						return resource.RetryableError(err)
					}
					return resource.NonRetryableError(err)
				}
				return nil
			})
			addDebug(action, response, request)
			if err != nil {
				return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
			}
			albServiceV2 := AlbServiceV2{client}
			stateConf := BuildStateConf([]string{}, []string{"Available"}, d.Timeout(schema.TimeoutUpdate), 5*time.Second, albServiceV2.AlbServerGroupStateRefreshFunc(d.Id(), "ServerGroupStatus", []string{}))
			if _, err := stateConf.WaitForState(); err != nil {
				return WrapErrorf(err, IdMsg, d.Id())
			}

		}

		if added.Len() > 0 {
			action := "AddServersToServerGroup"
			request = make(map[string]interface{})
			query = make(map[string]interface{})
			request["ServerGroupId"] = d.Id()

			request["ClientToken"] = buildClientToken(action)
			localData := added.List()
			serversMapsArray := make([]interface{}, 0)
			for _, dataLoop := range localData {
				dataLoopTmp := dataLoop.(map[string]interface{})
				dataLoopMap := make(map[string]interface{})
				if dataLoopTmp["port"].(int) > 0 {
					dataLoopMap["Port"] = dataLoopTmp["port"]
				}
				dataLoopMap["ServerId"] = dataLoopTmp["server_id"]
				dataLoopMap["Weight"] = dataLoopTmp["weight"]
				dataLoopMap["RemoteIpEnabled"] = dataLoopTmp["remote_ip_enabled"]
				if dataLoopTmp["description"] != "" {
					dataLoopMap["Description"] = dataLoopTmp["description"]
				}
				dataLoopMap["ServerIp"] = dataLoopTmp["server_ip"]
				dataLoopMap["ServerType"] = dataLoopTmp["server_type"]
				serversMapsArray = append(serversMapsArray, dataLoopMap)
			}
			request["Servers"] = serversMapsArray

			wait := incrementalWait(3*time.Second, 5*time.Second)
			err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
				response, err = client.RpcPost("Alb", "2020-06-16", action, query, request, true)
				if err != nil {
					if IsExpectedErrors(err, []string{"SystemBusy", "IdempotenceProcessing", "IncorrectStatus.ServerGroup"}) || NeedRetry(err) {
						wait()
						return resource.RetryableError(err)
					}
					return resource.NonRetryableError(err)
				}
				return nil
			})
			addDebug(action, response, request)
			if err != nil {
				return WrapErrorf(err, DefaultErrorMsg, d.Id(), action, AlibabaCloudSdkGoERROR)
			}
			albServiceV2 := AlbServiceV2{client}
			stateConf := BuildStateConf([]string{}, []string{"Available"}, d.Timeout(schema.TimeoutUpdate), 5*time.Second, albServiceV2.AlbServerGroupStateRefreshFunc(d.Id(), "ServerGroupStatus", []string{}))
			if _, err := stateConf.WaitForState(); err != nil {
				return WrapErrorf(err, IdMsg, d.Id())
			}

		}

	}
	if d.HasChange("tags") {
		albServiceV2 := AlbServiceV2{client}
		if err := albServiceV2.SetResourceTags(d, "servergroup"); err != nil {
			return WrapError(err)
		}
	}
	d.Partial(false)
	return resourceAliCloudAlbServerGroupRead(d, meta)
}