Skip to content

Commit e431a7a

Browse files
committed
[Storage] Support last access time
1 parent 57c03f2 commit e431a7a

21 files changed

+2217
-53
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,12 @@ public void TestStorageBlobContainerSoftDelete()
100100
{
101101
TestController.NewInstance.RunPsTest(_logger, "Test-StorageBlobContainerSoftDelete");
102102
}
103+
104+
[Fact]
105+
[Trait(Category.AcceptanceType, Category.CheckIn)]
106+
public void TestStorageBlobLastAccessTimeTracking()
107+
{
108+
TestController.NewInstance.RunPsTest(_logger, "Test-StorageBlobLastAccessTimeTracking");
109+
}
103110
}
104111
}

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,4 +818,72 @@ function Test-StorageBlobContainerSoftDelete
818818
}
819819
}
820820

821+
<#
822+
.SYNOPSIS
823+
Test StorageAccount Blob LastAccessTimeTracking
824+
.DESCRIPTION
825+
SmokeTest
826+
#>
827+
function Test-StorageBlobLastAccessTimeTracking
828+
{
829+
# Setup
830+
$rgname = Get-StorageManagementTestResourceName;
831+
832+
try
833+
{
834+
# Test
835+
$stoname = 'sto' + $rgname;
836+
$stotype = 'Standard_GRS';
837+
$loc = Get-ProviderLocation ResourceManagement;
838+
$kind = 'StorageV2'
839+
840+
Write-Verbose "RGName: $rgname | Loc: $loc"
841+
New-AzResourceGroup -Name $rgname -Location $loc;
842+
843+
$loc = Get-ProviderLocation_canary ResourceManagement;
844+
New-AzStorageAccount -ResourceGroupName $rgname -Name $stoname -Location $loc -Type $stotype -Kind $kind
845+
$stos = Get-AzStorageAccount -ResourceGroupName $rgname;
846+
847+
# Enable Blob LastAccessTimeTracking
848+
$policy = Enable-AzStorageBlobLastAccessTimeTracking -ResourceGroupName $rgname -StorageAccountName $stoname -PassThru
849+
Assert-AreEqual $true $policy.Enable
850+
$property = Get-AzStorageBlobServiceProperty -ResourceGroupName $rgname -StorageAccountName $stoname
851+
Assert-AreEqual $true $property.LastAccessTimeTrackingPolicy.Enable
852+
853+
# set management policy
854+
$action = Add-AzStorageAccountManagementPolicyAction -BaseBlobAction Delete -daysAfterModificationGreaterThan 100
855+
$action = Add-AzStorageAccountManagementPolicyAction -BaseBlobAction TierToArchive -DaysAfterLastAccessTimeGreaterThan 50 -InputObject $action
856+
$action = Add-AzStorageAccountManagementPolicyAction -BaseBlobAction TierToCool -DaysAfterLastAccessTimeGreaterThan 30 -EnableAutoTierToHotFromCool -InputObject $action
857+
$action = Add-AzStorageAccountManagementPolicyAction -SnapshotAction Delete -daysAfterCreationGreaterThan 100 -InputObject $action
858+
$filter = New-AzStorageAccountManagementPolicyFilter -PrefixMatch prefix1,prefix2
859+
$rule = New-AzStorageAccountManagementPolicyRule -Name Test -Action $action -Filter $filter
860+
$policy = Set-AzStorageAccountManagementPolicy -ResourceGroupName $rgname -StorageAccountName $stoname -Rule $rule
861+
Assert-AreEqual $true $policy.Rules[0].Definition.Actions.BaseBlob.EnableAutoTierToHotFromCool
862+
Assert-AreEqual 30 $policy.Rules[0].Definition.Actions.BaseBlob.TierToCool.DaysAfterLastAccessTimeGreaterThan
863+
Assert-AreEqual 50 $policy.Rules[0].Definition.Actions.BaseBlob.TierToArchive.DaysAfterLastAccessTimeGreaterThan
864+
Assert-AreEqual 100 $policy.Rules[0].Definition.Actions.BaseBlob.Delete.DaysAfterModificationGreaterThan
865+
866+
# remove management policy
867+
Remove-AzStorageAccountManagementPolicy -ResourceGroupName $rgname -StorageAccountName $stoname
868+
869+
# Disable Blob LastAccessTimeTracking
870+
$policy = Disable-AzStorageBlobLastAccessTimeTracking -ResourceGroupName $rgname -StorageAccountName $stoname -PassThru
871+
# Assert-AreEqual $true (($policy.Enable -eq $false) -or ($policy -eq $null))
872+
$property = Get-AzStorageBlobServiceProperty -ResourceGroupName $rgname -StorageAccountName $stoname
873+
#Assert-AreEqual $true (($property.LastAccessTimeTrackingPolicy.Enable -eq $false) -or ($property.LastAccessTimeTrackingPolicy -eq $null))
874+
# Assert-AreEqual $false $property.LastAccessTimeTrackingPolicy.Enable
875+
876+
Remove-AzStorageAccount -Force -ResourceGroupName $rgname -Name $stoname;
877+
}
878+
finally
879+
{
880+
# Cleanup
881+
Clean-ResourceGroup $rgname
882+
}
883+
}
884+
885+
886+
887+
888+
821889

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

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

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,9 @@ CmdletsToExport = 'Get-AzStorageAccount', 'Get-AzStorageAccountKey',
186186
'Remove-AzStorageBlobInventoryPolicy',
187187
'Enable-AzStorageContainerDeleteRetentionPolicy',
188188
'Disable-AzStorageContainerDeleteRetentionPolicy',
189-
'Restore-AzStorageContainer'
189+
'Restore-AzStorageContainer',
190+
'Enable-AzStorageBlobLastAccessTimeTracking',
191+
'Disable-AzStorageBlobLastAccessTimeTracking'
190192

