func resourceAliCloudFcv3FunctionCreate()

in alicloud/resource_alicloud_fcv3_function.go [586:907]


func resourceAliCloudFcv3FunctionCreate(d *schema.ResourceData, meta interface{}) error {

	client := meta.(*connectivity.AliyunClient)

	action := fmt.Sprintf("/2023-03-30/functions")
	var request map[string]interface{}
	var response map[string]interface{}
	query := make(map[string]*string)
	body := make(map[string]interface{})
	var err error
	request = make(map[string]interface{})
	if v, ok := d.GetOk("function_name"); ok {
		request["functionName"] = v
	}

	objectDataLocalMap := make(map[string]interface{})

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

		request["gpuConfig"] = objectDataLocalMap
	}

	objectDataLocalMap1 := make(map[string]interface{})

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

		request["logConfig"] = objectDataLocalMap1
	}

	objectDataLocalMap2 := make(map[string]interface{})

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

		objectDataLocalMap2["healthCheckConfig"] = healthCheckConfig
		accelerationType1, _ := jsonpath.Get("$[0].acceleration_type", v)
		if accelerationType1 != nil && accelerationType1 != "" {
			objectDataLocalMap2["accelerationType"] = accelerationType1
		}
		command1, _ := jsonpath.Get("$[0].command", v)
		if command1 != nil && command1 != "" {
			objectDataLocalMap2["command"] = command1
		}
		image1, _ := jsonpath.Get("$[0].image", v)
		if image1 != nil && image1 != "" {
			objectDataLocalMap2["image"] = image1
		}
		port1, _ := jsonpath.Get("$[0].port", v)
		if port1 != nil && port1 != "" {
			objectDataLocalMap2["port"] = port1
		}
		acrInstanceId1, _ := jsonpath.Get("$[0].acr_instance_id", v)
		if acrInstanceId1 != nil && acrInstanceId1 != "" {
			objectDataLocalMap2["acrInstanceId"] = acrInstanceId1
		}
		entrypoint1, _ := jsonpath.Get("$[0].entrypoint", v)
		if entrypoint1 != nil && entrypoint1 != "" {
			objectDataLocalMap2["entrypoint"] = entrypoint1
		}

		request["customContainerConfig"] = objectDataLocalMap2
	}

	objectDataLocalMap3 := make(map[string]interface{})

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

		objectDataLocalMap3["healthCheckConfig"] = healthCheckConfig1
		command3, _ := jsonpath.Get("$[0].command", v)
		if command3 != nil && command3 != "" {
			objectDataLocalMap3["command"] = command3
		}
		port3, _ := jsonpath.Get("$[0].port", v)
		if port3 != nil && port3 != "" && port3.(int) > 0 {
			objectDataLocalMap3["port"] = port3
		}
		args1, _ := jsonpath.Get("$[0].args", v)
		if args1 != nil && args1 != "" {
			objectDataLocalMap3["args"] = args1
		}

		request["customRuntimeConfig"] = objectDataLocalMap3
	}

	if v, ok := d.GetOk("layers"); ok {
		layersMapsArray := v.([]interface{})
		request["layers"] = layersMapsArray
	}

	if v, ok := d.GetOkExists("timeout"); ok && v.(int) > 0 {
		request["timeout"] = v
	}
	if v, ok := d.GetOkExists("instance_concurrency"); ok && v.(int) > 0 {
		request["instanceConcurrency"] = v
	}
	if v, ok := d.GetOk("tags"); ok {
		tagsMap := ConvertTags(v.(map[string]interface{}))
		request["Tags"] = tagsMap
	}

	objectDataLocalMap4 := make(map[string]interface{})

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

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

		objectDataLocalMap4["initializer"] = initializer

		request["instanceLifecycleConfig"] = objectDataLocalMap4
	}

	if v, ok := d.GetOkExists("internet_access"); ok {
		request["internetAccess"] = v
	}
	objectDataLocalMap5 := make(map[string]interface{})

	if v := d.Get("oss_mount_config"); !IsNil(v) {
		if v, ok := d.GetOk("oss_mount_config"); ok {
			localData2, err := jsonpath.Get("$[0].mount_points", v)
			if err != nil {
				localData2 = make([]interface{}, 0)
			}
			localMaps := 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["bucketPath"] = dataLoop2Tmp["bucket_path"]
				dataLoop2Map["mountDir"] = dataLoop2Tmp["mount_dir"]
				dataLoop2Map["readOnly"] = dataLoop2Tmp["read_only"]
				dataLoop2Map["endpoint"] = dataLoop2Tmp["endpoint"]
				localMaps = append(localMaps, dataLoop2Map)
			}
			objectDataLocalMap5["mountPoints"] = localMaps
		}

		request["ossMountConfig"] = objectDataLocalMap5
	}

	request["runtime"] = d.Get("runtime")
	if v, ok := d.GetOk("cpu"); ok && v.(float64) > 0 {
		request["cpu"] = v
	}
	if v, ok := d.GetOk("environment_variables"); ok {
		request["environmentVariables"] = v
	}
	objectDataLocalMap6 := make(map[string]interface{})

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

		request["code"] = objectDataLocalMap6
	}

	if v, ok := d.GetOk("role"); ok {
		request["role"] = v
	}
	if v, ok := d.GetOkExists("disk_size"); ok && v.(int) > 0 {
		request["diskSize"] = v
	}
	objectDataLocalMap7 := make(map[string]interface{})

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

		request["vpcConfig"] = objectDataLocalMap7
	}

	if v, ok := d.GetOk("description"); ok {
		request["description"] = v
	}
	request["handler"] = d.Get("handler")
	if v, ok := d.GetOkExists("memory_size"); ok && v.(int) > 0 {
		request["memorySize"] = v
	}
	body = request
	wait := incrementalWait(3*time.Second, 5*time.Second)
	err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
		response, err = client.RoaPost("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, "alicloud_fcv3_function", action, AlibabaCloudSdkGoERROR)
	}

	d.SetId(fmt.Sprint(response["functionName"]))

	return resourceAliCloudFcv3FunctionUpdate(d, meta)
}