日本熟妇hd丰满老熟妇,中文字幕一区二区三区在线不卡 ,亚洲成片在线观看,免费女同在线一区二区

通過SDK創建并使用ECS實例

阿里云ECS提供多種API和SDK,讓您可以通過編程創建和管理ECS實例,提高業務效率,實現系統自動化。本文介紹了如何利用V2.0 Java SDK創建ECS實例,借助云助手API執行腳本以及釋放資源等操作。

準備工作

  1. 由于阿里云賬號(主賬號)擁有資源的所有權限,其AccessKey一旦泄露風險巨大,所以建議您使用滿足最小化權限需求的RAM用戶的AccessKey。獲取方法請參見創建AccessKey

  2. 給RAM用戶授予操作云服務器ECS和專有網絡VPC相關資源的權限。本文提供的示例代碼需要創建實例、VPC、交換機等資源,建議授予以下權限:

    云產品

    授予權限

    專有網絡VPC

    本示例選擇系統策略:AliyunVPCFullAccess

    云服務器ECS

    本示例選擇系統策略:AliyunECSFullAccess

  3. 本文示例代碼會在系統環境變量中讀取AccessKey作為訪問云服務的憑證,具體操作步驟請參見在Linux、macOS和Windows系統配置環境變量

  4. 獲取ECS SDK和VPC SDK,本文通過添加Maven依賴的方式來安裝。更多安裝方式,請參見安裝ECS Java SDK安裝VPC Java SDK

    添加Maven依賴的示例如下:

    <dependencies>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>ecs20140526</artifactId>
            <version>5.3.0</version>
        </dependency>
        <dependency>
            <groupId>com.aliyun</groupId>
            <artifactId>vpc20160428</artifactId>
            <version>7.12.0</version>
        </dependency>
    </dependencies>

創建ECS實例