191193
# Variables to export from this module
192194
# VariablesToExport = @()
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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.Commands.ResourceManager.Common.ArgumentCompleters;
19+
using Microsoft.Azure.Management.Storage;
20+
using Microsoft.Azure.Management.Storage.Models;
21+
using System.Management.Automation;
22+
23+
/// <summary>
24+
/// Modify Azure Storage service properties
25+
/// </summary>
26+
[Cmdlet("Disable", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + StorageBlobLastAccessTimeTracking, SupportsShouldProcess = true, DefaultParameterSetName = AccountNameParameterSet), OutputType(typeof(bool))]
27+
public class DisableAzStorageBlobLastAccessTimeTrackingCommand : StorageBlobBaseCmdlet
28+
{
29+
30+
/// <summary>
31+
/// AccountName Parameter Set
32+
/// </summary>
33+
private const string AccountNameParameterSet = "AccountName";
34+
35+
/// <summary>
36+
/// Account object parameter set
37+
/// </summary>
38+
private const string AccountObjectParameterSet = "AccountObject";
39+
40+
[Parameter(
41+
Position = 0,
42+
Mandatory = true,
43+
HelpMessage = "Resource Group Name.",
44+
ParameterSetName = AccountNameParameterSet)]
45+
[ResourceGroupCompleter]
46+
[ValidateNotNullOrEmpty]
47+
public string ResourceGroupName { get; set; }
48+
49+
[Parameter(
50+
Position = 1,
51+
Mandatory = true,
52+
HelpMessage = "Storage Account Name.",
53+
ParameterSetName = AccountNameParameterSet)]
54+
[ResourceNameCompleter("Microsoft.Storage/storageAccounts", nameof(ResourceGroupName))]
55+
[Alias(AccountNameAlias, NameAlias)]
56+
[ValidateNotNullOrEmpty]
57+
public string StorageAccountName { get; set; }
58+
59+
[Parameter(Mandatory = true,
60+
HelpMessage = "Storage account object",
61+
ValueFromPipeline = true,
62+
ParameterSetName = AccountObjectParameterSet)]
63+
[ValidateNotNullOrEmpty]
64+
public PSStorageAccount StorageAccount { get; set; }
65+
66+
[Parameter(Mandatory = false, HelpMessage = "Display ServiceProperties")]
67+
public SwitchParameter PassThru { get; set; }
68+
69+
public override void ExecuteCmdlet()
70+
{
71+
base.ExecuteCmdlet();
72+
if (ShouldProcess("BlobLastAccessTimeTracking", "Disable"))
73+
{
74+
switch (ParameterSetName)
75+
{
76+
case AccountObjectParameterSet:
77+
this.ResourceGroupName = StorageAccount.ResourceGroupName;
78+
this.StorageAccountName = StorageAccount.StorageAccountName;
79+
break;
80+
default:
81+
// For AccountNameParameterSet, the ResourceGroupName and StorageAccountName can get from input directly
82+
break;
83+
}
84+
BlobServiceProperties serviceProperties = new BlobServiceProperties();
85+
86+
serviceProperties.LastAccessTimeTrackingPolicy = new LastAccessTimeTrackingPolicy();
87+
serviceProperties.LastAccessTimeTrackingPolicy.Enable = false;
88+
89+
serviceProperties = this.StorageClient.BlobServices.SetServiceProperties(this.ResourceGroupName, this.StorageAccountName, serviceProperties);
90+
91+
if (PassThru)
92+
{
93+
WriteObject(true);
94+
}
95+
96+
}
97+
}
98+
}
99+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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.Commands.ResourceManager.Common.ArgumentCompleters;
19+
using Microsoft.Azure.Management.Storage;
20+
using Microsoft.Azure.Management.Storage.Models;
21+
using System.Management.Automation;
22+
23+
/// <summary>
24+
/// Modify Azure Storage service properties
25+
/// </summary>
26+
[Cmdlet("Enable", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + StorageBlobLastAccessTimeTracking, DefaultParameterSetName = AccountNameParameterSet, SupportsShouldProcess = true), OutputType(typeof(PSRestorePolicy))]
27+
public class EnableAzStorageBlobLastAccessTimeTrackingCommand : StorageBlobBaseCmdlet
28+
{
29+
30+
/// <summary>
31+
/// AccountName Parameter Set
32+
/// </summary>
33+
private const string AccountNameParameterSet = "AccountName";
34+
35+
/// <summary>
36+
/// Account object parameter set
37+
/// </summary>
38+
private const string AccountObjectParameterSet = "AccountObject";
39+
40+
/// <summary>
41+
/// BlobServiceProperties ResourceId parameter set
42+
/// </summary>
43+
private const string PropertiesResourceIdParameterSet = "BlobServicePropertiesResourceId";
44+
45+
[Parameter(
46+
Position = 0,
47+
Mandatory = true,
48+
HelpMessage = "Resource Group Name.",
49+
ParameterSetName = AccountNameParameterSet)]
50+
[ResourceGroupCompleter]
51+
[ValidateNotNullOrEmpty]
52+
public string ResourceGroupName { get; set; }
53+
54+
[Parameter(
55+
Position = 1,
56+
Mandatory = true,
57+
HelpMessage = "Storage Account Name.",
58+
ParameterSetName = AccountNameParameterSet)]
59+
[ResourceNameCompleter("Microsoft.Storage/storageAccounts", nameof(ResourceGroupName))]
60+
[Alias(AccountNameAlias, NameAlias)]
61+
[ValidateNotNullOrEmpty]
62+
public string StorageAccountName { get; set; }
63+
64+
[Parameter(Mandatory = true,
65+
HelpMessage = "Storage account object",
66+
ValueFromPipeline = true,
67+
ParameterSetName = AccountObjectParameterSet)]
68+
[ValidateNotNullOrEmpty]
69+
public PSStorageAccount StorageAccount { get; set; }
70+
71+
[Parameter(Mandatory = false, HelpMessage = "Display ServiceProperties")]
72+
public SwitchParameter PassThru { get; set; }
73+
74+
public override void ExecuteCmdlet()
75+
{
76+
base.ExecuteCmdlet();
77+
if (ShouldProcess("BlobLastAccessTimeTracking", "Enable"))
78+
{
79+
switch (ParameterSetName)
80+
{
81+
case AccountObjectParameterSet:
82+
this.ResourceGroupName = StorageAccount.ResourceGroupName;
83+
this.StorageAccountName = StorageAccount.StorageAccountName;
84+
break;
85+
default:
86+
// For AccountNameParameterSet, the ResourceGroupName and StorageAccountName can get from input directly
87+
break;
88+
}
89+
BlobServiceProperties serviceProperties = new BlobServiceProperties();
90+
91+
serviceProperties.LastAccessTimeTrackingPolicy = new LastAccessTimeTrackingPolicy();
92+
serviceProperties.LastAccessTimeTrackingPolicy.Enable = true;
93+
94+
serviceProperties = this.StorageClient.BlobServices.SetServiceProperties(this.ResourceGroupName, this.StorageAccountName, serviceProperties);
95+
96+
97+
if (PassThru)
98+
{
99+
//Get the full service properties from server for output
100+
serviceProperties = this.StorageClient.BlobServices.GetServiceProperties(this.ResourceGroupName, this.StorageAccountName);
101+
102+
WriteObject(new PSLastAccessTimeTrackingPolicy(serviceProperties.LastAccessTimeTrackingPolicy));
103+
}
104+
105+
}
106+
}
107+
}
108+
}

src/Storage/Storage.Management/Blob/StorageBlobBaseCmdlet.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public abstract class StorageBlobBaseCmdlet : AzureRMCmdlet
3939
protected const string StorageBlobDeleteRetentionPolicy = "StorageBlobDeleteRetentionPolicy";
4040
protected const string StorageContainerDeleteRetentionPolicy = "StorageContainerDeleteRetentionPolicy";
4141
protected const string StorageBlobRestorePolicy = "StorageBlobRestorePolicy";
42+
protected const string StorageBlobLastAccessTimeTracking = "StorageBlobLastAccessTimeTracking";
4243

4344
public const string StorageAccountResourceType = "Microsoft.Storage/storageAccounts";
4445

src/Storage/Storage.Management/ChangeLog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
- Additional information about change #1
1919
-->
2020
## Upcoming Release
21+
* Supported Blob Last Access Time
22+
- `Enable-AzStorageBlobLastAccessTimeTracking`
23+
- `Disable-AzStorageBlobLastAccessTimeTracking`
24+
- `Add-AzStorageAccountManagementPolicyAction`
25+
* Fixed a display issue in list datalake gen2 items
26+
- `Get-AzDataLakeGen2ChildItem`
2127

2228
## Version 3.9.0
2329
* Supported enable/disable Blob container soft delete

