func resourceAlibabacloudStackDtsSubscriptionJobCreate()

in alibabacloudstack/resource_apsarastack_dts_subscriptionjob.go [200:393]


func resourceAlibabacloudStackDtsSubscriptionJobCreate(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*connectivity.AlibabacloudStackClient)
	action := "CreateDtsInstance"
	request := client.NewCommonRequest("POST", "Dts", "2020-01-01", action, "")
	request.Headers["x-acs-caller-sdk-source"] = "Terraform" // 必填,调用来源说明
	request.Headers["x-acs-regionid"] = client.RegionId
	request.Headers["x-acs-content-type"] = "application/json"
	request.Headers["Content-type"] = "application/json"
	request.QueryParams["AutoPay"] = "false"
	request.QueryParams["AutoStart"] = "true"
	if v, ok := d.GetOk("compute_unit"); ok {
		request.QueryParams["ComputeUnit"] = v.(string)
	}
	if v, ok := d.GetOk("database_count"); ok {
		request.QueryParams["DatabaseCount"] = fmt.Sprintf("%v", v.(int))
	}
	if v, ok := d.GetOk("destination_endpoint_engine_name"); ok {
		request.QueryParams["DestinationEndpointEngineName"] = v.(string)
	}
	if v, ok := d.GetOk("destination_region"); ok {
		request.QueryParams["DestinationRegion"] = v.(string)
	}
	if v, ok := d.GetOk("instance_class"); ok {
		request.QueryParams["InstanceClass"] = v.(string)
	}
	if v, ok := d.GetOk("payment_type"); ok {
		request.QueryParams["PayType"] = v.(string)
	}
	if v, ok := d.GetOk("payment_duration_unit"); ok {
		request.QueryParams["Period"] = v.(string)
	}
	if v, ok := d.GetOk("source_endpoint_engine_name"); ok {
		request.QueryParams["SourceEndpointEngineName"] = v.(string)
	}
	if v, ok := d.GetOk("source_endpoint_region"); ok {
		request.QueryParams["SourceRegion"] = v.(string)
	}
	if v, ok := d.GetOk("sync_architecture"); ok {
		request.QueryParams["SyncArchitecture"] = v.(string)
	}
	request.QueryParams["Type"] = "SUBSCRIBE"
	if v, ok := d.GetOk("payment_duration"); ok {
		request.QueryParams["UsedTime"] = fmt.Sprintf("%v", v.(int))
	}
	wait := incrementalWait(3*time.Second, 3*time.Second)
	response := make(map[string]interface{})
	request.Domain = client.Config.Endpoints[connectivity.DTSCode]
	var err error
	var dtsClient *sts.Client
	if client.Config.SecurityToken == "" {
		dtsClient, err = sts.NewClientWithAccessKey(client.Config.RegionId, client.Config.AccessKey, client.Config.SecretKey)
	} else {
		dtsClient, err = sts.NewClientWithStsToken(client.Config.RegionId, client.Config.AccessKey, client.Config.SecretKey, client.Config.SecurityToken)
	}
	dtsClient.Domain = client.Config.Endpoints[connectivity.DTSCode]
	err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
		raw, err := dtsClient.ProcessCommonRequest(request)
		addDebug(action, raw, request, request.QueryParams)
		if err != nil {
			if errmsgs.NeedRetry(err) {
				wait()
				return resource.RetryableError(err)
			}
			return resource.NonRetryableError(err)
		}
		if err != nil {
			errmsg := ""
			if raw != nil {
				errmsg = errmsgs.GetBaseResponseErrorMessage(raw.BaseResponse)
			}
			return resource.NonRetryableError(errmsgs.WrapErrorf(err, errmsgs.RequestV1ErrorMsg, "alibabacloudstack_dts_subscription_job", action, errmsgs.AlibabacloudStackSdkGoERROR, errmsg))
		}
		err = json.Unmarshal(raw.GetHttpContentBytes(), &response)
		if err != nil {
			return resource.NonRetryableError(err)
		}
		return nil
	})
	if err != nil {
		return err
	}
	d.Set("dts_instance_id", response["InstanceId"])
	configureSubscriptionReq := client.NewCommonRequest("POST", "Dts", "2020-01-01", "ConfigureSubscription", "")
	configureSubscriptionReq.Headers["x-acs-caller-sdk-source"] = "Terraform" // 必填,调用来源说明
	configureSubscriptionReq.QueryParams["DbList"] = d.Get("db_list").(string)
	configureSubscriptionReq.QueryParams["SubscriptionInstanceNetworkType"] = d.Get("subscription_instance_network_type").(string)
	configureSubscriptionReq.QueryParams["DtsInstanceId"] = d.Get("dts_instance_id").(string)
	configureSubscriptionReq.QueryParams["DtsJobName"] = d.Get("dts_job_name").(string)
	configureSubscriptionReq.Headers["x-acs-regionid"] = client.RegionId
	configureSubscriptionReq.Headers["x-acs-content-type"] = "application/json"
	configureSubscriptionReq.Headers["Content-type"] = "application/json"
	if v, ok := d.GetOk("checkpoint"); ok {
		configureSubscriptionReq.QueryParams["Checkpoint"] = v.(string)
	}
	if v, ok := d.GetOk("source_endpoint_database_name"); ok {
		configureSubscriptionReq.QueryParams["SourceEndpointDatabaseName"] = v.(string)
	}
	if v, ok := d.GetOk("source_endpoint_engine_name"); ok {
		configureSubscriptionReq.QueryParams["SourceEndpointEngineName"] = v.(string)
	}
	if v, ok := d.GetOk("source_endpoint_ip"); ok {
		configureSubscriptionReq.QueryParams["SourceEndpointIP"] = v.(string)
	}
	if v, ok := d.GetOk("source_endpoint_instance_id"); ok {
		configureSubscriptionReq.QueryParams["SourceEndpointInstanceID"] = v.(string)
	}
	if v, ok := d.GetOk("source_endpoint_instance_type"); ok {
		configureSubscriptionReq.QueryParams["SourceEndpointInstanceType"] = v.(string)
	}
	if v, ok := d.GetOk("source_endpoint_oracle_sid"); ok {
		configureSubscriptionReq.QueryParams["SourceEndpointOracleSID"] = v.(string)
	}
	if v, ok := d.GetOk("source_endpoint_owner_id"); ok {
		configureSubscriptionReq.QueryParams["SourceEndpointOwnerID"] = v.(string)
	}
	if v, ok := d.GetOk("source_endpoint_password"); ok {
		configureSubscriptionReq.QueryParams["SourceEndpointPassword"] = v.(string)
	}
	if v, ok := d.GetOk("source_endpoint_port"); ok {
		configureSubscriptionReq.QueryParams["SourceEndpointPort"] = v.(string)
	}
	if v, ok := d.GetOk("source_endpoint_region"); ok {
		configureSubscriptionReq.QueryParams["SourceEndpointRegion"] = v.(string)
	}
	if v, ok := d.GetOk("source_endpoint_role"); ok {
		configureSubscriptionReq.QueryParams["SourceEndpointRole"] = v.(string)
	}
	if v, ok := d.GetOk("source_endpoint_user_name"); ok {
		configureSubscriptionReq.QueryParams["SourceEndpointUserName"] = v.(string)
	}
	if v, ok := d.GetOkExists("subscription_data_type_ddl"); ok {
		configureSubscriptionReq.QueryParams["SubscriptionDataTypeDDL"] = v.(string)
	}
	if v, ok := d.GetOkExists("subscription_data_type_dml"); ok {
		configureSubscriptionReq.QueryParams["SubscriptionDataTypeDML"] = v.(string)
	}
	if v, ok := d.GetOk("subscription_instance_vpc_id"); ok {
		configureSubscriptionReq.QueryParams["SubscriptionInstanceVPCId"] = v.(string)
	}
	if v, ok := d.GetOk("subscription_instance_vswitch_id"); ok {
		configureSubscriptionReq.QueryParams["SubscriptionInstanceVSwitchId"] = v.(string)
	}
	if v, ok := d.GetOkExists("delay_notice"); ok {
		configureSubscriptionReq.QueryParams["DelayNotice"] = fmt.Sprintf("%t", v.(bool))
	}
	if v, ok := d.GetOk("delay_phone"); ok {
		configureSubscriptionReq.QueryParams["DelayPhone"] = v.(string)
	}
	if v, ok := d.GetOk("delay_rule_time"); ok {
		configureSubscriptionReq.QueryParams["DelayRuleTime"] = v.(string)
	}
	if v, ok := d.GetOkExists("error_notice"); ok {
		configureSubscriptionReq.QueryParams["ErrorNotice"] = fmt.Sprintf("%t", v.(bool))
	}
	if v, ok := d.GetOk("error_phone"); ok {
		configureSubscriptionReq.QueryParams["ErrorPhone"] = v.(string)
	}
	if v, ok := d.GetOk("reserve"); ok {
		configureSubscriptionReq.QueryParams["Reserve"] = v.(string)
	}
	configureSubscriptionRsp := make(map[string]interface{})
	err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
		raw, err := dtsClient.ProcessCommonRequest(configureSubscriptionReq)
		addDebug(action, raw, configureSubscriptionReq, configureSubscriptionReq.QueryParams)
		if err != nil {
			if errmsgs.NeedRetry(err) {
				wait()
				return resource.RetryableError(err)
			}
			return resource.NonRetryableError(err)
		}
		if err != nil {
			errmsg := ""
			if raw != nil {
				errmsg = errmsgs.GetBaseResponseErrorMessage(raw.BaseResponse)
			}
			return resource.NonRetryableError(errmsgs.WrapErrorf(err, errmsgs.RequestV1ErrorMsg, "alibabacloudstack_dts_subscription_job", action, errmsgs.AlibabacloudStackSdkGoERROR, errmsg))
		}
		err = json.Unmarshal(raw.GetHttpContentBytes(), &configureSubscriptionRsp)
		if err != nil {
			return resource.NonRetryableError(err)
		}
		return nil
	})
	if err != nil {
		return err
	}
	if fmt.Sprint(configureSubscriptionRsp["Success"]) == "false" {
		return errmsgs.WrapError(fmt.Errorf("%s failed, response: %v", "ConfigureSubscription", configureSubscriptionRsp))
	}
	fmt.Printf("==================================  %s", fmt.Sprint(configureSubscriptionRsp["DtsJobId"]))
	d.SetId(fmt.Sprint(configureSubscriptionRsp["DtsJobId"]))
	return nil
}