1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
use reqwest::Url;
use seaplane::{
api::{
compute::v1::{
ActiveConfigurations as ActiveConfigurationsModel, Container as ContainerModel,
Containers as ContainersModel, FormationConfiguration as FormationConfigurationModel,
FormationMetadata as FormationMetadataModel, FormationNames as FormationNamesModel,
FormationsRequest,
},
identity::v0::AccessToken,
ApiErrorKind,
},
error::SeaplaneError,
};
use uuid::Uuid;
use crate::{
api::request_token,
context::Ctx,
error::{CliError, Context, Result},
ops::formation::{Formation, FormationConfiguration, Formations},
printer::{Color, Pb},
};
#[derive(Debug)]
pub struct FormationsReq {
api_key: String,
name: Option<String>,
token: Option<AccessToken>,
inner: Option<FormationsRequest>,
identity_url: Option<Url>,
compute_url: Option<Url>,
insecure_urls: bool,
invalid_certs: bool,
}
impl FormationsReq {
pub fn new<S: Into<String>>(ctx: &Ctx, name: Option<S>) -> Result<Self> {
let mut this = Self::new_delay_token(ctx)?;
this.name = name.map(Into::into);
this.refresh_token()?;
Ok(this)
}
pub fn new_delay_token(ctx: &Ctx) -> Result<Self> {
Ok(Self {
api_key: ctx.args.api_key()?.into(),
name: None,
token: None,
inner: None,
identity_url: ctx.identity_url.clone(),
compute_url: ctx.compute_url.clone(),
#[cfg(feature = "allow_insecure_urls")]
insecure_urls: ctx.insecure_urls,
#[cfg(not(feature = "allow_insecure_urls"))]
insecure_urls: false,
#[cfg(feature = "allow_invalid_certs")]
invalid_certs: ctx.invalid_certs,
#[cfg(not(feature = "allow_invalid_certs"))]
invalid_certs: false,
})
}
pub fn refresh_token(&mut self) -> Result<()> {
self.token = Some(request_token(
&self.api_key,
self.identity_url.as_ref(),
self.insecure_urls,
self.invalid_certs,
)?);
Ok(())
}
fn refresh_inner(&mut self) -> Result<()> {
let mut builder = FormationsRequest::builder().token(self.token_or_refresh()?);
#[cfg(feature = "allow_insecure_urls")]
{
builder = builder.allow_http(self.insecure_urls);
}
#[cfg(feature = "allow_invalid_certs")]
{
builder = builder.allow_invalid_certs(self.invalid_certs);
}
if let Some(url) = &self.compute_url {
builder = builder.base_url(url);
}
if let Some(name) = &self.name {
builder = builder.name(name);
}
self.inner = Some(builder.build().map_err(CliError::from)?);
Ok(())
}
pub fn token_or_refresh(&mut self) -> Result<&str> {
if self.token.is_none() {
self.refresh_token()?;
}
Ok(&self.token.as_ref().unwrap().token)
}
pub fn set_name<S: Into<String>>(&mut self, name: S) -> Result<()> {
self.name = Some(name.into());
self.refresh_inner()
}
pub fn get_all_formations<S: AsRef<str>>(
&mut self,
formation_names: &[S],
pb: &Pb,
) -> Result<Formations> {
let mut formations = Formations::default();
for name in formation_names {
let name = name.as_ref();
self.set_name(name)?;
pb.set_message(format!("Syncing Formation {name}..."));
let mut formation = Formation::new(name);
let cfg_uuids = self
.list_configuration_ids()
.context("Context: failed to retrieve Formation Configuration IDs\n")?;
let active_cfgs = self
.get_active_configurations()
.context("Context: failed to retrieve Active Formation Configurations\n")?;
pb.set_message(format!("Syncing Formation {name} Configurations..."));
for uuid in cfg_uuids.into_iter() {
let cfg_model = self
.get_configuration(uuid)
.context("Context: failed to retrieve Formation Configuration\n\tUUID: ")
.with_color_context(|| (Color::Yellow, format!("{uuid}\n")))?;
let cfg = FormationConfiguration::with_uuid(uuid, cfg_model);
let is_active = active_cfgs.iter().any(|ac| ac.uuid() == &uuid);
formation.local.insert(cfg.id);
if is_active {
formation.in_air.insert(cfg.id);
} else {
formation.grounded.insert(cfg.id);
}
formations.configurations.push(cfg);
}
if !formation.is_empty() {
formations.formations.push(formation);
}
}
Ok(formations)
}
pub fn get_formation_names(&mut self) -> Result<Vec<String>> {
Ok(if let Some(name) = &self.name {
vec![name.to_owned()]
} else {
self.list_names()
.context("Context: failed to retrieve Formation Instance names\n")?
.into_inner()
})
}
}
impl FormationsReq {
pub fn list_names(&mut self) -> Result<FormationNamesModel> { maybe_retry!(self.list_names()) }
pub fn get_metadata(&mut self) -> Result<FormationMetadataModel> {
maybe_retry!(self.get_metadata())
}
pub fn create(
&mut self,
configuration: &FormationConfigurationModel,
active: bool,
) -> Result<Vec<Uuid>> {
maybe_retry!(self.create(configuration, active))
}
pub fn clone_from(&mut self, source_name: &str, active: bool) -> Result<Vec<Uuid>> {
maybe_retry!(self.clone_from(source_name, active))
}
pub fn delete(&mut self, force: bool) -> Result<Vec<Uuid>> { maybe_retry!(self.delete(force)) }
pub fn get_active_configurations(&mut self) -> Result<ActiveConfigurationsModel> {
maybe_retry!(self.get_active_configurations())
}
pub fn stop(&mut self) -> Result<()> { maybe_retry!(self.stop()) }
pub fn set_active_configurations(
&mut self,
configs: &ActiveConfigurationsModel,
force: bool,
) -> Result<()> {
maybe_retry!(self.set_active_configurations(configs, force))
}
pub fn get_containers(&mut self) -> Result<ContainersModel> {
maybe_retry!(self.get_containers())
}
pub fn get_container(&mut self, container_id: Uuid) -> Result<ContainerModel> {
maybe_retry!(self.get_container(container_id))
}
pub fn get_configuration(&mut self, uuid: Uuid) -> Result<FormationConfigurationModel> {
maybe_retry!(self.get_configuration(uuid))
}
pub fn list_configuration_ids(&mut self) -> Result<Vec<Uuid>> {
maybe_retry!(self.list_configuration_ids())
}
pub fn remove_configuration(&mut self, uuid: Uuid, force: bool) -> Result<Uuid> {
maybe_retry!(self.remove_configuration(uuid, force))
}
pub fn add_configuration(
&mut self,
configuration: &FormationConfigurationModel,
active: bool,
) -> Result<Uuid> {
maybe_retry!(self.add_configuration(configuration, active))
}
}