func resourceAliCloudFcv3FunctionUpdate()

in alicloud/resource_alicloud_fcv3_function.go [1318:1766]


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

	functionName := d.Id()
	action := fmt.Sprintf("/2023-03-30/functions/%s", functionName)
	var err error
	request = make(map[string]interface{})
	query = make(map[string]*string)
	body = make(map[string]interface{})
	request["functionName"] = d.Id()

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

		if v := d.Get("gpu_config"); v != nil {
			gpuMemorySize1, _ := jsonpath.Get("$[0].gpu_memory_size", v)
			if gpuMemorySize1 != nil && (d.HasChange("gpu_config.0.gpu_memory_size") || gpuMemorySize1 != "") {
				objectDataLocalMap["gpuMemorySize"] = gpuMemorySize1
			}
			gpuType1, _ := jsonpath.Get("$[0].gpu_type", v)
			if gpuType1 != nil && (d.HasChange("gpu_config.0.gpu_type") || gpuType1 != "") {
				objectDataLocalMap["gpuType"] = gpuType1
			}

			request["gpuConfig"] = objectDataLocalMap
		}
	}

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

		if v := d.Get("log_config"); v != nil {
			logBeginRule1, _ := jsonpath.Get("$[0].log_begin_rule", v)
			if logBeginRule1 != nil && (d.HasChange("log_config.0.log_begin_rule") || logBeginRule1 != "") {
				objectDataLocalMap1["logBeginRule"] = logBeginRule1
			}
			project1, _ := jsonpath.Get("$[0].project", v)
			if project1 != nil && (d.HasChange("log_config.0.project") || project1 != "") {
				objectDataLocalMap1["project"] = project1
			}
			enableInstanceMetrics1, _ := jsonpath.Get("$[0].enable_instance_metrics", v)
			if enableInstanceMetrics1 != nil && (d.HasChange("log_config.0.enable_instance_metrics") || enableInstanceMetrics1 != "") {
				objectDataLocalMap1["enableInstanceMetrics"] = enableInstanceMetrics1
			}
			enableRequestMetrics1, _ := jsonpath.Get("$[0].enable_request_metrics", v)
			if enableRequestMetrics1 != nil && (d.HasChange("log_config.0.enable_request_metrics") || enableRequestMetrics1 != "") {
				objectDataLocalMap1["enableRequestMetrics"] = enableRequestMetrics1
			}
			logstore1, _ := jsonpath.Get("$[0].logstore", v)
			if logstore1 != nil && (d.HasChange("log_config.0.logstore") || logstore1 != "") {
				objectDataLocalMap1["logstore"] = logstore1
			}

			request["logConfig"] = objectDataLocalMap1
		}
	}

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

		if v := d.Get("nas_config"); v != nil {
			if v, ok := d.GetOk("nas_config"); ok {
				localData, err := jsonpath.Get("$[0].mount_points", v)
				if err != nil {
					localData = make([]interface{}, 0)
				}
				localMaps := make([]interface{}, 0)
				for _, dataLoop := range localData.([]interface{}) {
					dataLoopTmp := make(map[string]interface{})
					if dataLoop != nil {
						dataLoopTmp = dataLoop.(map[string]interface{})
					}
					dataLoopMap := make(map[string]interface{})
					dataLoopMap["enableTLS"] = dataLoopTmp["enable_tls"]
					dataLoopMap["serverAddr"] = dataLoopTmp["server_addr"]
					dataLoopMap["mountDir"] = dataLoopTmp["mount_dir"]
					localMaps = append(localMaps, dataLoopMap)
				}
				objectDataLocalMap2["mountPoints"] = localMaps
			}

			userId1, _ := jsonpath.Get("$[0].user_id", v)
			if userId1 != nil && (d.HasChange("nas_config.0.user_id") || userId1 != "") {
				objectDataLocalMap2["userId"] = userId1
			}
			groupId1, _ := jsonpath.Get("$[0].group_id", v)
			if groupId1 != nil && (d.HasChange("nas_config.0.group_id") || groupId1 != "") {
				objectDataLocalMap2["groupId"] = groupId1
			}

			request["nasConfig"] = objectDataLocalMap2
		}
	}

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

	if d.HasChange("custom_runtime_config") {
		update = true
		objectDataLocalMap3 := make(map[string]interface{})

		if v := d.Get("custom_runtime_config"); v != nil {
			healthCheckConfig := make(map[string]interface{})
			timeoutSeconds1, _ := jsonpath.Get("$[0].health_check_config[0].timeout_seconds", v)
			if timeoutSeconds1 != nil && (d.HasChange("custom_runtime_config.0.health_check_config.0.timeout_seconds") || timeoutSeconds1 != "") && timeoutSeconds1.(int) > 0 {
				healthCheckConfig["timeoutSeconds"] = timeoutSeconds1
			}
			httpGetUrl1, _ := jsonpath.Get("$[0].health_check_config[0].http_get_url", v)
			if httpGetUrl1 != nil && (d.HasChange("custom_runtime_config.0.health_check_config.0.http_get_url") || httpGetUrl1 != "") {
				healthCheckConfig["httpGetUrl"] = httpGetUrl1
			}
			successThreshold1, _ := jsonpath.Get("$[0].health_check_config[0].success_threshold", v)
			if successThreshold1 != nil && (d.HasChange("custom_runtime_config.0.health_check_config.0.success_threshold") || successThreshold1 != "") && successThreshold1.(int) > 0 {
				healthCheckConfig["successThreshold"] = successThreshold1
			}
			initialDelaySeconds1, _ := jsonpath.Get("$[0].health_check_config[0].initial_delay_seconds", v)
			if initialDelaySeconds1 != nil && (d.HasChange("custom_runtime_config.0.health_check_config.0.initial_delay_seconds") || initialDelaySeconds1 != "") {
				healthCheckConfig["initialDelaySeconds"] = initialDelaySeconds1
			}
			periodSeconds1, _ := jsonpath.Get("$[0].health_check_config[0].period_seconds", v)
			if periodSeconds1 != nil && (d.HasChange("custom_runtime_config.0.health_check_config.0.period_seconds") || periodSeconds1 != "") && periodSeconds1.(int) > 0 {
				healthCheckConfig["periodSeconds"] = periodSeconds1
			}
			failureThreshold1, _ := jsonpath.Get("$[0].health_check_config[0].failure_threshold", v)
			if failureThreshold1 != nil && (d.HasChange("custom_runtime_config.0.health_check_config.0.failure_threshold") || failureThreshold1 != "") && failureThreshold1.(int) > 0 {
				healthCheckConfig["failureThreshold"] = failureThreshold1
			}

			objectDataLocalMap3["healthCheckConfig"] = healthCheckConfig
			args1, _ := jsonpath.Get("$[0].args", d.Get("custom_runtime_config"))
			if args1 != nil && (d.HasChange("custom_runtime_config.0.args") || args1 != "") {
				objectDataLocalMap3["args"] = args1
			}
			command1, _ := jsonpath.Get("$[0].command", d.Get("custom_runtime_config"))
			if command1 != nil && (d.HasChange("custom_runtime_config.0.command") || command1 != "") {
				objectDataLocalMap3["command"] = command1
			}
			port1, _ := jsonpath.Get("$[0].port", v)
			if port1 != nil && (d.HasChange("custom_runtime_config.0.port") || port1 != "") && port1.(int) > 0 {
				objectDataLocalMap3["port"] = port1
			}

			request["customRuntimeConfig"] = objectDataLocalMap3
		}
	}

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

		if v := d.Get("custom_container_config"); v != nil {
			accelerationType1, _ := jsonpath.Get("$[0].acceleration_type", v)
			if accelerationType1 != nil && (d.HasChange("custom_container_config.0.acceleration_type") || accelerationType1 != "") {
				objectDataLocalMap4["accelerationType"] = accelerationType1
			}
			healthCheckConfig1 := make(map[string]interface{})
			failureThreshold3, _ := jsonpath.Get("$[0].health_check_config[0].failure_threshold", v)
			if failureThreshold3 != nil && (d.HasChange("custom_container_config.0.health_check_config.0.failure_threshold") || failureThreshold3 != "") && failureThreshold3.(int) > 0 {
				healthCheckConfig1["failureThreshold"] = failureThreshold3
			}
			timeoutSeconds3, _ := jsonpath.Get("$[0].health_check_config[0].timeout_seconds", v)
			if timeoutSeconds3 != nil && (d.HasChange("custom_container_config.0.health_check_config.0.timeout_seconds") || timeoutSeconds3 != "") && timeoutSeconds3.(int) > 0 {
				healthCheckConfig1["timeoutSeconds"] = timeoutSeconds3
			}
			initialDelaySeconds3, _ := jsonpath.Get("$[0].health_check_config[0].initial_delay_seconds", v)
			if initialDelaySeconds3 != nil && (d.HasChange("custom_container_config.0.health_check_config.0.initial_delay_seconds") || initialDelaySeconds3 != "") {
				healthCheckConfig1["initialDelaySeconds"] = initialDelaySeconds3
			}
			periodSeconds3, _ := jsonpath.Get("$[0].health_check_config[0].period_seconds", v)
			if periodSeconds3 != nil && (d.HasChange("custom_container_config.0.health_check_config.0.period_seconds") || periodSeconds3 != "") && periodSeconds3.(int) > 0 {
				healthCheckConfig1["periodSeconds"] = periodSeconds3
			}
			httpGetUrl3, _ := jsonpath.Get("$[0].health_check_config[0].http_get_url", v)
			if httpGetUrl3 != nil && (d.HasChange("custom_container_config.0.health_check_config.0.http_get_url") || httpGetUrl3 != "") {
				healthCheckConfig1["httpGetUrl"] = httpGetUrl3
			}
			successThreshold3, _ := jsonpath.Get("$[0].health_check_config[0].success_threshold", v)
			if successThreshold3 != nil && (d.HasChange("custom_container_config.0.health_check_config.0.success_threshold") || successThreshold3 != "") && successThreshold3.(int) > 0 {
				healthCheckConfig1["successThreshold"] = successThreshold3
			}

			objectDataLocalMap4["healthCheckConfig"] = healthCheckConfig1
			entrypoint1, _ := jsonpath.Get("$[0].entrypoint", d.Get("custom_container_config"))
			if entrypoint1 != nil && (d.HasChange("custom_container_config.0.entrypoint") || entrypoint1 != "") {
				objectDataLocalMap4["entrypoint"] = entrypoint1
			}
			command3, _ := jsonpath.Get("$[0].command", d.Get("custom_container_config"))
			if command3 != nil && (d.HasChange("custom_container_config.0.command") || command3 != "") {
				objectDataLocalMap4["command"] = command3
			}
			image1, _ := jsonpath.Get("$[0].image", v)
			if image1 != nil && (d.HasChange("custom_container_config.0.image") || image1 != "") {
				objectDataLocalMap4["image"] = image1
			}
			port3, _ := jsonpath.Get("$[0].port", v)
			if port3 != nil && (d.HasChange("custom_container_config.0.port") || port3 != "") {
				objectDataLocalMap4["port"] = port3
			}
			acrInstanceId1, _ := jsonpath.Get("$[0].acr_instance_id", v)
			if acrInstanceId1 != nil && (d.HasChange("custom_container_config.0.acr_instance_id") || acrInstanceId1 != "") {
				objectDataLocalMap4["acrInstanceId"] = acrInstanceId1
			}

			request["customContainerConfig"] = objectDataLocalMap4
		}
	}

	if d.HasChange("custom_dns") {
		update = true
		objectDataLocalMap5 := make(map[string]interface{})

		if v := d.Get("custom_dns"); v != nil {
			searches1, _ := jsonpath.Get("$[0].searches", d.Get("custom_dns"))
			if searches1 != nil && (d.HasChange("custom_dns.0.searches") || searches1 != "") {
				objectDataLocalMap5["searches"] = searches1
			}
			if v, ok := d.GetOk("custom_dns"); ok {
				localData1, err := jsonpath.Get("$[0].dns_options", v)
				if err != nil {
					localData1 = make([]interface{}, 0)
				}
				localMaps1 := make([]interface{}, 0)
				for _, dataLoop1 := range localData1.([]interface{}) {
					dataLoop1Tmp := make(map[string]interface{})
					if dataLoop1 != nil {
						dataLoop1Tmp = dataLoop1.(map[string]interface{})
					}
					dataLoop1Map := make(map[string]interface{})
					dataLoop1Map["value"] = dataLoop1Tmp["value"]
					dataLoop1Map["name"] = dataLoop1Tmp["name"]
					localMaps1 = append(localMaps1, dataLoop1Map)
				}
				objectDataLocalMap5["dnsOptions"] = localMaps1
			}

			nameServers1, _ := jsonpath.Get("$[0].name_servers", d.Get("custom_dns"))
			if nameServers1 != nil && (d.HasChange("custom_dns.0.name_servers") || nameServers1 != "") {
				objectDataLocalMap5["nameServers"] = nameServers1
			}

			request["customDNS"] = objectDataLocalMap5
		}
	}

	if d.HasChange("instance_lifecycle_config") {
		update = true
		objectDataLocalMap6 := make(map[string]interface{})

		if v := d.Get("instance_lifecycle_config"); v != nil {
			preStop := make(map[string]interface{})
			timeout1, _ := jsonpath.Get("$[0].pre_stop[0].timeout", v)
			if timeout1 != nil && (d.HasChange("instance_lifecycle_config.0.pre_stop.0.timeout") || timeout1 != "") && timeout1.(int) > 0 {
				preStop["timeout"] = timeout1
			}
			handler1, _ := jsonpath.Get("$[0].pre_stop[0].handler", v)
			if handler1 != nil && (d.HasChange("instance_lifecycle_config.0.pre_stop.0.handler") || handler1 != "") {
				preStop["handler"] = handler1
			}

			objectDataLocalMap6["preStop"] = preStop
			initializer := make(map[string]interface{})
			handler3, _ := jsonpath.Get("$[0].initializer[0].handler", v)
			if handler3 != nil && (d.HasChange("instance_lifecycle_config.0.initializer.0.handler") || handler3 != "") {
				initializer["handler"] = handler3
			}
			timeout3, _ := jsonpath.Get("$[0].initializer[0].timeout", v)
			if timeout3 != nil && (d.HasChange("instance_lifecycle_config.0.initializer.0.timeout") || timeout3 != "") && timeout3.(int) > 0 {
				initializer["timeout"] = timeout3
			}

			objectDataLocalMap6["initializer"] = initializer

			request["instanceLifecycleConfig"] = objectDataLocalMap6
		}
	}

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

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

		if v := d.Get("oss_mount_config"); v != nil {
			if v, ok := d.GetOk("oss_mount_config"); ok {
				localData2, err := jsonpath.Get("$[0].mount_points", v)
				if err != nil {
					localData2 = make([]interface{}, 0)
				}
				localMaps2 := make([]interface{}, 0)
				for _, dataLoop2 := range localData2.([]interface{}) {
					dataLoop2Tmp := make(map[string]interface{})
					if dataLoop2 != nil {
						dataLoop2Tmp = dataLoop2.(map[string]interface{})
					}
					dataLoop2Map := make(map[string]interface{})
					dataLoop2Map["bucketName"] = dataLoop2Tmp["bucket_name"]
					dataLoop2Map["readOnly"] = dataLoop2Tmp["read_only"]
					dataLoop2Map["bucketPath"] = dataLoop2Tmp["bucket_path"]
					dataLoop2Map["mountDir"] = dataLoop2Tmp["mount_dir"]
					dataLoop2Map["endpoint"] = dataLoop2Tmp["endpoint"]
					localMaps2 = append(localMaps2, dataLoop2Map)
				}
				objectDataLocalMap7["mountPoints"] = localMaps2
			}

			request["ossMountConfig"] = objectDataLocalMap7
		}
	}

	if !d.IsNewResource() && d.HasChange("runtime") {
		update = true
	}
	request["runtime"] = d.Get("runtime")
	if !d.IsNewResource() && d.HasChange("environment_variables") {
		update = true
		request["environmentVariables"] = d.Get("environment_variables")
	}

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

		if v := d.Get("code"); v != nil {
			ossBucketName1, _ := jsonpath.Get("$[0].oss_bucket_name", v)
			if ossBucketName1 != nil && (d.HasChange("code.0.oss_bucket_name") || ossBucketName1 != "") {
				objectDataLocalMap8["ossBucketName"] = ossBucketName1
			}
			zipFile1, _ := jsonpath.Get("$[0].zip_file", v)
			if zipFile1 != nil && (d.HasChange("code.0.zip_file") || zipFile1 != "") {
				objectDataLocalMap8["zipFile"] = zipFile1
			}
			ossObjectName1, _ := jsonpath.Get("$[0].oss_object_name", v)
			if ossObjectName1 != nil && (d.HasChange("code.0.oss_object_name") || ossObjectName1 != "") {
				objectDataLocalMap8["ossObjectName"] = ossObjectName1
			}
			checksum1, _ := jsonpath.Get("$[0].checksum", v)
			if checksum1 != nil && (d.HasChange("code.0.checksum") || checksum1 != "") {
				objectDataLocalMap8["checksum"] = checksum1
			}

			request["code"] = objectDataLocalMap8
		}
	}

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

	if !d.IsNewResource() && d.HasChange("layers") {
		update = true
		if v, ok := d.GetOk("layers"); ok || d.HasChange("layers") {
			layersMapsArray := v.([]interface{})
			request["layers"] = layersMapsArray
		}
	}

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

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

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

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

		if v := d.Get("vpc_config"); v != nil {
			vpcId1, _ := jsonpath.Get("$[0].vpc_id", v)
			if vpcId1 != nil && (d.HasChange("vpc_config.0.vpc_id") || vpcId1 != "") {
				objectDataLocalMap9["vpcId"] = vpcId1
			}
			securityGroupId1, _ := jsonpath.Get("$[0].security_group_id", v)
			if securityGroupId1 != nil && (d.HasChange("vpc_config.0.security_group_id") || securityGroupId1 != "") {
				objectDataLocalMap9["securityGroupId"] = securityGroupId1
			}
			vSwitchIds1, _ := jsonpath.Get("$[0].vswitch_ids", d.Get("vpc_config"))
			if vSwitchIds1 != nil && (d.HasChange("vpc_config.0.vswitch_ids") || vSwitchIds1 != "") {
				objectDataLocalMap9["vSwitchIds"] = vSwitchIds1
			}

			request["vpcConfig"] = objectDataLocalMap9
		}
	}

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

	if !d.IsNewResource() && d.HasChange("handler") {
		update = true
	}
	request["handler"] = d.Get("handler")
	if !d.IsNewResource() && d.HasChange("memory_size") {
		update = true
		request["memorySize"] = d.Get("memory_size")
	}

	body = request
	if update {
		wait := incrementalWait(3*time.Second, 5*time.Second)
		err = resource.Retry(d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
			response, err = client.RoaPut("FC", "2023-03-30", action, query, nil, body, 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("tags") {
		fcv3ServiceV2 := Fcv3ServiceV2{client}
		if err := fcv3ServiceV2.SetResourceTags(d, "function"); err != nil {
			return WrapError(err)
		}
	}
	return resourceAliCloudFcv3FunctionRead(d, meta)
}