Skip to content

Commit 1dc3cef

Browse files
authored
Merge pull request Azure#8874 from wastoresh/blobservice
[Storage] Support Blob Service Properties in SRP
2 parents ab394cf + 280d148 commit 1dc3cef

21 files changed

+2957
-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: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
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, NameAlias)]
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 = "Input a Storage account Resource Id, or a Blob service properties Resource Id.",
79+
ParameterSetName = PropertiesResourceIdParameterSet)]
80+
[ValidateNotNullOrEmpty]
81+
public string ResourceId { get; set; }
82+
83+
[Parameter(Mandatory = false, HelpMessage = "Display ServiceProperties")]
84+
public SwitchParameter PassThru { get; set; }
85+
86+
public override void ExecuteCmdlet()
87+
{
88+
base.ExecuteCmdlet();
89+
if (ShouldProcess("BlobDeleteRetentionPolicy", "Disable"))
90+
{
91+
switch (ParameterSetName)
92+
{
93+
case AccountObjectParameterSet:
94+
this.ResourceGroupName = StorageAccount.ResourceGroupName;
95+
this.StorageAccountName = StorageAccount.StorageAccountName;
96+
break;
97+
case PropertiesResourceIdParameterSet:
98+
ResourceIdentifier blobServicePropertiesResource = new ResourceIdentifier(ResourceId);
99+
this.ResourceGroupName = blobServicePropertiesResource.ResourceGroupName;
100+
this.StorageAccountName = PSBlobServiceProperties.GetStorageAccountNameFromResourceId(ResourceId);
101+
break;
102+
default:
103+
// For AccountNameParameterSet, the ResourceGroupName and StorageAccountName can get from input directly
104+
break;
105+
}
106+
BlobServiceProperties serviceProperties = this.StorageClient.BlobServices.GetServiceProperties( this.ResourceGroupName, this.StorageAccountName);
107+
108+
serviceProperties.DeleteRetentionPolicy.Enabled = false;
109+
serviceProperties.DeleteRetentionPolicy.Days = null;
110+
111+
serviceProperties = this.StorageClient.BlobServices.SetServiceProperties(this.ResourceGroupName, this.StorageAccountName, serviceProperties);
112+
113+
if (PassThru)
114+
{
115+
WriteObject(new PSDeleteRetentionPolicy(serviceProperties.DeleteRetentionPolicy));
116+
}
117+
118+
}
119+
}
120+
}
121+
}
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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, NameAlias)]
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 = "Input a Storage account Resource Id, or a Blob service properties Resource Id.",
79+
ParameterSetName = PropertiesResourceIdParameterSet)]
80+
[ValidateNotNullOrEmpty]
81+
public string ResourceId { get; set; }
82+
83+
[Parameter(Mandatory = true, HelpMessage = "Sets the number of retention days for the DeleteRetentionPolicy.")]
84+
[Alias("Days")]
85+
public int RetentionDays { get; set; }
86+
87+
[Parameter(Mandatory = false, HelpMessage = "Display ServiceProperties")]
88+
public SwitchParameter PassThru { get; set; }
89+
90+
public override void ExecuteCmdlet()
91+
{
92+
base.ExecuteCmdlet();
93+
if (ShouldProcess("BlobDeleteRetentionPolicy", "Enable"))
94+
{
95+
switch (ParameterSetName)
96+
{
97+
case AccountObjectParameterSet:
98+
this.ResourceGroupName = StorageAccount.ResourceGroupName;
99+
this.StorageAccountName = StorageAccount.StorageAccountName;
100+
break;
101+
case PropertiesResourceIdParameterSet:
102+
ResourceIdentifier blobServicePropertiesResource = new ResourceIdentifier(ResourceId);
103+
this.ResourceGroupName = blobServicePropertiesResource.ResourceGroupName;
104+
this.StorageAccountName = PSBlobServiceProperties.GetStorageAccountNameFromResourceId(ResourceId);
105+
break;
106+
default:
107+
// For AccountNameParameterSet, the ResourceGroupName and StorageAccountName can get from input directly
108+
break;
109+
}
110+
BlobServiceProperties serviceProperties = this.StorageClient.BlobServices.GetServiceProperties( this.ResourceGroupName, this.StorageAccountName);
111+
112+
serviceProperties.DeleteRetentionPolicy.Enabled = true;
113+
serviceProperties.DeleteRetentionPolicy.Days = RetentionDays;
114+
115+
serviceProperties = this.StorageClient.BlobServices.SetServiceProperties(this.ResourceGroupName, this.StorageAccountName, serviceProperties);
116+
117+
if (PassThru)
118+
{
119+
WriteObject(new PSDeleteRetentionPolicy(serviceProperties.DeleteRetentionPolicy));
120+
}
121+
122+
}
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)