func resourceAlibabacloudStackDtsSubscriptionJobUpdate()

in alibabacloudstack/resource_apsarastack_dts_subscriptionjob.go [443:786]


func resourceAlibabacloudStackDtsSubscriptionJobUpdate(d *schema.ResourceData, meta interface{}) error {
	client := meta.(*connectivity.AlibabacloudStackClient)
	dtsService := DtsService{client}
	d.Partial(true)
	if d.HasChange("tags") {
		if err := dtsService.SetResourceTags(d, "ALIYUN::DTS::INSTANCE"); err != nil {
			return errmsgs.WrapError(err)
		}
		//d.SetPartial("tags")
	}
	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]
	if err != nil {
		return errmsgs.WrapError(err)
	}
	update := false
	request := client.NewCommonRequest("POST", "Dts", "2020-01-01", "", "")
	request.Headers["x-acs-caller-sdk-source"] = "Terraform" // 必填,调用来源说明
	request.Headers["x-acs-regionid"] = client.RegionId
	request.Headers["x-acs-content-type"] = "application/json"
	request.QueryParams["DtsJobId"] = d.Id()
	if d.HasChange("dts_job_name") {
		update = true
		if v, ok := d.GetOk("dts_job_name"); ok {
			request.QueryParams["DtsJobName"] = v.(string)
		}
	}
	request.Domain = client.Config.Endpoints[connectivity.DTSCode]
	if update {
		action := "ModifyDtsJobName"
		response := make(map[string]interface{})
		request.ApiName = action
		request.QueryParams["Action"] = action
		wait := incrementalWait(3*time.Second, 3*time.Second)
		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
		}
		if fmt.Sprint(response["Success"]) == "false" {
			return errmsgs.WrapError(fmt.Errorf("%s failed, response: %v", action, response))
		}
		//d.SetPartial("dts_job_name")
	}
	update = false
	modifyDtsJobPasswordReq := client.NewCommonRequest("POST", "Dts", "2020-01-01", "ModifyDtsJobPassword", "")
	modifyDtsJobPasswordReq.QueryParams["DtsJobId"] = d.Id()
	modifyDtsJobPasswordReq.Headers["x-acs-caller-sdk-source"] = "Terraform" // 必填,调用来源说明
	modifyDtsJobPasswordReq.Headers["x-acs-regionid"] = client.RegionId
	modifyDtsJobPasswordReq.Headers["x-acs-content-type"] = "application/json"
	modifyDtsJobPasswordReq.Headers["Content-type"] = "application/json"
	modifyDtsJobPasswordReq.QueryParams["Endpoint"] = "src"
	if !d.IsNewResource() && d.HasChange("source_endpoint_password") {
		update = true
		if v, ok := d.GetOk("source_endpoint_password"); ok {
			modifyDtsJobPasswordReq.QueryParams["Password"] = v.(string)
		}
		if v, ok := d.GetOk("source_endpoint_user_name"); ok {
			modifyDtsJobPasswordReq.QueryParams["UserName"] = v.(string)
		}
	}
	if update {
		action := "ModifyDtsJobPassword"
		response := make(map[string]interface{})
		modifyDtsJobPasswordReq.ApiName = action
		modifyDtsJobPasswordReq.QueryParams["Action"] = action
		wait := incrementalWait(3*time.Second, 3*time.Second)
		err = resource.Retry(d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
			raw, err := dtsClient.ProcessCommonRequest(modifyDtsJobPasswordReq)
			addDebug(action, raw, modifyDtsJobPasswordReq, modifyDtsJobPasswordReq.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
		}
		if fmt.Sprint(response["Success"]) == "false" {
			return errmsgs.WrapError(fmt.Errorf("%s failed, response: %v", action, response))
		}
		target := d.Get("status").(string)
		err = resourceAlibabacloudStackDtsSubscriptionJobStatusFlow(d, meta, target)
		if err != nil {
			return errmsgs.WrapError(errmsgs.Error(errmsgs.FailedToReachTargetStatus, d.Get("status")))
		}
	}

	if !d.IsNewResource() && d.HasChange("status") {
		target := d.Get("status").(string)
		err := resourceAlibabacloudStackDtsSubscriptionJobStatusFlow(d, meta, target)
		if err != nil {
			return errmsgs.WrapError(errmsgs.Error(errmsgs.FailedToReachTargetStatus, d.Get("status")))
		}
	}

	update = false
	configureSubscriptionReq := client.NewCommonRequest("POST", "Dts", "2020-01-01", "ConfigureSubscription", "")
	configureSubscriptionReq.QueryParams["DtsJobId"] = d.Id()
	configureSubscriptionReq.Headers["x-acs-caller-sdk-source"] = "Terraform" // 必填,调用来源说明
	configureSubscriptionReq.Headers["x-acs-regionid"] = client.RegionId
	configureSubscriptionReq.Headers["x-acs-content-type"] = "application/json"
	configureSubscriptionReq.Headers["Content-type"] = "application/json"
	if d.IsNewResource() {
		update = true
	}
	if d.HasChange("db_list") {
		update = true
	}
	if v, ok := d.GetOk("db_list"); ok {
		configureSubscriptionReq.QueryParams["DbList"] = v.(string)
	}
	if v, ok := d.GetOk("dts_job_name"); ok {
		configureSubscriptionReq.QueryParams["DtsJobName"] = v.(string)
	}
	if d.HasChange("subscription_instance_network_type") {
		update = true
	}
	if v, ok := d.GetOk("subscription_instance_network_type"); ok {
		configureSubscriptionReq.QueryParams["SubscriptionInstanceNetworkType"] = v.(string)
	}
	if d.HasChange("checkpoint") {
		update = true
	}
	if v, ok := d.GetOk("checkpoint"); ok {
		configureSubscriptionReq.QueryParams["Checkpoint"] = v.(string)
	}
	if d.HasChange("source_endpoint_database_name") {
		update = true
	}
	if v, ok := d.GetOk("source_endpoint_database_name"); ok {
		configureSubscriptionReq.QueryParams["SourceEndpointDatabaseName"] = v.(string)
	}
	if !d.IsNewResource() && d.HasChange("source_endpoint_engine_name") {
		update = true
	}
	if v, ok := d.GetOk("source_endpoint_engine_name"); ok {
		configureSubscriptionReq.QueryParams["SourceEndpointEngineName"] = v.(string)
	}
	if d.HasChange("source_endpoint_ip") {
		update = true
	}
	if v, ok := d.GetOk("source_endpoint_ip"); ok {
		configureSubscriptionReq.QueryParams["SourceEndpointIP"] = v.(string)
	}
	if d.HasChange("source_endpoint_instance_id") {
		update = true
	}
	if v, ok := d.GetOk("source_endpoint_instance_id"); ok {
		configureSubscriptionReq.QueryParams["SourceEndpointInstanceID"] = v.(string)
	}
	if d.HasChange("source_endpoint_instance_type") {
		update = true
	}
	if v, ok := d.GetOk("source_endpoint_instance_type"); ok {
		configureSubscriptionReq.QueryParams["SourceEndpointInstanceType"] = v.(string)
	}
	if d.HasChange("source_endpoint_oracle_sid") {
		update = true
	}
	if v, ok := d.GetOk("source_endpoint_oracle_sid"); ok {
		configureSubscriptionReq.QueryParams["SourceEndpointOracleSID"] = v.(string)
	}
	if d.HasChange("source_endpoint_owner_id") {
		update = true
	}
	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 d.HasChange("source_endpoint_port") {
		update = true
	}
	if v, ok := d.GetOk("source_endpoint_port"); ok {
		configureSubscriptionReq.QueryParams["SourceEndpointPort"] = v.(string)
	}
	if d.HasChange("source_endpoint_region") {
		update = true
	}
	if v, ok := d.GetOk("source_endpoint_region"); ok {
		configureSubscriptionReq.QueryParams["SourceEndpointRegion"] = v.(string)
	}
	if d.HasChange("source_endpoint_role") {
		update = true
	}
	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 d.HasChange("subscription_data_type_ddl") || d.IsNewResource() {
		update = true
	}
	if v, ok := d.GetOkExists("subscription_data_type_ddl"); ok {
		configureSubscriptionReq.QueryParams["SubscriptionDataTypeDDL"] = v.(string)
	}
	if d.HasChange("subscription_data_type_dml") || d.IsNewResource() {
		update = true
	}
	if v, ok := d.GetOkExists("subscription_data_type_dml"); ok {
		configureSubscriptionReq.QueryParams["SubscriptionDataTypeDML"] = v.(string)
	}
	if d.HasChange("subscription_instance_vpc_id") {
		update = true
	}
	if v, ok := d.GetOk("subscription_instance_vpc_id"); ok {
		configureSubscriptionReq.QueryParams["SubscriptionInstanceVPCId"] = v.(string)
	}
	if d.HasChange("subscription_instance_vswitch_id") {
		update = true
	}
	if v, ok := d.GetOk("subscription_instance_vswitch_id"); ok {
		configureSubscriptionReq.QueryParams["SubscriptionInstanceVSwitchId"] = v.(string)
	}
	if update {

		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)
		}
		action := "ConfigureSubscription"
		response := make(map[string]interface{})
		configureSubscriptionReq.ApiName = action
		configureSubscriptionReq.QueryParams["Action"] = action
		wait := incrementalWait(3*time.Second, 3*time.Second)
		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(), &response)
			if err != nil {
				return resource.NonRetryableError(err)
			}
			return nil
		})
		if err != nil {
			return err
		}
		if fmt.Sprint(response["Success"]) == "false" {
			return errmsgs.WrapError(fmt.Errorf("%s failed, response: %v", action, response))
		}
		//d.SetPartial("db_list")
		//d.SetPartial("dts_job_name")
		//d.SetPartial("subscription_instance_network_type")
		//d.SetPartial("checkpoint")
		//d.SetPartial("source_endpoint_database_name")
		//d.SetPartial("source_endpoint_engine_name")
		//d.SetPartial("source_endpoint_ip")
		//d.SetPartial("source_endpoint_instance_id")
		//d.SetPartial("source_endpoint_instance_type")
		//d.SetPartial("source_endpoint_oracle_sid")
		//d.SetPartial("source_endpoint_owner_id")
		//d.SetPartial("source_endpoint_password")
		//d.SetPartial("source_endpoint_port")
		//d.SetPartial("source_endpoint_region")
		//d.SetPartial("source_endpoint_role")
		//d.SetPartial("source_endpoint_user_name")
		//d.SetPartial("subscription_data_type_ddl")
		//d.SetPartial("subscription_data_type_dml")
		//d.SetPartial("subscription_instance_vpc_id")
		//d.SetPartial("subscription_instance_vswitch_id")

		target := d.Get("status").(string)
		err = resourceAlibabacloudStackDtsSubscriptionJobStatusFlow(d, meta, target)
		if err != nil {
			return errmsgs.WrapError(errmsgs.Error(errmsgs.FailedToReachTargetStatus, d.Get("status")))
		}
	}
	d.Partial(false)
	return nil
}