Repository navigation
Expand file tree
/
Copy pathprojects.rs
More file actions
336 lines (314 loc) · 11.5 KB
/
Copy pathprojects.rs
File metadata and controls
336 lines (314 loc) · 11.5 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
use crate::database::{
auth_details, last_from_url, page_size, response_message, OpenApiConfig, ProjectDetails,
ProjectError, NO_PAGE_COUNT, NO_PAGE_SIZE,
};
use cloudtruth_restapi::apis::projects_api::*;
use cloudtruth_restapi::apis::Error::ResponseError;
use cloudtruth_restapi::models::{PatchedProjectUpdate, ProjectCopy, ProjectCreate};
use std::collections::HashMap;
use std::result::Result;
const NO_DESC_CONTAINS: Option<&str> = None;
const NO_NAME_CONTAINS: Option<&str> = None;
const NO_ORDERING: Option<&str> = None;
pub struct Projects {}
/// This is used to map from an Project's URL to the Name.
pub type ProjectUrlMap = HashMap<String, String>;
fn response_error(status: &reqwest::StatusCode, content: &str) -> ProjectError {
ProjectError::ResponseError(response_message(status, content))
}
fn auth_error(content: &str) -> ProjectError {
ProjectError::Authentication(auth_details(content))
}
impl Projects {
pub fn new() -> Self {
Self {}
}
/// This provides a means to get an entire list of project URLs to names.
pub fn get_url_name_map(&self, rest_cfg: &OpenApiConfig) -> ProjectUrlMap {
let mut result: ProjectUrlMap = ProjectUrlMap::new();
let mut page_count = 1;
loop {
let response = projects_list(
rest_cfg,
NO_DESC_CONTAINS,
None,
NO_NAME_CONTAINS,
NO_ORDERING,
Some(page_count),
page_size(rest_cfg),
);
if let Ok(data) = response {
if let Some(projects) = data.results {
for prj in projects {
result.insert(prj.url, prj.name);
}
page_count += 1;
} else {
break;
}
if data.next.is_none() || data.next.as_ref().unwrap().is_empty() {
break;
}
} else {
break;
}
}
result
}
/// Use the project URL to get the corresponding name.
pub fn get_name_from_url(&self, rest_cfg: &OpenApiConfig, url: &str) -> String {
let id = last_from_url(url);
if id.is_empty() {
"".to_owned()
} else {
let response = projects_retrieve(rest_cfg, id);
if let Ok(project) = response {
project.name
} else {
"".to_owned()
}
}
}
/// Get the details for `proj_name`
pub fn get_details_by_name(
&self,
rest_cfg: &OpenApiConfig,
proj_name: &str,
resolve_parent: bool,
) -> Result<Option<ProjectDetails>, ProjectError> {
let response = projects_list(
rest_cfg,
NO_DESC_CONTAINS,
Some(proj_name),
NO_NAME_CONTAINS,
NO_ORDERING,
NO_PAGE_COUNT,
NO_PAGE_SIZE,
);
match response {
Ok(data) => match data.results {
Some(list) => {
if list.is_empty() {
Ok(None)
} else {
// TODO: handle more than one??
let proj = &list[0];
let mut details = ProjectDetails::from(proj);
if resolve_parent {
details.parent_name =
self.get_name_from_url(rest_cfg, &details.parent_url);
}
Ok(Some(details))
}
}
_ => Ok(None),
},
Err(ResponseError(ref content)) => match content.status.as_u16() {
401 => Err(auth_error(&content.content)),
403 => Err(auth_error(&content.content)),
_ => Err(response_error(&content.status, &content.content)),
},
Err(e) => Err(ProjectError::UnhandledError(e.to_string())),
}
}
/// Resolve the `proj_name` to a String
pub fn get_id(
&self,
rest_cfg: &OpenApiConfig,
proj_name: &str,
) -> Result<Option<String>, ProjectError> {
if let Some(details) = self.get_details_by_name(rest_cfg, proj_name, false)? {
Ok(Some(details.id))
} else {
Ok(None)
}
}
/// Get a complete list of projects for this organization.
pub fn get_project_details(
&self,
rest_cfg: &OpenApiConfig,
) -> Result<Vec<ProjectDetails>, ProjectError> {
let mut projects: Vec<ProjectDetails> = vec![];
let mut url_map: ProjectUrlMap = ProjectUrlMap::new();
let mut page_count = 1;
loop {
let response = projects_list(
rest_cfg,
NO_DESC_CONTAINS,
None,
NO_NAME_CONTAINS,
NO_ORDERING,
Some(page_count),
page_size(rest_cfg),
);
match response {
Ok(data) => {
if let Some(list) = data.results {
for api_prj in list {
let details = ProjectDetails::from(&api_prj);
url_map.insert(details.url.clone(), details.name.clone());
projects.push(details);
}
page_count += 1;
} else {
break;
}
if data.next.is_none() || data.next.as_ref().unwrap().is_empty() {
break;
}
}
Err(ResponseError(ref content)) => match content.status.as_u16() {
401 => return Err(auth_error(&content.content)),
403 => return Err(auth_error(&content.content)),
_ => return Err(response_error(&content.status, &content.content)),
},
Err(e) => return Err(ProjectError::UnhandledError(e.to_string())),
}
}
// populate the parent names
for prj in &mut projects {
if !prj.parent_url.is_empty() {
// concurrent user updates can cause parent_url to not be present in url_map
// handle the case by setting parent_url and parent_name to None
if let Some(parent_name) = url_map.get(&prj.parent_url) {
prj.parent_name = parent_name.clone()
} else {
prj.parent_name = String::new();
prj.parent_url = String::new()
}
}
}
projects.sort_by(|l, r| l.name.cmp(&r.name));
Ok(projects)
}
#[allow(clippy::only_used_in_recursion)]
fn get_all_descendants_from_list(
&self,
parent_url: &str,
all_projects: &[ProjectDetails],
) -> Vec<ProjectDetails> {
let mut descendants = vec![];
for prj in all_projects {
if prj.parent_url == parent_url {
descendants.push(prj.clone());
// recursively get the children
let mut grandchildren = self.get_all_descendants_from_list(&prj.url, all_projects);
descendants.append(&mut grandchildren);
}
}
descendants
}
pub fn get_project_descendants(
&self,
rest_cfg: &OpenApiConfig,
parent_name: &str,
) -> Result<Vec<ProjectDetails>, ProjectError> {
let all_details = self.get_project_details(rest_cfg)?;
// start by finding the specified parent by name
let parent = all_details
.iter()
.find(|&d| d.name == parent_name)
.expect("No project found with that name")
.clone();
let descendants = self.get_all_descendants_from_list(&parent.url, &all_details);
Ok(descendants)
}
/// Create a project with the specified name/description
pub fn create_project(
&self,
rest_cfg: &OpenApiConfig,
proj_name: &str,
description: Option<&str>,
parent_url: Option<&str>,
parameter_name_pattern: Option<&str>,
) -> Result<Option<String>, ProjectError> {
let proj = ProjectCreate {
name: proj_name.to_string(),
description: description.map(String::from),
depends_on: parent_url.map(String::from),
parameter_name_pattern: parameter_name_pattern.map(String::from),
};
let response = projects_create(rest_cfg, proj);
match response {
// return the project id of the newly minted project
Ok(project) => Ok(Some(project.id)),
Err(ResponseError(ref content)) => {
Err(response_error(&content.status, &content.content))
}
Err(e) => Err(ProjectError::UnhandledError(e.to_string())),
}
}
/// Delete the specified project
pub fn delete_project(
&self,
rest_cfg: &OpenApiConfig,
project_id: &str,
) -> Result<Option<String>, ProjectError> {
let response = projects_destroy(rest_cfg, project_id);
match response {
Ok(_) => Ok(Some(project_id.to_string())),
Err(ResponseError(ref content)) => {
Err(response_error(&content.status, &content.content))
}
Err(e) => Err(ProjectError::UnhandledError(e.to_string())),
}
}
/// Update the specified project
pub fn update_project(
&self,
rest_cfg: &OpenApiConfig,
project_name: &str,
project_id: &str,
description: Option<&str>,
parent_url: Option<&str>,
parameter_name_pattern: Option<&str>,
) -> Result<Option<String>, ProjectError> {
let proj = PatchedProjectUpdate {
id: None,
name: Some(project_name.to_string()),
description: description.map(|d| d.to_string()),
created_at: None,
modified_at: None,
depends_on: parent_url.map(String::from),
access_controlled: None,
role: None,
parameter_name_pattern: parameter_name_pattern.map(String::from),
};
let response = projects_partial_update(rest_cfg, project_id, Some(proj));
match response {
Ok(project) => Ok(Some(project.id)),
Err(ResponseError(ref content)) => {
Err(response_error(&content.status, &content.content))
}
Err(e) => Err(ProjectError::UnhandledError(e.to_string())),
}
}
pub fn copy_project(
&self,
rest_cfg: &OpenApiConfig,
src_project_id: &str,
name: &str,
description: Option<&str>,
recursive: bool,
child_names: Option<HashMap<String, String>>,
) -> Result<String, ProjectError> {
let response = projects_copy_create(
rest_cfg,
src_project_id,
ProjectCopy {
name: name.to_owned(),
description: description.map(String::from),
child_project_names: child_names,
recursive: Some(recursive),
depends_on: None,
},
);
match response {
Ok(proj) => Ok(proj.id),
Err(ResponseError(ref content)) => {
Err(response_error(&content.status, &content.content))
}
Err(e) => Err(ProjectError::UnhandledError(e.to_string())),
}
}
}