創建ECS實例時有很多必填參數,包括交換機ID、安全組、鏡像等。您可以傳入已經準備好的資源ID,或者調用以下OpenAPI創建對應資源。

  1. 創建VPC

    VPC是一種專有的云上私有網絡,允許用戶在公共云上配置和管理一個邏輯隔離的網絡區域。

    API

    參數

    示例取值

    CreateVpc

    RegionId

    地域:cn-hangzhou

    CidrBlock

    VPC網段:192.168.0.0/16

  2. 查詢VPC信息

    在調用CreateVpc之后,VPC需要一段配置時間,您可以調用該OpenAPI查詢VPC的狀態。當VPC的狀態處于Available(可用)時,請再調用后續OpenAPI。

    API

    參數

    示例取值

    DescribeVpcs

    RegionId

    地域:cn-hangzhou

    VpcId

    VPC的ID:vpc-bp1aag0sb9s4i92i3****

  3. 創建交換機

    交換機是一種在虛擬化環境中使用的網絡交換設備,它模擬了物理交換機的功能,使虛擬機(VMs)之間以及虛擬機與物理網絡之間可以進行通信。

    API

    參數

    示例取值

    RegionId

    地域:cn-hangzhou

    CreateVSwitch

    ZoneId

    可用區:cn-hangzhou-i

    VpcId

    VPC ID:vpc-bp1aag0sb9s4i92i3****

    CidrBlock

    交換機網段:192.168.0.0/24

  4. 創建安全組

    安全組是一種虛擬防火墻,能夠控制ECS實例的出入方向流量。

    API

    參數

    示例取值

    CreateSecurityGroup

    RegionId

    地域:cn-hangzhou

    VpcId

    VPC ID:vpc-bp1aag0sb9s4i92i3****

  5. 給安全組添加防護規則

    API

    參數

    示例取值

    AuthorizeSecurityGroup

    RegionId

    地域:cn-hangzhou

    SecurityGroupId

    安全組ID:sg-bp1esyhwfbqeyudt****

    IpProtocol

    協議:tcp

    SourceCidrIp

    源CIDR:0.0.0.0/0

    PortRange

    端口范圍:

    • Linux實例:22/22

    • Windows實例:3389/3389

  6. 創建SSH密鑰對

    阿里云SSH密鑰對是一種安全便捷的登錄認證方式,用于在SSH協議中進行身份驗證和加密通信。通過SSH密鑰對,您可以實現免密碼遠程登錄。

    API

    參數

    示例取值

    CreateKeyPair

    RegionId

    地域:cn-hangzhou

    KeyPairName

    密鑰對名稱:sdk-key-pair

  7. 創建ECS實例

    使用ECS您可以快速部署和運行應用程序,靈活調整資源以應對業務變化,同時享受高性能、高安全性和低成本的計算能力,適用于網站托管、應用開發、數據處理等多種場景。

    API

    參數

    示例取值

    RunInstances

    RegionId

    地域:cn-hangzhou

    ImageId

    鏡像:推薦使用Alibaba Cloud Linux鏡像aliyun_3_x64_20G_scc_alibase_20220225.vhd

    InstanceType

    實例規格: ecs.e-c1m2.xlarge

    SecurityGroupId

    安全組ID:sg-bp1esyhwfbqeyudt****

    VSwitchId

    交換機ID:vsw-bp1nzprm8h7mmnl8t****

    InstanceName

    實例名稱:sdk-test

    InstanceChargeType

    付費方式:實例按照按量付費的方式PostPaid

    說明

    您需要確保賬號余額能夠完成支付。

    KeyPairName

    密鑰對名稱:sdk-key-pair

    SystemDisk.Category

    系統盤的云盤種類:cloud_essd

  8. 查詢ECS實例狀態

    在調用RunInstances后,ECS實例需要一定的啟動時間。僅當ECS實例狀態達到Running時,才能通過遠程連接等方式登錄到實例,以進行各種操作和應用程序的部署。您可以調用該OpenAPI查詢ECS實例的狀態。

    API

    參數

    示例取值

    DescribeInstanceStatus

    RegionId

    地域:cn-hangzhou

    InstanceId

    實例ID集合:["i-bp17f3kzgtzzj91r****"]

完整的示例代碼如下:

import com.aliyun.ecs20140526.models.*;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.vpc20160428.models.CreateVSwitchRequest;
import com.aliyun.vpc20160428.models.CreateVSwitchResponse;
import com.aliyun.vpc20160428.models.CreateVpcRequest;
import com.aliyun.vpc20160428.models.CreateVpcResponse;
import com.aliyun.vpc20160428.models.DescribeVpcsRequest;

import java.util.ArrayList;
import java.util.List;

public class EcsDemo {
    public static void main(String[] args) {
        String vpcId = null;
        String vSwitchId = null;
        String securityGroupId = null;
        String instanceId = null;
        com.aliyun.vpc20160428.Client vpcClient = null;
        com.aliyun.ecs20140526.Client ecsClient = null;
        try {
            vpcClient = createVpcClient();
            ecsClient = createEcsClient();
            // 創建VPC
            vpcId = createVpc(vpcClient);
            System.out.println("VPC create success, vpcId :" + vpcId);
            // 當VPC的狀態為Available時,繼續后續步驟
            while (true) {
                String status = describeVpc(vpcId, vpcClient);
                if ("Available".equals(status)) {
                    break;
                }
            }
            // 創建交換機
            vSwitchId = CreateVSwitch(vpcId, vpcClient);
            System.out.println("VSwitch create success, vSwitchId :" + vSwitchId);
            // 創建安全組
            securityGroupId = createSecurityGroup(vpcId, ecsClient);
            System.out.println("SecurityGroup create success, securityGroupId :" + securityGroupId);
            // 添加安全組入方向規則
            authorizeSecurityGroup(securityGroupId, ecsClient);
            // 創建密鑰對。注意:保存密鑰對的信息,用于登錄ECS
            CreateKeyPairResponse keyPair = createKeyPair(ecsClient);
            System.out.println("KeyPair create success, keyPairName :" + keyPair.body.keyPairName);
            // 創建實例,當ECS實例狀態為Running時,表示ECS已經處于運行中
            instanceId = createInstance(ecsClient, vSwitchId, securityGroupId);
            System.out.println("ECS create success, instanceId :" + instanceId);
            while (true) {
                List<String> instanceIds = new ArrayList<>();
                instanceIds.add(instanceId);
                DescribeInstanceStatusResponse describeInstanceStatus = describeInstanceStatus(instanceIds, ecsClient);
                List<DescribeInstanceStatusResponseBody.DescribeInstanceStatusResponseBodyInstanceStatusesInstanceStatus> instanceStatusList = describeInstanceStatus.body.instanceStatuses.instanceStatus;
                if (instanceStatusList != null && !instanceStatusList.isEmpty()) {
                    String status = instanceStatusList.get(0).status;
                    if ("Running".equals(status)) {
                        break;
                    }
                }
            }
        } catch (Exception e) {
            // 當面對部分資源創建成功而其他資源創建失敗的情況時,需要增加合理的處理機制,例如記錄日志、回滾機制、補償機制等。
            System.out.println(e.getMessage());
        }
    }

