Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix azure_storage_queue set_blob_service_properties #1498

Merged
merged 1 commit into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion sdk/core/src/xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::error::{ErrorKind, ResultExt};
pub use quick_xml::serde_helpers::text_content;
use quick_xml::{
de::{from_reader, from_str},
se::to_string,
se::{to_string, to_string_with_root},
};
use serde::de::DeserializeOwned;

Expand Down Expand Up @@ -42,6 +42,16 @@ where
})
}

pub fn to_xml_with_root<T>(root_tag: &str, value: &T) -> crate::Result<String>
where
T: serde::Serialize,
{
to_string_with_root(root_tag, value).with_context(ErrorKind::DataConversion, || {
let t = core::any::type_name::<T>();
format!("failed to serialize {t} into xml")
})
}

/// Returns bytes without the UTF-8 BOM.
fn slice_bom(bytes: &[u8]) -> &[u8] {
if bytes.len() > 3 && bytes[0..3] == UTF8_BOM {
Expand Down
5 changes: 4 additions & 1 deletion sdk/storage_queues/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@ uuid = { version = "1.0", features = ["v4"] }
[dev-dependencies]
tokio = { version = "1.0", features = ["macros", "rt-multi-thread"] }
uuid = { version = "1.0", features = ["v4"] }
env_logger = "0.10"

[features]
default = ["enable_reqwest"]
default = ["enable_reqwest", "hmac_rust"]
test_e2e = []
enable_reqwest = ["azure_core/enable_reqwest", "azure_storage/enable_reqwest"]
enable_reqwest_rustls = ["azure_core/enable_reqwest_rustls", "azure_storage/enable_reqwest_rustls"]
test_integration = []
hmac_openssl = ["azure_core/hmac_openssl"]
hmac_rust = ["azure_core/hmac_rust"]

[package.metadata.docs.rs]
features = ["enable_reqwest", "enable_reqwest_rustls", "hmac_rust", "hmac_openssl"]
35 changes: 35 additions & 0 deletions sdk/storage_queues/examples/service_properties.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use azure_storage::prelude::*;
use azure_storage_queues::{prelude::*, CorsRule};

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

// First we retrieve the account name and access key from environment variables.
let account =
std::env::var("STORAGE_ACCOUNT").expect("Set env variable STORAGE_ACCOUNT first!");
let access_key =
std::env::var("STORAGE_ACCESS_KEY").expect("Set env variable STORAGE_ACCESS_KEY first!");

let storage_credentials = StorageCredentials::access_key(account.clone(), access_key);
let queue_service = QueueServiceClient::new(account, storage_credentials);
let properties = queue_service.get_queue_service_properties().await?;
println!("properties: {properties:#?}");

let mut properties = properties.queue_service_properties;
properties.cors.cors_rule = Some(vec![CorsRule {
allowed_origins: "http://www.contoso.com,http://www.fabrikam.com".to_owned(),
allowed_methods: "GET,PUT".to_owned(),
allowed_headers: "x-ms-meta-abc,x-ms-meta-data*,x-ms-meta-target*,x-ms-meta-xyz".to_owned(),
exposed_headers: "".to_owned(),
max_age_in_seconds: 50000,
}]);

queue_service
.set_queue_service_properties(properties.clone())
.await?;
let properties = queue_service.get_queue_service_properties().await?;
println!("properties: {properties:#?}");

Ok(())
}
2 changes: 1 addition & 1 deletion sdk/storage_queues/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,6 @@ pub use clients::*;
pub use message_ttl::MessageTTL;
pub use number_of_messages::NumberOfMessages;
pub use pop_receipt::PopReceipt;
pub use queue_service_properties::QueueServiceProperties;
pub use queue_service_properties::{CorsRule, QueueServiceProperties};
pub use queue_stored_access_policy::QueueStoredAccessPolicy;
pub use visibility_timeout::VisibilityTimeout;
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl GetQueueServicePropertiesResponse {
let (_, headers, body) = response.deconstruct();
let body = body.collect().await?;

let queue_service_properties: QueueServiceProperties = read_xml(&body)?;
let queue_service_properties = read_xml(&body)?;

Ok(GetQueueServicePropertiesResponse {
common_storage_response_headers: (&headers).try_into()?,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::{QueueServiceClient, QueueServiceProperties};
use azure_core::{error::Error, headers::Headers, xml::to_xml, Method, Response as AzureResponse};
use azure_core::{
error::Error, headers::Headers, xml::to_xml_with_root, Method, Response as AzureResponse,
};
use azure_storage::headers::CommonStorageResponseHeaders;
use std::convert::TryInto;

Expand All @@ -17,7 +19,7 @@ impl SetQueueServicePropertiesBuilder {
url.query_pairs_mut().append_pair("restype", "service");
url.query_pairs_mut().append_pair("comp", "properties");

let xml_body = to_xml(&self.properties)?;
let xml_body = to_xml_with_root("StorageServiceProperties", &self.properties)?;

let mut request = QueueServiceClient::finalize_request(
url,
Expand Down
4 changes: 3 additions & 1 deletion sdk/storage_queues/src/queue_service_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct QueueServiceProperties {
#[serde(rename_all = "PascalCase")]
pub struct RetentionPolicy {
pub enabled: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub days: Option<u64>,
}

Expand All @@ -29,14 +30,15 @@ pub struct Logging {
pub struct Metrics {
pub version: String,
pub enabled: bool,
#[serde(rename = "IncludeAPIs")]
#[serde(rename = "IncludeAPIs", skip_serializing_if = "Option::is_none")]
pub include_apis: Option<bool>,
pub retention_policy: RetentionPolicy,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "PascalCase")]
pub struct Cors {
#[serde(skip_serializing_if = "Option::is_none")]
pub cors_rule: Option<Vec<CorsRule>>,
}

Expand Down