linode_rs/builder/
create_instance.rs

1use crate::{LinodeApi, LinodeError, LinodeInstance};
2use serde::Serialize;
3
4pub struct LinodeCreateInstanceBuilder {
5    api: LinodeApi,
6    config: CreateInstanceConfig,
7}
8
9#[derive(Serialize, Debug)]
10struct CreateInstanceConfigMetadata {
11    user_data: Option<String>,
12}
13
14#[derive(Serialize, Debug)]
15struct CreateInstanceConfig {
16    #[serde(skip_serializing_if = "Option::is_none")]
17    authorized_keys: Option<Vec<String>>,
18    #[serde(skip_serializing_if = "Option::is_none")]
19    authorized_users: Option<Vec<String>>,
20    #[serde(skip_serializing_if = "Option::is_none")]
21    backup_id: Option<u64>,
22    #[serde(skip_serializing_if = "Option::is_none")]
23    backups_enabled: Option<bool>,
24    #[serde(skip_serializing_if = "Option::is_none")]
25    booted: Option<bool>,
26    #[serde(skip_serializing_if = "Option::is_none")]
27    firewall_id: Option<u64>,
28    #[serde(skip_serializing_if = "Option::is_none")]
29    group: Option<String>,
30    #[serde(skip_serializing_if = "Option::is_none")]
31    image: Option<String>,
32    //#[serde(skip_serializing_if = "Option::is_none")]
33    //interfaces: Option<Vec<String>>,
34    #[serde(skip_serializing_if = "Option::is_none")]
35    label: Option<String>,
36    #[serde(skip_serializing_if = "Option::is_none")]
37    metadata: Option<CreateInstanceConfigMetadata>,
38    #[serde(skip_serializing_if = "Option::is_none")]
39    private_ip: Option<bool>,
40    region: String,
41    #[serde(skip_serializing_if = "Option::is_none")]
42    root_pass: Option<String>,
43    #[serde(skip_serializing_if = "Option::is_none")]
44    stackscript_data: Option<String>,
45    #[serde(skip_serializing_if = "Option::is_none")]
46    stackscript_id: Option<u64>,
47    #[serde(skip_serializing_if = "Option::is_none")]
48    swap_size: Option<u64>,
49    #[serde(skip_serializing_if = "Option::is_none")]
50    tags: Option<Vec<String>>,
51    #[serde(rename = "type")]
52    ltype: String,
53}
54
55impl LinodeCreateInstanceBuilder {
56    /// region: The Region where the Linode will be located.
57    ///
58    /// ltype: The Linode Type of the Linode you are creating.
59    pub fn new(api: LinodeApi, region: &str, ltype: &str) -> Self {
60        LinodeCreateInstanceBuilder {
61            api,
62            config: CreateInstanceConfig {
63                authorized_keys: None,
64                authorized_users: None,
65                backup_id: None,
66                backups_enabled: None,
67                booted: None,
68                firewall_id: None,
69                group: None,
70                image: None,
71                label: None,
72                metadata: None,
73                private_ip: None,
74                region: region.to_string(),
75                root_pass: None,
76                stackscript_data: None,
77                stackscript_id: None,
78                swap_size: None,
79                tags: None,
80                ltype: ltype.to_string(),
81            },
82        }
83    }
84
85    /// A list of public SSH keys that will be automatically appended to the root user’s
86    /// ~/.ssh/authorized_keys file when deploying from an Image.
87    pub fn authorized_keys(mut self, authorized_keys: Vec<String>) -> LinodeCreateInstanceBuilder {
88        self.config.authorized_keys = Some(authorized_keys);
89        self
90    }
91
92    /// A list of usernames. If the usernames have associated SSH keys, the keys
93    /// will be appended to the root users ~/.ssh/authorized_keys file automatically when deploying from an Image.
94    pub fn authorized_users(
95        mut self,
96        authorized_users: Vec<String>,
97    ) -> LinodeCreateInstanceBuilder {
98        self.config.authorized_users = Some(authorized_users);
99        self
100    }
101
102    /// A Backup ID from another Linode’s available backups. Your User must have read_write access
103    /// to that Linode, the Backup must have a status of successful, and the Linode must be deployed
104    /// to the same region as the Backup. See GET /linode/instances/{linodeId}/backups for a Linode’s available backups.
105    ///
106    /// This field and the image field are mutually exclusive.
107    pub fn backup_id(mut self, backup_id: u64) -> LinodeCreateInstanceBuilder {
108        self.config.backup_id = Some(backup_id);
109        self
110    }
111
112    /// If this field is set to true, the created Linode will automatically be enrolled in the
113    /// Linode Backup service. This will incur an additional charge. The cost for the Backup service
114    /// is dependent on the Type of Linode deployed.
115    ///
116    /// This option is always treated as true if the account-wide backups_enabled setting is true. See account settings for more information.
117    ///
118    /// Backup pricing is included in the response from /linodes/types
119    pub fn backups_enabled(mut self, backups_enabled: bool) -> LinodeCreateInstanceBuilder {
120        self.config.backups_enabled = Some(backups_enabled);
121        self
122    }
123
124    /// This field defaults to true if the Linode is created with an Image or from a Backup. If it is deployed
125    /// from an Image or a Backup and you wish it to remain offline after deployment, set this to false.
126    pub fn booted(mut self, booted: bool) -> LinodeCreateInstanceBuilder {
127        self.config.booted = Some(booted);
128        self
129    }
130
131    /// The id of the Firewall to attach this Linode to upon creation.
132    pub fn firewall_id(mut self, firewall_id: u64) -> LinodeCreateInstanceBuilder {
133        self.config.firewall_id = Some(firewall_id);
134        self
135    }
136
137    /// A deprecated property denoting a group label for this Linode.
138    pub fn group(mut self, group: &str) -> LinodeCreateInstanceBuilder {
139        self.config.group = Some(group.to_string());
140        self
141    }
142
143    /// An Image ID to deploy the Linode Disk from.
144    ///
145    /// Access the Images List (GET /images) endpoint with authentication to view all available Images.
146    /// Official Linode Images start with linode/, while your Account’s Images start with private/.
147    /// Creating a disk from a Private Image requires read_only or read_write permissions for that Image.
148    /// Access the User’s Grant Update (PUT /account/users/{username}/grants) endpoint to adjust permissions for an Account Image.
149    pub fn image(mut self, image: &str) -> LinodeCreateInstanceBuilder {
150        self.config.image = Some(image.to_string());
151        self
152    }
153
154    /// The Linode’s label is for display purposes only. If no label is provided for a Linode, a default will be assigned.
155    ///
156    /// Linode labels have the following constraints:
157    /// * 3..64 characters
158    /// * Must begin and end with an alphanumeric character.
159    /// * May only consist of alphanumeric characters, dashes (-), underscores (_) or periods (.).
160    /// * Cannot have two dashes (--), underscores (__) or periods (..) in a row.
161    pub fn label(mut self, label: &str) -> LinodeCreateInstanceBuilder {
162        self.config.label = Some(label.to_string());
163        self
164    }
165
166    /// Base64-encoded cloud-config data.
167    ///
168    /// Cannot be modified after provisioning. To update, use either the Linode Clone or Linode Rebuild commands.
169    ///
170    /// Must not be included when cloning to an existing Linode.
171    ///
172    /// Unencoded data must not exceed 65535 bytes, or about 16kb encoded.
173    pub fn user_data(mut self, user_data: &str) -> LinodeCreateInstanceBuilder {
174        self.config.metadata = Some(CreateInstanceConfigMetadata {
175            user_data: Some(user_data.to_string()),
176        });
177        self
178    }
179
180    /// If true, the created Linode will have private networking enabled and assigned a private IPv4 address.
181    pub fn private_ip(mut self, private_ip: bool) -> LinodeCreateInstanceBuilder {
182        self.config.private_ip = Some(private_ip);
183        self
184    }
185
186    /// If true, the created Linode will have private networking enabled and assigned a private IPv4 address.
187    pub fn root_pass(mut self, root_pass: &str) -> LinodeCreateInstanceBuilder {
188        self.config.root_pass = Some(root_pass.to_string());
189        self
190    }
191
192    /// Default: 512
193    ///
194    /// When deploying from an Image, this field is optional, otherwise it is ignored.
195    /// This is used to set the swap disk size for the newly-created Linode.
196    pub fn swap_size(mut self, swap_size: u64) -> LinodeCreateInstanceBuilder {
197        self.config.swap_size = Some(swap_size);
198        self
199    }
200
201    /// An array of tags applied to this object. Tags are for organizational purposes only.
202    pub fn tags(mut self, tags: Vec<String>) -> LinodeCreateInstanceBuilder {
203        self.config.tags = Some(tags);
204        self
205    }
206
207    #[cfg(feature = "blocking")]
208    pub fn run(self) -> Result<LinodeInstance, LinodeError> {
209        Ok(self
210            .api
211            .post("https://api.linode.com/v4/linode/instances", self.config)?
212            .json::<LinodeInstance>()?)
213    }
214
215    pub async fn run_async(self) -> Result<LinodeInstance, LinodeError> {
216        Ok(self
217            .api
218            .post_async("https://api.linode.com/v4/linode/instances", self.config)
219            .await?
220            .json::<LinodeInstance>()
221            .await?)
222    }
223}