-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cli parser, Identify api_call functions
- Loading branch information
Showing
9 changed files
with
326 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:#?}"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
Oops, something went wrong.