新增实例:创建、查询、释放
parent
8acc307211
commit
1750dbd6d4
|
@ -36,6 +36,7 @@
|
|||
</license>
|
||||
</licenses>
|
||||
<developers>
|
||||
|
||||
<developer>
|
||||
<id>aliyundeveloper</id>
|
||||
<name>Aliyun SDK</name>
|
||||
|
@ -58,6 +59,11 @@
|
|||
<url></url>
|
||||
</scm>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>aliyun-java-sdk-core</artifactId>
|
||||
<version>4.6.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>ecs20140526</artifactId>
|
||||
|
|
|
@ -20,9 +20,9 @@ public class ClientConfig {
|
|||
// 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。
|
||||
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||
.setAccessKeyId("LTAI5tANGefs2gi8nsu4AoSZ")
|
||||
.setAccessKeyId("LTAI5tANGefs2gi8nsu4Ao")
|
||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||
.setAccessKeySecret("Ut5RaJvvG7dP8hgK82qjdtvyUA6x8g");
|
||||
.setAccessKeySecret("Ut5RaJvvG7dP8hgK82qjdtvyUA6x");
|
||||
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||
config.endpoint = "ecs.cn-zhangjiakou.aliyuncs.com";
|
||||
return new com.aliyun.ecs20140526.Client(config);
|
||||
|
|
|
@ -0,0 +1,43 @@
|
|||
package com.aliyun.demo.delete;
|
||||
|
||||
import com.aliyun.demo.config.ClientConfig;
|
||||
import com.aliyun.tea.TeaException;
|
||||
|
||||
public class DeleteEsc {
|
||||
|
||||
/**
|
||||
* 使用AK&SK初始化账号Client
|
||||
|
||||
* @return Client
|
||||
* @throws Exception
|
||||
*/
|
||||
public static void main(String[] args_) throws Exception {
|
||||
java.util.List<String> args = java.util.Arrays.asList(args_);
|
||||
com.aliyun.ecs20140526.Client client = ClientConfig.createClient();
|
||||
com.aliyun.ecs20140526.models.DeleteInstancesRequest deleteInstancesRequest = new com.aliyun.ecs20140526.models.DeleteInstancesRequest()
|
||||
.setRegionId("cn-zhangjiakou")
|
||||
.setDryRun(false)
|
||||
.setForce(true)
|
||||
.setTerminateSubscription(false)
|
||||
.setInstanceId(java.util.Arrays.asList( "i-8vb4iyv1xa4qcn426ts7"));
|
||||
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||
try {
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
client.deleteInstancesWithOptions(deleteInstancesRequest, runtime);
|
||||
} catch (TeaException error) {
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
System.out.println(error);
|
||||
// 诊断地址
|
||||
System.out.println(error.getData().get("Recommend"));
|
||||
com.aliyun.teautil.Common.assertAsString(error);
|
||||
} catch (Exception _error) {
|
||||
TeaException error = new TeaException(_error.getMessage(), _error);
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
System.out.println(error);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -24,9 +24,11 @@ public class SelectServerInfo {
|
|||
com.aliyun.ecs20140526.Client client = ClientConfig.createClient();
|
||||
com.aliyun.ecs20140526.models.DescribeInstancesRequest describeInstancesRequest = new com.aliyun.ecs20140526.models.DescribeInstancesRequest()
|
||||
.setRegionId("cn-zhangjiakou")
|
||||
.setInstanceName("huangdaju-hdj")
|
||||
.setInstanceName("Test-huangdaju")
|
||||
.setPageSize(10);
|
||||
|
||||
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||
|
||||
try {
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
DescribeInstancesResponse describeInstancesResponse = client.describeInstancesWithOptions(describeInstancesRequest, runtime);
|
||||
|
|
|
@ -1,80 +0,0 @@
|
|||
// This file is auto-generated, don't edit it. Thanks.
|
||||
package com.aliyun.sample;
|
||||
|
||||
import com.aliyun.tea.*;
|
||||
|
||||
public class Sample {
|
||||
|
||||
/**
|
||||
* 使用AK&SK初始化账号Client
|
||||
* @return Client
|
||||
* @throws Exception
|
||||
*/
|
||||
public static com.aliyun.ecs20140526.Client createClient() throws Exception {
|
||||
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||||
// 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。
|
||||
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||
.setAccessKeyId(System.getenv("LTAI5tANGefs2gi8nsu4AoSZ"))
|
||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||
.setAccessKeySecret(System.getenv("Ut5RaJvvG7dP8hgK82qjdtvyUA6x8g"));
|
||||
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||
config.endpoint = "ecs.cn-zhangjiakou.aliyuncs.com";
|
||||
return new com.aliyun.ecs20140526.Client(config);
|
||||
}
|
||||
|
||||
public static void main(String[] args_) throws Exception {
|
||||
java.util.List<String> args = java.util.Arrays.asList(args_);
|
||||
com.aliyun.ecs20140526.Client client = Sample.createClient();
|
||||
com.aliyun.ecs20140526.models.RunInstancesRequest.RunInstancesRequestCpuOptions cpuOptions = new com.aliyun.ecs20140526.models.RunInstancesRequest.RunInstancesRequestCpuOptions()
|
||||
.setCore(2)
|
||||
.setThreadsPerCore(2);
|
||||
com.aliyun.ecs20140526.models.RunInstancesRequest.RunInstancesRequestSystemDisk systemDisk = new com.aliyun.ecs20140526.models.RunInstancesRequest.RunInstancesRequestSystemDisk()
|
||||
.setSize("20")
|
||||
.setCategory("cloud_efficiency")
|
||||
.setPerformanceLevel("PL0");
|
||||
com.aliyun.ecs20140526.models.RunInstancesRequest runInstancesRequest = new com.aliyun.ecs20140526.models.RunInstancesRequest()
|
||||
.setRegionId("cn-zhangjiakou")
|
||||
.setImageId("m-8vb8z0ygyrzgqt54k3wi")
|
||||
.setInstanceType("ecs.e-c1m1.large")
|
||||
.setInternetMaxBandwidthIn(5)
|
||||
.setSystemDisk(systemDisk)
|
||||
.setInternetChargeType("PayByTraffic")
|
||||
.setZoneId("cn-zhangjiakou-c")
|
||||
.setPassword("Huangdaju520.")
|
||||
.setPeriodUnit("Week")
|
||||
.setPeriod(1)
|
||||
.setDryRun(false)
|
||||
.setSecurityEnhancementStrategy("Deactive")
|
||||
.setAmount(1)
|
||||
.setVSwitchId("vsw-8vbn6cq2uy0mmw69l6ryq")
|
||||
.setInstanceName("huandgaju")
|
||||
.setSecurityGroupId("sg-8vbfx0e48cekrpzgsa72")
|
||||
.setPasswordInherit(false)
|
||||
.setUniqueSuffix(false)
|
||||
.setAutoRenew(false)
|
||||
.setInstanceChargeType("PostPaid")
|
||||
.setDeletionProtection(false)
|
||||
.setCpuOptions(cpuOptions);
|
||||
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||
try {
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
client.runInstancesWithOptions(runInstancesRequest, runtime);
|
||||
} catch (TeaException error) {
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
System.out.println(error.getMessage());
|
||||
// 诊断地址
|
||||
System.out.println(error.getData().get("Recommend"));
|
||||
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||
} catch (Exception _error) {
|
||||
TeaException error = new TeaException(_error.getMessage(), _error);
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
System.out.println(error.getMessage());
|
||||
// 诊断地址
|
||||
System.out.println(error.getData().get("Recommend"));
|
||||
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,53 +0,0 @@
|
|||
// This file is auto-generated, don't edit it. Thanks.
|
||||
package com.aliyun.sample;
|
||||
|
||||
import com.aliyun.tea.*;
|
||||
|
||||
public class Sample1 {
|
||||
|
||||
/**
|
||||
* 使用AK&SK初始化账号Client
|
||||
|
||||
* @return Client
|
||||
* @throws Exception
|
||||
*/
|
||||
public static com.aliyun.ecs20140526.Client createClient() throws Exception {
|
||||
// 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
||||
// 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。
|
||||
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
||||
.setAccessKeyId("LTAI5tANGefs2gi8nsu4AoSZ")
|
||||
// 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
||||
.setAccessKeySecret("Ut5RaJvvG7dP8hgK82qjdtvyUA6x8g");
|
||||
// Endpoint 请参考 https://api.aliyun.com/product/Ecs
|
||||
config.endpoint = "cn-zhangjiakou";
|
||||
return new com.aliyun.ecs20140526.Client(config);
|
||||
}
|
||||
|
||||
public static void main(String[] args_) throws Exception {
|
||||
// java.util.List<String> args = java.util.Arrays.asList(args_);
|
||||
com.aliyun.ecs20140526.Client client = Sample1.createClient();
|
||||
com.aliyun.ecs20140526.models.DescribeUserDataRequest describeUserDataRequest = new com.aliyun.ecs20140526.models.DescribeUserDataRequest()
|
||||
.setInstanceId("i-8vbamj8u1q8xnmkze5m0");
|
||||
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
|
||||
try {
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
client.describeUserDataWithOptions(describeUserDataRequest, runtime);
|
||||
} catch (TeaException error) {
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
System.out.println(error.getMessage());
|
||||
// 诊断地址
|
||||
System.out.println(error.getData().get("Recommend"));
|
||||
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||
} catch (Exception _error) {
|
||||
TeaException error = new TeaException(_error.getMessage(), _error);
|
||||
// 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
||||
// 错误 message
|
||||
System.out.println(error.getMessage());
|
||||
// 诊断地址
|
||||
System.out.println(error.getData().get("Recommend"));
|
||||
com.aliyun.teautil.Common.assertAsString(error.message);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,88 +0,0 @@
|
|||
// This file is auto-generated, don't edit it. Thanks.
|
||||
package com.aliyun.sample;
|
||||
|
||||
import com.aliyun.darabonba.env.EnvClient;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstanceMonitorDataRequest;
|
||||
import com.aliyun.ecs20140526.models.DescribeInstanceMonitorDataResponse;
|
||||
import com.aliyun.ecs20140526.models.DescribeUserDataRequest;
|
||||
import com.aliyun.ecs20140526.models.DescribeUserDataResponse;
|
||||
import com.aliyun.tea.TeaException;
|
||||
import com.aliyun.tea.TeaModel;
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import com.aliyun.teautil.Common;
|
||||
|
||||
public class Sample2 {
|
||||
|
||||
/**
|
||||
* Initialization 初始化公共请求参数
|
||||
*/
|
||||
public static com.aliyun.ecs20140526.Client Initialization(String regionId) throws Exception {
|
||||
Config config = new Config();
|
||||
// 您的AccessKey ID
|
||||
config.accessKeyId = EnvClient.getEnv("LTAI5tANGefs2gi8nsu4AoSZ");
|
||||
// 您的AccessKey Secret
|
||||
config.accessKeySecret = EnvClient.getEnv("Ut5RaJvvG7dP8hgK82qjdtvyUA6x8g");
|
||||
// 您的可用区ID
|
||||
config.regionId = regionId;
|
||||
return new com.aliyun.ecs20140526.Client(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* DescribeInstanceMonitorData 查询一台 Ecs 实例所有相关的监控信息
|
||||
*/
|
||||
public static void DescribeInstanceMonitorData(com.aliyun.ecs20140526.Client client, String instanceId, String startTime, String endTime) throws Exception {
|
||||
DescribeInstanceMonitorDataRequest req = new DescribeInstanceMonitorDataRequest();
|
||||
// 待查询的实例 ID
|
||||
req.instanceId = instanceId;
|
||||
// 获取数据的起始时间点。按照 ISO8601 标准表示,并需要使用 UTC 时间,格式为:YYYY-MM-DDThh:mm:ssZ。如果指定的秒(ss)不是 00,则自动换算为下一分钟。
|
||||
req.startTime = startTime;
|
||||
// 获取数据的结束时间点。按照 ISO8601 标准表示,并需要使用 UTC 时间,格式为:YYYY-MM-DDThh:mm:ssZ。 如果指定的秒(ss)不是 00,则自动换算为下一分钟。
|
||||
req.endTime = endTime;
|
||||
try {
|
||||
DescribeInstanceMonitorDataResponse resp = client.describeInstanceMonitorData(req);
|
||||
com.aliyun.teaconsole.Client.log("--------------------查询Ecs所有相关的监控信息--------------------");
|
||||
com.aliyun.teaconsole.Client.log(Common.toJSONString(TeaModel.buildMap(resp)));
|
||||
} catch (TeaException error) {
|
||||
com.aliyun.teaconsole.Client.log(error.message);
|
||||
} catch (Exception _error) {
|
||||
TeaException error = new TeaException(_error.getMessage(), _error);
|
||||
com.aliyun.teaconsole.Client.log(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* DescribeUserData 查询一台Ecs实例的自定义数据
|
||||
*/
|
||||
public static void DescribeUserData(com.aliyun.ecs20140526.Client client, String instanceId, String regionId) throws Exception {
|
||||
DescribeUserDataRequest req = new DescribeUserDataRequest();
|
||||
req.regionId = regionId;
|
||||
req.instanceId = instanceId;
|
||||
try {
|
||||
DescribeUserDataResponse resp = client.describeUserData(req);
|
||||
com.aliyun.teaconsole.Client.log("--------------------查询Ecs自定义数据--------------------");
|
||||
com.aliyun.teaconsole.Client.log(Common.toJSONString(TeaModel.buildMap(resp)));
|
||||
} catch (TeaException error) {
|
||||
com.aliyun.teaconsole.Client.log(error.message);
|
||||
} catch (Exception _error) {
|
||||
TeaException error = new TeaException(_error.getMessage(), _error);
|
||||
com.aliyun.teaconsole.Client.log(error.message);
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args_) throws Exception {
|
||||
// java.util.List<String> args = java.util.Arrays.asList(args_);
|
||||
// 可用区域Id
|
||||
String regionId = "cn-zhangjiakou";
|
||||
// 待查询的实例 ID
|
||||
String instanceId = "i-8vbamj8u1q8xnmkze5m0";
|
||||
// 获取数据的起始时间点。按照 ISO8601 表示,并需要使用 UTC 时间,格式为:YYYY-MM-DDThh:mm:ssZ。如果指定的秒(ss)不是 00,则自动换算为下一分钟。
|
||||
String startTime = "2024-03-14T12:12:12Z";
|
||||
// 获取数据的结束时间点。按照 ISO8601 标准表示,并需要使用 UTC 时间,格式为:YYYY-MM-DDThh:mm:ssZ。 如果指定的秒(ss)不是 00,则自动换算为下一分钟。
|
||||
String endTime = "2024-04-10T12:12:12Z";
|
||||
com.aliyun.ecs20140526.Client client = Sample2.Initialization(regionId);
|
||||
// 查询一台 Ecs 实例所有相关的监控信息
|
||||
Sample2.DescribeInstanceMonitorData(client, instanceId, startTime, endTime);
|
||||
// 查询一台Ecs实例的自定义数据
|
||||
Sample2.DescribeUserData(client, instanceId, regionId);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue