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_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("test".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: 1,
77 PublicIpAssigned: Some(true),
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 // 添加开机运行脚本 - 安装nginx
93 // 这里提供的脚本需要使用Base64编码
94 // 原始脚本内容:
95 // #!/bin/bash
96 // yum update -y
97 // yum install -y nginx
98 // systemctl enable nginx
99 // systemctl start nginx
100 // echo "<h1>Hello from Chisato73</h1>" > /usr/share/nginx/html/index.html
101 UserData: Some("IyEvYmluL2Jhc2gKeXVtIHVwZGF0ZSAteQp5dW0gaW5zdGFsbCAteSBuZ2lueApzeXN0ZW1jdGwgZW5hYmxlIG5naW54CnN5c3RlbWN0bCBzdGFydCBuZ2lueAplY2hvICI8aDE+SGVsbG8gZnJvbSBDaGlzYXRvNzM8L2gxPiIgPiAvdXNyL3NoYXJlL25naW54L2h0bWwvaW5kZXguaHRtbA==".to_string()),
102
103 // 其他参数
104 DataDisks: None,
105 ClientToken: None,
106 HostName: None,
107 TagSpecification: None,
108 ProjectId: None,
109 InstanceChargePrepaid: None,
110 ActionTimer: None,
111 DisasterRecoverGroupIds: None,
112 InstanceMarketOptions: None,
113 DryRun: None,
114 CpuTopology: None,
115 CamRoleName: None,
116 HpcClusterId: None,
117 LaunchTemplate: None,
118 DedicatedClusterId: None,
119 ChcIds: None,
120 DisableApiTermination: None,
121 };
122
123 // 发送创建请求
124 println!("正在创建竞价实例...");
125 match instance_service.run_instances(&request, region).await {
126 Ok(response) => {
127 println!("实例创建请求已提交,实例ID列表:");
128 for id in response.Response.InstanceIdSet {
129 println!("- {}", id);
130 }
131 println!("\n注意:返回实例ID列表并不代表实例创建成功,请通过DescribeInstances接口查询实例状态");
132 },
133 Err(err) => {
134 println!("创建实例失败: {}", err);
135 }
136 }
137
138 Ok(())
139}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 125)
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("test".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: 1,
77 PublicIpAssigned: Some(true),
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 // 添加开机运行脚本 - 安装nginx
93 // 这里提供的脚本需要使用Base64编码
94 // 原始脚本内容:
95 // #!/bin/bash
96 // yum update -y
97 // yum install -y nginx
98 // systemctl enable nginx
99 // systemctl start nginx
100 // echo "<h1>Hello from Chisato73</h1>" > /usr/share/nginx/html/index.html
101 UserData: Some("IyEvYmluL2Jhc2gKeXVtIHVwZGF0ZSAteQp5dW0gaW5zdGFsbCAteSBuZ2lueApzeXN0ZW1jdGwgZW5hYmxlIG5naW54CnN5c3RlbWN0bCBzdGFydCBuZ2lueAplY2hvICI8aDE+SGVsbG8gZnJvbSBDaGlzYXRvNzM8L2gxPiIgPiAvdXNyL3NoYXJlL25naW54L2h0bWwvaW5kZXguaHRtbA==".to_string()),
102
103 // 其他参数
104 DataDisks: None,
105 ClientToken: None,
106 HostName: None,
107 TagSpecification: None,
108 ProjectId: None,
109 InstanceChargePrepaid: None,
110 ActionTimer: None,
111 DisasterRecoverGroupIds: None,
112 InstanceMarketOptions: None,
113 DryRun: None,
114 CpuTopology: None,
115 CamRoleName: None,
116 HpcClusterId: None,
117 LaunchTemplate: None,
118 DedicatedClusterId: None,
119 ChcIds: None,
120 DisableApiTermination: None,
121 };
122
123 // 发送创建请求
124 println!("正在创建竞价实例...");
125 match instance_service.run_instances(&request, region).await {
126 Ok(response) => {
127 println!("实例创建请求已提交,实例ID列表:");
128 for id in response.Response.InstanceIdSet {
129 println!("- {}", id);
130 }
131 println!("\n注意:返回实例ID列表并不代表实例创建成功,请通过DescribeInstances接口查询实例状态");
132 },
133 Err(err) => {
134 println!("创建实例失败: {}", err);
135 }
136 }
137
138 Ok(())
139}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