pub struct InstanceService<'a> { /* private fields */ }Expand description
实例服务
Implementations§
Source§impl<'a> InstanceService<'a>
impl<'a> InstanceService<'a>
Sourcepub fn new(client: &'a TencentCloudClient) -> Self
pub fn new(client: &'a TencentCloudClient) -> Self
创建新的实例服务
Examples found in repository?
examples/instance_terminate.rs (line 19)
8async fn main() -> Result<(), Box<dyn std::error::Error>> {
9 // 从环境变量读取密钥
10 let secret_id = env::var("TENCENTCLOUD_SECRET_ID")
11 .expect("请设置环境变量TENCENTCLOUD_SECRET_ID");
12 let secret_key = env::var("TENCENTCLOUD_SECRET_KEY")
13 .expect("请设置环境变量TENCENTCLOUD_SECRET_KEY");
14
15 // 创建客户端
16 let client = TencentCloudClient::new(secret_id, secret_key);
17
18 // 创建实例服务
19 let instance_service = InstanceService::new(&client);
20
21 // 设置区域
22 let region = "ap-guangzhou";
23
24 // 获取要退还的实例ID,可以从命令行参数传入
25 let instance_ids: Vec<String> = env::args()
26 .skip(1) // 跳过程序名称
27 .collect();
28
29 if instance_ids.is_empty() {
30 eprintln!("请提供至少一个实例ID作为命令行参数");
31 eprintln!("用法: cargo run --example instance_terminate <实例ID1> [<实例ID2> ...]");
32 return Ok(());
33 }
34
35 // 确认是否继续
36 println!("将退还以下实例:");
37 for id in &instance_ids {
38 println!("- {}", id);
39 }
40 println!("警告: 这个操作不可逆!按量计费实例将被直接销毁,包年包月实例将被移至回收站。");
41 println!("是否继续?[y/N]");
42
43 let mut input = String::new();
44 std::io::stdin().read_line(&mut input)?;
45
46 if input.trim().to_lowercase() != "y" {
47 println!("操作已取消");
48 return Ok(());
49 }
50
51 // 创建退还实例请求
52 let request = TerminateInstancesRequest {
53 InstanceIds: instance_ids,
54 ReleasePrepaidDataDisks: Some(false), // 默认不释放包年包月数据盘
55 };
56
57 // 发送退还请求
58 println!("正在退还实例...");
59 match instance_service.terminate_instances(&request, region).await {
60 Ok(_) => {
61 println!("实例退还请求已提交成功");
62 println!("按量计费实例将被直接销毁,包年包月实例将被移至回收站");
63 },
64 Err(err) => {
65 println!("退还实例失败: {}", err);
66 }
67 }
68
69 Ok(())
70}More examples
examples/instance_create.rs (line 22)
11async fn main() -> Result<(), Box<dyn std::error::Error>> {
12 // 从环境变量读取密钥
13 let secret_id = std::env::var("TENCENTCLOUD_SECRET_ID")
14 .expect("请设置环境变量TENCENTCLOUD_SECRET_ID");
15 let secret_key = std::env::var("TENCENTCLOUD_SECRET_KEY")
16 .expect("请设置环境变量TENCENTCLOUD_SECRET_KEY");
17
18 // 创建客户端
19 let client = TencentCloudClient::new(secret_id, secret_key);
20
21 // 创建实例服务
22 let instance_service = InstanceService::new(&client);
23
24 // 设置区域
25 let region = "ap-guangzhou";
26
27 // 创建实例请求参数
28 let request = RunInstancesRequest {
29 // 设置实例位置(广州六区)
30 Placement: Some(Placement {
31 Zone: Some("ap-guangzhou-6".to_string()),
32 ProjectId: None,
33 HostIds: None,
34 HostIps: None,
35 DedicatedClusterId: None,
36 }),
37
38 // 指定镜像ID - TencentOS
39 ImageId: Some("img-6n21msk1".to_string()),
40
41 // 实例计费类型 - 竞价实例
42 InstanceChargeType: Some(InstanceChargeType::Spotpaid),
43
44 // 实例配置 - S5.MEDIUM2
45 InstanceType: Some("S5.MEDIUM2".to_string()),
46
47 // 系统盘 - 使用CLOUD_BSSD
48 SystemDisk: Some(SystemDisk {
49 DiskType: Some("CLOUD_BSSD".to_string()),
50 DiskId: None,
51 DiskSize: Some(20),
52 }),
53
54 // 不指定VPC配置,使用默认VPC和子网
55 VirtualPrivateCloud: None,
56
57 // 使用默认安全组
58 SecurityGroupIds: None,
59
60 // 实例数量
61 InstanceCount: Some(1),
62
63 // 实例名称
64 InstanceName: Some("frp".to_string()),
65
66 // 登录设置
67 LoginSettings: Some(LoginSettings {
68 Password: Some("Test@123456789".to_string()),
69 KeyIds: None,
70 KeepImageLogin: None,
71 }),
72
73 // 公网带宽 - 不需要公网IP
74 InternetAccessible: Some(InternetAccessible {
75 InternetChargeType: "TRAFFIC_POSTPAID_BY_HOUR".to_string(),
76 InternetMaxBandwidthOut: 0,
77 PublicIpAssigned: Some(false),
78 BandwidthPackageId: None,
79 }),
80
81 // 增强服务
82 EnhancedService: Some(EnhancedService {
83 SecurityService: Some(RunSecurityServiceEnabled {
84 Enabled: true,
85 }),
86 MonitorService: Some(RunMonitorServiceEnabled {
87 Enabled: true,
88 }),
89 AutomationService: None,
90 }),
91
92 // 其他参数
93 DataDisks: None,
94 ClientToken: None,
95 HostName: None,
96 TagSpecification: None,
97 ProjectId: None,
98 InstanceChargePrepaid: None,
99 };
100
101 // 发送创建请求
102 println!("正在创建竞价实例...");
103 match instance_service.run_instances(&request, region).await {
104 Ok(response) => {
105 println!("实例创建请求已提交,实例ID列表:");
106 for id in response.Response.InstanceIdSet {
107 println!("- {}", id);
108 }
109 println!("\n注意:返回实例ID列表并不代表实例创建成功,请通过DescribeInstances接口查询实例状态");
110 },
111 Err(err) => {
112 println!("创建实例失败: {}", err);
113 }
114 }
115
116 Ok(())
117}examples/instance_lifecycle.rs (line 21)
10async fn main() -> Result<(), Box<dyn std::error::Error>> {
11 // 从环境变量读取密钥
12 let secret_id = env::var("TENCENTCLOUD_SECRET_ID")
13 .expect("请设置环境变量TENCENTCLOUD_SECRET_ID");
14 let secret_key = env::var("TENCENTCLOUD_SECRET_KEY")
15 .expect("请设置环境变量TENCENTCLOUD_SECRET_KEY");
16
17 // 创建客户端
18 let client = TencentCloudClient::new(secret_id, secret_key);
19
20 // 创建实例服务
21 let instance_service = InstanceService::new(&client);
22
23 // 设置区域
24 let region = "ap-guangzhou";
25
26 // 获取要操作的实例ID
27 let args: Vec<String> = env::args().collect();
28 if args.len() < 3 {
29 eprintln!("用法: cargo run --example instance_lifecycle <操作> <实例ID1> [<实例ID2> ...]");
30 eprintln!("操作可以是: start, stop, reboot");
31 return Ok(());
32 }
33
34 let operation = &args[1];
35 let instance_ids: Vec<String> = args[2..].iter().cloned().collect();
36
37 // 确认是否继续
38 println!("将对以下实例执行{}操作:", operation);
39 for id in &instance_ids {
40 println!("- {}", id);
41 }
42 println!("是否继续?[y/N]");
43
44 let mut input = String::new();
45 std::io::stdin().read_line(&mut input)?;
46
47 if input.trim().to_lowercase() != "y" {
48 println!("操作已取消");
49 return Ok(());
50 }
51
52 match operation.as_str() {
53 "start" => {
54 // 创建启动实例请求
55 let request = StartInstancesRequest {
56 InstanceIds: instance_ids,
57 };
58
59 // 发送启动请求
60 println!("正在启动实例...");
61 match instance_service.start_instances(&request, region).await {
62 Ok(_) => {
63 println!("实例启动请求已提交成功");
64 println!("实例将从STOPPED状态变为STARTING状态,然后再变为RUNNING状态");
65 },
66 Err(err) => {
67 println!("启动实例失败: {}", err);
68 }
69 }
70 },
71 "stop" => {
72 // 创建关闭实例请求
73 let request = StopInstancesRequest {
74 InstanceIds: instance_ids,
75 StopType: Some("SOFT".to_string()), // 软关机
76 ForceStop: None, // 弃用的参数
77 StoppedMode: Some("KEEP_CHARGING".to_string()), // 关机继续收费
78 };
79
80 // 发送关闭请求
81 println!("正在关闭实例...");
82 match instance_service.stop_instances(&request, region).await {
83 Ok(_) => {
84 println!("实例关闭请求已提交成功");
85 println!("实例将从RUNNING状态变为STOPPING状态,然后再变为STOPPED状态");
86 },
87 Err(err) => {
88 println!("关闭实例失败: {}", err);
89 }
90 }
91 },
92 "reboot" => {
93 // 创建重启实例请求
94 let request = RebootInstancesRequest {
95 InstanceIds: instance_ids,
96 StopType: Some("SOFT".to_string()), // 软重启
97 ForceReboot: None, // 弃用的参数
98 };
99
100 // 发送重启请求
101 println!("正在重启实例...");
102 match instance_service.reboot_instances(&request, region).await {
103 Ok(_) => {
104 println!("实例重启请求已提交成功");
105 println!("实例将从RUNNING状态变为REBOOTING状态,然后再变为RUNNING状态");
106 },
107 Err(err) => {
108 println!("重启实例失败: {}", err);
109 }
110 }
111 },
112 _ => {
113 eprintln!("未知操作: {}", operation);
114 eprintln!("支持的操作: start, stop, reboot");
115 }
116 }
117
118 Ok(())
119}examples/instance_describe.rs (line 19)
8async fn main() -> Result<(), Box<dyn std::error::Error>> {
9 // 从环境变量读取密钥
10 let secret_id = env::var("TENCENTCLOUD_SECRET_ID")
11 .expect("请设置环境变量TENCENTCLOUD_SECRET_ID");
12 let secret_key = env::var("TENCENTCLOUD_SECRET_KEY")
13 .expect("请设置环境变量TENCENTCLOUD_SECRET_KEY");
14
15 // 创建客户端
16 let client = TencentCloudClient::new(secret_id, secret_key);
17
18 // 创建实例服务
19 let instance_service = InstanceService::new(&client);
20
21 // 设置区域
22 let region = "ap-guangzhou";
23
24 // 获取命令行参数
25 let args: Vec<String> = env::args().collect();
26
27 if args.len() > 1 {
28 match args[1].as_str() {
29 // 按ID查询实例
30 "id" => {
31 if args.len() < 3 {
32 println!("缺少实例ID参数,用法: cargo run --example instance_describe id <实例ID>");
33 return Ok(());
34 }
35
36 let instance_ids = args[2..].to_vec();
37 println!("正在查询以下实例ID: {:?}", instance_ids);
38
39 let request = DescribeInstancesRequest {
40 InstanceIds: Some(instance_ids),
41 Filters: None,
42 Offset: None,
43 Limit: None,
44 };
45
46 let response = instance_service.describe_instances(&request, region).await?;
47 display_instances(&response.Response);
48 },
49
50 // 按可用区查询实例
51 "zone" => {
52 if args.len() < 3 {
53 println!("缺少可用区参数,用法: cargo run --example instance_describe zone <可用区>");
54 return Ok(());
55 }
56
57 let zone = &args[2];
58 println!("正在查询可用区 {} 的实例", zone);
59
60 let mut filters = Vec::new();
61 filters.push(Filter {
62 Name: "zone".to_string(),
63 Values: vec![zone.clone()],
64 });
65
66 let request = DescribeInstancesRequest {
67 InstanceIds: None,
68 Filters: Some(filters),
69 Offset: None,
70 Limit: Some(20),
71 };
72
73 let response = instance_service.describe_instances(&request, region).await?;
74 display_instances(&response.Response);
75 },
76
77 // 按实例名称查询实例
78 "name" => {
79 if args.len() < 3 {
80 println!("缺少实例名称参数,用法: cargo run --example instance_describe name <实例名称>");
81 return Ok(());
82 }
83
84 let name = &args[2];
85 println!("正在查询名称为 {} 的实例", name);
86
87 let mut filters = Vec::new();
88 filters.push(Filter {
89 Name: "instance-name".to_string(),
90 Values: vec![name.clone()],
91 });
92
93 let request = DescribeInstancesRequest {
94 InstanceIds: None,
95 Filters: Some(filters),
96 Offset: None,
97 Limit: Some(20),
98 };
99
100 let response = instance_service.describe_instances(&request, region).await?;
101 display_instances(&response.Response);
102 },
103
104 // 按标签查询实例
105 "tag" => {
106 if args.len() < 4 {
107 println!("缺少标签参数,用法: cargo run --example instance_describe tag <标签键> <标签值>");
108 return Ok(());
109 }
110
111 let tag_key = &args[2];
112 let tag_value = &args[3];
113 println!("正在查询标签键值对为 {}:{} 的实例", tag_key, tag_value);
114
115 let mut filters = Vec::new();
116 filters.push(Filter {
117 Name: format!("tag:{}", tag_key),
118 Values: vec![tag_value.clone()],
119 });
120
121 let request = DescribeInstancesRequest {
122 InstanceIds: None,
123 Filters: Some(filters),
124 Offset: None,
125 Limit: Some(20),
126 };
127
128 let response = instance_service.describe_instances(&request, region).await?;
129 display_instances(&response.Response);
130 },
131
132 // 查询所有实例
133 "all" => {
134 println!("正在查询所有实例");
135
136 let request = DescribeInstancesRequest {
137 InstanceIds: None,
138 Filters: None,
139 Offset: None,
140 Limit: Some(20),
141 };
142
143 let response = instance_service.describe_instances(&request, region).await?;
144 display_instances(&response.Response);
145 },
146
147 // 按状态查询实例
148 "state" => {
149 if args.len() < 3 {
150 println!("缺少实例状态参数,用法: cargo run --example instance_describe state <状态>");
151 println!("支持的状态: PENDING, RUNNING, STOPPED, STOPPING, REBOOTING, STARTING, SHUTDOWN, TERMINATING");
152 return Ok(());
153 }
154
155 let state = &args[2];
156 println!("正在查询状态为 {} 的实例", state);
157
158 let mut filters = Vec::new();
159 filters.push(Filter {
160 Name: "instance-state".to_string(),
161 Values: vec![state.clone()],
162 });
163
164 let request = DescribeInstancesRequest {
165 InstanceIds: None,
166 Filters: Some(filters),
167 Offset: None,
168 Limit: Some(20),
169 };
170
171 let response = instance_service.describe_instances(&request, region).await?;
172 display_instances(&response.Response);
173 },
174
175 _ => {
176 print_usage();
177 }
178 }
179 } else {
180 print_usage();
181 }
182
183 Ok(())
184}Sourcepub async fn run_instances(
&self,
request: &RunInstancesRequest,
region: &str,
) -> Result<RunInstancesResponseType>
pub async fn run_instances( &self, request: &RunInstancesRequest, region: &str, ) -> Result<RunInstancesResponseType>
创建一个或多个指定配置的实例
本接口(RunInstances)用于创建一个或多个指定配置的实例。
Examples found in repository?
examples/instance_create.rs (line 103)
11async fn main() -> Result<(), Box<dyn std::error::Error>> {
12 // 从环境变量读取密钥
13 let secret_id = std::env::var("TENCENTCLOUD_SECRET_ID")
14 .expect("请设置环境变量TENCENTCLOUD_SECRET_ID");
15 let secret_key = std::env::var("TENCENTCLOUD_SECRET_KEY")
16 .expect("请设置环境变量TENCENTCLOUD_SECRET_KEY");
17
18 // 创建客户端
19 let client = TencentCloudClient::new(secret_id, secret_key);
20
21 // 创建实例服务
22 let instance_service = InstanceService::new(&client);
23
24 // 设置区域
25 let region = "ap-guangzhou";
26
27 // 创建实例请求参数
28 let request = RunInstancesRequest {
29 // 设置实例位置(广州六区)
30 Placement: Some(Placement {
31 Zone: Some("ap-guangzhou-6".to_string()),
32 ProjectId: None,
33 HostIds: None,
34 HostIps: None,
35 DedicatedClusterId: None,
36 }),
37
38 // 指定镜像ID - TencentOS
39 ImageId: Some("img-6n21msk1".to_string()),
40
41 // 实例计费类型 - 竞价实例
42 InstanceChargeType: Some(InstanceChargeType::Spotpaid),
43
44 // 实例配置 - S5.MEDIUM2
45 InstanceType: Some("S5.MEDIUM2".to_string()),
46
47 // 系统盘 - 使用CLOUD_BSSD
48 SystemDisk: Some(SystemDisk {
49 DiskType: Some("CLOUD_BSSD".to_string()),
50 DiskId: None,
51 DiskSize: Some(20),
52 }),
53
54 // 不指定VPC配置,使用默认VPC和子网
55 VirtualPrivateCloud: None,
56
57 // 使用默认安全组
58 SecurityGroupIds: None,
59
60 // 实例数量
61 InstanceCount: Some(1),
62
63 // 实例名称
64 InstanceName: Some("frp".to_string()),
65
66 // 登录设置
67 LoginSettings: Some(LoginSettings {
68 Password: Some("Test@123456789".to_string()),
69 KeyIds: None,
70 KeepImageLogin: None,
71 }),
72
73 // 公网带宽 - 不需要公网IP
74 InternetAccessible: Some(InternetAccessible {
75 InternetChargeType: "TRAFFIC_POSTPAID_BY_HOUR".to_string(),
76 InternetMaxBandwidthOut: 0,
77 PublicIpAssigned: Some(false),
78 BandwidthPackageId: None,
79 }),
80
81 // 增强服务
82 EnhancedService: Some(EnhancedService {
83 SecurityService: Some(RunSecurityServiceEnabled {
84 Enabled: true,
85 }),
86 MonitorService: Some(RunMonitorServiceEnabled {
87 Enabled: true,
88 }),
89 AutomationService: None,
90 }),
91
92 // 其他参数
93 DataDisks: None,
94 ClientToken: None,
95 HostName: None,
96 TagSpecification: None,
97 ProjectId: None,
98 InstanceChargePrepaid: None,
99 };
100
101 // 发送创建请求
102 println!("正在创建竞价实例...");
103 match instance_service.run_instances(&request, region).await {
104 Ok(response) => {
105 println!("实例创建请求已提交,实例ID列表:");
106 for id in response.Response.InstanceIdSet {
107 println!("- {}", id);
108 }
109 println!("\n注意:返回实例ID列表并不代表实例创建成功,请通过DescribeInstances接口查询实例状态");
110 },
111 Err(err) => {
112 println!("创建实例失败: {}", err);
113 }
114 }
115
116 Ok(())
117}Sourcepub async fn terminate_instances(
&self,
request: &TerminateInstancesRequest,
region: &str,
) -> Result<TerminateInstancesResponseType>
pub async fn terminate_instances( &self, request: &TerminateInstancesRequest, region: &str, ) -> Result<TerminateInstancesResponseType>
主动退还实例
本接口(TerminateInstances)用于主动退还实例。
- 不再使用的实例,可通过本接口主动退还。
- 按量计费的实例通过本接口可直接退还;包年包月实例如符合退还规则,也可通过本接口主动退还。
- 包年包月实例首次调用本接口,实例将被移至回收站,再次调用本接口,实例将被销毁,且不可恢复。按量计费实例调用本接口将被直接销毁。
Examples found in repository?
examples/instance_terminate.rs (line 59)
8async fn main() -> Result<(), Box<dyn std::error::Error>> {
9 // 从环境变量读取密钥
10 let secret_id = env::var("TENCENTCLOUD_SECRET_ID")
11 .expect("请设置环境变量TENCENTCLOUD_SECRET_ID");
12 let secret_key = env::var("TENCENTCLOUD_SECRET_KEY")
13 .expect("请设置环境变量TENCENTCLOUD_SECRET_KEY");
14
15 // 创建客户端
16 let client = TencentCloudClient::new(secret_id, secret_key);
17
18 // 创建实例服务
19 let instance_service = InstanceService::new(&client);
20
21 // 设置区域
22 let region = "ap-guangzhou";
23
24 // 获取要退还的实例ID,可以从命令行参数传入
25 let instance_ids: Vec<String> = env::args()
26 .skip(1) // 跳过程序名称
27 .collect();
28
29 if instance_ids.is_empty() {
30 eprintln!("请提供至少一个实例ID作为命令行参数");
31 eprintln!("用法: cargo run --example instance_terminate <实例ID1> [<实例ID2> ...]");
32 return Ok(());
33 }
34
35 // 确认是否继续
36 println!("将退还以下实例:");
37 for id in &instance_ids {
38 println!("- {}", id);
39 }
40 println!("警告: 这个操作不可逆!按量计费实例将被直接销毁,包年包月实例将被移至回收站。");
41 println!("是否继续?[y/N]");
42
43 let mut input = String::new();
44 std::io::stdin().read_line(&mut input)?;
45
46 if input.trim().to_lowercase() != "y" {
47 println!("操作已取消");
48 return Ok(());
49 }
50
51 // 创建退还实例请求
52 let request = TerminateInstancesRequest {
53 InstanceIds: instance_ids,
54 ReleasePrepaidDataDisks: Some(false), // 默认不释放包年包月数据盘
55 };
56
57 // 发送退还请求
58 println!("正在退还实例...");
59 match instance_service.terminate_instances(&request, region).await {
60 Ok(_) => {
61 println!("实例退还请求已提交成功");
62 println!("按量计费实例将被直接销毁,包年包月实例将被移至回收站");
63 },
64 Err(err) => {
65 println!("退还实例失败: {}", err);
66 }
67 }
68
69 Ok(())
70}Sourcepub async fn reboot_instances(
&self,
request: &RebootInstancesRequest,
region: &str,
) -> Result<RebootInstancesResponseType>
pub async fn reboot_instances( &self, request: &RebootInstancesRequest, region: &str, ) -> Result<RebootInstancesResponseType>
重启实例
本接口 (RebootInstances) 用于重启实例。
- 只有状态为RUNNING的实例才可以进行此操作。
- 接口调用成功时,实例会进入REBOOTING状态;重启实例成功时,实例会进入RUNNING状态。
- 支持强制重启,强制重启可能会导致数据丢失或文件系统损坏,请仅在服务器不能正常重启时使用。
Examples found in repository?
examples/instance_lifecycle.rs (line 102)
10async fn main() -> Result<(), Box<dyn std::error::Error>> {
11 // 从环境变量读取密钥
12 let secret_id = env::var("TENCENTCLOUD_SECRET_ID")
13 .expect("请设置环境变量TENCENTCLOUD_SECRET_ID");
14 let secret_key = env::var("TENCENTCLOUD_SECRET_KEY")
15 .expect("请设置环境变量TENCENTCLOUD_SECRET_KEY");
16
17 // 创建客户端
18 let client = TencentCloudClient::new(secret_id, secret_key);
19
20 // 创建实例服务
21 let instance_service = InstanceService::new(&client);
22
23 // 设置区域
24 let region = "ap-guangzhou";
25
26 // 获取要操作的实例ID
27 let args: Vec<String> = env::args().collect();
28 if args.len() < 3 {
29 eprintln!("用法: cargo run --example instance_lifecycle <操作> <实例ID1> [<实例ID2> ...]");
30 eprintln!("操作可以是: start, stop, reboot");
31 return Ok(());
32 }
33
34 let operation = &args[1];
35 let instance_ids: Vec<String> = args[2..].iter().cloned().collect();
36
37 // 确认是否继续
38 println!("将对以下实例执行{}操作:", operation);
39 for id in &instance_ids {
40 println!("- {}", id);
41 }
42 println!("是否继续?[y/N]");
43
44 let mut input = String::new();
45 std::io::stdin().read_line(&mut input)?;
46
47 if input.trim().to_lowercase() != "y" {
48 println!("操作已取消");
49 return Ok(());
50 }
51
52 match operation.as_str() {
53 "start" => {
54 // 创建启动实例请求
55 let request = StartInstancesRequest {
56 InstanceIds: instance_ids,
57 };
58
59 // 发送启动请求
60 println!("正在启动实例...");
61 match instance_service.start_instances(&request, region).await {
62 Ok(_) => {
63 println!("实例启动请求已提交成功");
64 println!("实例将从STOPPED状态变为STARTING状态,然后再变为RUNNING状态");
65 },
66 Err(err) => {
67 println!("启动实例失败: {}", err);
68 }
69 }
70 },
71 "stop" => {
72 // 创建关闭实例请求
73 let request = StopInstancesRequest {
74 InstanceIds: instance_ids,
75 StopType: Some("SOFT".to_string()), // 软关机
76 ForceStop: None, // 弃用的参数
77 StoppedMode: Some("KEEP_CHARGING".to_string()), // 关机继续收费
78 };
79
80 // 发送关闭请求
81 println!("正在关闭实例...");
82 match instance_service.stop_instances(&request, region).await {
83 Ok(_) => {
84 println!("实例关闭请求已提交成功");
85 println!("实例将从RUNNING状态变为STOPPING状态,然后再变为STOPPED状态");
86 },
87 Err(err) => {
88 println!("关闭实例失败: {}", err);
89 }
90 }
91 },
92 "reboot" => {
93 // 创建重启实例请求
94 let request = RebootInstancesRequest {
95 InstanceIds: instance_ids,
96 StopType: Some("SOFT".to_string()), // 软重启
97 ForceReboot: None, // 弃用的参数
98 };
99
100 // 发送重启请求
101 println!("正在重启实例...");
102 match instance_service.reboot_instances(&request, region).await {
103 Ok(_) => {
104 println!("实例重启请求已提交成功");
105 println!("实例将从RUNNING状态变为REBOOTING状态,然后再变为RUNNING状态");
106 },
107 Err(err) => {
108 println!("重启实例失败: {}", err);
109 }
110 }
111 },
112 _ => {
113 eprintln!("未知操作: {}", operation);
114 eprintln!("支持的操作: start, stop, reboot");
115 }
116 }
117
118 Ok(())
119}Sourcepub async fn start_instances(
&self,
request: &StartInstancesRequest,
region: &str,
) -> Result<StartInstancesResponseType>
pub async fn start_instances( &self, request: &StartInstancesRequest, region: &str, ) -> Result<StartInstancesResponseType>
启动实例
本接口 (StartInstances) 用于启动一个或多个实例。
- 只有状态为STOPPED的实例才可以进行此操作。
- 接口调用成功时,实例会进入STARTING状态;启动实例成功时,实例会进入RUNNING状态。
- 本接口为异步接口,启动实例请求发送成功后会返回一个RequestId,此时操作并未立即完成。
Examples found in repository?
examples/instance_lifecycle.rs (line 61)
10async fn main() -> Result<(), Box<dyn std::error::Error>> {
11 // 从环境变量读取密钥
12 let secret_id = env::var("TENCENTCLOUD_SECRET_ID")
13 .expect("请设置环境变量TENCENTCLOUD_SECRET_ID");
14 let secret_key = env::var("TENCENTCLOUD_SECRET_KEY")
15 .expect("请设置环境变量TENCENTCLOUD_SECRET_KEY");
16
17 // 创建客户端
18 let client = TencentCloudClient::new(secret_id, secret_key);
19
20 // 创建实例服务
21 let instance_service = InstanceService::new(&client);
22
23 // 设置区域
24 let region = "ap-guangzhou";
25
26 // 获取要操作的实例ID
27 let args: Vec<String> = env::args().collect();
28 if args.len() < 3 {
29 eprintln!("用法: cargo run --example instance_lifecycle <操作> <实例ID1> [<实例ID2> ...]");
30 eprintln!("操作可以是: start, stop, reboot");
31 return Ok(());
32 }
33
34 let operation = &args[1];
35 let instance_ids: Vec<String> = args[2..].iter().cloned().collect();
36
37 // 确认是否继续
38 println!("将对以下实例执行{}操作:", operation);
39 for id in &instance_ids {
40 println!("- {}", id);
41 }
42 println!("是否继续?[y/N]");
43
44 let mut input = String::new();
45 std::io::stdin().read_line(&mut input)?;
46
47 if input.trim().to_lowercase() != "y" {
48 println!("操作已取消");
49 return Ok(());
50 }
51
52 match operation.as_str() {
53 "start" => {
54 // 创建启动实例请求
55 let request = StartInstancesRequest {
56 InstanceIds: instance_ids,
57 };
58
59 // 发送启动请求
60 println!("正在启动实例...");
61 match instance_service.start_instances(&request, region).await {
62 Ok(_) => {
63 println!("实例启动请求已提交成功");
64 println!("实例将从STOPPED状态变为STARTING状态,然后再变为RUNNING状态");
65 },
66 Err(err) => {
67 println!("启动实例失败: {}", err);
68 }
69 }
70 },
71 "stop" => {
72 // 创建关闭实例请求
73 let request = StopInstancesRequest {
74 InstanceIds: instance_ids,
75 StopType: Some("SOFT".to_string()), // 软关机
76 ForceStop: None, // 弃用的参数
77 StoppedMode: Some("KEEP_CHARGING".to_string()), // 关机继续收费
78 };
79
80 // 发送关闭请求
81 println!("正在关闭实例...");
82 match instance_service.stop_instances(&request, region).await {
83 Ok(_) => {
84 println!("实例关闭请求已提交成功");
85 println!("实例将从RUNNING状态变为STOPPING状态,然后再变为STOPPED状态");
86 },
87 Err(err) => {
88 println!("关闭实例失败: {}", err);
89 }
90 }
91 },
92 "reboot" => {
93 // 创建重启实例请求
94 let request = RebootInstancesRequest {
95 InstanceIds: instance_ids,
96 StopType: Some("SOFT".to_string()), // 软重启
97 ForceReboot: None, // 弃用的参数
98 };
99
100 // 发送重启请求
101 println!("正在重启实例...");
102 match instance_service.reboot_instances(&request, region).await {
103 Ok(_) => {
104 println!("实例重启请求已提交成功");
105 println!("实例将从RUNNING状态变为REBOOTING状态,然后再变为RUNNING状态");
106 },
107 Err(err) => {
108 println!("重启实例失败: {}", err);
109 }
110 }
111 },
112 _ => {
113 eprintln!("未知操作: {}", operation);
114 eprintln!("支持的操作: start, stop, reboot");
115 }
116 }
117
118 Ok(())
119}Sourcepub async fn stop_instances(
&self,
request: &StopInstancesRequest,
region: &str,
) -> Result<StopInstancesResponseType>
pub async fn stop_instances( &self, request: &StopInstancesRequest, region: &str, ) -> Result<StopInstancesResponseType>
关闭实例
本接口 (StopInstances) 用于关闭一个或多个实例。
- 只有状态为RUNNING的实例才可以进行此操作。
- 接口调用成功时,实例会进入STOPPING状态;关闭实例成功时,实例会进入STOPPED状态。
- 支持强制关闭,强制关闭可能会导致数据丢失或文件系统损坏,请仅在服务器不能正常关机时使用。
- 本接口为异步接口,关闭实例请求发送成功后会返回一个RequestId,此时操作并未立即完成。
Examples found in repository?
examples/instance_lifecycle.rs (line 82)
10async fn main() -> Result<(), Box<dyn std::error::Error>> {
11 // 从环境变量读取密钥
12 let secret_id = env::var("TENCENTCLOUD_SECRET_ID")
13 .expect("请设置环境变量TENCENTCLOUD_SECRET_ID");
14 let secret_key = env::var("TENCENTCLOUD_SECRET_KEY")
15 .expect("请设置环境变量TENCENTCLOUD_SECRET_KEY");
16
17 // 创建客户端
18 let client = TencentCloudClient::new(secret_id, secret_key);
19
20 // 创建实例服务
21 let instance_service = InstanceService::new(&client);
22
23 // 设置区域
24 let region = "ap-guangzhou";
25
26 // 获取要操作的实例ID
27 let args: Vec<String> = env::args().collect();
28 if args.len() < 3 {
29 eprintln!("用法: cargo run --example instance_lifecycle <操作> <实例ID1> [<实例ID2> ...]");
30 eprintln!("操作可以是: start, stop, reboot");
31 return Ok(());
32 }
33
34 let operation = &args[1];
35 let instance_ids: Vec<String> = args[2..].iter().cloned().collect();
36
37 // 确认是否继续
38 println!("将对以下实例执行{}操作:", operation);
39 for id in &instance_ids {
40 println!("- {}", id);
41 }
42 println!("是否继续?[y/N]");
43
44 let mut input = String::new();
45 std::io::stdin().read_line(&mut input)?;
46
47 if input.trim().to_lowercase() != "y" {
48 println!("操作已取消");
49 return Ok(());
50 }
51
52 match operation.as_str() {
53 "start" => {
54 // 创建启动实例请求
55 let request = StartInstancesRequest {
56 InstanceIds: instance_ids,
57 };
58
59 // 发送启动请求
60 println!("正在启动实例...");
61 match instance_service.start_instances(&request, region).await {
62 Ok(_) => {
63 println!("实例启动请求已提交成功");
64 println!("实例将从STOPPED状态变为STARTING状态,然后再变为RUNNING状态");
65 },
66 Err(err) => {
67 println!("启动实例失败: {}", err);
68 }
69 }
70 },
71 "stop" => {
72 // 创建关闭实例请求
73 let request = StopInstancesRequest {
74 InstanceIds: instance_ids,
75 StopType: Some("SOFT".to_string()), // 软关机
76 ForceStop: None, // 弃用的参数
77 StoppedMode: Some("KEEP_CHARGING".to_string()), // 关机继续收费
78 };
79
80 // 发送关闭请求
81 println!("正在关闭实例...");
82 match instance_service.stop_instances(&request, region).await {
83 Ok(_) => {
84 println!("实例关闭请求已提交成功");
85 println!("实例将从RUNNING状态变为STOPPING状态,然后再变为STOPPED状态");
86 },
87 Err(err) => {
88 println!("关闭实例失败: {}", err);
89 }
90 }
91 },
92 "reboot" => {
93 // 创建重启实例请求
94 let request = RebootInstancesRequest {
95 InstanceIds: instance_ids,
96 StopType: Some("SOFT".to_string()), // 软重启
97 ForceReboot: None, // 弃用的参数
98 };
99
100 // 发送重启请求
101 println!("正在重启实例...");
102 match instance_service.reboot_instances(&request, region).await {
103 Ok(_) => {
104 println!("实例重启请求已提交成功");
105 println!("实例将从RUNNING状态变为REBOOTING状态,然后再变为RUNNING状态");
106 },
107 Err(err) => {
108 println!("重启实例失败: {}", err);
109 }
110 }
111 },
112 _ => {
113 eprintln!("未知操作: {}", operation);
114 eprintln!("支持的操作: start, stop, reboot");
115 }
116 }
117
118 Ok(())
119}Sourcepub async fn describe_instances(
&self,
request: &DescribeInstancesRequest,
region: &str,
) -> Result<DescribeInstancesResponseType>
pub async fn describe_instances( &self, request: &DescribeInstancesRequest, region: &str, ) -> Result<DescribeInstancesResponseType>
查询实例列表
本接口 (DescribeInstances) 用于查询一个或多个实例的详细信息。
- 可以根据实例ID、实例名称或者实例计费模式等信息来查询实例的详细信息
- 如果参数为空,返回当前用户一定数量(Limit所指定的数量,默认为20)的实例
- 支持查询实例的最新操作(LatestOperation)以及最新操作状态(LatestOperationState)
Examples found in repository?
examples/instance_describe.rs (line 46)
8async fn main() -> Result<(), Box<dyn std::error::Error>> {
9 // 从环境变量读取密钥
10 let secret_id = env::var("TENCENTCLOUD_SECRET_ID")
11 .expect("请设置环境变量TENCENTCLOUD_SECRET_ID");
12 let secret_key = env::var("TENCENTCLOUD_SECRET_KEY")
13 .expect("请设置环境变量TENCENTCLOUD_SECRET_KEY");
14
15 // 创建客户端
16 let client = TencentCloudClient::new(secret_id, secret_key);
17
18 // 创建实例服务
19 let instance_service = InstanceService::new(&client);
20
21 // 设置区域
22 let region = "ap-guangzhou";
23
24 // 获取命令行参数
25 let args: Vec<String> = env::args().collect();
26
27 if args.len() > 1 {
28 match args[1].as_str() {
29 // 按ID查询实例
30 "id" => {
31 if args.len() < 3 {
32 println!("缺少实例ID参数,用法: cargo run --example instance_describe id <实例ID>");
33 return Ok(());
34 }
35
36 let instance_ids = args[2..].to_vec();
37 println!("正在查询以下实例ID: {:?}", instance_ids);
38
39 let request = DescribeInstancesRequest {
40 InstanceIds: Some(instance_ids),
41 Filters: None,
42 Offset: None,
43 Limit: None,
44 };
45
46 let response = instance_service.describe_instances(&request, region).await?;
47 display_instances(&response.Response);
48 },
49
50 // 按可用区查询实例
51 "zone" => {
52 if args.len() < 3 {
53 println!("缺少可用区参数,用法: cargo run --example instance_describe zone <可用区>");
54 return Ok(());
55 }
56
57 let zone = &args[2];
58 println!("正在查询可用区 {} 的实例", zone);
59
60 let mut filters = Vec::new();
61 filters.push(Filter {
62 Name: "zone".to_string(),
63 Values: vec![zone.clone()],
64 });
65
66 let request = DescribeInstancesRequest {
67 InstanceIds: None,
68 Filters: Some(filters),
69 Offset: None,
70 Limit: Some(20),
71 };
72
73 let response = instance_service.describe_instances(&request, region).await?;
74 display_instances(&response.Response);
75 },
76
77 // 按实例名称查询实例
78 "name" => {
79 if args.len() < 3 {
80 println!("缺少实例名称参数,用法: cargo run --example instance_describe name <实例名称>");
81 return Ok(());
82 }
83
84 let name = &args[2];
85 println!("正在查询名称为 {} 的实例", name);
86
87 let mut filters = Vec::new();
88 filters.push(Filter {
89 Name: "instance-name".to_string(),
90 Values: vec![name.clone()],
91 });
92
93 let request = DescribeInstancesRequest {
94 InstanceIds: None,
95 Filters: Some(filters),
96 Offset: None,
97 Limit: Some(20),
98 };
99
100 let response = instance_service.describe_instances(&request, region).await?;
101 display_instances(&response.Response);
102 },
103
104 // 按标签查询实例
105 "tag" => {
106 if args.len() < 4 {
107 println!("缺少标签参数,用法: cargo run --example instance_describe tag <标签键> <标签值>");
108 return Ok(());
109 }
110
111 let tag_key = &args[2];
112 let tag_value = &args[3];
113 println!("正在查询标签键值对为 {}:{} 的实例", tag_key, tag_value);
114
115 let mut filters = Vec::new();
116 filters.push(Filter {
117 Name: format!("tag:{}", tag_key),
118 Values: vec![tag_value.clone()],
119 });
120
121 let request = DescribeInstancesRequest {
122 InstanceIds: None,
123 Filters: Some(filters),
124 Offset: None,
125 Limit: Some(20),
126 };
127
128 let response = instance_service.describe_instances(&request, region).await?;
129 display_instances(&response.Response);
130 },
131
132 // 查询所有实例
133 "all" => {
134 println!("正在查询所有实例");
135
136 let request = DescribeInstancesRequest {
137 InstanceIds: None,
138 Filters: None,
139 Offset: None,
140 Limit: Some(20),
141 };
142
143 let response = instance_service.describe_instances(&request, region).await?;
144 display_instances(&response.Response);
145 },
146
147 // 按状态查询实例
148 "state" => {
149 if args.len() < 3 {
150 println!("缺少实例状态参数,用法: cargo run --example instance_describe state <状态>");
151 println!("支持的状态: PENDING, RUNNING, STOPPED, STOPPING, REBOOTING, STARTING, SHUTDOWN, TERMINATING");
152 return Ok(());
153 }
154
155 let state = &args[2];
156 println!("正在查询状态为 {} 的实例", state);
157
158 let mut filters = Vec::new();
159 filters.push(Filter {
160 Name: "instance-state".to_string(),
161 Values: vec![state.clone()],
162 });
163
164 let request = DescribeInstancesRequest {
165 InstanceIds: None,
166 Filters: Some(filters),
167 Offset: None,
168 Limit: Some(20),
169 };
170
171 let response = instance_service.describe_instances(&request, region).await?;
172 display_instances(&response.Response);
173 },
174
175 _ => {
176 print_usage();
177 }
178 }
179 } else {
180 print_usage();
181 }
182
183 Ok(())
184}Auto Trait Implementations§
impl<'a> Freeze for InstanceService<'a>
impl<'a> !RefUnwindSafe for InstanceService<'a>
impl<'a> Send for InstanceService<'a>
impl<'a> Sync for InstanceService<'a>
impl<'a> Unpin for InstanceService<'a>
impl<'a> !UnwindSafe for InstanceService<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more