本文介绍如何通过服务器迁移工具SMC Java SDK调用DeleteSourceServer删除一个迁移源。

背景信息

您可以调用DeleteSourceServer接口删除一个迁移源,详情请参见DeleteSourceServer

说明
  • 迁移源若关联了Running(运行中)状态的迁移任务,则无法删除。
  • 迁移源若关联了除Running状态外的其他状态的迁移任务,则需将参数Force设置为true来强制删除迁移源。

代码示例

以下代码适用于强制删除迁移源:

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.smc.model.v20190601.DeleteSourceServerRequest;
import com.aliyuncs.smc.model.v20190601.DeleteSourceServerResponse;
import com.google.gson.Gson;

public class DeleteSourceServer {
    public static void main(String[] args) {
        //初始化AcsClient实例,设置地域ID及您的AccessKey信息
        DefaultProfile profile = DefaultProfile.getProfile("<region-Id>", "<accessKeyId>", "<accessSecret>");
        IAcsClient client = new DefaultAcsClient(profile);

        //创建API请求,并设置参数
        DeleteSourceServerRequest request = new DeleteSourceServerRequest();
        //迁移源ID
        request.setSourceId("s-bp152mkfswviw7sd****");
        //是否强制删除迁移源
        request.setForce(true);

        //发送请求获取返回值或处理异常
        try {
            DeleteSourceServerResponse response = client.getAcsResponse(request);
            System.out.println(new Gson().toJson(response));
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
    }
}

执行结果

{    
    "RequestId": "0A01347D-887F-4816-85B3-2BB8A298AA27"
}