Skip to content

Commit 61cb0d6

Browse files
committed
[Storage] Support Blob Service Properties in SRP
1 parent ab394cf commit 61cb0d6

21 files changed

+3021
-6
lines changed

src/Storage/Storage.Management.Test/ScenarioTests/StorageBlobTests.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,12 @@ public void TestStorageBlobContainerImmutabilityPolicy()
5151
{
5252
TestController.NewInstance.RunPsTest(_logger, "Test-StorageBlobContainerImmutabilityPolicy");
5353
}
54+
55+
[Fact]
56+
[Trait(Category.AcceptanceType, Category.CheckIn)]
57+
public void TestStorageBlobServiceProperties()
58+
{
59+
TestController.NewInstance.RunPsTest(_logger, "Test-StorageBlobServiceProperties");
60+
}
5461
}
5562
}

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,4 +326,61 @@ function Test-StorageBlobContainerImmutabilityPolicy
326326
}
327327
}
328328

329+
<#
330+
.SYNOPSIS
331+
Test StorageAccount Blob Service Properties
332+
.DESCRIPTION
333+
SmokeTest
334+
#>
335+
function Test-StorageBlobServiceProperties
336+
{
337+
# Setup
338+
$rgname = Get-StorageManagementTestResourceName;
339+
340+
try
341+
{
342+
# Test
343+
$stoname = 'sto' + $rgname;
344+
$stotype = 'Standard_GRS';
345+
$loc = Get-ProviderLocation ResourceManagement;
346+
$kind = 'StorageV2'
347+
348+
Write-Verbose "RGName: $rgname | Loc: $loc"
349+
New-AzResourceGroup -Name $rgname -Location $loc;
350+
351+
New-AzStorageAccount -ResourceGroupName $rgname -Name $stoname -Location $loc -Type $stotype -Kind $kind
352+
$stos = Get-AzStorageAccount -ResourceGroupName $rgname;
353+
354+
# Update and Get Blob Service Properties
355+
$property = Update-AzStorageBlobServiceProperty -ResourceGroupName $rgname -StorageAccountName $stoname -DefaultServiceVersion 2018-03-28
356+
Assert-AreEqual '2018-03-28' $property.DefaultServiceVersion
357+
$property = Get-AzStorageBlobServiceProperty -ResourceGroupName $rgname -StorageAccountName $stoname
358+
Assert-AreEqual '2018-03-28' $property.DefaultServiceVersion
359+
360+
# Enable and Disable Blob Delete Retention Policy
361+
$policy = Enable-AzStorageBlobDeleteRetentionPolicy -ResourceGroupName $rgname -StorageAccountName $stoname -PassThru -RetentionDays 3
362+
Assert-AreEqual $true $policy.Enabled
363+
Assert-AreEqual 3 $policy.Days
364+
$property = Get-AzStorageBlobServiceProperty -ResourceGroupName $rgname -StorageAccountName $stoname
365+
Assert-AreEqual '2018-03-28' $property.DefaultServiceVersion
366+
Assert-AreEqual $true $property.DeleteRetentionPolicy.Enabled
367+
Assert-AreEqual 3 $property.DeleteRetentionPolicy.Days
368+
369+
$policy = Disable-AzStorageBlobDeleteRetentionPolicy -ResourceGroupName $rgname -StorageAccountName $stoname -PassThru
370+
Assert-AreEqual $false $policy.Enabled
371+
Assert-AreEqual $null $policy.Days
372+
$property = Get-AzStorageBlobServiceProperty -ResourceGroupName $rgname -StorageAccountName $stoname
373+
Assert-AreEqual '2018-03-28' $property.DefaultServiceVersion
374+
Assert-AreEqual $false $property.DeleteRetentionPolicy.Enabled
375+
Assert-AreEqual $null $property.DeleteRetentionPolicy.Days
376+
377+
Remove-AzStorageAccount -Force -ResourceGroupName $rgname -Name $stoname;
378+
}
379+
finally
380+
{
381+
# Cleanup
382+
Clean-ResourceGroup $rgname
383+
}
384+
}
385+
329386

src/Storage/Storage.Management.Test/SessionRecords/Microsoft.Azure.Commands.Management.Storage.Test.ScenarioTests.StorageBlobTests/TestStorageBlobServiceProperties.json

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

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,11 @@ CmdletsToExport = 'Get-AzStorageAccount', 'Get-AzStorageAccountKey',
150150
'Remove-AzStorageAccountManagementPolicy',
151151
'New-AzStorageAccountManagementPolicyFilter',
152152
'New-AzStorageAccountManagementPolicyRule',
153-
'Add-AzStorageAccountManagementPolicyAction'
153+
'Add-AzStorageAccountManagementPolicyAction',
154+
'Update-AzStorageBlobServiceProperty',
155+
'Get-AzStorageBlobServiceProperty',
156+
'Enable-AzStorageBlobDeleteRetentionPolicy',
157+
'Disable-AzStorageBlobDeleteRetentionPolicy'
154158

