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 (#2463)
  • Loading branch information
valfirst authored Feb 11, 2022
1 parent b85bfc0 commit 19f2f11
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 16 deletions.
21 changes: 21 additions & 0 deletions docs/modules/plugins/pages/plugin-azure-storage-account.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,27 @@ in this case the authentication configuration is not required.

=== Steps

==== Retrieve the blob service properties

Retrieves the properties of a storage account’s Blob service and saves them as
JSON to a variable. For more information, see the
https://docs.microsoft.com/rest/api/storageservices/get-blob-service-properties[Azure Docs].

[source,gherkin]
----
When I retrieve blob service properties of storage account `$storageAccountKey` and save them to $scopes variable `$variableName`
----

* `$storageAccountKey` - The key of storage account from the configuration.
* `$scopes` - xref:commons:variables.adoc#_scopes[The comma-separated set of the variables scopes].
* `$variableName` - The variable name to store the blob service properties.

.Retrieve the blob service properties
[source,gherkin]
----
When I retrieve blob service properties of storage account `testaccount` and save them to scenario variable `blob-service-properties`
----

==== Download the blob

Downloads the entire blob from the container and saves its content as a text to a variable.
Expand Down
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 @@ -35,6 +35,7 @@
import com.azure.storage.blob.BlobServiceClientBuilder;
import com.azure.storage.blob.models.BlobItem;
import com.azure.storage.blob.models.BlobProperties;
import com.azure.storage.blob.models.BlobServiceProperties;
import com.azure.storage.blob.models.ListBlobsOptions;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
Expand Down Expand Up @@ -81,6 +82,31 @@ public BlobStorageSteps(PropertyMappedCollection<String> storageAccountEndpoints
this.jsonUtils = jsonUtils;
}

/**
* Retrieves the properties of a storage account’s Blob service and saves them as JSON to a variable.
* For more information, see the
* <a href="https://docs.microsoft.com/rest/api/storageservices/get-blob-service-properties">Azure Docs</a>.
*
* @param storageAccountKey The key to Storage Account endpoint.
* @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.
*/
@When("I retrieve blob service properties of storage account `$storageAccountKey` and save them to $scopes variable"
+ " `$variableName`")
public void retrieveBlobServiceProperties(String storageAccountKey, Set<VariableScope> scopes, String variableName)
{
BlobServiceProperties properties = createBlobStorageClient(storageAccountKey).getProperties();
saveJsonVariable(scopes, variableName, properties);
}

/**
* Downloads the entire blob from the container and saves its content as a text to a variable
*
Expand Down Expand Up @@ -147,7 +173,7 @@ public void downloadBlobToFile(String blobName, String containerName, String sto

/**
* Retrieves the blob properties (all user-defined metadata, standard HTTP properties, and system properties for the
* blob) and saves them as JSON to a variable
* blob) and saves them as JSON to a variable.
*
* @param blobName The full path to the blob in the container.
* @param containerName The name of the container to point to.
Expand All @@ -169,8 +195,8 @@ public void downloadBlobToFile(String blobName, String containerName, String sto
public void retrieveBlobProperties(String blobName, String containerName, String storageAccountKey,
Set<VariableScope> scopes, String variableName)
{
BlobProperties blobProperties = createBlobClient(blobName, containerName, storageAccountKey).getProperties();
variableContext.putVariable(scopes, variableName, jsonUtils.toJson(blobProperties));
BlobProperties properties = createBlobClient(blobName, containerName, storageAccountKey).getProperties();
saveJsonVariable(scopes, variableName, properties);
}

/**
Expand Down Expand Up @@ -305,17 +331,25 @@ public void findBlobs(BlobFilter filter, String containerName, String storageAcc
variableContext.putVariable(scopes, variableName, result);
}

private BlobContainerClient createBlobContainerClient(String containerName, String storageAccountKey)
private BlobServiceClient createBlobStorageClient(String storageAccountKey)
{
String endpoint = storageAccountEndpoints.get(storageAccountKey,
"Storage account with key '%s' is not configured in properties", storageAccountKey);
BlobServiceClient blobStorageClient = blobStorageClients.getUnchecked(endpoint);
return blobStorageClient.getBlobContainerClient(containerName);
return blobStorageClients.getUnchecked(endpoint);
}

private BlobContainerClient createBlobContainerClient(String containerName, String storageAccountKey)
{
return createBlobStorageClient(storageAccountKey).getBlobContainerClient(containerName);
}

private BlobClient createBlobClient(String blobName, String containerName, String storageAccountKey)
{
BlobContainerClient blobContainerClient = createBlobContainerClient(containerName, storageAccountKey);
return blobContainerClient.getBlobClient(blobName);
return createBlobContainerClient(containerName, storageAccountKey).getBlobClient(blobName);
}

private void saveJsonVariable(Set<VariableScope> scopes, String variableName, Object data)
{
variableContext.putVariable(scopes, variableName, jsonUtils.toJson(data));
}
}
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 @@ -51,6 +51,7 @@
import com.azure.storage.blob.BlobServiceClientBuilder;
import com.azure.storage.blob.models.BlobItem;
import com.azure.storage.blob.models.BlobProperties;
import com.azure.storage.blob.models.BlobServiceProperties;
import com.azure.storage.blob.models.ListBlobsOptions;
import com.github.valfirst.slf4jtest.TestLogger;
import com.github.valfirst.slf4jtest.TestLoggerFactory;
Expand Down Expand Up @@ -98,6 +99,20 @@ class BlobStorageStepsTests
@Mock private JsonUtils jsonUtils;
@Mock private TokenCredential tokenCredential;

@Test
void shouldRetrieveBlobServiceProperties()
{
runWithStorageClient((steps, client) ->
{
BlobServiceProperties properties = mock(BlobServiceProperties.class);
when(client.getProperties()).thenReturn(properties);
String propertiesAsJson = "{\"blob-storage\":\"properties\"}";
when(jsonUtils.toJson(properties)).thenReturn(propertiesAsJson);
steps.retrieveBlobServiceProperties(KEY, SCOPES, VARIABLE);
verify(variableContext).putVariable(SCOPES, VARIABLE, propertiesAsJson);
});
}

@Test
void shouldDownloadBlob()
{
Expand Down Expand Up @@ -276,7 +291,7 @@ private BlobItem blobItem(String name)
return first;
}

private void runWithClient(FailableBiConsumer<BlobStorageSteps, BlobContainerClient, IOException> testToRun)
private void runWithStorageClient(FailableBiConsumer<BlobStorageSteps, BlobServiceClient, IOException> testToRun)
{
BlobServiceClient blobServiceClient = mock(BlobServiceClient.class);
String endpoint = "endpoint";
Expand All @@ -289,11 +304,9 @@ private void runWithClient(FailableBiConsumer<BlobStorageSteps, BlobContainerCli
when(mock.buildClient()).thenReturn(blobServiceClient);
}))
{
BlobContainerClient blobContainerClient = mock(BlobContainerClient.class);
when(blobServiceClient.getBlobContainerClient(CONTAINER)).thenReturn(blobContainerClient);
BlobStorageSteps steps = new BlobStorageSteps(storageAccountEndpoints, tokenCredential,
variableContext, jsonUtils);
testToRun.accept(steps, blobContainerClient);
BlobStorageSteps steps = new BlobStorageSteps(storageAccountEndpoints, tokenCredential, variableContext,
jsonUtils);
testToRun.accept(steps, blobServiceClient);
assertThat(serviceClientBuilder.constructed(), hasSize(1));
BlobServiceClientBuilder builder = serviceClientBuilder.constructed().get(0);
InOrder ordered = inOrder(builder);
Expand All @@ -307,6 +320,15 @@ private void runWithClient(FailableBiConsumer<BlobStorageSteps, BlobContainerCli
}
}

private void runWithClient(FailableBiConsumer<BlobStorageSteps, BlobContainerClient, IOException> testToRun)
{
runWithStorageClient((steps, blobServiceClient) -> {
BlobContainerClient blobContainerClient = mock(BlobContainerClient.class);
when(blobServiceClient.getBlobContainerClient(CONTAINER)).thenReturn(blobContainerClient);
testToRun.accept(steps, blobContainerClient);
});
}

private BlobClient mockBlobClient(BlobContainerClient blobContainerClient)
{
BlobClient blobClient = mock(BlobClient.class);
Expand Down

0 comments on commit 19f2f11

Please # to comment.