src/Storage/Storage.Management/Models/PSBlobServiceProperties.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class PSBlobServiceProperties
4747
public PSDeleteRetentionPolicy ContainerDeleteRetentionPolicy { get; set; }
4848
public PSCorsRules Cors { get; set; }
4949
public bool? IsVersioningEnabled { get; set; }
50+
public PSLastAccessTimeTrackingPolicy LastAccessTimeTrackingPolicy { get; set; }
5051

5152
public PSBlobServiceProperties()
5253
{ }
@@ -65,6 +66,7 @@ public PSBlobServiceProperties(BlobServiceProperties policy)
6566
this.ChangeFeed = policy.ChangeFeed is null ? null : new PSChangeFeed(policy.ChangeFeed);
6667
this.IsVersioningEnabled = policy.IsVersioningEnabled;
6768
this.ContainerDeleteRetentionPolicy = policy.ContainerDeleteRetentionPolicy is null ? null : new PSDeleteRetentionPolicy(policy.ContainerDeleteRetentionPolicy);
69+
this.LastAccessTimeTrackingPolicy = policy.LastAccessTimeTrackingPolicy is null? null : new PSLastAccessTimeTrackingPolicy(policy.LastAccessTimeTrackingPolicy);
6870
}
6971
public BlobServiceProperties ParseBlobServiceProperties()
7072
{
@@ -77,6 +79,7 @@ public BlobServiceProperties ParseBlobServiceProperties()
7779
ChangeFeed = this.ChangeFeed is null ? null : this.ChangeFeed.ParseChangeFeed(),
7880
IsVersioningEnabled = this.IsVersioningEnabled,
7981
ContainerDeleteRetentionPolicy = this.ContainerDeleteRetentionPolicy is null ? null : this.ContainerDeleteRetentionPolicy.ParseDeleteRetentionPolicy(),
82+
LastAccessTimeTrackingPolicy = this.LastAccessTimeTrackingPolicy is null ? null : this.LastAccessTimeTrackingPolicy.ParseLastAccessTimeTrackingPolicy()
8083
};
8184
}
8285

@@ -296,4 +299,36 @@ private static string[] ListToArray(IList<string> stringList)
296299
return stringArray;
297300
}
298301
}
302+
303+
/// <summary>
304+
/// Wrapper of SDK type LastAccessTimeTrackingPolicy
305+
/// </summary>
306+
public class PSLastAccessTimeTrackingPolicy
307+
{
308+
public bool Enable { get; set; }
309+
public string Name { get; set; }
310+
public int? TrackingGranularityInDays { get; set; }
311+
public string[] BlobType { get; set; }
312+
313+
314+
public PSLastAccessTimeTrackingPolicy(LastAccessTimeTrackingPolicy policy)
315+
{
316+
this.Name = policy.Name;
317+
this.Enable = policy.Enable;
318+
this.TrackingGranularityInDays = policy.TrackingGranularityInDays;
319+
this.BlobType = policy.BlobType is null ? null : new List<string>(policy.BlobType).ToArray();
320+
}
321+
322+
public LastAccessTimeTrackingPolicy ParseLastAccessTimeTrackingPolicy()
323+
{
324+
return new LastAccessTimeTrackingPolicy()
325+
{
326+
327+
Name = this.Name,
328+
Enable = this.Enable,
329+
TrackingGranularityInDays = this.TrackingGranularityInDays,
330+
BlobType = this.BlobType is null ? null : new List<string>(this.BlobType)
331+
};
332+
}
333+
}
299334
}

0 commit comments

Comments
 (0)