Skip to content

Commit

Permalink
Add cli parser, Identify api_call functions
Browse files Browse the repository at this point in the history
  • Loading branch information
YannikSc committed Jul 3, 2024
1 parent 9b5ec44 commit 4dfa376
Show file tree
Hide file tree
Showing 9 changed files with 326 additions and 18 deletions.
85 changes: 85 additions & 0 deletions rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ ssh-key = { version = "0.6", features = ["serde", "ed25519", "dsa", "crypto", "r
[dev-dependencies]
env_logger = "0.11"
tokio = { version = "1.37", features = ["full"] }
clap = "4.5"
futures = "0.3"
26 changes: 8 additions & 18 deletions rust/examples/adminapi.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
use adminapi::api::new_object;
use adminapi::filter::*;
use adminapi::cli::parse_filter_args;
use adminapi::query::Query;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
env_logger::init();

let mut args = std::env::args();
args.next();

let filters = parse_filter_args(args)?;

let query = Query::builder()
.filter("hostname", regexp(".*payment-staging.*"))
.filter("os", not(empty()))
.filters(filters)
.restrict(["hostname", "responsible_admin", "os"])
.build();

// The fields are unused, but this example still should show, that you can have the query return structured data
#[allow(dead_code)]
#[derive(Clone, Debug, serde::Deserialize)]
struct MyServer {
hostname: String,
os: String,
responsible_admin: Vec<String>,
}

let servers = query.request_typed::<MyServer>().await?;
let servers = query.request().await?;
for server in servers.into_iter() {
println!("{server:#?}");
}

let obj = new_object("service_group").await?;

println!("{obj:#?}");

Ok(())
}
15 changes: 15 additions & 0 deletions rust/examples/parse_args.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use adminapi::cli::parse_filter_args;

pub fn main() {
let mut args = std::env::args();
args.next();

match parse_filter_args(args) {
Ok(arg) => {
println!("{arg:#?}");
}
Err(err) => {
eprintln!("{err:#?}");
}
}
}
64 changes: 64 additions & 0 deletions rust/examples/prepare_bookworm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
use adminapi::cli::parse_filter_args;
use adminapi::commit::AttributeValue;
use adminapi::query::Query;

#[tokio::main]
pub async fn main() -> anyhow::Result<()> {
let clap = clap::Command::new("prepare_bookworm")
.arg(clap::arg!([QUERY]).action(clap::ArgAction::Append).help("Query for the preparation").value_parser(clap::value_parser!(String)).num_args(1..).required(true))
.arg(clap::arg!(--"puppet-environment" <ENVIRONMENT> "Sets an optional puppet environment").value_parser(clap::value_parser!(String)).required(false))
.arg(clap::arg!(--"maintenance" "Sets the servers to maintenance").action(clap::ArgAction::SetTrue).required(false));

let matches = clap.get_matches();
let query = matches.get_many::<String>("QUERY").unwrap().cloned().collect::<Vec<_>>();
let filters = parse_filter_args(query.into_iter())?;

let query = Query::builder()
.filters(filters)
.restrict(["os", "repositories", "puppet_environment", "state"])
.build();
let response = query.request().await?;
let mut updates = vec![];

for mut server in response.all() {
let AttributeValue::String(base_os) = server.get("os") else {
return Err(anyhow::anyhow!("Unexpected value for os"));
};

server.set("os", "bookworm")?;
let AttributeValue::Array(repos) = server.get("repositories") else {
return Err(anyhow::anyhow!("Unexpected value for repositories"));
};

for repo in repos {
let AttributeValue::String(repo) = repo else {
return Err(anyhow::anyhow!("Unexpected value for repository"));
};

if !repo.contains(&base_os) {
continue;
}

server.add("repositories", repo.replace(&base_os, "bookworm"))?;
server.remove("repositories", repo)?;

if matches.get_flag("maintenance") {
server.set("state", "maintenance")?;
}

if let Some(environment) = matches.get_one::<String>("puppet-environment") {
if !server.get("puppet_environment").is_null() {
return Err(anyhow::anyhow!("Puppet environment is already set. Aborting!"));
}

server.set("puppet_environment", environment.clone())?;
}
}

updates.push(async move { server.commit().await });
}

futures::future::try_join_all(updates).await?;

Ok(())
}
Loading

0 comments on commit 4dfa376

Please sign in to comment.