scaleway_rs/builder/
create_instance_builder.rs1use crate::{data::instance::ScalewayInstanceRoot, ScalewayApi, ScalewayError, ScalewayInstance};
2use serde::Serialize;
3
4pub struct ScalewayCreateInstanceBuilder {
5 api: ScalewayApi,
6 zone: String,
7 config: CreateInstanceConfig,
8}
9
10#[derive(Serialize, Debug)]
11struct CreateInstanceConfig {
12 name: String,
13 commercial_type: String,
14 #[serde(skip_serializing_if = "Option::is_none")]
15 routed_ip_enabled: Option<bool>,
16 #[serde(skip_serializing_if = "Option::is_none")]
17 image: Option<String>,
18 #[serde(skip_serializing_if = "Option::is_none")]
19 boot_type: Option<String>,
20 #[serde(skip_serializing_if = "Option::is_none")]
21 bootscript: Option<String>,
22 #[serde(skip_serializing_if = "Option::is_none")]
23 project: Option<String>,
24 #[serde(skip_serializing_if = "Option::is_none")]
25 public_ip: Option<String>,
26 #[serde(skip_serializing_if = "Option::is_none")]
27 enable_ipv6: Option<bool>,
28 #[serde(skip_serializing_if = "Option::is_none")]
29 tags: Option<Vec<String>>,
30 #[serde(skip_serializing_if = "Option::is_none")]
31 security_group: Option<String>,
32 #[serde(skip_serializing_if = "Option::is_none")]
33 placement_group: Option<String>,
34}
35
36impl ScalewayCreateInstanceBuilder {
37 pub fn new(api: ScalewayApi, zone: &str, name: &str, commercial_type: &str) -> Self {
38 ScalewayCreateInstanceBuilder {
39 api,
40 zone: zone.to_string(),
41 config: CreateInstanceConfig {
42 name: name.to_string(),
43 commercial_type: commercial_type.to_string(),
44 routed_ip_enabled: None,
45 image: None,
46 boot_type: None,
47 bootscript: None,
48 enable_ipv6: None,
49 public_ip: None,
50 project: None,
51 tags: None,
52 security_group: None,
53 placement_group: None,
54 },
55 }
56 }
57
58 pub fn dynamic_ip_required(mut self, b: bool) -> ScalewayCreateInstanceBuilder {
60 self.config.routed_ip_enabled = Some(b);
61 self
62 }
63
64 pub fn routed_ip_enabled(mut self, routed_ip_enabled: bool) -> ScalewayCreateInstanceBuilder {
66 self.config.routed_ip_enabled = Some(routed_ip_enabled);
67 self
68 }
69
70 pub fn image(mut self, image: &str) -> ScalewayCreateInstanceBuilder {
72 self.config.image = Some(image.to_string());
73 self
74 }
75
76 pub fn boot_type(mut self, boot_type: &str) -> ScalewayCreateInstanceBuilder {
80 self.config.boot_type = Some(boot_type.to_string());
81 self
82 }
83
84 pub fn bootscript(mut self, bootscript: &str) -> ScalewayCreateInstanceBuilder {
86 self.config.bootscript = Some(bootscript.to_string());
87 self
88 }
89
90 pub fn enable_ipv6(mut self, enable_ipv6: bool) -> ScalewayCreateInstanceBuilder {
92 self.config.enable_ipv6 = Some(enable_ipv6);
93 self
94 }
95
96 pub fn public_ip(mut self, public_ip: &str) -> ScalewayCreateInstanceBuilder {
98 self.config.public_ip = Some(public_ip.to_string());
99 self
100 }
101
102 pub fn project(mut self, project: &str) -> ScalewayCreateInstanceBuilder {
104 self.config.project = Some(project.to_string());
105 self
106 }
107
108 pub fn tags(mut self, tags: Vec<String>) -> Self {
110 self.config.tags = Some(tags);
111 self
112 }
113
114 pub fn security_group(mut self, security_group: &str) -> ScalewayCreateInstanceBuilder {
116 self.config.security_group = Some(security_group.to_string());
117 self
118 }
119
120 pub fn placement_group(mut self, placement_group: &str) -> ScalewayCreateInstanceBuilder {
122 self.config.placement_group = Some(placement_group.to_string());
123 self
124 }
125
126 #[cfg(feature = "blocking")]
127 pub fn run(self) -> Result<ScalewayInstance, ScalewayError> {
128 let url = format!(
129 "https://api.scaleway.com/instance/v1/zones/{zone}/servers",
130 zone = self.zone
131 );
132 Ok(self
133 .api
134 .post(&url, self.config)?
135 .json::<ScalewayInstanceRoot>()?
136 .server)
137 }
138
139 pub async fn run_async(self) -> Result<ScalewayInstance, ScalewayError> {
140 let url = format!(
141 "https://api.scaleway.com/instance/v1/zones/{zone}/servers",
142 zone = self.zone
143 );
144 Ok(self
145 .api
146 .post_async(&url, self.config)
147 .await?
148 .json::<ScalewayInstanceRoot>()
149 .await?
150 .server)
151 }
152}