func resourceAliCloudSlsAlertCreate()

in alicloud/resource_alicloud_sls_alert.go [434:749]


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

	client := meta.(*connectivity.AliyunClient)

	action := fmt.Sprintf("/alerts")
	var request map[string]interface{}
	var response map[string]interface{}
	query := make(map[string]*string)
	body := make(map[string]interface{})
	hostMap := make(map[string]*string)
	var err error
	request = make(map[string]interface{})
	hostMap["project"] = StringPointer(d.Get("project_name").(string))
	request["name"] = d.Get("alert_name")

	request["displayName"] = d.Get("display_name")
	if v, ok := d.GetOk("description"); ok {
		request["description"] = v
	}
	objectDataLocalMap := make(map[string]interface{})
	if v := d.Get("configuration"); v != nil {
		nodeNative, _ := jsonpath.Get("$[0].mute_until", d.Get("configuration"))
		if nodeNative != nil && nodeNative != "" {
			objectDataLocalMap["muteUntil"] = nodeNative
		}
		nodeNative1, _ := jsonpath.Get("$[0].version", d.Get("configuration"))
		if nodeNative1 != nil && nodeNative1 != "" {
			objectDataLocalMap["version"] = nodeNative1
		}
		nodeNative2, _ := jsonpath.Get("$[0].type", d.Get("configuration"))
		if nodeNative2 != nil && nodeNative2 != "" {
			objectDataLocalMap["type"] = nodeNative2
		}
		templateConfiguration := make(map[string]interface{})
		nodeNative3, _ := jsonpath.Get("$[0].template_configuration[0].type", d.Get("configuration"))
		if nodeNative3 != nil && nodeNative3 != "" {
			templateConfiguration["type"] = nodeNative3
		}
		nodeNative4, _ := jsonpath.Get("$[0].template_configuration[0].version", d.Get("configuration"))
		if nodeNative4 != nil && nodeNative4 != "" {
			templateConfiguration["version"] = nodeNative4
		}
		nodeNative5, _ := jsonpath.Get("$[0].template_configuration[0].lang", d.Get("configuration"))
		if nodeNative5 != nil && nodeNative5 != "" {
			templateConfiguration["lang"] = nodeNative5
		}
		nodeNative6, _ := jsonpath.Get("$[0].template_configuration[0].template_id", d.Get("configuration"))
		if nodeNative6 != nil && nodeNative6 != "" {
			templateConfiguration["id"] = nodeNative6
		}
		nodeNative7, _ := jsonpath.Get("$[0].template_configuration[0].tokens", d.Get("configuration"))
		if nodeNative7 != nil && nodeNative7 != "" {
			templateConfiguration["tokens"] = nodeNative7
		}
		nodeNative8, _ := jsonpath.Get("$[0].template_configuration[0].annotations", d.Get("configuration"))
		if nodeNative8 != nil && nodeNative8 != "" {
			templateConfiguration["aonotations"] = nodeNative8
		}

		objectDataLocalMap["templateConfiguration"] = templateConfiguration
		nodeNative9, _ := jsonpath.Get("$[0].dashboard", d.Get("configuration"))
		if nodeNative9 != nil && nodeNative9 != "" {
			objectDataLocalMap["dashboard"] = nodeNative9
		}
		nodeNative10, _ := jsonpath.Get("$[0].threshold", d.Get("configuration"))
		if nodeNative10 != nil && nodeNative10 != "" {
			objectDataLocalMap["threshold"] = nodeNative10
		}
		nodeNative11, _ := jsonpath.Get("$[0].no_data_fire", d.Get("configuration"))
		if nodeNative11 != nil && nodeNative11 != "" {
			objectDataLocalMap["noDataFire"] = nodeNative11
		}
		nodeNative12, _ := jsonpath.Get("$[0].no_data_severity", d.Get("configuration"))
		if nodeNative12 != nil && nodeNative12 != "" {
			objectDataLocalMap["noDataSeverity"] = nodeNative12
		}
		nodeNative13, _ := jsonpath.Get("$[0].send_resolved", d.Get("configuration"))
		if nodeNative13 != nil && nodeNative13 != "" {
			objectDataLocalMap["sendResolved"] = nodeNative13
		}
		if v, ok := d.GetOk("configuration"); ok {
			localData, err := jsonpath.Get("$[0].query_list", v)
			if err != nil {
				return WrapError(err)
			}
			localMaps := make([]interface{}, 0)
			for _, dataLoop := range localData.([]interface{}) {
				dataLoopTmp := dataLoop.(map[string]interface{})
				dataLoopMap := make(map[string]interface{})
				dataLoopMap["chartTitle"] = dataLoopTmp["chart_title"]
				dataLoopMap["query"] = dataLoopTmp["query"]
				dataLoopMap["timeSpanType"] = dataLoopTmp["time_span_type"]
				dataLoopMap["start"] = dataLoopTmp["start"]
				dataLoopMap["end"] = dataLoopTmp["end"]
				dataLoopMap["storeType"] = dataLoopTmp["store_type"]
				dataLoopMap["project"] = dataLoopTmp["project"]
				dataLoopMap["store"] = dataLoopTmp["store"]
				dataLoopMap["region"] = dataLoopTmp["region"]
				dataLoopMap["roleArn"] = dataLoopTmp["role_arn"]
				dataLoopMap["dashboardId"] = dataLoopTmp["dashboard_id"]
				dataLoopMap["powerSqlMode"] = dataLoopTmp["power_sql_mode"]
				dataLoopMap["ui"] = dataLoopTmp["ui"]
				localMaps = append(localMaps, dataLoopMap)
			}
			objectDataLocalMap["queryList"] = localMaps
		}
		if v, ok := d.GetOk("configuration"); ok {
			localData1, err := jsonpath.Get("$[0].annotations", v)
			if err != nil {
				return WrapError(err)
			}
			localMaps1 := make([]interface{}, 0)
			for _, dataLoop1 := range localData1.([]interface{}) {
				dataLoop1Tmp := dataLoop1.(map[string]interface{})
				dataLoop1Map := make(map[string]interface{})
				dataLoop1Map["key"] = dataLoop1Tmp["key"]
				dataLoop1Map["value"] = dataLoop1Tmp["value"]
				localMaps1 = append(localMaps1, dataLoop1Map)
			}
			objectDataLocalMap["annotations"] = localMaps1
		}
		if v, ok := d.GetOk("configuration"); ok {
			localData2, err := jsonpath.Get("$[0].labels", v)
			if err != nil {
				return WrapError(err)
			}
			localMaps2 := make([]interface{}, 0)
			for _, dataLoop2 := range localData2.([]interface{}) {
				dataLoop2Tmp := dataLoop2.(map[string]interface{})
				dataLoop2Map := make(map[string]interface{})
				dataLoop2Map["key"] = dataLoop2Tmp["key"]
				dataLoop2Map["value"] = dataLoop2Tmp["value"]
				localMaps2 = append(localMaps2, dataLoop2Map)
			}
			objectDataLocalMap["labels"] = localMaps2
		}
		conditionConfiguration := make(map[string]interface{})
		nodeNative31, _ := jsonpath.Get("$[0].condition_configuration[0].count_condition", d.Get("configuration"))
		if nodeNative31 != nil && nodeNative31 != "" {
			conditionConfiguration["countCondition"] = nodeNative31
		}
		nodeNative32, _ := jsonpath.Get("$[0].condition_configuration[0].condition", d.Get("configuration"))
		if nodeNative32 != nil && nodeNative32 != "" {
			conditionConfiguration["condition"] = nodeNative32
		}

		objectDataLocalMap["conditionConfiguration"] = conditionConfiguration
		if v, ok := d.GetOk("configuration"); ok {
			localData3, err := jsonpath.Get("$[0].severity_configurations", v)
			if err != nil {
				return WrapError(err)
			}
			localMaps3 := make([]interface{}, 0)
			for _, dataLoop3 := range localData3.([]interface{}) {
				dataLoop3Tmp := dataLoop3.(map[string]interface{})
				dataLoop3Map := make(map[string]interface{})
				dataLoop3Map["severity"] = dataLoop3Tmp["severity"]
				if !IsNil(dataLoop3Tmp["eval_condition"]) {
					localData4 := make(map[string]interface{})
					nodeNative34, _ := jsonpath.Get("$[0].condition", dataLoop3Tmp["eval_condition"])
					if nodeNative34 != nil && nodeNative34 != "" {
						localData4["condition"] = nodeNative34
					}
					nodeNative35, _ := jsonpath.Get("$[0].count_condition", dataLoop3Tmp["eval_condition"])
					if nodeNative35 != nil && nodeNative35 != "" {
						localData4["countCondition"] = nodeNative35
					}
					dataLoop3Map["evalCondition"] = localData4
				}
				localMaps3 = append(localMaps3, dataLoop3Map)
			}
			objectDataLocalMap["severityConfigurations"] = localMaps3
		}
		if v, ok := d.GetOk("configuration"); ok {
			localData5, err := jsonpath.Get("$[0].join_configurations", v)
			if err != nil {
				return WrapError(err)
			}
			localMaps5 := make([]interface{}, 0)
			for _, dataLoop5 := range localData5.([]interface{}) {
				dataLoop5Tmp := dataLoop5.(map[string]interface{})
				dataLoop5Map := make(map[string]interface{})
				dataLoop5Map["type"] = dataLoop5Tmp["type"]
				dataLoop5Map["condition"] = dataLoop5Tmp["condition"]
				localMaps5 = append(localMaps5, dataLoop5Map)
			}
			objectDataLocalMap["joinConfigurations"] = localMaps5
		}
		groupConfiguration := make(map[string]interface{})
		nodeNative38, _ := jsonpath.Get("$[0].group_configuration[0].type", d.Get("configuration"))
		if nodeNative38 != nil && nodeNative38 != "" {
			groupConfiguration["type"] = nodeNative38
		}
		nodeNative39, _ := jsonpath.Get("$[0].group_configuration[0].fields", v)
		if nodeNative39 != nil && nodeNative39 != "" {
			groupConfiguration["fields"] = nodeNative39
		}

		objectDataLocalMap["groupConfiguration"] = groupConfiguration
		policyConfiguration := make(map[string]interface{})
		nodeNative40, _ := jsonpath.Get("$[0].policy_configuration[0].alert_policy_id", d.Get("configuration"))
		if nodeNative40 != nil && nodeNative40 != "" {
			policyConfiguration["alertPolicyId"] = nodeNative40
		}
		nodeNative41, _ := jsonpath.Get("$[0].policy_configuration[0].action_policy_id", d.Get("configuration"))
		if nodeNative41 != nil && nodeNative41 != "" {
			policyConfiguration["actionPolicyId"] = nodeNative41
		}
		nodeNative42, _ := jsonpath.Get("$[0].policy_configuration[0].repeat_interval", d.Get("configuration"))
		if nodeNative42 != nil && nodeNative42 != "" {
			policyConfiguration["repeatInterval"] = nodeNative42
		}

		objectDataLocalMap["policyConfiguration"] = policyConfiguration
		nodeNative43, _ := jsonpath.Get("$[0].auto_annotation", d.Get("configuration"))
		if nodeNative43 != nil && nodeNative43 != "" {
			objectDataLocalMap["autoAnnotation"] = nodeNative43
		}
		sinkEventStore := make(map[string]interface{})
		nodeNative44, _ := jsonpath.Get("$[0].sink_event_store[0].enabled", d.Get("configuration"))
		if nodeNative44 != nil && nodeNative44 != "" {
			sinkEventStore["enabled"] = nodeNative44
		}
		nodeNative45, _ := jsonpath.Get("$[0].sink_event_store[0].endpoint", d.Get("configuration"))
		if nodeNative45 != nil && nodeNative45 != "" {
			sinkEventStore["endpoint"] = nodeNative45
		}
		nodeNative46, _ := jsonpath.Get("$[0].sink_event_store[0].project", d.Get("configuration"))
		if nodeNative46 != nil && nodeNative46 != "" {
			sinkEventStore["project"] = nodeNative46
		}
		nodeNative47, _ := jsonpath.Get("$[0].sink_event_store[0].event_store", d.Get("configuration"))
		if nodeNative47 != nil && nodeNative47 != "" {
			sinkEventStore["eventStore"] = nodeNative47
		}
		nodeNative48, _ := jsonpath.Get("$[0].sink_event_store[0].role_arn", d.Get("configuration"))
		if nodeNative48 != nil && nodeNative48 != "" {
			sinkEventStore["roleArn"] = nodeNative48
		}

		objectDataLocalMap["sinkEventStore"] = sinkEventStore
		sinkCms := make(map[string]interface{})
		nodeNative49, _ := jsonpath.Get("$[0].sink_cms[0].enabled", d.Get("configuration"))
		if nodeNative49 != nil && nodeNative49 != "" {
			sinkCms["enabled"] = nodeNative49
		}

		objectDataLocalMap["sinkCms"] = sinkCms
		sinkAlerthub := make(map[string]interface{})
		nodeNative50, _ := jsonpath.Get("$[0].sink_alerthub[0].enabled", d.Get("configuration"))
		if nodeNative50 != nil && nodeNative50 != "" {
			sinkAlerthub["enabled"] = nodeNative50
		}

		objectDataLocalMap["sinkAlerthub"] = sinkAlerthub
		nodeNative51, _ := jsonpath.Get("$[0].tags", v)
		if nodeNative51 != nil && nodeNative51 != "" {
			objectDataLocalMap["tags"] = nodeNative51
		}

		request["configuration"] = objectDataLocalMap
	}

	objectDataLocalMap1 := make(map[string]interface{})
	if v := d.Get("schedule"); v != nil {
		nodeNative52, _ := jsonpath.Get("$[0].type", d.Get("schedule"))
		if nodeNative52 != nil && nodeNative52 != "" {
			objectDataLocalMap1["type"] = nodeNative52
		}
		nodeNative53, _ := jsonpath.Get("$[0].cron_expression", d.Get("schedule"))
		if nodeNative53 != nil && nodeNative53 != "" {
			objectDataLocalMap1["cronExpression"] = nodeNative53
		}
		nodeNative54, _ := jsonpath.Get("$[0].run_immdiately", d.Get("schedule"))
		if nodeNative54 != nil && nodeNative54 != "" {
			objectDataLocalMap1["runImmediately"] = nodeNative54
		}
		nodeNative55, _ := jsonpath.Get("$[0].time_zone", d.Get("schedule"))
		if nodeNative55 != nil && nodeNative55 != "" {
			objectDataLocalMap1["timeZone"] = nodeNative55
		}
		nodeNative56, _ := jsonpath.Get("$[0].delay", d.Get("schedule"))
		if nodeNative56 != nil && nodeNative56 != "" {
			objectDataLocalMap1["delay"] = nodeNative56
		}
		nodeNative57, _ := jsonpath.Get("$[0].interval", d.Get("schedule"))
		if nodeNative57 != nil && nodeNative57 != "" {
			objectDataLocalMap1["interval"] = nodeNative57
		}

		request["schedule"] = objectDataLocalMap1
	}

	body = request
	wait := incrementalWait(3*time.Second, 5*time.Second)
	err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
		response, err = client.Do("Sls", roaParam("POST", "2020-12-30", "CreateAlert", action), query, body, nil, hostMap, false)
		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_sls_alert", action, AlibabaCloudSdkGoERROR)
	}

	d.SetId(fmt.Sprintf("%v:%v", *hostMap["project"], request["name"]))

	return resourceAliCloudSlsAlertUpdate(d, meta)
}