    public static class Constants {
        // 名稱
        public static final String NAME = "sdk-test";
        // 地域
        public static final String REGION_ID = "cn-hangzhou";
        // 可用區
        public static final String ZONE_ID = "cn-hangzhou-i";
        // 創建的VPC網絡和交換機網段
        public static final String CIDR_BLOCK_VPC = "192.168.0.0/16";
        public static final String CIDR_BLOCK_VSWITCH = "192.168.0.0/24";
        // 鏡像
        public static final String IMAGE_ID = "aliyun_3_x64_20G_scc_alibase_20220225.vhd";
        // 實例規格
        public static final String INSTANCE_TYPE = "ecs.e-c1m2.xlarge";
        // 磁盤類型
        public static final String SYSTEM_DISK_CATEGORY = "cloud_essd";

        // 密鑰對名稱
        public static final String KEY_PAIR_NAME = "sdk-key-pair";

        // 服務接入點
        public static final class ENDPOINT {
            public static final String VPC = "vpc.cn-hangzhou.aliyuncs.com";
            public static final String ECS = "ecs.cn-hangzhou.aliyuncs.com";
        }

        // 付費類型
        public static final class INSTANCE_CHARGE_TYPE {
            // 包年包月
            public static final String PRE_PAID = "PrePaid";
            // 按量付費
            public static final String POST_PAID = "PostPaid";
        }
    }

    public static com.aliyun.vpc20160428.Client createVpcClient() throws Exception {
        Config config = new Config();
        config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
        config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        config.endpoint = Constants.ENDPOINT.VPC;
        return new com.aliyun.vpc20160428.Client(config);
    }

    public static com.aliyun.ecs20140526.Client createEcsClient() throws Exception {
        Config config = new Config();
        config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
        config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        config.endpoint = Constants.ENDPOINT.ECS;
        return new com.aliyun.ecs20140526.Client(config);
    }

    /**
     * 創建VPC(虛擬私有云)網絡
     * 該方法通過調用阿里云VPC服務的API來創建一個VPC網絡,并返回新創建VPC的ID
     *
     * @param vpcClient VPC客戶端
     * @return 返回新創建的VPC的ID
     */
    public static String createVpc(com.aliyun.vpc20160428.Client vpcClient) {
        try {
            CreateVpcRequest createVpcRequest = new CreateVpcRequest()
                    .setRegionId(Constants.REGION_ID)
                    .setCidrBlock(Constants.CIDR_BLOCK_VPC)
                    .setVpcName(Constants.NAME);
            CreateVpcResponse vpc = vpcClient.createVpc(createVpcRequest);
            return vpc.body.vpcId;
        } catch (Exception e) {
            throw new RuntimeException("createVpc failed: " + e.getMessage());
        }
    }

