Skip to content

Commit

Permalink
Add Include function to ABS Optional Parameters (#24253)
Browse files Browse the repository at this point in the history
This PR will add an Include function to ABS Optional Parameters.
You can add a value as text that will be set as a value to the include
parameter.
A unit test is also added.

---------

Co-authored-by: Gijs <[email protected]>
Co-authored-by: Mahesh <[email protected]>
  • Loading branch information
3 people authored Aug 11, 2023
1 parent cd0e7e9 commit 535ebb0
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,55 @@ codeunit 132920 "ABS Blob Client Test"
ABSContainerClient.DeleteContainer(ContainerName);
end;

[Test] // Failing if ran against azurite, as BLOB tags are not supported there
procedure ListBlobsTestIncludeTags()
var
ABSOperationResponse: Codeunit "ABS Operation Response";
ABSOptionalParameters: Codeunit "ABS Optional Parameters";
BlobList: Dictionary of [Text, XmlNode];
Tags, BlobTags : Dictionary of [Text, Text];
ContainerName, BlobName, BlobContent : Text;
begin
// [SCENARIO] Given a storage account and a container with BLOB, ListBlobs operation succeeds and returns the content with tags.
// [GIVEN] Shared Key Authorization
SharedKeyAuthorization := StorageServiceAuthorization.CreateSharedKey(AzuriteTestLibrary.GetAccessKey());

// [GIVEN] ABS Container
ContainerName := ABSTestLibrary.GetContainerName();
ABSContainerClient.Initialize(AzuriteTestLibrary.GetStorageAccountName(), SharedKeyAuthorization);
ABSContainerClient.SetBaseUrl(AzuriteTestLibrary.GetBlobStorageBaseUrl());

ABSOperationResponse := ABSContainerClient.CreateContainer(ContainerName);
Assert.IsTrue(ABSOperationResponse.IsSuccessful(), 'Operation CreateContainer failed');

// [GIVEN] Block Blob
BlobName := ABSTestLibrary.GetBlobName();
BlobContent := ABSTestLibrary.GetSampleTextBlobContent();
ABSBlobClient.Initialize(AzuriteTestLibrary.GetStorageAccountName(), ContainerName, SharedKeyAuthorization);
ABSBlobClient.SetBaseUrl(AzuriteTestLibrary.GetBlobStorageBaseUrl());

ABSOperationResponse := ABSBlobClient.PutBlobBlockBlobText(BlobName, BlobContent);
Assert.IsTrue(ABSOperationResponse.IsSuccessful(), 'Operation PutBlobBlockBlob failed');

// [GIVEN] Blob Tags
Tags := ABSTestLibrary.GetBlobTags();

// [WHEN] Tags are Set
ABSOperationResponse := ABSBlobClient.SetBlobTags(BlobName, Tags);
Assert.IsTrue(ABSOperationResponse.IsSuccessful(), 'Operation SetBlobTags failed: ' + ABSOperationResponse.GetError());

// Listing the BLOBs in the container
ABSOptionalParameters.Include('tags');
ABSOperationResponse := ABSBlobClient.ListBlobs(BlobList, ABSOptionalParameters);
Assert.IsTrue(ABSOperationResponse.IsSuccessful(), 'Operation ListBlobs failed');

// [THEN] The get tags are equal to set tags
Assert.AreEqual(Tags, GetBlobTagsFromABSContainerBlobList(BlobName, BlobList));

// Clean-up
ABSContainerClient.DeleteContainer(ContainerName);
end;

[Test]
procedure ListBlobHierarchyTest()
var
Expand Down Expand Up @@ -535,6 +584,48 @@ codeunit 132920 "ABS Blob Client Test"
ABSContainerClient.DeleteContainer(ContainerName);
end;

local procedure GetBlobTagsFromABSContainerBlobList(BlobName: Text; BlobList: Dictionary of [Text, XmlNode]): Dictionary of [Text, Text]
var
BlobNode: XmlNode;
begin
BlobNode := BlobList.Get(BlobName);
exit(XmlNodeToTagsDictionary(BlobNode));
end;

local procedure XmlNodeToTagsDictionary(Node: XmlNode): Dictionary of [Text, Text]
var
Tags: Dictionary of [Text, Text];
TagNodesList: XmlNodeList;
TagNode: XmlNode;
KeyValue: Text;
Value: Text;
begin
if not Node.SelectNodes('Tags/TagSet/Tag', TagNodesList) then
exit;

foreach TagNode in TagNodesList do begin
KeyValue := GetSingleNodeInnerText(TagNode, 'Key');
Value := GetSingleNodeInnerText(TagNode, 'Value');
if KeyValue = '' then begin
Clear(Tags);
exit;
end;
Tags.Add(KeyValue, Value);
end;
exit(Tags);
end;

local procedure GetSingleNodeInnerText(Node: XmlNode; XPath: Text): Text
var
ChildNode: XmlNode;
XmlElement: XmlElement;
begin
if not Node.SelectSingleNode(XPath, ChildNode) then
exit;
XmlElement := ChildNode.AsXmlElement();
exit(XmlElement.InnerText());
end;

var
Assert: Codeunit "Library Assert";
ABSBlobClient: Codeunit "ABS Blob Client";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,16 @@ codeunit 9047 "ABS Optional Parameters"
SetParameter('blockid', "Value");
end;

/// <summary>
/// Specifies one or more datasets to include in the response.
/// </summary>
/// see: https://learn.microsoft.com/en-us/rest/api/storageservices/list-blobs?tabs=azure-ad#uri-parameters
/// <param name="Value">The dataset(s) to include in text</param>
procedure Include("Value": Text)
begin
SetParameter('include', "Value");
end;

local procedure SetParameter(Header: Text; HeaderValue: Text)
begin
Parameters.Remove(Header);
Expand Down

0 comments on commit 535ebb0

Please sign in to comment.