func resourceAliCloudArmsSyntheticTaskCreate()

in alicloud/resource_alicloud_arms_synthetic_task.go [557:1027]


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

	client := meta.(*connectivity.AliyunClient)

	action := "CreateTimingSyntheticTask"
	var request map[string]interface{}
	var response map[string]interface{}
	query := make(map[string]interface{})
	var err error
	request = make(map[string]interface{})
	request["RegionId"] = client.RegionId

	request["TaskType"] = d.Get("task_type")
	if v, ok := d.GetOk("resource_group_id"); ok {
		request["ResourceGroupId"] = v
	}
	if v, ok := d.GetOk("tags"); ok {
		tagsMap := ConvertTags(v.(map[string]interface{}))
		request["Tags"] = tagsMap
	}

	if v, ok := d.GetOk("monitors"); ok {
		monitorsMaps := make([]map[string]interface{}, 0)
		for _, dataLoop1 := range v.([]interface{}) {
			dataLoop1Tmp := dataLoop1.(map[string]interface{})
			dataLoop1Map := make(map[string]interface{})
			dataLoop1Map["OperatorCode"] = dataLoop1Tmp["operator_code"]
			dataLoop1Map["CityCode"] = dataLoop1Tmp["city_code"]
			dataLoop1Map["ClientType"] = dataLoop1Tmp["client_type"]
			monitorsMaps = append(monitorsMaps, dataLoop1Map)
		}
		request["Monitors"], _ = convertListMapToJsonString(monitorsMaps)
	}

	request["MonitorCategory"] = d.Get("monitor_category")
	request["Name"] = d.Get("synthetic_task_name")
	request["Frequency"] = d.Get("frequency")
	objectDataLocalMap := make(map[string]interface{})
	if v := d.Get("monitor_conf"); !IsNil(v) {
		netICMP_map, _ := jsonpath.Get("$[0].net_icmp[0]", v)
		if !IsNil(netICMP_map) {
			netICMP := make(map[string]interface{})
			nodeNative5, _ := jsonpath.Get("$[0].net_icmp[0].package_num", d.Get("monitor_conf"))
			if nodeNative5 != "" {
				netICMP["PackageNum"] = nodeNative5
			}
			nodeNative6, _ := jsonpath.Get("$[0].net_icmp[0].package_size", d.Get("monitor_conf"))
			if nodeNative6 != "" {
				netICMP["PackageSize"] = nodeNative6
			}
			nodeNative7, _ := jsonpath.Get("$[0].net_icmp[0].split_package", d.Get("monitor_conf"))
			if nodeNative7 != "" {
				netICMP["SplitPackage"] = nodeNative7
			}
			nodeNative8, _ := jsonpath.Get("$[0].net_icmp[0].interval", d.Get("monitor_conf"))
			if nodeNative8 != "" {
				netICMP["Interval"] = nodeNative8
			}
			nodeNative9, _ := jsonpath.Get("$[0].net_icmp[0].tracert_enable", d.Get("monitor_conf"))
			if nodeNative9 != "" {
				netICMP["TracertEnable"] = nodeNative9
			}
			nodeNative10, _ := jsonpath.Get("$[0].net_icmp[0].tracert_num_max", d.Get("monitor_conf"))
			if nodeNative10 != "" {
				netICMP["TracertNumMax"] = nodeNative10
			}
			nodeNative11, _ := jsonpath.Get("$[0].net_icmp[0].tracert_timeout", d.Get("monitor_conf"))
			if nodeNative11 != "" {
				netICMP["TracertTimeout"] = nodeNative11
			}
			nodeNative12, _ := jsonpath.Get("$[0].net_icmp[0].timeout", d.Get("monitor_conf"))
			if nodeNative12 != "" {
				netICMP["Timeout"] = nodeNative12
			}
			nodeNative13, _ := jsonpath.Get("$[0].net_icmp[0].target_url", d.Get("monitor_conf"))
			if nodeNative13 != "" {
				netICMP["TargetUrl"] = nodeNative13
			}
			objectDataLocalMap["NetICMP"] = netICMP
		}
		netTCP_map, _ := jsonpath.Get("$[0].net_tcp[0]", v)
		if !IsNil(netTCP_map) {
			netTCP := make(map[string]interface{})
			nodeNative14, _ := jsonpath.Get("$[0].net_tcp[0].connect_times", d.Get("monitor_conf"))
			if nodeNative14 != "" {
				netTCP["ConnectTimes"] = nodeNative14
			}
			nodeNative15, _ := jsonpath.Get("$[0].net_tcp[0].interval", d.Get("monitor_conf"))
			if nodeNative15 != "" {
				netTCP["Interval"] = nodeNative15
			}
			nodeNative16, _ := jsonpath.Get("$[0].net_tcp[0].tracert_enable", d.Get("monitor_conf"))
			if nodeNative16 != "" {
				netTCP["TracertEnable"] = nodeNative16
			}
			nodeNative17, _ := jsonpath.Get("$[0].net_tcp[0].tracert_num_max", d.Get("monitor_conf"))
			if nodeNative17 != "" {
				netTCP["TracertNumMax"] = nodeNative17
			}
			nodeNative18, _ := jsonpath.Get("$[0].net_tcp[0].tracert_timeout", d.Get("monitor_conf"))
			if nodeNative18 != "" {
				netTCP["TracertTimeout"] = nodeNative18
			}
			nodeNative19, _ := jsonpath.Get("$[0].net_tcp[0].timeout", d.Get("monitor_conf"))
			if nodeNative19 != "" {
				netTCP["Timeout"] = nodeNative19
			}
			nodeNative20, _ := jsonpath.Get("$[0].net_tcp[0].target_url", d.Get("monitor_conf"))
			if nodeNative20 != "" {
				netTCP["TargetUrl"] = nodeNative20
			}
			objectDataLocalMap["NetTCP"] = netTCP
		}
		netDNS_map, _ := jsonpath.Get("$[0].net_dns[0]", v)
		if !IsNil(netDNS_map) {
			netDNS := make(map[string]interface{})
			nodeNative21, _ := jsonpath.Get("$[0].net_dns[0].dns_server_ip_type", d.Get("monitor_conf"))
			if nodeNative21 != "" {
				netDNS["DnsServerIpType"] = nodeNative21
			}
			nodeNative22, _ := jsonpath.Get("$[0].net_dns[0].ns_server", d.Get("monitor_conf"))
			if nodeNative22 != "" {
				netDNS["NsServer"] = nodeNative22
			}
			nodeNative23, _ := jsonpath.Get("$[0].net_dns[0].query_method", d.Get("monitor_conf"))
			if nodeNative23 != "" {
				netDNS["QueryMethod"] = nodeNative23
			}
			nodeNative24, _ := jsonpath.Get("$[0].net_dns[0].timeout", d.Get("monitor_conf"))
			if nodeNative24 != "" {
				netDNS["Timeout"] = nodeNative24
			}
			nodeNative25, _ := jsonpath.Get("$[0].net_dns[0].target_url", d.Get("monitor_conf"))
			if nodeNative25 != "" {
				netDNS["TargetUrl"] = nodeNative25
			}
			objectDataLocalMap["NetDNS"] = netDNS
		}
		apiHTTP_map, _ := jsonpath.Get("$[0].api_http[0]", v)
		if !IsNil(apiHTTP_map) {
			apiHTTP := make(map[string]interface{})
			nodeNative26, _ := jsonpath.Get("$[0].api_http[0].target_url", d.Get("monitor_conf"))
			if nodeNative26 != "" {
				apiHTTP["TargetUrl"] = nodeNative26
			}
			nodeNative27, _ := jsonpath.Get("$[0].api_http[0].method", d.Get("monitor_conf"))
			if nodeNative27 != "" {
				apiHTTP["Method"] = nodeNative27
			}
			requestBody_map, _ := jsonpath.Get("$[0].api_http[0].request_body[0]", v)
			if !IsNil(requestBody_map) {
				requestBody := make(map[string]interface{})
				nodeNative28, _ := jsonpath.Get("$[0].api_http[0].request_body[0].content", d.Get("monitor_conf"))
				if nodeNative28 != "" {
					requestBody["Content"] = nodeNative28
				}
				nodeNative29, _ := jsonpath.Get("$[0].api_http[0].request_body[0].type", d.Get("monitor_conf"))
				if nodeNative29 != "" {
					requestBody["Type"] = nodeNative29
				}
				apiHTTP["RequestBody"] = requestBody
			}
			nodeNative30, _ := jsonpath.Get("$[0].api_http[0].connect_timeout", d.Get("monitor_conf"))
			if nodeNative30 != "" {
				apiHTTP["ConnectTimeout"] = nodeNative30
			}
			nodeNative31, _ := jsonpath.Get("$[0].api_http[0].request_headers", d.Get("monitor_conf"))
			if nodeNative31 != "" {
				apiHTTP["RequestHeaders"] = nodeNative31
			}
			nodeNative32, _ := jsonpath.Get("$[0].api_http[0].timeout", d.Get("monitor_conf"))
			if nodeNative32 != "" {
				apiHTTP["Timeout"] = nodeNative32
			}
			objectDataLocalMap["ApiHTTP"] = apiHTTP
		}
		website_map, _ := jsonpath.Get("$[0].website[0]", v)
		if !IsNil(website_map) {
			website := make(map[string]interface{})
			nodeNative33, _ := jsonpath.Get("$[0].website[0].automatic_scrolling", d.Get("monitor_conf"))
			if nodeNative33 != "" {
				website["AutomaticScrolling"] = nodeNative33
			}
			nodeNative34, _ := jsonpath.Get("$[0].website[0].custom_header", d.Get("monitor_conf"))
			if nodeNative34 != "" {
				website["CustomHeader"] = nodeNative34
			}
			nodeNative35, _ := jsonpath.Get("$[0].website[0].disable_cache", d.Get("monitor_conf"))
			if nodeNative35 != "" {
				website["DisableCache"] = nodeNative35
			}
			nodeNative36, _ := jsonpath.Get("$[0].website[0].disable_compression", d.Get("monitor_conf"))
			if nodeNative36 != "" {
				website["DisableCompression"] = nodeNative36
			}
			nodeNative37, _ := jsonpath.Get("$[0].website[0].filter_invalid_ip", d.Get("monitor_conf"))
			if nodeNative37 != "" {
				website["FilterInvalidIP"] = nodeNative37
			}
			nodeNative38, _ := jsonpath.Get("$[0].website[0].ignore_certificate_error", d.Get("monitor_conf"))
			if nodeNative38 != "" {
				website["IgnoreCertificateError"] = nodeNative38
			}
			nodeNative39, _ := jsonpath.Get("$[0].website[0].slow_element_threshold", d.Get("monitor_conf"))
			if nodeNative39 != "" {
				website["SlowElementThreshold"] = nodeNative39
			}
			nodeNative40, _ := jsonpath.Get("$[0].website[0].wait_completion_time", d.Get("monitor_conf"))
			if nodeNative40 != "" {
				website["WaitCompletionTime"] = nodeNative40
			}
			nodeNative41, _ := jsonpath.Get("$[0].website[0].verify_string_blacklist", d.Get("monitor_conf"))
			if nodeNative41 != "" {
				website["VerifyStringBlacklist"] = nodeNative41
			}
			nodeNative42, _ := jsonpath.Get("$[0].website[0].verify_string_whitelist", d.Get("monitor_conf"))
			if nodeNative42 != "" {
				website["VerifyStringWhitelist"] = nodeNative42
			}
			nodeNative43, _ := jsonpath.Get("$[0].website[0].element_blacklist", d.Get("monitor_conf"))
			if nodeNative43 != "" {
				website["ElementBlacklist"] = nodeNative43
			}
			nodeNative44, _ := jsonpath.Get("$[0].website[0].dns_hijack_whitelist", d.Get("monitor_conf"))
			if nodeNative44 != "" {
				website["DNSHijackWhitelist"] = nodeNative44
			}
			nodeNative45, _ := jsonpath.Get("$[0].website[0].page_tamper", d.Get("monitor_conf"))
			if nodeNative45 != "" {
				website["PageTamper"] = nodeNative45
			}
			nodeNative46, _ := jsonpath.Get("$[0].website[0].flow_hijack_jump_times", d.Get("monitor_conf"))
			if nodeNative46 != "" {
				website["FlowHijackJumpTimes"] = nodeNative46
			}
			nodeNative47, _ := jsonpath.Get("$[0].website[0].flow_hijack_logo", d.Get("monitor_conf"))
			if nodeNative47 != "" {
				website["FlowHijackLogo"] = nodeNative47
			}
			nodeNative48, _ := jsonpath.Get("$[0].website[0].target_url", d.Get("monitor_conf"))
			if nodeNative48 != "" {
				website["TargetUrl"] = nodeNative48
			}
			nodeNative49, _ := jsonpath.Get("$[0].website[0].custom_header_content", d.Get("monitor_conf"))
			if nodeNative49 != "" {
				website["CustomHeaderContent"] = nodeNative49
			}
			nodeNative50, _ := jsonpath.Get("$[0].website[0].monitor_timeout", d.Get("monitor_conf"))
			if nodeNative50 != "" {
				website["MonitorTimeout"] = nodeNative50
			}
			nodeNative51, _ := jsonpath.Get("$[0].website[0].redirection", d.Get("monitor_conf"))
			if nodeNative51 != "" {
				website["Redirection"] = nodeNative51
			}
			objectDataLocalMap["Website"] = website
		}
		fileDownload_map, _ := jsonpath.Get("$[0].file_download[0]", v)
		if !IsNil(fileDownload_map) {
			fileDownload := make(map[string]interface{})
			nodeNative52, _ := jsonpath.Get("$[0].file_download[0].download_kernel", d.Get("monitor_conf"))
			if nodeNative52 != "" {
				fileDownload["DownloadKernel"] = nodeNative52
			}
			nodeNative53, _ := jsonpath.Get("$[0].file_download[0].quick_protocol", d.Get("monitor_conf"))
			if nodeNative53 != "" {
				fileDownload["QuickProtocol"] = nodeNative53
			}
			nodeNative54, _ := jsonpath.Get("$[0].file_download[0].connection_timeout", d.Get("monitor_conf"))
			if nodeNative54 != "" {
				fileDownload["ConnectionTimeout"] = nodeNative54
			}
			nodeNative55, _ := jsonpath.Get("$[0].file_download[0].ignore_certificate_status_error", d.Get("monitor_conf"))
			if nodeNative55 != "" {
				fileDownload["IgnoreCertificateStatusError"] = nodeNative55
			}
			nodeNative56, _ := jsonpath.Get("$[0].file_download[0].ignore_certificate_untrustworthy_error", d.Get("monitor_conf"))
			if nodeNative56 != "" {
				fileDownload["IgnoreCertificateUntrustworthyError"] = nodeNative56
			}
			nodeNative57, _ := jsonpath.Get("$[0].file_download[0].ignore_invalid_host_error", d.Get("monitor_conf"))
			if nodeNative57 != "" {
				fileDownload["IgnoreInvalidHostError"] = nodeNative57
			}
			nodeNative58, _ := jsonpath.Get("$[0].file_download[0].transmission_size", d.Get("monitor_conf"))
			if nodeNative58 != "" {
				fileDownload["TransmissionSize"] = nodeNative58
			}
			nodeNative59, _ := jsonpath.Get("$[0].file_download[0].ignore_certificate_canceled_error", d.Get("monitor_conf"))
			if nodeNative59 != "" {
				fileDownload["IgnoreCertificateCanceledError"] = nodeNative59
			}
			nodeNative60, _ := jsonpath.Get("$[0].file_download[0].ignore_certificate_auth_error", d.Get("monitor_conf"))
			if nodeNative60 != "" {
				fileDownload["IgnoreCertificateAuthError"] = nodeNative60
			}
			nodeNative61, _ := jsonpath.Get("$[0].file_download[0].ignore_certificate_out_of_date_error", d.Get("monitor_conf"))
			if nodeNative61 != "" {
				fileDownload["IgnoreCertificateOutOfDateError"] = nodeNative61
			}
			nodeNative62, _ := jsonpath.Get("$[0].file_download[0].ignore_certificate_using_error", d.Get("monitor_conf"))
			if nodeNative62 != "" {
				fileDownload["IgnoreCertificateUsingError"] = nodeNative62
			}
			nodeNative63, _ := jsonpath.Get("$[0].file_download[0].verify_way", d.Get("monitor_conf"))
			if nodeNative63 != "" {
				fileDownload["VerifyWay"] = nodeNative63
			}
			nodeNative64, _ := jsonpath.Get("$[0].file_download[0].validate_keywords", d.Get("monitor_conf"))
			if nodeNative64 != "" {
				fileDownload["ValidateKeywords"] = nodeNative64
			}
			nodeNative65, _ := jsonpath.Get("$[0].file_download[0].target_url", d.Get("monitor_conf"))
			if nodeNative65 != "" {
				fileDownload["TargetUrl"] = nodeNative65
			}
			nodeNative66, _ := jsonpath.Get("$[0].file_download[0].monitor_timeout", d.Get("monitor_conf"))
			if nodeNative66 != "" {
				fileDownload["MonitorTimeout"] = nodeNative66
			}
			nodeNative67, _ := jsonpath.Get("$[0].file_download[0].custom_header_content", d.Get("monitor_conf"))
			if nodeNative67 != "" {
				fileDownload["CustomHeaderContent"] = nodeNative67
			}
			nodeNative68, _ := jsonpath.Get("$[0].file_download[0].redirection", d.Get("monitor_conf"))
			if nodeNative68 != "" {
				fileDownload["Redirection"] = nodeNative68
			}
			nodeNative69, _ := jsonpath.Get("$[0].file_download[0].white_list", d.Get("monitor_conf"))
			if nodeNative69 != "" {
				fileDownload["WhiteList"] = nodeNative69
			}
			objectDataLocalMap["FileDownload"] = fileDownload
		}
		stream_map, _ := jsonpath.Get("$[0].stream[0]", v)
		if !IsNil(stream_map) {
			stream := make(map[string]interface{})
			nodeNative70, _ := jsonpath.Get("$[0].stream[0].stream_type", d.Get("monitor_conf"))
			if nodeNative70 != "" {
				stream["StreamType"] = nodeNative70
			}
			nodeNative71, _ := jsonpath.Get("$[0].stream[0].stream_monitor_timeout", d.Get("monitor_conf"))
			if nodeNative71 != "" {
				stream["StreamMonitorTimeout"] = nodeNative71
			}
			nodeNative72, _ := jsonpath.Get("$[0].stream[0].stream_address_type", d.Get("monitor_conf"))
			if nodeNative72 != "" {
				stream["StreamAddressType"] = nodeNative72
			}
			nodeNative73, _ := jsonpath.Get("$[0].stream[0].player_type", d.Get("monitor_conf"))
			if nodeNative73 != "" {
				stream["PlayerType"] = nodeNative73
			}
			nodeNative74, _ := jsonpath.Get("$[0].stream[0].white_list", d.Get("monitor_conf"))
			if nodeNative74 != "" {
				stream["WhiteList"] = nodeNative74
			}
			nodeNative75, _ := jsonpath.Get("$[0].stream[0].custom_header_content", d.Get("monitor_conf"))
			if nodeNative75 != "" {
				stream["CustomHeaderContent"] = nodeNative75
			}
			nodeNative76, _ := jsonpath.Get("$[0].stream[0].target_url", d.Get("monitor_conf"))
			if nodeNative76 != "" {
				stream["TargetUrl"] = nodeNative76
			}
			objectDataLocalMap["Stream"] = stream
		}
		request["MonitorConf"] = convertMapToJsonStringIgnoreError(objectDataLocalMap)
	}

	objectDataLocalMap1 := make(map[string]interface{})
	if v := d.Get("custom_period"); !IsNil(v) {
		nodeNative77, _ := jsonpath.Get("$[0].end_hour", d.Get("custom_period"))
		if nodeNative77 != "" {
			objectDataLocalMap1["EndHour"] = nodeNative77
		}
		nodeNative78, _ := jsonpath.Get("$[0].start_hour", d.Get("custom_period"))
		if nodeNative78 != "" {
			objectDataLocalMap1["StartHour"] = nodeNative78
		}
		request["CustomPeriod"] = convertMapToJsonStringIgnoreError(objectDataLocalMap1)
	}

	objectDataLocalMap2 := make(map[string]interface{})
	if v := d.Get("common_setting"); !IsNil(v) {
		customHost_map, _ := jsonpath.Get("$[0].custom_host[0]", v)
		if !IsNil(customHost_map) {
			customHost := make(map[string]interface{})
			if v, ok := d.GetOk("common_setting"); ok {
				localData2, err := jsonpath.Get("$[0].custom_host[0].hosts", v)
				if err != nil {
					return WrapError(err)
				}
				localMaps := make([]map[string]interface{}, 0)
				for _, dataLoop2 := range localData2.([]interface{}) {
					dataLoop2Tmp := dataLoop2.(map[string]interface{})
					dataLoop2Map := make(map[string]interface{})
					dataLoop2Map["Domain"] = dataLoop2Tmp["domain"]
					dataLoop2Map["Ips"] = dataLoop2Tmp["ips"]
					dataLoop2Map["IpType"] = dataLoop2Tmp["ip_type"]
					localMaps = append(localMaps, dataLoop2Map)
				}
				customHost["Hosts"] = localMaps
			}
			nodeNative82, _ := jsonpath.Get("$[0].custom_host[0].select_type", d.Get("common_setting"))
			if nodeNative82 != "" {
				customHost["SelectType"] = nodeNative82
			}
			objectDataLocalMap2["CustomHost"] = customHost
		}
		nodeNative83, _ := jsonpath.Get("$[0].monitor_samples", d.Get("common_setting"))
		if nodeNative83 != "" {
			objectDataLocalMap2["MonitorSamples"] = nodeNative83
		}
		nodeNative84, _ := jsonpath.Get("$[0].is_open_trace", d.Get("common_setting"))
		if nodeNative84 != "" {
			objectDataLocalMap2["IsOpenTrace"] = nodeNative84
		}
		nodeNative85, _ := jsonpath.Get("$[0].trace_client_type", d.Get("common_setting"))
		if nodeNative85 != "" {
			objectDataLocalMap2["TraceClientType"] = nodeNative85
		}
		nodeNative86, _ := jsonpath.Get("$[0].xtrace_region", d.Get("common_setting"))
		if nodeNative86 != "" {
			objectDataLocalMap2["XtraceRegion"] = nodeNative86
		}
		nodeNative87, _ := jsonpath.Get("$[0].ip_type", d.Get("common_setting"))
		if nodeNative87 != "" {
			objectDataLocalMap2["IpType"] = nodeNative87
		}
		request["CommonSetting"] = convertMapToJsonStringIgnoreError(objectDataLocalMap2)
	}

	if v, ok := d.GetOk("available_assertions"); ok {
		availableAssertionsMaps := make([]map[string]interface{}, 0)
		for _, dataLoop3 := range v.([]interface{}) {
			dataLoop3Tmp := dataLoop3.(map[string]interface{})
			dataLoop3Map := make(map[string]interface{})
			dataLoop3Map["Type"] = dataLoop3Tmp["type"]
			dataLoop3Map["Target"] = dataLoop3Tmp["target"]
			dataLoop3Map["Operator"] = dataLoop3Tmp["operator"]
			dataLoop3Map["Expect"] = dataLoop3Tmp["expect"]
			availableAssertionsMaps = append(availableAssertionsMaps, dataLoop3Map)
		}
		request["AvailableAssertions"], _ = convertListMapToJsonString(availableAssertionsMaps)
	}

	wait := incrementalWait(3*time.Second, 5*time.Second)
	err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
		response, err = client.RpcPost("ARMS", "2019-08-08", action, query, request, true)

		if err != nil {
			if NeedRetry(err) {
				wait()
				return resource.RetryableError(err)
			}
			return resource.NonRetryableError(err)
		}
		addDebug(action, response, request)
		return nil
	})

	if err != nil {
		return WrapErrorf(err, DefaultErrorMsg, "alicloud_arms_synthetic_task", action, AlibabaCloudSdkGoERROR)
	}

	id, _ := jsonpath.Get("$.Data.TaskId", response)
	d.SetId(fmt.Sprint(id))

	return resourceAliCloudArmsSyntheticTaskUpdate(d, meta)
}