diff --git a/Cargo.lock b/Cargo.lock index a50d70b..16025e5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1004,26 +1004,6 @@ version = "0.4.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fbdcdcb6d86f71c5e97409ad45898af11cbc995b4ee8112d59095a28d376c935" -[[package]] -name = "const_format" -version = "0.2.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e3a214c7af3d04997541b18d432afaff4c455e79e2029079647e72fc2bd27673" -dependencies = [ - "const_format_proc_macros", -] - -[[package]] -name = "const_format_proc_macros" -version = "0.2.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7f6ff08fd20f4f299298a28e2dfa8a8ba1036e6cd2460ac1de7b425d76f2500" -dependencies = [ - "proc-macro2", - "quote", - "unicode-xid", -] - [[package]] name = "convert_case" version = "0.4.0" @@ -2453,7 +2433,6 @@ dependencies = [ "base64 0.13.1", "bzip2", "clap", - "const_format", "data-encoding", "directories", "env_logger", diff --git a/Cargo.toml b/Cargo.toml index 359c04a..c625a3e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,7 +20,6 @@ azure_storage_blobs = { git = "https://github.com/Azure/azure-sdk-for-rust.git", base64 = "0.13" bzip2 = "0.4" clap = { version = "4.0", features = ["derive"] } -const_format = "0.2" directories = "5.0" env_logger = "0.8" filemagic = "0.12" diff --git a/src/device_update.rs b/src/device_update.rs index 296ef55..e9f1b96 100644 --- a/src/device_update.rs +++ b/src/device_update.rs @@ -268,8 +268,9 @@ pub async fn import_update( debug!("import update: {import_update}"); - let import_update_response = client.import_update(instance_id, import_update).await?; - info!("Result of import update: {:?}", &import_update_response); + client.import_update(instance_id, import_update).await?; + + info!("Import update succeeded"); Ok(()) } diff --git a/src/device_update_client/mod.rs b/src/device_update_client/mod.rs index f037668..d6e457f 100644 --- a/src/device_update_client/mod.rs +++ b/src/device_update_client/mod.rs @@ -1,11 +1,11 @@ +use anyhow::Result; use azure_core::{ auth::{AccessToken, TokenCredential}, error::{Error, ErrorKind, ResultExt}, - from_json, sleep, Url, + from_json, Url, }; -use const_format::formatcp; use getset::Getters; -use log::debug; +use log::{debug, error}; use serde::de::DeserializeOwned; use serde::Deserialize; use serde_json::Value; @@ -13,8 +13,7 @@ use std::sync::Arc; use std::time::Duration; use time::OffsetDateTime; -pub(crate) const API_VERSION: &str = "2022-10-01"; -pub(crate) const API_VERSION_PARAM: &str = formatcp!("api-version={}", API_VERSION); +const API_VERSION: &str = "api-version=2022-10-01"; #[derive(Debug, Deserialize, Getters)] #[getset(get = "pub")] @@ -61,8 +60,8 @@ pub struct UpdateOperation { #[derive(Clone)] pub struct DeviceUpdateClient { - pub(crate) device_update_url: Url, - pub(crate) token_credential: Arc, + device_update_url: Url, + token_credential: Arc, } impl DeviceUpdateClient { @@ -78,7 +77,7 @@ impl DeviceUpdateClient { pub fn new( device_update_url: &str, token_credential: Arc, - ) -> azure_core::Result { + ) -> Result { let device_update_url = Url::parse(device_update_url) .with_context(ErrorKind::DataConversion, || { format!("failed to parse update url: {device_update_url}") @@ -93,40 +92,30 @@ impl DeviceUpdateClient { /// Import new update version. /// `POST https://{endpoint}/deviceupdate/{instanceId}/updates?action=import&api-version=2021-06-01-preview` - pub async fn import_update( - &self, - instance_id: &str, - import_json: String, - ) -> azure_core::Result { + pub async fn import_update(&self, instance_id: &str, import_json: String) -> Result<()> { let mut uri = self.device_update_url.clone(); let path = format!("deviceupdate/{instance_id}/updates:import"); uri.set_path(&path); - uri.set_query(Some(API_VERSION_PARAM)); + uri.set_query(Some(API_VERSION)); debug!("Import request: {}", &uri); let resp_body = self.post(uri.to_string(), Some(import_json)).await?; debug!("Import response: {}", &resp_body); loop { - sleep(Duration::from_secs(5)).await; + tokio::time::sleep(Duration::from_secs(5)).await; let mut uri = self.device_update_url.clone(); uri.set_path(&resp_body); debug!("Requesting operational status: {}", &uri); - let update_operation: UpdateOperation = self.get(uri.to_string()).await.expect("error while getting operation status"); - - match update_operation.status { - OperationStatus::Failed => { - return Err(Error::with_message(ErrorKind::Other, { - || { - format!( - "import unsuccessful with status failed. status: {:?}", - update_operation.status - ) - } - })) - } - OperationStatus::Succeeded => return Ok(update_operation), - OperationStatus::NotStarted | OperationStatus::Running => continue, + + match self.get::(uri.to_string()).await { + Ok(op) => match op.status { + OperationStatus::Succeeded => return Ok(()), + OperationStatus::Failed => anyhow::bail!("Import failed"), + OperationStatus::NotStarted => debug!("Import not started"), + OperationStatus::Running => debug!("Import running"), + }, + Err(e) => error!("Error while requesting operational status: {e}"), } } } @@ -143,7 +132,7 @@ impl DeviceUpdateClient { let mut uri = self.device_update_url.clone(); let path = format!("deviceupdate/{instance_id}/updates/providers/{provider}/names/{name}/versions/{version}"); uri.set_path(&path); - uri.set_query(Some(API_VERSION_PARAM)); + uri.set_query(Some(API_VERSION)); self.delete(uri.to_string()).await } @@ -155,7 +144,7 @@ impl DeviceUpdateClient { .context(ErrorKind::Credential, "get token failed") } - pub(crate) async fn get(&self, uri: String) -> azure_core::Result + async fn get(&self, uri: String) -> azure_core::Result where R: DeserializeOwned, { @@ -171,14 +160,11 @@ impl DeviceUpdateClient { let body = resp.bytes().await.with_context(ErrorKind::Io, || { format!("failed to read response body text. uri: {uri}") })?; + debug!("{:?}", body); from_json(&body) } - pub(crate) async fn post( - &self, - uri: String, - json_body: Option, - ) -> azure_core::Result { + async fn post(&self, uri: String, json_body: Option) -> azure_core::Result { let mut req = reqwest::Client::new() .post(&uri) .bearer_auth(self.get_token().await?.token.secret()); @@ -212,7 +198,7 @@ impl DeviceUpdateClient { })) } - pub(crate) async fn delete(&self, uri: String) -> azure_core::Result { + async fn delete(&self, uri: String) -> azure_core::Result { let resp = reqwest::Client::new() .delete(&uri) .bearer_auth(self.get_token().await?.token.secret())