Skip to content

Commit 9a62274

Browse files
committed
feat: add http requests to add and delete a hsm group
1 parent 864799c commit 9a62274

File tree

1 file changed

+118
-119
lines changed

1 file changed

+118
-119
lines changed

src/hsm/mod.rs

Lines changed: 118 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ pub mod service {
1616
}
1717

1818
pub mod http_client {
19-
use serde_json::Value;
2019

21-
use crate::{error::Error, hsm::group::r#struct::HsmGroup};
20+
use crate::error::Error;
2221

2322
use super::r#struct::Role;
2423

@@ -113,13 +112,13 @@ pub mod group {
113112
/// Constructor
114113
pub fn new(
115114
label: &str,
116-
member_vec_opt: Option<Vec<&str>>,
115+
member_vec_opt: Option<Vec<String>>,
117116
tag_vec_opt: Option<Vec<String>>,
118117
exclusive_opt: Option<String>,
119118
) -> Self {
120119
let members_opt = if let Some(member_vec) = member_vec_opt {
121120
Some(Member {
122-
ids: Some(member_vec.iter().map(|&id| id.to_string()).collect()),
121+
ids: Some(member_vec.iter().map(|id| id.to_string()).collect()),
123122
})
124123
} else {
125124
None
@@ -308,7 +307,7 @@ pub mod group {
308307

309308
log::debug!("Response:\n{:#?}", response);
310309

311-
if let Err(e) = response.error_for_status_ref() {
310+
if let Err(_e) = response.error_for_status_ref() {
312311
match response.status() {
313312
reqwest::StatusCode::UNAUTHORIZED => {
314313
return Err(Error::Message(response.text().await?));
@@ -327,6 +326,86 @@ pub mod group {
327326
.map_err(|error| Error::NetError(error))
328327
}
329328

329+
/// https://github.com/Cray-HPE/docs-csm/blob/release/1.5/api/smd.md#post-groups
330+
pub async fn create_new_hsm_group(
331+
shasta_token: &str,
332+
shasta_base_url: &str,
333+
shasta_root_cert: &[u8],
334+
hsm_group_name_opt: &str, // label in HSM
335+
xnames: &[String],
336+
exclusive: &str,
337+
description: &str,
338+
tags: &[String],
339+
) -> Result<Vec<HsmGroup>, reqwest::Error> {
340+
let client;
341+
342+
let client_builder = reqwest::Client::builder()
343+
.add_root_certificate(reqwest::Certificate::from_pem(shasta_root_cert)?);
344+
345+
// Build client
346+
if std::env::var("SOCKS5").is_ok() {
347+
// socks5 proxy
348+
log::debug!("SOCKS5 enabled");
349+
let socks5proxy = reqwest::Proxy::all(std::env::var("SOCKS5").unwrap())?;
350+
351+
// rest client to authenticate
352+
client = client_builder.proxy(socks5proxy).build()?;
353+
} else {
354+
client = client_builder.build()?;
355+
}
356+
// Example body to create a new group:
357+
// {
358+
// "label": "blue",
359+
// "description": "This is the blue group",
360+
// "tags": [
361+
// "optional_tag1",
362+
// "optional_tag2"
363+
// ],
364+
// "exclusiveGroup": "optional_excl_group",
365+
// "members": {
366+
// "ids": [
367+
// "x1c0s1b0n0",
368+
// "x1c0s1b0n1",
369+
// "x1c0s2b0n0",
370+
// "x1c0s2b0n1"
371+
// ]
372+
// }
373+
// }
374+
// Describe the JSON object
375+
376+
// Create the variables that represent our JSON object
377+
let myxnames = Member {
378+
ids: Some(xnames.to_owned()),
379+
};
380+
381+
let hsm_group_json = HsmGroup {
382+
label: hsm_group_name_opt.to_owned(),
383+
description: Option::from(description.to_string().clone()),
384+
tags: Option::from(tags.to_owned()),
385+
exclusive_group: Option::from(exclusive.to_string().clone()),
386+
members: Some(myxnames),
387+
};
388+
389+
let hsm_group_json_body = match serde_json::to_string(&hsm_group_json) {
390+
Ok(m) => m,
391+
Err(_) => panic!("Error parsing the JSON generated, one or more of the fields could have invalid chars."),
392+
};
393+
394+
println!("{:#?}", &hsm_group_json_body);
395+
396+
let url_api = shasta_base_url.to_owned() + "/smd/hsm/v2/groups";
397+
398+
client
399+
.post(url_api)
400+
.header("Authorization", format!("Bearer {}", shasta_token))
401+
.json(&hsm_group_json) // make sure this is not a string!
402+
.send()
403+
.await?
404+
.error_for_status()?
405+
.json()
406+
.await
407+
}
408+
330409
pub async fn delete(
331410
shasta_token: &str,
332411
shasta_base_url: &str,
@@ -373,14 +452,12 @@ pub mod group {
373452
}
374453
}
375454

376-
pub async fn post_member(
455+
pub async fn delete_hsm_group(
377456
shasta_token: &str,
378457
shasta_base_url: &str,
379458
shasta_root_cert: &[u8],
380-
hsm_group_name: &str,
381-
member_id: &str,
382-
) -> Result<(), reqwest::Error> {
383-
log::info!("Add member {}/{}", hsm_group_name, member_id);
459+
hsm_group_name_opt: &String, // label in HSM
460+
) -> Result<String, reqwest::Error> {
384461
let client;
385462

386463
let client_builder = reqwest::Client::builder()
@@ -397,35 +474,26 @@ pub mod group {
397474
} else {
398475
client = client_builder.build()?;
399476
}
400-
401-
let api_url: String =
402-
shasta_base_url.to_owned() + "/smd/hsm/v2/groups/" + hsm_group_name + "/members";
403-
404-
let xname = XnameId {
405-
id: Some(member_id.to_owned()),
406-
};
477+
let url_api = shasta_base_url.to_owned() + "/smd/hsm/v2/groups/" + &hsm_group_name_opt;
407478

408479
client
409-
.post(api_url)
480+
.delete(url_api)
410481
.header("Authorization", format!("Bearer {}", shasta_token))
411-
.json(&xname) // make sure this is not a string!
412482
.send()
413483
.await?
414-
.error_for_status()?;
415-
// TODO Parse the output!!!
416-
// TODO add some debugging output
417-
418-
Ok(())
484+
.error_for_status()?
485+
.json()
486+
.await
419487
}
420488

421-
pub async fn delete_member(
489+
pub async fn post_member(
422490
shasta_token: &str,
423491
shasta_base_url: &str,
424492
shasta_root_cert: &[u8],
425493
hsm_group_name: &str,
426494
member_id: &str,
427495
) -> Result<(), reqwest::Error> {
428-
log::info!("Delete member {}/{}", hsm_group_name, member_id);
496+
log::info!("Add member {}/{}", hsm_group_name, member_id);
429497
let client;
430498

431499
let client_builder = reqwest::Client::builder()
@@ -443,35 +511,34 @@ pub mod group {
443511
client = client_builder.build()?;
444512
}
445513

446-
let api_url: String = shasta_base_url.to_owned()
447-
+ "/smd/hsm/v2/groups/"
448-
+ hsm_group_name
449-
+ "/members/"
450-
+ member_id;
514+
let api_url: String =
515+
shasta_base_url.to_owned() + "/smd/hsm/v2/groups/" + hsm_group_name + "/members";
516+
517+
let xname = XnameId {
518+
id: Some(member_id.to_owned()),
519+
};
451520

452521
client
453-
.delete(api_url)
522+
.post(api_url)
454523
.header("Authorization", format!("Bearer {}", shasta_token))
524+
.json(&xname) // make sure this is not a string!
455525
.send()
456526
.await?
457527
.error_for_status()?;
458-
459528
// TODO Parse the output!!!
460529
// TODO add some debugging output
530+
461531
Ok(())
462532
}
463533

464-
/// https://github.com/Cray-HPE/docs-csm/blob/release/1.5/api/smd.md#post-groups
465-
pub async fn create_new_hsm_group(
534+
pub async fn delete_member(
466535
shasta_token: &str,
467536
shasta_base_url: &str,
468537
shasta_root_cert: &[u8],
469-
hsm_group_name_opt: &str, // label in HSM
470-
xnames: &[String],
471-
exclusive: &str,
472-
description: &str,
473-
tags: &[String],
474-
) -> Result<Vec<HsmGroup>, reqwest::Error> {
538+
hsm_group_name: &str,
539+
member_id: &str,
540+
) -> Result<(), reqwest::Error> {
541+
log::info!("Delete member {}/{}", hsm_group_name, member_id);
475542
let client;
476543

477544
let client_builder = reqwest::Client::builder()
@@ -488,91 +555,23 @@ pub mod group {
488555
} else {
489556
client = client_builder.build()?;
490557
}
491-
// Example body to create a new group:
492-
// {
493-
// "label": "blue",
494-
// "description": "This is the blue group",
495-
// "tags": [
496-
// "optional_tag1",
497-
// "optional_tag2"
498-
// ],
499-
// "exclusiveGroup": "optional_excl_group",
500-
// "members": {
501-
// "ids": [
502-
// "x1c0s1b0n0",
503-
// "x1c0s1b0n1",
504-
// "x1c0s2b0n0",
505-
// "x1c0s2b0n1"
506-
// ]
507-
// }
508-
// }
509-
// Describe the JSON object
510-
511-
// Create the variables that represent our JSON object
512-
let myxnames = Member {
513-
ids: Some(xnames.to_owned()),
514-
};
515-
516-
let hsm_group_json = HsmGroup {
517-
label: hsm_group_name_opt.to_owned(),
518-
description: Option::from(description.to_string().clone()),
519-
tags: Option::from(tags.to_owned()),
520-
exclusive_group: Option::from(exclusive.to_string().clone()),
521-
members: Some(myxnames),
522-
};
523-
524-
let hsm_group_json_body = match serde_json::to_string(&hsm_group_json) {
525-
Ok(m) => m,
526-
Err(_) => panic!("Error parsing the JSON generated, one or more of the fields could have invalid chars."),
527-
};
528-
529-
println!("{:#?}", &hsm_group_json_body);
530558

531-
let url_api = shasta_base_url.to_owned() + "/smd/hsm/v2/groups";
559+
let api_url: String = shasta_base_url.to_owned()
560+
+ "/smd/hsm/v2/groups/"
561+
+ hsm_group_name
562+
+ "/members/"
563+
+ member_id;
532564

533565
client
534-
.post(url_api)
566+
.delete(api_url)
535567
.header("Authorization", format!("Bearer {}", shasta_token))
536-
.json(&hsm_group_json) // make sure this is not a string!
537568
.send()
538569
.await?
539-
.error_for_status()?
540-
.json()
541-
.await
542-
}
543-
544-
pub async fn delete_hsm_group(
545-
shasta_token: &str,
546-
shasta_base_url: &str,
547-
shasta_root_cert: &[u8],
548-
hsm_group_name_opt: &String, // label in HSM
549-
) -> Result<String, reqwest::Error> {
550-
let client;
551-
552-
let client_builder = reqwest::Client::builder()
553-
.add_root_certificate(reqwest::Certificate::from_pem(shasta_root_cert)?);
554-
555-
// Build client
556-
if std::env::var("SOCKS5").is_ok() {
557-
// socks5 proxy
558-
log::debug!("SOCKS5 enabled");
559-
let socks5proxy = reqwest::Proxy::all(std::env::var("SOCKS5").unwrap())?;
560-
561-
// rest client to authenticate
562-
client = client_builder.proxy(socks5proxy).build()?;
563-
} else {
564-
client = client_builder.build()?;
565-
}
566-
let url_api = shasta_base_url.to_owned() + "/smd/hsm/v2/groups/" + &hsm_group_name_opt;
570+
.error_for_status()?;
567571

568-
client
569-
.delete(url_api)
570-
.header("Authorization", format!("Bearer {}", shasta_token))
571-
.send()
572-
.await?
573-
.error_for_status()?
574-
.json()
575-
.await
572+
// TODO Parse the output!!!
573+
// TODO add some debugging output
574+
Ok(())
576575
}
577576
}
578577

0 commit comments

Comments
 (0)