func resourceAliCloudGwlbServerGroupUpdate()

in alicloud/resource_alicloud_gwlb_server_group.go [514:846]


func resourceAliCloudGwlbServerGroupUpdate(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)
	action := "UpdateServerGroupAttribute"
	var err error
	request = make(map[string]interface{})
	query = make(map[string]interface{})
	request["ServerGroupId"] = d.Id()
	request["RegionId"] = client.RegionId
	request["ClientToken"] = buildClientToken(action)
	if !d.IsNewResource() && d.HasChange("health_check_config.0.health_check_connect_timeout") {
		update = true
		jsonPathResult, err := jsonpath.Get("$[0].health_check_connect_timeout", d.Get("health_check_config"))
		if err == nil {
			request["HealthCheckConfig.HealthCheckConnectTimeout"] = jsonPathResult
		}
	}

	if !d.IsNewResource() && d.HasChange("health_check_config.0.health_check_connect_port") {
		update = true
		jsonPathResult1, err := jsonpath.Get("$[0].health_check_connect_port", d.Get("health_check_config"))
		if err == nil {
			request["HealthCheckConfig.HealthCheckConnectPort"] = jsonPathResult1
		}
	}

	if !d.IsNewResource() && d.HasChange("health_check_config.0.health_check_path") {
		update = true
		jsonPathResult2, err := jsonpath.Get("$[0].health_check_path", d.Get("health_check_config"))
		if err == nil {
			request["HealthCheckConfig.HealthCheckPath"] = jsonPathResult2
		}
	}

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

	if !d.IsNewResource() && d.HasChange("health_check_config.0.health_check_enabled") {
		update = true
		jsonPathResult4, err := jsonpath.Get("$[0].health_check_enabled", d.Get("health_check_config"))
		if err == nil {
			request["HealthCheckConfig.HealthCheckEnabled"] = jsonPathResult4
		}
	}

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

		if v := d.Get("health_check_config"); !IsNil(v) {
			healthCheckConnectTimeout1, _ := jsonpath.Get("$[0].health_check_connect_timeout", v)
			if healthCheckConnectTimeout1 != nil && (d.HasChange("health_check_config.0.health_check_connect_timeout") || healthCheckConnectTimeout1 != "") {
				objectDataLocalMap["HealthCheckConnectTimeout"] = healthCheckConnectTimeout1
			}
			healthCheckConnectPort1, _ := jsonpath.Get("$[0].health_check_connect_port", v)
			if healthCheckConnectPort1 != nil && (d.HasChange("health_check_config.0.health_check_connect_port") || healthCheckConnectPort1 != "") {
				objectDataLocalMap["HealthCheckConnectPort"] = healthCheckConnectPort1
			}
			healthCheckPath1, _ := jsonpath.Get("$[0].health_check_path", v)
			if healthCheckPath1 != nil && (d.HasChange("health_check_config.0.health_check_path") || healthCheckPath1 != "") {
				objectDataLocalMap["HealthCheckPath"] = healthCheckPath1
			}
			healthCheckEnabled1, _ := jsonpath.Get("$[0].health_check_enabled", v)
			if healthCheckEnabled1 != nil && (d.HasChange("health_check_config.0.health_check_enabled") || healthCheckEnabled1 != "") {
				objectDataLocalMap["HealthCheckEnabled"] = healthCheckEnabled1
			}
			healthCheckHttpCode1, _ := jsonpath.Get("$[0].health_check_http_code", d.Get("health_check_config"))
			if healthCheckHttpCode1 != nil && (d.HasChange("health_check_config.0.health_check_http_code") || healthCheckHttpCode1 != "") {
				objectDataLocalMap["HealthCheckHttpCode"] = healthCheckHttpCode1.(*schema.Set).List()
			}
			unhealthyThreshold1, _ := jsonpath.Get("$[0].unhealthy_threshold", v)
			if unhealthyThreshold1 != nil && (d.HasChange("health_check_config.0.unhealthy_threshold") || unhealthyThreshold1 != "") && unhealthyThreshold1.(int) > 0 {
				objectDataLocalMap["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 {
				objectDataLocalMap["HealthCheckInterval"] = healthCheckInterval1
			}
			healthCheckProtocol1, _ := jsonpath.Get("$[0].health_check_protocol", v)
			if healthCheckProtocol1 != nil && (d.HasChange("health_check_config.0.health_check_protocol") || healthCheckProtocol1 != "") {
				objectDataLocalMap["HealthCheckProtocol"] = healthCheckProtocol1
			}
			healthCheckDomain1, _ := jsonpath.Get("$[0].health_check_domain", v)
			if healthCheckDomain1 != nil && (d.HasChange("health_check_config.0.health_check_domain") || healthCheckDomain1 != "") {
				objectDataLocalMap["HealthCheckDomain"] = healthCheckDomain1
			}
			healthyThreshold1, _ := jsonpath.Get("$[0].healthy_threshold", v)
			if healthyThreshold1 != nil && (d.HasChange("health_check_config.0.healthy_threshold") || healthyThreshold1 != "") && healthyThreshold1.(int) > 0 {
				objectDataLocalMap["HealthyThreshold"] = healthyThreshold1
			}

			request["HealthCheckConfig"] = objectDataLocalMap
		}
	}

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

	if !d.IsNewResource() && d.HasChange("health_check_config.0.unhealthy_threshold") {
		update = true
		jsonPathResult6, err := jsonpath.Get("$[0].unhealthy_threshold", d.Get("health_check_config"))
		if err == nil && jsonPathResult6.(int) > 0 {
			request["HealthCheckConfig.UnhealthyThreshold"] = jsonPathResult6
		}
	}

	if !d.IsNewResource() && d.HasChange("health_check_config.0.health_check_interval") {
		update = true
		jsonPathResult7, err := jsonpath.Get("$[0].health_check_interval", d.Get("health_check_config"))
		if err == nil && jsonPathResult7.(int) > 0 {
			request["HealthCheckConfig.HealthCheckInterval"] = jsonPathResult7
		}
	}

	if v, ok := d.GetOkExists("dry_run"); ok {
		request["DryRun"] = v
	}
	if !d.IsNewResource() && d.HasChange("health_check_config.0.health_check_protocol") {
		update = true
		jsonPathResult9, err := jsonpath.Get("$[0].health_check_protocol", d.Get("health_check_config"))
		if err == nil {
			request["HealthCheckConfig.HealthCheckProtocol"] = jsonPathResult9
		}
	}

	if !d.IsNewResource() && d.HasChange("connection_drain_config.0.connection_drain_enabled") {
		update = true
		jsonPathResult10, err := jsonpath.Get("$[0].connection_drain_enabled", d.Get("connection_drain_config"))
		if err == nil {
			request["ConnectionDrainConfig.ConnectionDrainEnabled"] = jsonPathResult10
		}
	}

	if !d.IsNewResource() && d.HasChange("connection_drain_config.0.connection_drain_timeout") {
		update = true
		jsonPathResult11, err := jsonpath.Get("$[0].connection_drain_timeout", d.Get("connection_drain_config"))
		if err == nil {
			request["ConnectionDrainConfig.ConnectionDrainTimeout"] = jsonPathResult11
		}
	}

	if !d.IsNewResource() && d.HasChange("health_check_config.0.health_check_domain") {
		update = true
		jsonPathResult12, err := jsonpath.Get("$[0].health_check_domain", d.Get("health_check_config"))
		if err == nil {
			request["HealthCheckConfig.HealthCheckDomain"] = jsonPathResult12
		}
	}

	if !d.IsNewResource() && d.HasChange("health_check_config.0.healthy_threshold") {
		update = true
		jsonPathResult13, err := jsonpath.Get("$[0].healthy_threshold", d.Get("health_check_config"))
		if err == nil && jsonPathResult13.(int) > 0 {
			request["HealthCheckConfig.HealthyThreshold"] = jsonPathResult13
		}
	}

	if update {
		wait := incrementalWait(3*time.Second, 5*time.Second)
		err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
			response, err = client.RpcPost("Gwlb", "2024-04-15", 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)
		}
		gwlbServiceV2 := GwlbServiceV2{client}
		stateConf := BuildStateConf([]string{}, []string{"Available"}, d.Timeout(schema.TimeoutUpdate), 5*time.Second, gwlbServiceV2.GwlbServerGroupStateRefreshFunc(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()
	request["RegionId"] = client.RegionId
	request["ClientToken"] = buildClientToken(action)
	if _, ok := d.GetOk("resource_group_id"); ok && !d.IsNewResource() && d.HasChange("resource_group_id") {
		update = true
	}
	request["NewResourceGroupId"] = d.Get("resource_group_id")
	if v, ok := d.GetOkExists("dry_run"); ok {
		request["DryRun"] = v
	}
	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("Gwlb", "2024-04-15", 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["RegionId"] = client.RegionId
			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{})
				dataLoopMap["Port"] = 6081
				dataLoopMap["ServerId"] = dataLoopTmp["server_id"]
				dataLoopMap["ServerIp"] = dataLoopTmp["server_ip"]
				dataLoopMap["ServerType"] = dataLoopTmp["server_type"]
				serversMapsArray = append(serversMapsArray, dataLoopMap)
			}
			request["Servers"] = serversMapsArray

			if v, ok := d.GetOkExists("dry_run"); ok {
				request["DryRun"] = v
			}
			wait := incrementalWait(3*time.Second, 5*time.Second)
			err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
				response, err = client.RpcPost("Gwlb", "2024-04-15", action, query, request, true)
				if err != nil {
					if IsExpectedErrors(err, []string{"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)
			}
			gwlbServiceV2 := GwlbServiceV2{client}
			stateConf := BuildStateConf([]string{}, []string{"Available"}, d.Timeout(schema.TimeoutUpdate), 20*time.Second, gwlbServiceV2.GwlbServerGroupStateRefreshFunc(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["RegionId"] = client.RegionId
			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{})
				dataLoopMap["Port"] = 6081
				dataLoopMap["ServerId"] = dataLoopTmp["server_id"]
				dataLoopMap["ServerIp"] = dataLoopTmp["server_ip"]
				dataLoopMap["ServerType"] = dataLoopTmp["server_type"]
				serversMapsArray = append(serversMapsArray, dataLoopMap)
			}
			request["Servers"] = serversMapsArray

			if v, ok := d.GetOkExists("dry_run"); ok {
				request["DryRun"] = v
			}
			wait := incrementalWait(3*time.Second, 5*time.Second)
			err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
				response, err = client.RpcPost("Gwlb", "2024-04-15", action, query, request, true)
				if err != nil {
					if IsExpectedErrors(err, []string{"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)
			}
			gwlbServiceV2 := GwlbServiceV2{client}
			stateConf := BuildStateConf([]string{}, []string{"Available"}, d.Timeout(schema.TimeoutUpdate), 20*time.Second, gwlbServiceV2.GwlbServerGroupStateRefreshFunc(d.Id(), "ServerGroupStatus", []string{}))
			if _, err := stateConf.WaitForState(); err != nil {
				return WrapErrorf(err, IdMsg, d.Id())
			}

		}

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