Skip to content

Commit

Permalink
[plugin-azure-storage-account] Add step to collect blob service prope…
Browse files Browse the repository at this point in the history
…rties via management API (#2485)
  • Loading branch information
valfirst authored Feb 15, 2022
1 parent b009d85 commit a24ef7c
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 18 deletions.
26 changes: 25 additions & 1 deletion docs/modules/plugins/pages/plugin-azure-storage-account.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,8 @@ include::partial$azure-profile-and-subscription.adoc[leveloffset=+1]
==== Collect storage accounts info

Collects storage accounts info in the specified resource group and saves it as
JSON to a variable.
JSON to a variable. For more information, see the
https://docs.microsoft.com/en-us/rest/api/storagerp/storage-accounts/list[Azure Docs].

[source,gherkin]
----
Expand All @@ -276,3 +277,26 @@ over scope `/subscriptions/{subscription ID}/resourceGroups/{resource group name
----
When I collect storage accounts in resource group `TEST-SA` and save them as JSON to scenario variable `storage-accounts`
----

==== Retrieve the blob service properties

Retrieves the properties of a storage account’s Blob service, including
properties for Storage Analytics and CORS (Cross-Origin Resource Sharing) rules,
and saves them as JSON to a variable. For more information, see the
https://docs.microsoft.com/en-us/rest/api/storageservices/get-blob-service-properties[Azure Docs].

[source,gherkin]
----
When I retrieve blob service properties of storage account with name `$storageAccountName` from resource group `$resourceGroupName` and save them as JSON to $scopes variable `$variableName`
----

* `$storageAccountName` - The name of the storage account within the specified resource group.
* `$resourceGroupName` - The name of the resource group within the user's subscription to retrieve the storage account from. The name is case-insensitive.
* `$scopes` - xref:commons:variables.adoc#_scopes[The comma-separated set of the variables scopes].
* `$variableName` - The variable name to store the blob service properties as JSON.

.Retrieve the blob service properties
[source,gherkin]
----
When I retrieve blob service properties of storage account with name `testaccountname` from resource group `TEST-SA` and save them as JSON to scenario variable `blob-service-properties`
----
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 the original author or authors.
* Copyright 2019-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,6 +25,7 @@
import com.azure.core.credential.TokenCredential;
import com.azure.core.management.profile.AzureProfile;
import com.azure.resourcemanager.storage.StorageManager;
import com.azure.resourcemanager.storage.fluent.models.BlobServicePropertiesInner;
import com.azure.resourcemanager.storage.fluent.models.StorageAccountInner;

import org.jbehave.core.annotations.When;
Expand All @@ -48,7 +49,8 @@ public StorageAccountManagementSteps(AzureProfile azureProfile, TokenCredential

/**
* Collects the info about all the storage accounts under the specified resource group and saves it as JSON to a
* variable. Note that storage keys are not returned.
* variable. Note that storage keys are not returned. For more information, see the
* <a href="https://docs.microsoft.com/en-us/rest/api/storagerp/storage-accounts/list">Azure Docs</a>.
*
* @param resourceGroupName The name of the resource group within the user's subscription to list the storage
* accounts from. The name is case-insensitive.
Expand All @@ -61,7 +63,8 @@ public StorageAccountManagementSteps(AzureProfile azureProfile, TokenCredential
* <li><b>STORY</b> - the variable will be available within the whole story,
* <li><b>NEXT_BATCHES</b> - the variable will be available starting from next batch
* </ul>
* @param variableName The variable name to store the blob properties.
* @param variableName The variable name to store the info about storage accounts as JSON.
* @throws IOException If an input or output exception occurred
*/
@When("I collect storage accounts in resource group `$resourceGroupName` and save them as JSON to $scopes variable "
+ "`$variableName`")
Expand All @@ -76,4 +79,36 @@ public void listStorageAccounts(String resourceGroupName, Set<VariableScope> sco

variableContext.putVariable(scopes, variableName, innersJacksonAdapter.serializeToJson(storageAccounts));
}

/**
* Retrieves the properties of a storage account’s Blob service, including properties for Storage Analytics and CORS
* (Cross-Origin Resource Sharing) rules, and saves them as JSON to a variable. For more information, see the
* <a href="https://docs.microsoft.com/en-us/rest/api/storageservices/get-blob-service-properties">Azure Docs</a>.
*
* @param storageAccountName The name of the storage account within the specified resource group.
* @param resourceGroupName The name of the resource group within the user's subscription to retrieve the storage
* account from. The name is case-insensitive.
* @param scopes The set (comma separated list of scopes e.g.: STORY, NEXT_BATCHES) of the variable
* scopes.<br>
* <i>Available scopes:</i>
* <ul>
* <li><b>STEP</b> - the variable will be available only within the step,
* <li><b>SCENARIO</b> - the variable will be available only within the scenario,
* <li><b>STORY</b> - the variable will be available within the whole story,
* <li><b>NEXT_BATCHES</b> - the variable will be available starting from next batch
* </ul>
* @param variableName The variable name to store the blob service properties as JSON.
* @throws IOException If an input or output exception occurred
*/
@When("I retrieve blob service properties of storage account with name `$storageAccountName` from resource group "
+ "`$resourceGroupName` and save them as JSON to $scopes variable `$variableName`")
public void retrieveBlobServiceProperties(String storageAccountName, String resourceGroupName,
Set<VariableScope> scopes, String variableName) throws IOException
{
BlobServicePropertiesInner properties = storageManager.serviceClient()
.getBlobServices()
.getServiceProperties(resourceGroupName, storageAccountName);

variableContext.putVariable(scopes, variableName, innersJacksonAdapter.serializeToJson(properties));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2021 the original author or authors.
* Copyright 2019-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -30,10 +30,14 @@
import com.azure.core.http.rest.PagedIterable;
import com.azure.core.management.profile.AzureProfile;
import com.azure.resourcemanager.storage.StorageManager;
import com.azure.resourcemanager.storage.fluent.BlobServicesClient;
import com.azure.resourcemanager.storage.fluent.StorageAccountsClient;
import com.azure.resourcemanager.storage.fluent.StorageManagementClient;
import com.azure.resourcemanager.storage.fluent.models.BlobServicePropertiesInner;
import com.azure.resourcemanager.storage.fluent.models.StorageAccountInner;
import com.azure.resourcemanager.storage.models.DeleteRetentionPolicy;

import org.apache.commons.lang3.function.FailableBiConsumer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
Expand All @@ -46,6 +50,10 @@
@ExtendWith(MockitoExtension.class)
class StorageAccountManagementStepsTests
{
private static final String RESOURCE_GROUP_NAME = "resourceGroupName";
private static final Set<VariableScope> SCOPES = Set.of(VariableScope.STORY);
private static final String VAR_NAME = "varName";

@Mock private AzureProfile azureProfile;
@Mock private TokenCredential tokenCredential;
@Mock private VariableContext variableContext;
Expand All @@ -54,29 +62,54 @@ class StorageAccountManagementStepsTests
@SuppressWarnings("unchecked")
void shouldListStorageAccounts() throws IOException
{
try (MockedStatic<StorageManager> storageManagerStaticMock = mockStatic(StorageManager.class))
runWithStorageManagementClient((storageManagementClient, steps) ->
{
var storageManager = mock(StorageManager.class);
storageManagerStaticMock.when(() -> StorageManager.authenticate(tokenCredential, azureProfile)).thenReturn(
storageManager);
var storageManagementClient = mock(StorageManagementClient.class);
when(storageManager.serviceClient()).thenReturn(storageManagementClient);
var storageAccountsClient = mock(StorageAccountsClient.class);
when(storageManagementClient.getStorageAccounts()).thenReturn(storageAccountsClient);
PagedIterable<StorageAccountInner> storageAccounts = mock(PagedIterable.class);
var resourceGroupName = "resourceGroupName";
when(storageAccountsClient.listByResourceGroup(resourceGroupName)).thenReturn(storageAccounts);
when(storageAccountsClient.listByResourceGroup(RESOURCE_GROUP_NAME)).thenReturn(storageAccounts);
var storageAccount = new StorageAccountInner();
storageAccount.withAllowBlobPublicAccess(Boolean.TRUE);
storageAccount.withTags(Map.of());
when(storageAccounts.stream()).thenReturn(Stream.of(storageAccount));
steps.listStorageAccounts(RESOURCE_GROUP_NAME, SCOPES, VAR_NAME);
verify(variableContext).putVariable(SCOPES, VAR_NAME,
"[{\"tags\":{},\"properties\":{\"allowBlobPublicAccess\":true}}]");
});
}

@Test
void shouldRetrieveBlobServiceProperties() throws IOException
{
runWithStorageManagementClient((storageManagementClient, steps) ->
{
var blobServicesClient = mock(BlobServicesClient.class);
when(storageManagementClient.getBlobServices()).thenReturn(blobServicesClient);
var storageAccountName = "storageaccountname";
var properties = new BlobServicePropertiesInner();
properties.withContainerDeleteRetentionPolicy(new DeleteRetentionPolicy().withEnabled(Boolean.TRUE));
when(blobServicesClient.getServiceProperties(RESOURCE_GROUP_NAME, storageAccountName)).thenReturn(
properties);
steps.retrieveBlobServiceProperties(storageAccountName, RESOURCE_GROUP_NAME, SCOPES, VAR_NAME);
verify(variableContext).putVariable(SCOPES, VAR_NAME,
"{\"properties\":{\"containerDeleteRetentionPolicy\":{\"enabled\":true}}}");
});
}

private void runWithStorageManagementClient(
FailableBiConsumer<StorageManagementClient, StorageAccountManagementSteps, IOException> test)
throws IOException
{
try (MockedStatic<StorageManager> storageManagerStaticMock = mockStatic(StorageManager.class))
{
var storageManager = mock(StorageManager.class);
storageManagerStaticMock.when(() -> StorageManager.authenticate(tokenCredential, azureProfile)).thenReturn(
storageManager);
var storageManagementClient = mock(StorageManagementClient.class);
when(storageManager.serviceClient()).thenReturn(storageManagementClient);
var steps = new StorageAccountManagementSteps(azureProfile, tokenCredential, new InnersJacksonAdapter(),
variableContext);
var scopes = Set.of(VariableScope.STORY);
var varName = "varName";
steps.listStorageAccounts(resourceGroupName, scopes, varName);
verify(variableContext).putVariable(scopes, varName,
"[{\"tags\":{},\"properties\":{\"allowBlobPublicAccess\":true}}]");
test.accept(storageManagementClient, steps);
}
}
}

0 comments on commit a24ef7c

Please # to comment.