    /**
     * 創建VSwitch(虛擬交換機).
     * 該方法負責通過阿里云VPC客戶端創建一個新的VSwitch. 它配置了必要的請求參數,如區域ID、可用區ID、
     * CIDR段、VPC ID和VSwitch名稱,然后發送請求并返回新創建VSwitch的ID.
     *
     * @param vpcId     VPC的ID,標識要在其中創建VSwitch的VPC
     * @param vpcClient VPC客戶端
     * @return 返回新創建的VSwitch的ID
     */
    public static String CreateVSwitch(String vpcId, com.aliyun.vpc20160428.Client vpcClient) {
        try {
            CreateVSwitchRequest createVSwitchRequest = new CreateVSwitchRequest()
                    .setRegionId(Constants.REGION_ID)
                    .setZoneId(Constants.ZONE_ID)
                    .setCidrBlock(Constants.CIDR_BLOCK_VSWITCH)
                    .setVpcId(vpcId)
                    .setVSwitchName(Constants.NAME);
            CreateVSwitchResponse vSwitch = vpcClient.createVSwitch(createVSwitchRequest);
            return vSwitch.body.vSwitchId;
        } catch (Exception e) {
            throw new RuntimeException("CreateVSwitch failed: " + e.getMessage());
        }
    }

    /**
     * 創建安全組
     *
     * @param vpcId     專有網絡的ID
     * @param ecsClient ECS客戶端
     * @return 創建的安全組的ID
     */
    public static String createSecurityGroup(String vpcId, com.aliyun.ecs20140526.Client ecsClient) {
        try {
            CreateSecurityGroupRequest createSecurityGroupRequest = new CreateSecurityGroupRequest()
                    .setRegionId(Constants.REGION_ID)
                    .setVpcId(vpcId)
                    .setSecurityGroupName(Constants.NAME);
            CreateSecurityGroupResponse securityGroup = ecsClient.createSecurityGroup(createSecurityGroupRequest);
            return securityGroup.body.securityGroupId;
        } catch (Exception e) {
            throw new RuntimeException("createSecurityGroup failed: " + e.getMessage());
        }
    }

    /**
     * 授權安全組規則,以允許特定的入站流量
     * 例如:允許TCP協議的22端口(SSH)從任何IP地址(0.0.0.0/0)訪問。
     *
     * @param securityGroupId 安全組的ID,用于標識哪個安全組的規則需要被授權。
     * @param ecsClient       ECS客戶端
     */
    public static void authorizeSecurityGroup(String securityGroupId, com.aliyun.ecs20140526.Client ecsClient) {
        try {
            AuthorizeSecurityGroupRequest.AuthorizeSecurityGroupRequestPermissions permission = new AuthorizeSecurityGroupRequest.AuthorizeSecurityGroupRequestPermissions()
                    .setIpProtocol("tcp")
                    .setPortRange("22/22")
                    .setSourceCidrIp("0.0.0.0/0");
            List<AuthorizeSecurityGroupRequest.AuthorizeSecurityGroupRequestPermissions> permissions = new ArrayList<>();
            permissions.add(permission);
            AuthorizeSecurityGroupRequest authorizeSecurityGroupRequest = new AuthorizeSecurityGroupRequest()
                    .setRegionId(Constants.REGION_ID)
                    .setSecurityGroupId(securityGroupId)
                    .setPermissions(permissions);

            ecsClient.authorizeSecurityGroup(authorizeSecurityGroupRequest);
        } catch (Exception e) {
            throw new RuntimeException("authorizeSecurityGroup failed: " + e.getMessage());
        }
    }

