|
| 1 | +/* |
| 2 | +Creates a batch job and task using the data plane APIs |
| 3 | +
|
| 4 | +This example shows how to do error handling when you wish |
| 5 | +to match any error from azure_svc_batch crate. |
| 6 | +
|
| 7 | +cargo run --package azure_svc_batch --example create_task_thiserror |
| 8 | +*/ |
| 9 | + |
| 10 | +use azure_identity::token_credentials::AzureCliCredential; |
| 11 | +use azure_svc_batch::models::{JobAddParameter, PoolInformation, TaskAddParameter}; |
| 12 | +use std::sync::Arc; |
| 13 | + |
| 14 | +#[derive(Debug, thiserror::Error)] |
| 15 | +pub enum Error { |
| 16 | + #[error("please specify batch account")] |
| 17 | + AccountNameRequired, |
| 18 | + #[error("please specify region")] |
| 19 | + RegionRequired, |
| 20 | + #[error("please specify pool id")] |
| 21 | + PoolIdRequired, |
| 22 | + #[error("please specify job id")] |
| 23 | + JobIdRequired, |
| 24 | + #[error("please specify task id")] |
| 25 | + TaskIdRequired, |
| 26 | + #[error("batch error")] |
| 27 | + Batch(#[source] azure_svc_batch::Error), |
| 28 | +} |
| 29 | + |
| 30 | +/// Any azure_svc_batch error ths is not mapped to a specific error |
| 31 | +/// is mapped to Error::Batch by default with a `?`, which performs an `into()`. |
| 32 | +impl<T: Into<azure_svc_batch::Error>> From<T> for Error { |
| 33 | + fn from(error: T) -> Self { |
| 34 | + Self::Batch(error.into()) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +async fn run() -> Result<(), Error> { |
| 39 | + let account_name = std::env::args().nth(1).ok_or(Error::AccountNameRequired)?; |
| 40 | + let region = std::env::args().nth(2).ok_or(Error::RegionRequired)?; |
| 41 | + let pool_id = std::env::args().nth(3).ok_or(Error::PoolIdRequired)?; |
| 42 | + let job_id = std::env::args().nth(4).ok_or(Error::JobIdRequired)?; |
| 43 | + let task_id = std::env::args().nth(5).ok_or(Error::TaskIdRequired)?; |
| 44 | + |
| 45 | + let endpoint = format!("https://{}.{}.batch.azure.com", account_name, region); |
| 46 | + let scopes = &["https://batch.core.windows.net/"]; |
| 47 | + let credential = Arc::new(AzureCliCredential {}); |
| 48 | + let client = azure_svc_batch::ClientBuilder::new(credential) |
| 49 | + .endpoint(endpoint) |
| 50 | + .scopes(scopes) |
| 51 | + .build(); |
| 52 | + |
| 53 | + let pool_info = PoolInformation { |
| 54 | + pool_id: Some(pool_id), |
| 55 | + ..PoolInformation::new() |
| 56 | + }; |
| 57 | + |
| 58 | + println!("creating job"); |
| 59 | + let job_params = JobAddParameter::new(job_id.clone(), pool_info); |
| 60 | + client.job().add(job_params).into_future().await?; |
| 61 | + |
| 62 | + println!("creating task"); |
| 63 | + let command_line = "echo hello there".to_string(); |
| 64 | + let task = TaskAddParameter::new(task_id.to_string(), command_line); |
| 65 | + client.task().add(job_id, task).into_future().await?; |
| 66 | + |
| 67 | + Ok(()) |
| 68 | +} |
| 69 | + |
| 70 | +/* |
| 71 | +The eyre crate is recommended for printing the report with the full error chain. |
| 72 | +You can use it instead of the below print_error_chain. |
| 73 | +
|
| 74 | +#[tokio::main] |
| 75 | +async fn main() -> eyre::Result<()> { |
| 76 | + Ok(run().await?) |
| 77 | +} |
| 78 | +*/ |
| 79 | + |
| 80 | +#[tokio::main] |
| 81 | +async fn main() { |
| 82 | + match run().await { |
| 83 | + Ok(_) => (), |
| 84 | + Err(error) => { |
| 85 | + print_error_chain(error); |
| 86 | + std::process::exit(1); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +fn print_error_chain(error: impl std::error::Error) { |
| 92 | + println!("- {}", error.to_string()); |
| 93 | + if let Some(source) = error.source() { |
| 94 | + print_error_chain(source); |
| 95 | + } |
| 96 | +} |
0 commit comments