Skip to content

Commit 20931a8

Browse files
[Storage] list_containers for BlobServiceClient (#2717)
1 parent 7e4c7d9 commit 20931a8

File tree

6 files changed

+131
-17
lines changed

6 files changed

+131
-17
lines changed

sdk/storage/assets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"AssetsRepo": "Azure/azure-sdk-assets",
33
"AssetsRepoPrefixPath": "rust",
4-
"Tag": "rust/azure_storage_blob_c7d15a00d1",
4+
"Tag": "rust/azure_storage_blob_a55c38eec2",
55
"TagPrefix": "rust/azure_storage_blob"
66
}

sdk/storage/azure_storage_blob/src/clients/blob_service_client.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33

44
use crate::{
55
generated::clients::BlobServiceClient as GeneratedBlobServiceClient,
6-
models::{BlobServiceClientGetPropertiesOptions, StorageServiceProperties},
6+
models::{
7+
BlobServiceClientGetPropertiesOptions, BlobServiceClientListContainersSegmentOptions,
8+
ListContainersSegmentResponse, StorageServiceProperties,
9+
},
710
pipeline::StorageHeadersPolicy,
811
BlobContainerClient, BlobServiceClientOptions,
912
};
1013
use azure_core::{
1114
credentials::TokenCredential,
1215
http::{
1316
policies::{BearerTokenCredentialPolicy, Policy},
14-
Response, Url, XmlFormat,
17+
PageIterator, Response, Url, XmlFormat,
1518
},
1619
Result,
1720
};
@@ -89,4 +92,16 @@ impl BlobServiceClient {
8992
) -> Result<Response<StorageServiceProperties, XmlFormat>> {
9093
self.client.get_properties(options).await
9194
}
95+
96+
/// Returns a list of the containers under the specified Storage account.
97+
///
98+
/// # Arguments
99+
///
100+
/// * `options` - Optional configuration for the request.
101+
pub fn list_containers(
102+
&self,
103+
options: Option<BlobServiceClientListContainersSegmentOptions<'_>>,
104+
) -> Result<PageIterator<Response<ListContainersSegmentResponse, XmlFormat>>> {
105+
self.client.list_containers_segment(options)
106+
}
92107
}

sdk/storage/azure_storage_blob/src/generated/models/header_traits.rs

Lines changed: 6 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/storage/azure_storage_blob/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,13 @@ pub mod models {
2323
BlobContainerClientGetPropertiesOptions, BlobContainerClientGetPropertiesResult,
2424
BlobContainerClientGetPropertiesResultHeaders,
2525
BlobContainerClientListBlobFlatSegmentOptions, BlobContainerClientSetMetadataOptions,
26-
BlobImmutabilityPolicyMode, BlobServiceClientGetPropertiesOptions, BlobType,
26+
BlobImmutabilityPolicyMode, BlobServiceClientGetPropertiesOptions,
27+
BlobServiceClientListContainersSegmentOptions, BlobType,
2728
BlockBlobClientCommitBlockListOptions, BlockBlobClientCommitBlockListResult,
2829
BlockBlobClientGetBlockListOptions, BlockBlobClientStageBlockOptions,
2930
BlockBlobClientStageBlockResult, BlockBlobClientUploadOptions, BlockBlobClientUploadResult,
3031
BlockList, BlockListType, BlockLookupList, CopyStatus, LeaseState, LeaseStatus,
31-
ListBlobsFlatSegmentResponse, PublicAccessType, RehydratePriority,
32-
StorageServiceProperties,
32+
ListBlobsFlatSegmentResponse, ListContainersSegmentResponse, PublicAccessType,
33+
RehydratePriority, StorageServiceProperties,
3334
};
3435
}

sdk/storage/azure_storage_blob/tests/blob_service_client.rs

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22
// Licensed under the MIT License.
33

44
use azure_core_test::{recorded, TestContext};
5-
use azure_storage_blob::models::BlobServiceClientGetPropertiesOptions;
6-
use azure_storage_blob_test::get_blob_service_client;
5+
use azure_storage_blob::models::{
6+
BlobServiceClientGetPropertiesOptions, BlobServiceClientListContainersSegmentOptions,
7+
};
8+
use azure_storage_blob_test::{get_blob_service_client, get_container_name};
9+
use futures::StreamExt;
10+
use std::collections::HashMap;
711
use std::error::Error;
812