    /**
     * 創建ECS實例
     *
     * @param ecsClient       ECS客戶端
     * @param vSwitchId       交換機ID
     * @param securityGroupId 安全組ID
     * @return 新創建的ECS實例的ID
     */
    public static String createInstance(com.aliyun.ecs20140526.Client ecsClient, String vSwitchId, String securityGroupId) {
        try {
            RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk = new RunInstancesRequest.RunInstancesRequestSystemDisk()
                    .setCategory(Constants.SYSTEM_DISK_CATEGORY);
            RunInstancesRequest runInstancesRequest = new RunInstancesRequest()
                    .setRegionId(Constants.REGION_ID)
                    .setImageId(Constants.IMAGE_ID)
                    .setInstanceType(Constants.INSTANCE_TYPE)
                    .setSecurityGroupId(securityGroupId)
                    .setVSwitchId(vSwitchId)
                    .setInstanceChargeType(Constants.INSTANCE_CHARGE_TYPE.POST_PAID)
                    .setInstanceName(Constants.NAME)
                    .setInternetMaxBandwidthOut(1)
                    .setSystemDisk(systemDisk)
                    .setKeyPairName(Constants.KEY_PAIR_NAME);
            RunInstancesResponse resp = ecsClient.runInstances(runInstancesRequest);
            return resp.body.getInstanceIdSets().getInstanceIdSet().get(0);
        } catch (Exception e) {
            throw new RuntimeException("createInstance failed: " + e.getMessage());
        }
    }

    /**
     * 使用指定的安全組ID創建密鑰對
     *
     * @param ecsClient ECS客戶端
     * @return 返回創建密鑰對的響應對象
     */
    public static CreateKeyPairResponse createKeyPair(com.aliyun.ecs20140526.Client ecsClient) {
        try {
            // 創建一個創建密鑰對的請求對象,并設置區域ID、密鑰對名稱和資源組ID
            CreateKeyPairRequest createKeyPairRequest = new CreateKeyPairRequest()
                    .setRegionId(Constants.REGION_ID)
                    .setKeyPairName(Constants.KEY_PAIR_NAME);
            // 通過ECS客戶端發送創建密鑰對的請求,并返回響應對象
            return ecsClient.createKeyPair(createKeyPairRequest);
        } catch (Exception e) {
            // 如果創建密鑰對失敗,則拋出運行時異常,包含錯誤信息
            throw new RuntimeException("createKeyPair failed: " + e.getMessage());
        }
    }

    /**
     * 查詢VPC狀態
     *
     * @param vpcId     VPC的ID
     * @param vpcClient VPC客戶端
     * @return 返回VPC的狀態信息
     */
    public static String describeVpc(String vpcId, com.aliyun.vpc20160428.Client vpcClient) {
        try {
            DescribeVpcsRequest request = new DescribeVpcsRequest()
                    .setRegionId(Constants.REGION_ID)
                    .setVpcId(vpcId);
            return vpcClient.describeVpcs(request).body.vpcs.getVpc().get(0).status;
        } catch (Exception e) {
            // 如果發生異常,拋出自定義異常
            throw new RuntimeException("describeVpc failed: " + e.getMessage());
        }
    }

    /**
     * 根據實例ID列表查詢ECS實例信息
     *
     * @param instanceIds 實例ID列表
     * @param ecsClient   ECS客戶端
     * @return 返回查詢到的實例狀態信息響應對象
     */
    public static DescribeInstanceStatusResponse describeInstanceStatus(List<String> instanceIds, com.aliyun.ecs20140526.Client ecsClient) {
        try {
            DescribeInstanceStatusRequest request = new DescribeInstanceStatusRequest()
                    .setRegionId(Constants.REGION_ID)
                    .setInstanceId(instanceIds);
            return ecsClient.describeInstanceStatus(request);
        } catch (Exception e) {
            throw new RuntimeException("describeInstanceStatus failed: " + e.getMessage());
        }
    }
}

免登錄運維

您可以通過云服務器ECS提供的原生自動化運維工具云助手,免密碼、免登錄,且無需使用跳板機,即可批量執行命令(Shell、PowerShell、Bat等),從而實現自動化運維腳本的執行、進程的輪詢、軟件的安裝與卸載、服務的啟動或停止,以及補丁或安全更新的安裝等任務。更多詳細信息,請參見云助手概述