155159
# Variables to export from this module
156160
# VariablesToExport = @()
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
namespace Microsoft.Azure.Commands.Management.Storage
16+
{
17+
using Microsoft.Azure.Commands.Management.Storage.Models;
18+
using Microsoft.Azure.Management.Storage;
19+
using Microsoft.Azure.Management.Storage.Models;
20+
using System;
21+
using System.Collections.Generic;
22+
using System.Management.Automation;
23+
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
24+
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
25+
26+
/// <summary>
27+
/// Modify Azure Storage service properties
28+
/// </summary>
29+
[Cmdlet("Disable", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + StorageBlobDeleteRetentionPolicy, SupportsShouldProcess = true, DefaultParameterSetName = AccountNameParameterSet), OutputType(typeof(PSDeleteRetentionPolicy))]
30+
public class DisableAzStorageBlobDeleteRetentionPolicyCommand : StorageBlobBaseCmdlet
31+
{
32+
33+
/// <summary>
34+
/// AccountName Parameter Set
35+
/// </summary>
36+
private const string AccountNameParameterSet = "AccountName";
37+
38+
/// <summary>
39+
/// Account object parameter set
40+
/// </summary>
41+
private const string AccountObjectParameterSet = "AccountObject";
42+
43+
/// <summary>
44+
/// BlobServiceProperties ResourceId parameter set
45+
/// </summary>
46+
private const string PropertiesResourceIdParameterSet = "BlobServicePropertiesResourceId";
47+
48+
[Parameter(
49+
Position = 0,
50+
Mandatory = true,
51+
HelpMessage = "Resource Group Name.",
52+
ParameterSetName = AccountNameParameterSet)]
53+
[ResourceGroupCompleter]
54+
[ValidateNotNullOrEmpty]
55+
public string ResourceGroupName { get; set; }
56+
57+
[Parameter(
58+
Position = 1,
59+
Mandatory = true,
60+
HelpMessage = "Storage Account Name.",
61+
ParameterSetName = AccountNameParameterSet)]
62+
[ResourceNameCompleter("Microsoft.Storage/storageAccounts", nameof(ResourceGroupName))]
63+
[Alias(AccountNameAlias)]
64+
[ValidateNotNullOrEmpty]
65+
public string StorageAccountName { get; set; }
66+
67+
[Parameter(Mandatory = true,
68+
HelpMessage = "Storage account object",
69+
ValueFromPipeline = true,
70+
ParameterSetName = AccountObjectParameterSet)]
71+
[ValidateNotNullOrEmpty]
72+
public PSStorageAccount StorageAccount { get; set; }
73+
74+
[Parameter(
75+
Position = 0,
76+
Mandatory = true,
77+
ValueFromPipelineByPropertyName = true,
78+
HelpMessage = "Blob Service Properties Resource Id.",
79+
ParameterSetName = PropertiesResourceIdParameterSet)]
80+
[Alias(ResourceIdAlias)]
81+
[ValidateNotNullOrEmpty]
82+
public string BlobServicePropertyResourceId { get; set; }
83+
84+
[Parameter(Mandatory = false, HelpMessage = "Display ServiceProperties")]
85+
public SwitchParameter PassThru { get; set; }
86+
87+
public override void ExecuteCmdlet()
88+
{
89+
base.ExecuteCmdlet();
90+
if (ShouldProcess("BlobDeleteRetentionPolicy", "Disable"))
91+
{
92+
switch (ParameterSetName)
93+
{
94+
case AccountObjectParameterSet:
95+
this.ResourceGroupName = StorageAccount.ResourceGroupName;
96+
this.StorageAccountName = StorageAccount.StorageAccountName;
97+
break;
98+
case PropertiesResourceIdParameterSet:
99+
ResourceIdentifier blobServicePropertiesResource = new ResourceIdentifier(BlobServicePropertyResourceId);
100+
this.ResourceGroupName = blobServicePropertiesResource.ResourceGroupName;
101+
this.StorageAccountName = PSBlobServiceProperties.GetStorageAccountNameFromBlobServicePropertiesResourceId(BlobServicePropertyResourceId);
102+
break;
103+
default:
104+
// For AccountNameParameterSet, the ResourceGroupName and StorageAccountName can get from input directly
105+
break;
106+
}
107+
BlobServiceProperties serviceProperties = this.StorageClient.BlobServices.GetServiceProperties( this.ResourceGroupName, this.StorageAccountName);
108+
109+
serviceProperties.DeleteRetentionPolicy.Enabled = false;
110+
serviceProperties.DeleteRetentionPolicy.Days = null;
111+
112+
serviceProperties = this.StorageClient.BlobServices.SetServiceProperties(this.ResourceGroupName, this.StorageAccountName, serviceProperties);
113+
114+
if (PassThru)
115+
{
116+
WriteObject(new PSDeleteRetentionPolicy(serviceProperties.DeleteRetentionPolicy));
117+
}
118+
119+
}
120+
}
121+
}
122+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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+
namespace Microsoft.Azure.Commands.Management.Storage
16+
{
17+
using Microsoft.Azure.Commands.Management.Storage.Models;
18+
using Microsoft.Azure.Management.Storage;
19+
using Microsoft.Azure.Management.Storage.Models;
20+
using System;
21+
using System.Collections.Generic;
22+
using System.Management.Automation;
23+
using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters;
24+
using Microsoft.Azure.Management.Internal.Resources.Utilities.Models;
25+
26+
/// <summary>
27+
/// Modify Azure Storage service properties
28+
/// </summary>
29+
[Cmdlet("Enable", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + StorageBlobDeleteRetentionPolicy, DefaultParameterSetName = AccountNameParameterSet, SupportsShouldProcess = true), OutputType(typeof(PSBlobServiceProperties))]
30+
public class EnableAzStorageBlobDeleteRetentionPolicyCommand : StorageBlobBaseCmdlet
31+
{
32+
33+
/// <summary>
34+
/// AccountName Parameter Set
35+
/// </summary>
36+
private const string AccountNameParameterSet = "AccountName";
37+
38+
/// <summary>
39+
/// Account object parameter set
40+
/// </summary>
41+
private const string AccountObjectParameterSet = "AccountObject";
42+
43+
/// <summary>
44+
/// BlobServiceProperties ResourceId parameter set
45+
/// </summary>
46+
private const string PropertiesResourceIdParameterSet = "BlobServicePropertiesResourceId";
47+
48+
[Parameter(
49+
Position = 0,
50+
Mandatory = true,
51+
HelpMessage = "Resource Group Name.",
52+
ParameterSetName = AccountNameParameterSet)]
53+
[ResourceGroupCompleter]
54+
[ValidateNotNullOrEmpty]
55+
public string ResourceGroupName { get; set; }
56+
57+
[Parameter(
58+
Position = 1,
59+
Mandatory = true,
60+
HelpMessage = "Storage Account Name.",
61+
ParameterSetName = AccountNameParameterSet)]
62+
[ResourceNameCompleter("Microsoft.Storage/storageAccounts", nameof(ResourceGroupName))]
63+
[Alias(AccountNameAlias)]
64+
[ValidateNotNullOrEmpty]
65+
public string StorageAccountName { get; set; }
66+
67+
[Parameter(Mandatory = true,
68+
HelpMessage = "Storage account object",
69+
ValueFromPipeline = true,
70+
ParameterSetName = AccountObjectParameterSet)]
71+
[ValidateNotNullOrEmpty]
72+
public PSStorageAccount StorageAccount { get; set; }
73+
74+
[Parameter(
75+
Position = 0,
76+
Mandatory = true,
77+
ValueFromPipelineByPropertyName = true,
78+
HelpMessage = "Blob Service Properties Resource Id.",
79+
ParameterSetName = PropertiesResourceIdParameterSet)]
80+
[Alias(ResourceIdAlias)]
81+
[ValidateNotNullOrEmpty]
82+
public string BlobServicePropertyResourceId { get; set; }
83+
84+
[Parameter(Mandatory = true, HelpMessage = "Sets the number of retention days for the DeleteRetentionPolicy.")]
85+
[Alias("Days")]
86+
public int RetentionDays { get; set; }
87+
88+
[Parameter(Mandatory = false, HelpMessage = "Display ServiceProperties")]
89+
public SwitchParameter PassThru { get; set; }
90+
91+
public override void ExecuteCmdlet()
92+
{
93+
base.ExecuteCmdlet();
94+
if (ShouldProcess("BlobDeleteRetentionPolicy", "Enable"))
95+
{
96+
switch (ParameterSetName)
97+
{
98+
case AccountObjectParameterSet:
99+
this.ResourceGroupName = StorageAccount.ResourceGroupName;
100+
this.StorageAccountName = StorageAccount.StorageAccountName;
101+
break;
102+
case PropertiesResourceIdParameterSet:
103+
ResourceIdentifier blobServicePropertiesResource = new ResourceIdentifier(BlobServicePropertyResourceId);
104+
this.ResourceGroupName = blobServicePropertiesResource.ResourceGroupName;
105+
this.StorageAccountName = PSBlobServiceProperties.GetStorageAccountNameFromBlobServicePropertiesResourceId(BlobServicePropertyResourceId);
106+
break;
107+
default:
108+
// For AccountNameParameterSet, the ResourceGroupName and StorageAccountName can get from input directly
109+
break;
110+
}
111+
BlobServiceProperties serviceProperties = this.StorageClient.BlobServices.GetServiceProperties( this.ResourceGroupName, this.StorageAccountName);
112+
113+
serviceProperties.DeleteRetentionPolicy.Enabled = true;
114+
serviceProperties.DeleteRetentionPolicy.Days = RetentionDays;
115+
116+
serviceProperties = this.StorageClient.BlobServices.SetServiceProperties(this.ResourceGroupName, this.StorageAccountName, serviceProperties);
117+
118+
if (PassThru)
119+
{
120+
WriteObject(new PSDeleteRetentionPolicy(serviceProperties.DeleteRetentionPolicy));
121+
}
122+
123+
}
124+
}
125+
}
126+
}

0 commit comments

Comments
 (0)