913
#[recorded::test]
@@ -22,3 +26,99 @@ async fn test_get_service_properties(ctx: TestContext) -> Result<(), Box<dyn Err
2226
assert!(hour_metrics.is_some());
2327
Ok(())
2428
}
29+
30+
#[recorded::test]
31+
async fn test_list_containers(ctx: TestContext) -> Result<(), Box<dyn Error>> {
32+
// Recording Setup
33+
let recording = ctx.recording();
34+
let service_client = get_blob_service_client(recording)?;
35+
let mut container_names = HashMap::from([
36+
(get_container_name(recording), 0),
37+
(get_container_name(recording), 0),
38+
(get_container_name(recording), 0),
39+
(get_container_name(recording), 0),
40+
]);
41+
let mut container_clients = Vec::new();
42+
for container_name in container_names.keys() {
43+
let container_client = service_client.blob_container_client(container_name.to_string());
44+
container_client.create_container(None).await?;
45+
container_clients.push(container_client);
46+
}
47+
48+
// Assert
49+
let mut pager_response = service_client.list_containers(None)?;
50+
while let Some(page) = pager_response.next().await {
51+
let current_page = page.unwrap().into_body().await?;
52+
let container_list = current_page.container_items;
53+
for container in container_list {
54+
let container_name = container.name.unwrap();
55+
if container_names.contains_key(&container_name) {
56+
container_names
57+
.entry(container_name)
58+
.and_modify(|val| *val = 1);
59+
}
60+
}
61+
}
62+
63+
for containers in container_names {
64+
assert_eq!(containers.1, 1)
65+
}
66+
67+
for container_client in container_clients {
68+
container_client.delete_container(None).await?;
69+
}
70+
71+
Ok(())
72+
}
73+
74+
#[recorded::test]
75+
async fn test_list_containers_with_continuation(ctx: TestContext) -> Result<(), Box<dyn Error>> {
76+
// Recording Setup
77+
let recording = ctx.recording();
78+
let service_client = get_blob_service_client(recording)?;
79+
let mut container_names = HashMap::from([
80+
(get_container_name(recording), 0),
81+
(get_container_name(recording), 0),
82+
(get_container_name(recording), 0),
83+
(get_container_name(recording), 0),
84+
]);
85+
let mut container_clients = Vec::new();
86+
for container_name in container_names.keys() {
87+
let container_client = service_client.blob_container_client(container_name.to_string());
88+
container_client.create_container(None).await?;
89+
container_clients.push(container_client);
90+
}
91+
92+
let list_containers_options = BlobServiceClientListContainersSegmentOptions {
93+
maxresults: Some(2),
94+
..Default::default()
95+
};
96+
97+
// Assert
98+
let mut pager_response = service_client.list_containers(Some(list_containers_options))?;
99+
let mut page_count = 0;
100+
while let Some(page) = pager_response.next().await {
101+
page_count += 1;
102+
let current_page = page.unwrap().into_body().await?;
103+
let container_list = current_page.container_items;
104+
for container in container_list {
105+
let container_name = container.name.unwrap();
106+
if container_names.contains_key(&container_name) {
107+
container_names
108+
.entry(container_name)
109+
.and_modify(|val| *val = 1);
110+
}
111+
}
112+
}
113+
114+
for containers in container_names {
115+
assert_eq!(containers.1, 1)
116+
}
117+
assert!(page_count >= 2);
118+
119+
for container_client in container_clients {
120+
container_client.delete_container(None).await?;
121+
}
122+
123+
Ok(())
124+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
directory: specification/storage/Microsoft.BlobStorage
2-
commit: 37843c7f2116830dccd5883d6960bc05c66d30b1
2+
commit: 0fb24e5a13f2eaa86af724acef4b725d0b3d0ca7
33
repo: Azure/azure-rest-api-specs
44
additionalDirectories:

0 commit comments

Comments
 (0)