例如在ECS中安裝Java和Tomcat,您可以調用RunCommand接口執行命令:

API

參數

示例取值

RunCommand

RegionId

地域:cn-hangzhou

Type

運維命令的語言類型:RunShellScript

CommandContent

命令內容:

#!/bin/bash
if cat /etc/issue|grep -i Ubuntu ; then
	sudo apt-get update
	sudo apt-get install -y default-jdk
	sudo apt-get install -y tomcat9
else
        yum install -y java
	yum install -y tomcat
fi

Timeout

可選,執行命令的超時時間,單位:秒。 示例:60

InstanceId

ECS實例ID集合:["i-bp17f3kzgtzzj91r****"]

完整的示例代碼如下

import com.aliyun.ecs20140526.Client;
import com.aliyun.ecs20140526.models.RunCommandRequest;
import com.aliyun.ecs20140526.models.RunCommandResponse;
import com.aliyun.teaopenapi.models.Config;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;

public class CloudAssistant {
    public static void main(String[] args) throws Exception {
        Client ecsClient = createEcsClient();
        List<String> instanceIds = new ArrayList<>();
        instanceIds.add("i-bp17f3kzgtzzj91r****");
        String commandContent = "#!/bin/bash\n" +
                "if cat /etc/issue|grep -i Ubuntu ; then\n" +
                "\tsudo apt-get update\n" +
                "\tsudo apt-get install -y default-jdk\n" +
                "\tsudo apt-get install -y tomcat9\n" +
                "else\n" +
                "  yum install -y java\n" +
                "\tyum install -y tomcat\n" +
                "fi";
        System.out.println("commandContent:" + commandContent);
        runCommand(commandContent, instanceIds, ecsClient);
    }

    public static class Constants {
        // 地域
        public static final String REGION_ID = "cn-hangzhou";

        // 服務接入點
        public static final class ENDPOINT {
            public static final String VPC = "vpc.cn-hangzhou.aliyuncs.com";
            public static final String ECS = "ecs.cn-hangzhou.aliyuncs.com";
        }

        // 命令類型
        public static final class COMMAND_TYPE {
            // 適用于Linux實例的Shell命令。
            public static final String RUN_SHELL_SCRIPT = "RunShellScript";
            // 適用于Windows實例的Bat命令。
            public static final String RUN_BAT_SCRIPT = "RunBatScript";
            // 適用于Windows實例的PowerShell命令。
            public static final String RUN_POWERSHELL_SCRIPT = "RunPowerShellScript";
        }
    }

    public static com.aliyun.ecs20140526.Client createEcsClient() throws Exception {
        Config config = new Config();
        config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
        config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        config.endpoint = Constants.ENDPOINT.ECS;
        return new com.aliyun.ecs20140526.Client(config);
    }

    public static void runCommand(String commandContent, List<String> instanceIds, com.aliyun.ecs20140526.Client ecsClient) {
        try {
            RunCommandRequest request = new RunCommandRequest();
            request.setRegionId(Constants.REGION_ID);
            // RunShellScript適用于Linux實例的Shell命令。
            request.setType(Constants.COMMAND_TYPE.RUN_SHELL_SCRIPT);
            request.setCommandContent(commandContent);
            request.setInstanceId(instanceIds);
            request.setTimeout(60L);
            RunCommandResponse runCommandResponse = ecsClient.runCommand(request);
            System.out.println(new Gson().toJson(runCommandResponse));
        } catch (Exception e) {
            throw new RuntimeException("runCommand failed:" + e);
        }
    }
}

釋放資源

當您不再需要所創建的資源時,可以調用以下OpenAPI接口以釋放該資源。

說明

