-
Notifications
You must be signed in to change notification settings - Fork 1
/
keypair.rs
71 lines (66 loc) · 2.37 KB
/
keypair.rs
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
use outscale_api::apis::configuration::{AWSv4Key, Configuration};
use outscale_api::apis::keypair_api::{create_keypair, delete_keypair, read_keypairs};
use outscale_api::models::{
CreateKeypairRequest, DeleteKeypairRequest, FiltersKeypair, ReadKeypairsRequest,
};
use rand::Rng;
use secrecy::SecretString;
use std::env;
fn main() {
let mut config = Configuration::new();
config.aws_v4_key = Some(AWSv4Key {
access_key: env::var("OSC_ACCESS_KEY").unwrap(),
secret_key: SecretString::new(env::var("OSC_SECRET_KEY").unwrap()),
region: "eu-west-2".to_string(),
service: "oapi".to_string(),
});
match env::var("OSC_ENDPOINT_API") {
Ok(enpoint) => config.base_path = enpoint,
_ => (),
};
// Example reading all keypairs
print!("Reading all keypairs... ");
let request = ReadKeypairsRequest::new();
let response = match read_keypairs(&config, Some(request)) {
Err(error) => {
println!("Error: {:?}", error);
return;
}
Ok(resp) => resp,
};
if let Some(keypairs) = response.keypairs {
println!("OK -> there are {} keypairs", keypairs.len());
}
// Example creating a keypair
print!("Creating new keypair... ");
let mut rng = rand::thread_rng();
let keypair_name = format!("osc-sdk-rust-test-{}", rng.gen::<u64>());
let request = CreateKeypairRequest::new(keypair_name.clone());
match create_keypair(&config, Some(request)) {
Err(error) => {
println!("Error: {:?}", error);
return;
}
Ok(resp) => resp,
};
println!("OK -> created keypair {}", keypair_name);
// Filtering on newly created keypair
print!("Filtering on {}... ", keypair_name);
let mut filters = FiltersKeypair::new();
filters.keypair_names = Some(vec![keypair_name.clone()]);
let mut request = ReadKeypairsRequest::new();
request.filters = Some(Box::new(filters));
if let Err(error) = read_keypairs(&config, Some(request)) {
println!("Error: {:?}", error);
return;
}
println!("OK");
// Deleting a keypair
print!("Deleting keypair {}... ", keypair_name);
let request = DeleteKeypairRequest::new(keypair_name.clone());
if let Err(error) = delete_keypair(&config, Some(request)) {
println!("Error: {:?}", error);
return;
}
println!("OK");
}