Struct InstanceService

Source
pub struct InstanceService<'a> { /* private fields */ }
Expand description

实例服务

Implementations§

Source§

impl<'a> InstanceService<'a>

Source

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}
Source

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T