根據您的實際需求,選擇相應的OpenAPI釋放資源 。本示例釋放上述步驟創建的所有資源。

  1. 釋放ECS實例

    API

    參數

    示例取值

    DeleteInstances

    RegionId

    地域:cn-hangzhou

    InstanceId

    實例 ID:i-bp17f3kzgtzzj91r****

  2. 刪除SSH密鑰對

    API

    參數

    示例取值

    DeleteKeyPairs

    RegionId

    地域:cn-hangzhou

    KeyPairNames

    SSH密鑰對名稱:["sdk-key-pair"]

  3. 刪除安全組

    API

    參數

    示例取值

    DeleteSecurityGroup

    RegionId

    地域:cn-hangzhou

    SecurityGroupId

    安全組ID:sg-bp1esyhwfbqeyudt****

  4. 刪除交換機

    API

    參數

    示例取值

    DeleteVSwitch

    RegionId

    地域:cn-hangzhou

    VSwitchId

    交換機ID:vsw-bp1nzprm8h7mmnl8t****

  5. 刪除VPC

    API

    參數

    示例取值

    DeleteVpc

    RegionId

    地域:cn-hangzhou

    VpcId

    VPC ID:vpc-bp1aag0sb9s4i92i3****

示例代碼如下:

import com.aliyun.ecs20140526.Client;
import com.aliyun.ecs20140526.models.*;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.vpc20160428.models.DeleteVSwitchRequest;
import com.aliyun.vpc20160428.models.DeleteVpcRequest;
import com.google.gson.Gson;

