Skip to content

Commit c8c1de4

Browse files
bluewwBethanyZhou
andauthored
[Storage] support blobIndexMatch in management policy (#18921)
* support blobIndexMatch * Update src/Storage/Storage.Management/ChangeLog.md Co-authored-by: Beisi Zhou <[email protected]> * revise per review comments * Update src/Storage/Storage.Management/ChangeLog.md Co-authored-by: Beisi Zhou <[email protected]> * Fix a CI failure Co-authored-by: Beisi Zhou <[email protected]>
1 parent 81660d9 commit c8c1de4

File tree

12 files changed

+545
-240
lines changed

12 files changed

+545
-240
lines changed

src/Storage/Storage.Management.Test/ScenarioTests/StorageAccountTests.ps1

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1066,7 +1066,9 @@ function Test-StorageAccountManagementPolicy
10661066

10671067
# create Rule2
10681068
$action2 = Add-AzStorageAccountManagementPolicyAction -BaseBlobAction Delete -daysAfterModificationGreaterThan 100
1069-
$filter2 = New-AzStorageAccountManagementPolicyFilter -BlobType appendBlob,blockBlob
1069+
$blobindexmatch1 = New-AzStorageAccountManagementPolicyBlobIndexMatchObject -Name "tag1" -Value "value1"
1070+
$blobindexmatch2 = New-AzStorageAccountManagementPolicyBlobIndexMatchObject -Name "tag2" -Value "value2"
1071+
$filter2 = New-AzStorageAccountManagementPolicyFilter -BlobType appendBlob,blockBlob -BlobIndexMatch $blobindexmatch1,$blobindexmatch2
10701072
$rule2 = New-AzStorageAccountManagementPolicyRule -Name Test2 -Action $action2 -Filter $filter2 -Disabled
10711073

10721074
# create Rule3
@@ -1102,6 +1104,10 @@ function Test-StorageAccountManagementPolicy
11021104
Assert-AreEqual $rule2.Definition.Filters.BlobTypes[0] $policy.Rules[1].Definition.Filters.BlobTypes[0]
11031105
Assert-AreEqual $rule2.Definition.Filters.BlobTypes[1] $policy.Rules[1].Definition.Filters.BlobTypes[1]
11041106
Assert-AreEqual $rule2.Definition.Filters.PrefixMatch $policy.Rules[1].Definition.Filters.PrefixMatch
1107+
Assert-AreEqual $rule2.Definition.Filters.BlobIndexMatch[0].Name $policy.Rules[1].Definition.Filters.BlobIndexMatch[0].Name
1108+
Assert-AreEqual $rule2.Definition.Filters.BlobIndexMatch[0].Value $policy.Rules[1].Definition.Filters.BlobIndexMatch[0].Value
1109+
Assert-AreEqual $rule2.Definition.Filters.BlobIndexMatch[1].Name $policy.Rules[1].Definition.Filters.BlobIndexMatch[1].Name
1110+
Assert-AreEqual $rule2.Definition.Filters.BlobIndexMatch[1].Value $policy.Rules[1].Definition.Filters.BlobIndexMatch[1].Value
11051111
Assert-AreEqual $rule3.Enabled $policy.Rules[2].Enabled
11061112
Assert-AreEqual $rule3.Name $policy.Rules[2].Name
11071113
Assert-AreEqual $rule3.Definition.Actions.BaseBlob $policy.Rules[2].Definition.Actions.BaseBlob
@@ -1138,6 +1144,10 @@ function Test-StorageAccountManagementPolicy
11381144
Assert-AreEqual $rule2.Definition.Filters.BlobTypes[0] $policy.Rules[1].Definition.Filters.BlobTypes[0]
11391145
Assert-AreEqual $rule2.Definition.Filters.BlobTypes[1] $policy.Rules[1].Definition.Filters.BlobTypes[1]
11401146
Assert-AreEqual $rule2.Definition.Filters.PrefixMatch $policy.Rules[1].Definition.Filters.PrefixMatch
1147+
Assert-AreEqual $rule2.Definition.Filters.BlobIndexMatch[0].Name $policy.Rules[1].Definition.Filters.BlobIndexMatch[0].Name
1148+
Assert-AreEqual $rule2.Definition.Filters.BlobIndexMatch[0].Value $policy.Rules[1].Definition.Filters.BlobIndexMatch[0].Value
1149+
Assert-AreEqual $rule2.Definition.Filters.BlobIndexMatch[1].Name $policy.Rules[1].Definition.Filters.BlobIndexMatch[1].Name
1150+
Assert-AreEqual $rule2.Definition.Filters.BlobIndexMatch[1].Value $policy.Rules[1].Definition.Filters.BlobIndexMatch[1].Value
11411151
Assert-AreEqual $rule3.Enabled $policy.Rules[2].Enabled
11421152
Assert-AreEqual $rule3.Name $policy.Rules[2].Name
11431153
Assert-AreEqual $rule3.Definition.Actions.BaseBlob $policy.Rules[2].Definition.Actions.BaseBlob
@@ -1178,6 +1188,10 @@ function Test-StorageAccountManagementPolicy
11781188
Assert-AreEqual $rule2.Definition.Filters.BlobTypes[0] $policy.Rules[1].Definition.Filters.BlobTypes[0]
11791189
Assert-AreEqual $rule2.Definition.Filters.BlobTypes[1] $policy.Rules[1].Definition.Filters.BlobTypes[1]
11801190
Assert-AreEqual $rule2.Definition.Filters.PrefixMatch $policy.Rules[1].Definition.Filters.PrefixMatch
1191+
Assert-AreEqual $rule2.Definition.Filters.BlobIndexMatch[0].Name $policy.Rules[1].Definition.Filters.BlobIndexMatch[0].Name
1192+
Assert-AreEqual $rule2.Definition.Filters.BlobIndexMatch[0].Value $policy.Rules[1].Definition.Filters.BlobIndexMatch[0].Value
1193+
Assert-AreEqual $rule2.Definition.Filters.BlobIndexMatch[1].Name $policy.Rules[1].Definition.Filters.BlobIndexMatch[1].Name
1194+
Assert-AreEqual $rule2.Definition.Filters.BlobIndexMatch[1].Value $policy.Rules[1].Definition.Filters.BlobIndexMatch[1].Value
11811195
Assert-AreEqual $rule3.Enabled $policy.Rules[2].Enabled
11821196
Assert-AreEqual $rule3.Name $policy.Rules[2].Name
11831197
Assert-AreEqual $rule3.Definition.Actions.BaseBlob $policy.Rules[2].Definition.Actions.BaseBlob

src/Storage/Storage.Management.Test/SessionRecords/Microsoft.Azure.Commands.Management.Storage.Test.ScenarioTests.StorageAccountTests/TestStorageAccountManagementPolicy.json

Lines changed: 198 additions & 198 deletions
Large diffs are not rendered by default.

src/Storage/Storage.Management/Az.Storage.psd1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ CmdletsToExport = 'Get-AzStorageAccount', 'Get-AzStorageAccountKey',
198198
'Remove-AzStorageBlobImmutabilityPolicy',
199199
'Set-AzStorageBlobLegalHold',
200200
'Invoke-AzRmStorageContainerImmutableStorageWithVersioningMigration',
201-
'New-AzDataLakeGen2SasToken'
201+
'New-AzDataLakeGen2SasToken',
202+
'New-AzStorageAccountManagementPolicyBlobIndexMatchObject'
202203

203204
# Variables to export from this module
204205
# VariablesToExport = @()

src/Storage/Storage.Management/ChangeLog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
## Upcoming Release
2121
* Added check for storage account sas token is secured with the storage account key.
2222
- `New-AzStorageAccountSASToken`
23+
* Supported Management Policy rule filter BlobIndexMatch
24+
- Added a new cmdlet `New-AzStorageAccountManagementPolicyBlobIndexMatchObject`
25+
- Added a new parameter `BlobIndexMatch` in `New-AzStorageAccountManagementPolicyFilter`
2326

2427
## Version 4.7.0
2528
* Supported BaseBlob DaysAfterCreationGreaterThan in Management Policy

src/Storage/Storage.Management/Models/PSDataPolicy.cs

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ public class PSManagementPolicyRuleFilter
149149
{
150150
public string[] PrefixMatch { get; set; }
151151
public string[] BlobTypes { get; set; }
152+
public PSTagFilter[] BlobIndexMatch { get; set; }
152153

153154
public PSManagementPolicyRuleFilter()
154155
{ }
@@ -157,13 +158,15 @@ public PSManagementPolicyRuleFilter(ManagementPolicyFilter filter)
157158
{
158159
this.PrefixMatch = StringListToArray(filter.PrefixMatch);
159160
this.BlobTypes = StringListToArray(filter.BlobTypes);
161+
this.BlobIndexMatch = PSTagFilter.ParsePSTagFilterArray(filter.BlobIndexMatch);
160162
}
161163
public ManagementPolicyFilter ParseManagementPolicyFilter()
162164
{
163165
return new ManagementPolicyFilter()
164166
{
165167
PrefixMatch = StringArrayToList(this.PrefixMatch),
166168
BlobTypes = StringArrayToList(this.BlobTypes),
169+
BlobIndexMatch = PSTagFilter.ParseTagFilterList(this.BlobIndexMatch)
167170
};
168171
}
169172

@@ -179,9 +182,81 @@ public static List<string> StringArrayToList(string[] array)
179182
}
180183

181184
/// <summary>
182-
/// Wrapper of SDK type ManagementPolicyAction
185+
/// Wrapper of SDK type TagFilter
183186
/// </summary>
184-
public class PSManagementPolicyActionGroup
187+
public class PSTagFilter
188+
{
189+
// Gets or sets this is the filter tag name, it can have 1 - 128 characters
190+
public string Name { get; set; }
191+
192+
// Gets or sets this is the comparison operator which is used for object comparison and filtering.
193+
// Only == (equality operator) is currently supported
194+
public string Op { get; set; }
195+
196+
// Gets or sets this is the filter tag value field used for tag based filtering, it can have 0 - 256 characters
197+
public string Value { get; set; }
198+
199+
public PSTagFilter()
200+
{
201+
}
202+
203+
public PSTagFilter(string name, string op, string value)
204+
{
205+
Name = name;
206+
Op = op;
207+
Value = value;
208+
}
209+
210+
public PSTagFilter(TagFilter tagFilter)
211+
{
212+
Name = tagFilter.Name;
213+
Op = tagFilter.Op;
214+
Value = tagFilter.Value;
215+
}
216+
217+
public TagFilter ParseTagFilter()
218+
{
219+
return new TagFilter()
220+
{
221+
Name = this.Name,
222+
Op = this.Op,
223+
Value = this.Value,
224+
};
225+
}
226+
227+
public static PSTagFilter[] ParsePSTagFilterArray(IList<TagFilter> tagFilterList)
228+
{
229+
if (tagFilterList is null)
230+
{
231+
return null;
232+
}
233+
List<PSTagFilter> psTagFilterList = new List<PSTagFilter>();
234+
foreach (TagFilter filter in tagFilterList)
235+
{
236+
psTagFilterList.Add(new PSTagFilter(filter));
237+
}
238+
return psTagFilterList.ToArray();
239+
}
240+
241+
public static IList<TagFilter> ParseTagFilterList(PSTagFilter[] psTagFilterArray)
242+
{
243+
if (psTagFilterArray is null)
244+
{
245+
return null;
246+
}
247+
List<TagFilter> tagFilterList = new List<TagFilter>();
248+
foreach (PSTagFilter filter in psTagFilterArray)
249+
{
250+
tagFilterList.Add(filter.ParseTagFilter());
251+
}
252+
return tagFilterList;
253+
}
254+
}
255+
256+
/// <summary>
257+
/// Wrapper of SDK type ManagementPolicyAction
258+
/// </summary>
259+
public class PSManagementPolicyActionGroup
185260
{
186261
public PSManagementPolicyBaseBlob BaseBlob { get; set; }
187262
public PSManagementPolicySnapShot Snapshot { get; set; }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using Microsoft.Azure.Commands.Management.Storage.Models;
16+
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
17+
using Microsoft.Azure.Management.Storage;
18+
using Microsoft.Azure.Management.Storage.Models;
19+
using System.Management.Automation;
20+
21+
namespace Microsoft.Azure.Commands.Management.Storage
22+
{
23+
[Cmdlet("New", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "StorageAccountManagementPolicyBlobIndexMatchObject"), OutputType(typeof(PSTagFilter))]
24+
public class NewAzureStorageAccountManagementPolicyBlobIndexMatchObjectCommand : StorageAccountBaseCmdlet
25+
{
26+
27+
[Parameter(Mandatory = false,
28+
HelpMessage = "Gets or sets this is the filter tag name, it can have 1 - 128 characters")]
29+
[ValidateNotNullOrEmpty]
30+
public string Name { get; set; }
31+
32+
[Parameter(Mandatory = false,
33+
HelpMessage = "Gets or sets this is the filter tag value field used for tag based filtering, it can have 0 - 256 characters.")]
34+
[ValidateNotNullOrEmpty]
35+
public string Value { get; set; }
36+
37+
public override void ExecuteCmdlet()
38+
{
39+
base.ExecuteCmdlet();
40+
41+
PSTagFilter tagFilter = new PSTagFilter()
42+
{
43+
Name = this.Name,
44+
Op = "==",
45+
Value = this.Value,
46+
};
47+
48+
WriteObject(tagFilter);
49+
}
50+
}
51+
}

src/Storage/Storage.Management/StorageAccount/NewAzureStorageAccountManagementPolicyFilter.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ public class NewAzureStorageAccountManagementPolicyFilterCommand : StorageAccoun
4444
[ValidateNotNullOrEmpty]
4545
public string[] BlobType { get; set; }
4646

47+
[Parameter(Mandatory = false,
48+
HelpMessage = "An array of blob index tag based filters, there can be at most 10 tag filters.")]
49+
[ValidateNotNullOrEmpty]
50+
public PSTagFilter[] BlobIndexMatch { get; set; }
51+
4752
public override void ExecuteCmdlet()
4853
{
4954
base.ExecuteCmdlet();
@@ -71,6 +76,7 @@ public override void ExecuteCmdlet()
7176
{
7277
BlobTypes = blobType,
7378
PrefixMatch = this.PrefixMatch,
79+
BlobIndexMatch = this.BlobIndexMatch,
7480
};
7581

7682
WriteObject(filter);

src/Storage/Storage.Management/help/Az.Storage.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ Move a file or directory to another a file or directory in same Storage account.
198198
Create a file or directory in a filesystem.
199199

200200
### [New-AzDataLakeGen2SasToken](New-AzDataLakeGen2SasToken.md)
201-
Generates a SAS token for an Azure storage blob.
201+
Generates a SAS token for Azure DatalakeGen2 item.
202202

203203
### [New-AzRmStorageContainer](New-AzRmStorageContainer.md)
204204
Creates a Storage blob container
@@ -212,6 +212,9 @@ Creates a Storage account.
212212
### [New-AzStorageAccountKey](New-AzStorageAccountKey.md)
213213
Regenerates a storage key for an Azure Storage account.
214214

215+
### [New-AzStorageAccountManagementPolicyBlobIndexMatchObject](New-AzStorageAccountManagementPolicyBlobIndexMatchObject.md)
216+
Creates a ManagementPolicy BlobIndexMatch object, which can be used in New-AzStorageAccountManagementPolicyFilter.
217+
215218
### [New-AzStorageAccountManagementPolicyFilter](New-AzStorageAccountManagementPolicyFilter.md)
216219
Creates a ManagementPolicy rule filter object, which can be used in New-AzStorageAccountManagementPolicyRule.
217220

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
external help file: Microsoft.Azure.PowerShell.Cmdlets.Storage.Management.dll-Help.xml
3+
Module Name: Az.Storage
4+
online version: https://docs.microsoft.com/powershell/module/Az.storage/new-Azstorageaccountmanagementpolicyblobindexmatchobject
5+
schema: 2.0.0
6+
---
7+
8+
# New-AzStorageAccountManagementPolicyBlobIndexMatchObject
9+
10+
## SYNOPSIS
11+
Creates a ManagementPolicy BlobIndexMatch object, which can be used in New-AzStorageAccountManagementPolicyFilter.
12+
13+
## SYNTAX
14+
15+
```
16+
New-AzStorageAccountManagementPolicyBlobIndexMatchObject [-Name <String>] [-Value <String>]
17+
[-DefaultProfile <IAzureContextContainer>] [<CommonParameters>]
18+
```
19+
20+
## DESCRIPTION
21+
The **New-AzStorageAccountManagementPolicyBlobIndexMatchObject** cmdlet creates a ManagementPolicy BlobIndexMatch object, which can be used in New-AzStorageAccountManagementPolicyFilter.
22+
23+
## EXAMPLES
24+
25+
### Example 1: Creates 2 ManagementPolicy BlobIndexMatch object3, then add them to a management policy rule filter
26+
<!-- Skip: Output cannot be splitted from code -->
27+
```
28+
PS C:\> $blobindexmatch1 = New-AzStorageAccountManagementPolicyBlobIndexMatchObject -Name "tag1" -Value "value1"
29+
PS C:\> $blobindexmatch1
30+
31+
Name Op Value
32+
---- -- -----
33+
tag1 == value1
34+
35+
PS C:\> $blobindexmatch2 = New-AzStorageAccountManagementPolicyBlobIndexMatchObject -Name "tag2" -Value "value2"
36+
37+
PS C:\> New-AzStorageAccountManagementPolicyFilter -PrefixMatch prefix1,prefix2 -BlobType blockBlob `
38+
-BlobIndexMatch $blobindexmatch1,$blobindexmatch2
39+
40+
PrefixMatch BlobTypes BlobIndexMatch
41+
----------- --------- --------------
42+
{prefix1, prefix2} {blockBlob} {tag1, tag2}
43+
```
44+
45+
This command creates 2 ManagementPolicy BlobIndexMatch objects, then add themto a management policy rule filter.
46+
47+
## PARAMETERS
48+
49+
### -DefaultProfile
50+
The credentials, account, tenant, and subscription used for communication with Azure.
51+
52+
```yaml
53+
Type: Microsoft.Azure.Commands.Common.Authentication.Abstractions.Core.IAzureContextContainer
54+
Parameter Sets: (All)
55+
Aliases: AzContext, AzureRmContext, AzureCredential
56+
57+
Required: False
58+
Position: Named
59+
Default value: None
60+
Accept pipeline input: False
61+
Accept wildcard characters: False
62+
```
63+
64+
### -Name
65+
Gets or sets this is the filter tag name, it can have 1 - 128 characters
66+
67+
```yaml
68+
Type: System.String
69+
Parameter Sets: (All)
70+
Aliases:
71+
72+
Required: False
73+
Position: Named
74+
Default value: None
75+
Accept pipeline input: False
76+
Accept wildcard characters: False
77+
```
78+
79+
### -Value
80+
Gets or sets this is the filter tag value field used for tag based filtering, it can have 0 - 256 characters.
81+
82+
```yaml
83+
Type: System.String
84+
Parameter Sets: (All)
85+
Aliases:
86+
87+
Required: False
88+
Position: Named
89+
Default value: None
90+
Accept pipeline input: False
91+
Accept wildcard characters: False
92+
```
93+
94+
### CommonParameters
95+
This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see [about_CommonParameters](http://go.microsoft.com/fwlink/?LinkID=113216).
96+
97+
## INPUTS
98+
99+
### None
100+
101+
## OUTPUTS
102+
103+
### Microsoft.Azure.Commands.Management.Storage.Models.PSTagFilter
104+
105+
## NOTES
106+
107+
## RELATED LINKS

0 commit comments

Comments
 (0)