import java.util.Collections;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class DeleteResources {
    public static void main(String[] args) {
        String vpcId = "vpc-bp1aag0sb9s4i92i3****";
        String vSwitchId = "vsw-bp1nzprm8h7mmnl8t****";
        String securityGroupId = "sg-bp1esyhwfbqeyudt****";
        String instanceId = "i-bp17f3kzgtzzj91r****";
        String keyPairName = "sdk-key-pair";
        ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
        try {
            com.aliyun.ecs20140526.Client ecsClient = createEcsClient();
            com.aliyun.vpc20160428.Client vpcClient = createVpcClient();
            // 刪除ECS實例
            executorService.schedule(() -> deleteInstance(instanceId, ecsClient), 1, TimeUnit.SECONDS);
            // 刪除密鑰對
            executorService.schedule(() -> deleteKeyPairs(keyPairName, ecsClient), 1, TimeUnit.SECONDS);
            // 刪除安全組
            executorService.schedule(() -> deleteSecurityGroup(securityGroupId, ecsClient), 60, TimeUnit.SECONDS);
            // 刪除交換機
            executorService.schedule(() -> deleteVSwitch(vSwitchId, vpcClient), 60, TimeUnit.SECONDS);
            // 刪除VPC
            executorService.schedule(() -> deleteVpc(vpcId, vpcClient), 65, TimeUnit.SECONDS);
        } catch (Exception e) {
            System.err.println("An error occurred: " + e.getMessage());
            // 異常處理,因為釋放實例需要一定時間,可能會存在部分刪除成功,部分刪除失敗的情況,請增加合理的刪除失敗數據處理機制。
        } finally {
            executorService.shutdown();
        }
    }

    public static class Constants {
        // 服務接入點
        public static final class ENDPOINT {
            public static final String VPC = "vpc.cn-hangzhou.aliyuncs.com";
            public static final String ECS = "ecs.cn-hangzhou.aliyuncs.com";
        }

        // 地域
        public static final String REGION_ID = "cn-hangzhou";
    }

    public static com.aliyun.vpc20160428.Client createVpcClient() throws Exception {
        Config config = new Config();
        config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
        config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        config.endpoint = Constants.ENDPOINT.VPC;
        return new com.aliyun.vpc20160428.Client(config);
    }

    public static com.aliyun.ecs20140526.Client createEcsClient() throws Exception {
        Config config = new Config();
        config.setAccessKeyId(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"));
        config.setAccessKeySecret(System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        config.endpoint = Constants.ENDPOINT.ECS;
        return new com.aliyun.ecs20140526.Client(config);
    }

    /**
     * 刪除指定的VPC(虛擬私有云)資源
     *
     * @param vpcId     VPC的ID,標識了要刪除的VPC資源
     * @param vpcClient VPC客戶端
     */
    public static void deleteVpc(String vpcId, com.aliyun.vpc20160428.Client vpcClient) {
        try {
            System.out.println("VPC is deleting, please wait...");
            DeleteVpcRequest deleteVpcRequest = new DeleteVpcRequest()
                    .setRegionId(Constants.REGION_ID)
                    .setVpcId(vpcId);
            vpcClient.deleteVpc(deleteVpcRequest);
        } catch (Exception e) {
            throw new RuntimeException("DeleteVpc failed: " + e.getMessage());
        }
    }

    /**
     * 刪除指定的虛擬交換機(VSwitch)
     *
     * @param vSwitchId 虛擬交換機的ID
     * @param vpcClient VPC客戶端
     */
    public static void deleteVSwitch(String vSwitchId, com.aliyun.vpc20160428.Client vpcClient) {
        System.out.println("VSwitch is deleting, please wait...");
        try {
            DeleteVSwitchRequest deleteVSwitchRequest = new DeleteVSwitchRequest()
                    .setRegionId(Constants.REGION_ID)
                    .setVSwitchId(vSwitchId);
            vpcClient.deleteVSwitch(deleteVSwitchRequest);
        } catch (Exception e) {
            throw new RuntimeException("DeleteVSwitch failed: " + e.getMessage());
        }
    }

    /**
     * 刪除指定的安全組
     *
     * @param securityGroupId 安全組的ID,標識要刪除的安全組
     * @param ecsClient       ECS客戶端
     */
    public static void deleteSecurityGroup(String securityGroupId, Client ecsClient) {
        System.out.println("SecurityGroup is deleting, please wait...");
        try {
            DeleteSecurityGroupRequest deleteSecurityGroupRequest = new DeleteSecurityGroupRequest()
                    .setRegionId(Constants.REGION_ID)
                    .setSecurityGroupId(securityGroupId);
            ecsClient.deleteSecurityGroup(deleteSecurityGroupRequest);
        } catch (Exception e) {
            throw new RuntimeException("DeleteSecurityGroup failed: " + e.getMessage());
        }
    }

    /**
     * 刪除云服務器實例.
     *
     * @param instanceId 實例ID,標識了要刪除的云服務器實例
     * @param ecsClient  ECS客戶端
     */
    public static void deleteInstance(String instanceId, Client ecsClient) {
        System.out.println("ECS instance is deleting, please wait...");
        try {
            DeleteInstanceRequest request = new DeleteInstanceRequest()
                    .setForce(true)
                    .setInstanceId(instanceId);
            ecsClient.deleteInstance(request);
        } catch (Exception e) {
            throw new RuntimeException("DeleteInstance failed: " + e.getMessage());
        }
    }

    /**
     * 使用指定的ECS客戶端刪除密鑰對
     *
     * @param keyPairName 密鑰對名稱
     * @param ecsClient   ECS客戶端
     */
    public static void deleteKeyPairs(String keyPairName, Client ecsClient) {
        System.out.println("KeyPair is deleting, please wait...");
        try {
            // 創建刪除密鑰對請求,并設置區域ID和密鑰對名稱
            DeleteKeyPairsRequest deleteKeyPairsRequest = new DeleteKeyPairsRequest()
                    .setRegionId(Constants.REGION_ID)
                    .setKeyPairNames(new Gson().toJson(Collections.singletonList(keyPairName)));
            ecsClient.deleteKeyPairs(deleteKeyPairsRequest);
        } catch (Exception e) {
            // 如果刪除密鑰對失敗,拋出運行時異常,包含錯誤信息
            throw new RuntimeException("DeleteKeyPairs failed: " + e.getMessage());
        }
    }
}