diff --git a/src/Storage/Storage.Management/Models/PSBlobRestore.cs b/src/Storage/Storage.Management/Models/PSBlobRestore.cs index 33384b2d60a6..b0609e3ccff7 100644 --- a/src/Storage/Storage.Management/Models/PSBlobRestore.cs +++ b/src/Storage/Storage.Management/Models/PSBlobRestore.cs @@ -57,7 +57,7 @@ public PSBlobRestoreRange(Track2Models.BlobRestoreRange range) foreach (PSBlobRestoreRange range in ranges) { re.Add( - new Track2Models.BlobRestoreRange(range.EndRange, range.StartRange)); + new Track2Models.BlobRestoreRange(range.StartRange, range.EndRange)); } } return re; @@ -112,6 +112,14 @@ public PSBlobRestoreStatus(Track2Models.BlobRestoreStatus status) this.Parameters = status.Parameters is null ? null : new PSBlobRestoreParameters(status.Parameters); } } + + public PSBlobRestoreStatus(string status, string failureReason, string restoreId, PSBlobRestoreParameters parameters) + { + Status = status; + FailureReason = failureReason; + RestoreId = restoreId; + Parameters = parameters; + } } /// diff --git a/src/Storage/Storage.Management/Models/PSDataPolicy.cs b/src/Storage/Storage.Management/Models/PSDataPolicy.cs index 79df6fec2255..6f17877168f3 100644 --- a/src/Storage/Storage.Management/Models/PSDataPolicy.cs +++ b/src/Storage/Storage.Management/Models/PSDataPolicy.cs @@ -12,7 +12,8 @@ // limitations under the License. // ---------------------------------------------------------------------------------- -using Microsoft.Azure.Management.Storage.Models; +using Track2 = global::Azure.ResourceManager.Storage; +using Azure.ResourceManager.Storage.Models; using Microsoft.WindowsAzure.Commands.Common.Attributes; using System; using System.Collections.Generic; @@ -27,16 +28,17 @@ public class PSManagementPolicy public PSManagementPolicy() { } - public PSManagementPolicy(ManagementPolicy policy, string ResourceGroupName, string StorageAccountName) + public PSManagementPolicy(Track2.ManagementPolicyResource policyResource, string resourceGroupName, string storageAccountName) { - this.ResourceGroupName = ResourceGroupName; - this.StorageAccountName = StorageAccountName; - this.Id = policy.Id; - this.Name = policy.Name; - this.Type = policy.Type; - this.LastModifiedTime = policy.LastModifiedTime; - this.Rules = PSManagementPolicyRule.GetPSManagementPolicyRules(policy.Policy.Rules); + this.ResourceGroupName = resourceGroupName; + this.StorageAccountName = storageAccountName; + this.Id = policyResource.Id; + this.Name = policyResource.Data.Name; + this.Type = Track2.ManagementPolicyResource.ResourceType.ToString(); + this.LastModifiedTime = policyResource.Data.LastModifiedOn; + this.Rules = PSManagementPolicyRule.GetPSManagementPolicyRules(policyResource.Data.Rules); } + [Ps1Xml(Label = "ResourceGroupName", Target = ViewControl.List, Position = 0)] public string ResourceGroupName { get; set; } [Ps1Xml(Label = "StorageAccountName", Target = ViewControl.List, Position = 1)] @@ -47,7 +49,7 @@ public PSManagementPolicy(ManagementPolicy policy, string ResourceGroupName, str [Ps1Xml(Label = "Type", Target = ViewControl.List, Position = 3)] public string Type { get; set; } [Ps1Xml(Label = "LastModifiedTime", Target = ViewControl.List, Position = 4)] - public DateTime? LastModifiedTime { get; set; } + public DateTimeOffset? LastModifiedTime { get; set; } [Ps1Xml(Label = "Rules", Target = ViewControl.List, ScriptBlock = "ConvertTo-Json $_ -Depth 10", Position = 5)] public PSManagementPolicyRule[] Rules { get; set; } } @@ -65,43 +67,45 @@ public PSManagementPolicyRule() { } - public PSManagementPolicyRule(ManagementPolicyRule rule) + public PSManagementPolicyRule(global::Azure.ResourceManager.Storage.Models.ManagementPolicyRule rule) { this.Enabled = rule.Enabled; this.Name = rule.Name; this.Definition = rule.Definition is null ? null : new PSManagementPolicyDefinition(rule.Definition); } - public ManagementPolicyRule ParseManagementPolicyRule() + public Track2.Models.ManagementPolicyRule ParseManagementPolicyRule() { - ManagementPolicyRule rule = new ManagementPolicyRule(); + Track2.Models.ManagementPolicyRule rule = new Track2.Models.ManagementPolicyRule( + this.Name, + RuleType.Lifecycle, + this.Definition?.ParseManagementPolicyDefination() + ); rule.Enabled = this.Enabled; - rule.Name = this.Name; - rule.Definition = this.Definition is null ? null : this.Definition.ParseManagementPolicyDefination(); return rule; } - public static PSManagementPolicyRule[] GetPSManagementPolicyRules(IList rules) + public static PSManagementPolicyRule[] GetPSManagementPolicyRules(IList rules) { if (rules == null) { return null; } List psrules = new List(); - foreach (ManagementPolicyRule rule in rules) + foreach (Track2.Models.ManagementPolicyRule rule in rules) { psrules.Add(new PSManagementPolicyRule(rule)); } return psrules.ToArray(); } - public static List ParseManagementPolicyRules(PSManagementPolicyRule[] psrules) + public static List ParseManagementPolicyRules(PSManagementPolicyRule[] psrules) { if (psrules == null) { return null; } - List rules = new List(); + List rules = new List(); foreach (PSManagementPolicyRule psrule in psrules) { rules.Add(psrule.ParseManagementPolicyRule()); @@ -122,18 +126,18 @@ public PSManagementPolicyDefinition() { } - public PSManagementPolicyDefinition(ManagementPolicyDefinition defination) + public PSManagementPolicyDefinition(Track2.Models.ManagementPolicyDefinition defination) { this.Actions = defination.Actions is null ? null : new PSManagementPolicyActionGroup(defination.Actions); this.Filters = defination.Filters is null ? null : new PSManagementPolicyRuleFilter(defination.Filters); } - public ManagementPolicyDefinition ParseManagementPolicyDefination() + public Track2.Models.ManagementPolicyDefinition ParseManagementPolicyDefination() { - return new ManagementPolicyDefinition() - { - Actions = this.Actions is null ? null : this.Actions.ParseManagementPolicyAction(), - Filters = this.Filters is null ? null : this.Filters.ParseManagementPolicyFilter() - }; + Track2.Models.ManagementPolicyAction actions = this.Actions?.ParseManagementPolicyAction(); + Track2.Models.ManagementPolicyDefinition policyDefinition = new Track2.Models.ManagementPolicyDefinition(actions); + policyDefinition.Filters = this.Filters?.ParseManagementPolicyFilter(); + + return policyDefinition; } public static string[] StringListToArray(IList list) @@ -153,23 +157,38 @@ public class PSManagementPolicyRuleFilter public PSManagementPolicyRuleFilter() { } - public PSManagementPolicyRuleFilter(ManagementPolicyFilter filter) + public PSManagementPolicyRuleFilter(Track2.Models.ManagementPolicyFilter filter) { this.PrefixMatch = StringListToArray(filter.PrefixMatch); this.BlobTypes = StringListToArray(filter.BlobTypes); } - public ManagementPolicyFilter ParseManagementPolicyFilter() + public Track2.Models.ManagementPolicyFilter ParseManagementPolicyFilter() { - return new ManagementPolicyFilter() + Track2.Models.ManagementPolicyFilter policyFilter = new Track2.Models.ManagementPolicyFilter(StringArrayToList(this.BlobTypes)); + if (this.PrefixMatch != null) { - PrefixMatch = StringArrayToList(this.PrefixMatch), - BlobTypes = StringArrayToList(this.BlobTypes), - }; + foreach (string prefixMatch in this.PrefixMatch) + { + policyFilter.PrefixMatch.Add(prefixMatch); + } + } + return policyFilter; } public static string[] StringListToArray(IList list) { - return (list is null ? null : ((List)list).ToArray()); + if (list is null) + { + return null; + } + + var result = new string[list.Count]; + + for (int i = 0; i < list.Count; i++) + { + result[i] = list[i].ToString(); + } + return result; } public static List StringArrayToList(string[] array) @@ -190,19 +209,19 @@ public class PSManagementPolicyActionGroup public PSManagementPolicyActionGroup() { } - public PSManagementPolicyActionGroup(ManagementPolicyAction action) + public PSManagementPolicyActionGroup(Track2.Models.ManagementPolicyAction action) { this.BaseBlob = (action is null || action.BaseBlob is null) ? null : new PSManagementPolicyBaseBlob(action.BaseBlob); this.Snapshot = (action is null || action.Snapshot is null) ? null : new PSManagementPolicySnapShot(action.Snapshot); this.Version = (action is null || action.Version is null) ? null : new PSManagementPolicyVersion(action.Version); } - public ManagementPolicyAction ParseManagementPolicyAction() + public Track2.Models.ManagementPolicyAction ParseManagementPolicyAction() { - return new ManagementPolicyAction() + return new Track2.Models.ManagementPolicyAction() { - BaseBlob = this.BaseBlob is null ? null : this.BaseBlob.ParseManagementPolicyBaseBlob(), - Snapshot = this.Snapshot is null ? null : this.Snapshot.ParseManagementPolicySnapShot(), - Version = this.Version is null ? null : this.Version.ParseManagementPolicyVersion(), + BaseBlob = this.BaseBlob?.ParseManagementPolicyBaseBlob(), + Snapshot = this.Snapshot?.ParseManagementPolicySnapShot(), + Version = this.Version?.ParseManagementPolicyVersion(), }; } } @@ -220,20 +239,20 @@ public class PSManagementPolicyBaseBlob public PSManagementPolicyBaseBlob() { } - public PSManagementPolicyBaseBlob(ManagementPolicyBaseBlob blobAction) + public PSManagementPolicyBaseBlob(Track2.Models.ManagementPolicyBaseBlob blobAction) { this.TierToCool = blobAction.TierToCool is null ? null : new PSDateAfterModification(blobAction.TierToCool); this.TierToArchive = blobAction.TierToArchive is null ? null : new PSDateAfterModification(blobAction.TierToArchive); this.Delete = blobAction.Delete is null ? null : new PSDateAfterModification(blobAction.Delete); this.EnableAutoTierToHotFromCool = blobAction.EnableAutoTierToHotFromCool; } - public ManagementPolicyBaseBlob ParseManagementPolicyBaseBlob() + public Track2.Models.ManagementPolicyBaseBlob ParseManagementPolicyBaseBlob() { - return new ManagementPolicyBaseBlob() + return new Track2.Models.ManagementPolicyBaseBlob() { - TierToCool = this.TierToCool is null ? null : this.TierToCool.ParseDateAfterModification(), - TierToArchive = this.TierToArchive is null ? null : this.TierToArchive.ParseDateAfterModification(), - Delete = this.Delete is null ? null : this.Delete.ParseDateAfterModification(), + TierToCool = this.TierToCool?.ParseDateAfterModification(), + TierToArchive = this.TierToArchive?.ParseDateAfterModification(), + Delete = this.Delete?.ParseDateAfterModification(), EnableAutoTierToHotFromCool = this.EnableAutoTierToHotFromCool }; } @@ -251,20 +270,32 @@ public class PSManagementPolicySnapShot public PSManagementPolicySnapShot() { } - public PSManagementPolicySnapShot(ManagementPolicySnapShot blobAction) + public PSManagementPolicySnapShot(Track2.Models.ManagementPolicySnapShot blobAction) { - this.Delete = blobAction.Delete is null ? null : new PSDateAfterCreation(blobAction.Delete); - this.TierToCool = blobAction.TierToCool is null ? null : new PSDateAfterCreation(blobAction.TierToCool); - this.TierToArchive = blobAction.TierToArchive is null ? null : new PSDateAfterCreation(blobAction.TierToArchive); + + this.Delete = blobAction.DeleteDaysAfterCreationGreaterThan is null ? null : new PSDateAfterCreation((int)blobAction.DeleteDaysAfterCreationGreaterThan); + this.TierToCool = blobAction.TierToCoolDaysAfterCreationGreaterThan is null ? null : new PSDateAfterCreation((int)blobAction.TierToCoolDaysAfterCreationGreaterThan); + TierToArchive = blobAction.TierToArchiveDaysAfterCreationGreaterThan is null ? null : new PSDateAfterCreation((int)blobAction.TierToArchiveDaysAfterCreationGreaterThan); } - public ManagementPolicySnapShot ParseManagementPolicySnapShot() + public Track2.Models.ManagementPolicySnapShot ParseManagementPolicySnapShot() { - return new ManagementPolicySnapShot() + + Track2.Models.ManagementPolicySnapShot snapShot = new Track2.Models.ManagementPolicySnapShot(); + + if (this.Delete != null) { - Delete = this.Delete is null ? null : this.Delete.ParseDateAfterCreation(), - TierToCool = this.TierToCool is null ? null : this.TierToCool.ParseDateAfterCreation(), - TierToArchive = this.TierToArchive is null ? null : this.TierToArchive.ParseDateAfterCreation() - }; + snapShot.DeleteDaysAfterCreationGreaterThan = this.Delete.DaysAfterCreationGreaterThan; + } + if (this.TierToCool != null) + { + snapShot.TierToCoolDaysAfterCreationGreaterThan = this.TierToCool.DaysAfterCreationGreaterThan; + } + if (this.TierToArchive != null) + { + snapShot.TierToArchiveDaysAfterCreationGreaterThan = this.TierToArchive.DaysAfterCreationGreaterThan; + } + + return snapShot; } } @@ -280,20 +311,29 @@ public class PSManagementPolicyVersion public PSManagementPolicyVersion() { } - public PSManagementPolicyVersion(ManagementPolicyVersion blobAction) + public PSManagementPolicyVersion(Track2.Models.ManagementPolicyVersion blobAction) { - this.Delete = blobAction.Delete is null ? null : new PSDateAfterCreation(blobAction.Delete); - this.TierToCool = blobAction.TierToCool is null ? null : new PSDateAfterCreation(blobAction.TierToCool); - this.TierToArchive = blobAction.TierToArchive is null ? null : new PSDateAfterCreation(blobAction.TierToArchive); + this.Delete = blobAction.DeleteDaysAfterCreationGreaterThan is null ? null : new PSDateAfterCreation((int)blobAction.DeleteDaysAfterCreationGreaterThan); + this.TierToCool = blobAction.TierToCoolDaysAfterCreationGreaterThan is null ? null :new PSDateAfterCreation((int)blobAction.TierToCoolDaysAfterCreationGreaterThan); + this.TierToArchive = blobAction.TierToArchiveDaysAfterCreationGreaterThan is null ? null : new PSDateAfterCreation((int)blobAction.TierToArchiveDaysAfterCreationGreaterThan); } - public ManagementPolicyVersion ParseManagementPolicyVersion() + public Track2.Models.ManagementPolicyVersion ParseManagementPolicyVersion() { - return new ManagementPolicyVersion() + Track2.Models.ManagementPolicyVersion policyVersion = new Track2.Models.ManagementPolicyVersion(); + if (this.Delete != null) { - Delete = this.Delete is null ? null : this.Delete.ParseDateAfterCreation(), - TierToCool = this.TierToCool is null ? null : this.TierToCool.ParseDateAfterCreation(), - TierToArchive = this.TierToArchive is null ? null : this.TierToArchive.ParseDateAfterCreation() - }; + policyVersion.DeleteDaysAfterCreationGreaterThan = this.Delete.DaysAfterCreationGreaterThan; + } + if (this.TierToCool != null) + { + policyVersion.TierToCoolDaysAfterCreationGreaterThan = this.TierToCool.DaysAfterCreationGreaterThan; + } + if (this.TierToArchive != null) + { + policyVersion.TierToArchiveDaysAfterCreationGreaterThan = this.TierToArchive.DaysAfterCreationGreaterThan; + } + + return policyVersion; } } @@ -304,6 +344,8 @@ public class PSDateAfterModification { public int? DaysAfterModificationGreaterThan { get; set; } public int? DaysAfterLastAccessTimeGreaterThan { get; set; } + + // TODO: DaysAfterLastTierChangeGreaterThan it not supported by SDK yet. Will add later. public int? DaysAfterLastTierChangeGreaterThan { get; set; } public PSDateAfterModification() @@ -331,7 +373,7 @@ public PSDateAfterModification(int? daysAfterModificationGreaterThan, int? daysA this.DaysAfterLastTierChangeGreaterThan = DaysAfterLastTierChangeGreaterThan; } - public PSDateAfterModification(DateAfterModification data) + public PSDateAfterModification(Track2.Models.DateAfterModification data) { if (data.DaysAfterModificationGreaterThan is null) { @@ -349,18 +391,15 @@ public PSDateAfterModification(DateAfterModification data) { this.DaysAfterLastAccessTimeGreaterThan = Convert.ToInt32(data.DaysAfterLastAccessTimeGreaterThan); } - if (data.DaysAfterLastTierChangeGreaterThan is null) - { - this.DaysAfterLastTierChangeGreaterThan = null; - } - else - { - this.DaysAfterLastTierChangeGreaterThan = Convert.ToInt32(data.DaysAfterLastTierChangeGreaterThan); - } } - public DateAfterModification ParseDateAfterModification() + public Track2.Models.DateAfterModification ParseDateAfterModification() { - return new DateAfterModification(this.DaysAfterModificationGreaterThan, this.DaysAfterLastAccessTimeGreaterThan, this.DaysAfterLastTierChangeGreaterThan); + Track2.Models.DateAfterModification dateAfterModification = new Track2.Models.DateAfterModification(); + dateAfterModification.DaysAfterLastAccessTimeGreaterThan = this.DaysAfterLastAccessTimeGreaterThan; + dateAfterModification.DaysAfterModificationGreaterThan = this.DaysAfterModificationGreaterThan; + // TODO: Add DaysAfterLastTierChangeGreaterThan once supported by Track2 SDK + return dateAfterModification; + } } @@ -373,6 +412,8 @@ public class PSDateAfterCreation public int DaysAfterCreationGreaterThan { get; set; } public int? DaysAfterLastTierChangeGreaterThan { get; set; } + // TODO: DaysAfterLastTierChangeGreaterThan is still not supported by SDK, will add later. + public PSDateAfterCreation() { this.DaysAfterCreationGreaterThan = 0; @@ -382,29 +423,10 @@ public PSDateAfterCreation(int daysAfterCreationGreaterThan) { this.DaysAfterCreationGreaterThan = daysAfterCreationGreaterThan; } - public PSDateAfterCreation(int daysAfterCreationGreaterThan, int? DaysAfterLastTierChangeGreaterThan) { this.DaysAfterCreationGreaterThan = daysAfterCreationGreaterThan; this.DaysAfterLastTierChangeGreaterThan = DaysAfterLastTierChangeGreaterThan; } - - public PSDateAfterCreation(DateAfterCreation data) - { - this.DaysAfterCreationGreaterThan = Convert.ToInt32(data.DaysAfterCreationGreaterThan); - if (data.DaysAfterLastTierChangeGreaterThan is null) - { - this.DaysAfterLastTierChangeGreaterThan = null; - } - else - { - this.DaysAfterLastTierChangeGreaterThan = Convert.ToInt32(data.DaysAfterLastTierChangeGreaterThan); - } - } - public DateAfterCreation ParseDateAfterCreation() - { - return new DateAfterCreation(this.DaysAfterCreationGreaterThan, this.DaysAfterLastTierChangeGreaterThan); - } } - } diff --git a/src/Storage/Storage.Management/Storage.Management.csproj b/src/Storage/Storage.Management/Storage.Management.csproj index af2abb9c9c5f..a4f7615c7603 100644 --- a/src/Storage/Storage.Management/Storage.Management.csproj +++ b/src/Storage/Storage.Management/Storage.Management.csproj @@ -16,11 +16,10 @@ - - + diff --git a/src/Storage/Storage.Management/StorageAccount/AddAzureStorageAccountManagementPolicyAction.cs b/src/Storage/Storage.Management/StorageAccount/AddAzureStorageAccountManagementPolicyAction.cs index 7a705a7de168..0ffd7acb1e11 100644 --- a/src/Storage/Storage.Management/StorageAccount/AddAzureStorageAccountManagementPolicyAction.cs +++ b/src/Storage/Storage.Management/StorageAccount/AddAzureStorageAccountManagementPolicyAction.cs @@ -14,8 +14,6 @@ using Microsoft.Azure.Commands.Management.Storage.Models; using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; -using Microsoft.Azure.Management.Storage; -using Microsoft.Azure.Management.Storage.Models; using Microsoft.WindowsAzure.Commands.Common.CustomAttributes; using System.Globalization; using System.Management.Automation; diff --git a/src/Storage/Storage.Management/StorageAccount/GetAzureStorageAccountManagementPolicy.cs b/src/Storage/Storage.Management/StorageAccount/GetAzureStorageAccountManagementPolicy.cs index 3a733089ac2d..5cef34258a16 100644 --- a/src/Storage/Storage.Management/StorageAccount/GetAzureStorageAccountManagementPolicy.cs +++ b/src/Storage/Storage.Management/StorageAccount/GetAzureStorageAccountManagementPolicy.cs @@ -12,11 +12,10 @@ // limitations under the License. // ---------------------------------------------------------------------------------- +using Azure.ResourceManager.Storage; using Microsoft.Azure.Commands.Management.Storage.Models; using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; using Microsoft.Azure.Management.Internal.Resources.Utilities.Models; -using Microsoft.Azure.Management.Storage; -using Microsoft.Azure.Management.Storage.Models; using Microsoft.WindowsAzure.Commands.Common.CustomAttributes; using System.Management.Automation; @@ -95,11 +94,11 @@ public override void ExecuteCmdlet() break; } - ManagementPolicy managementPolicy = this.StorageClient.ManagementPolicies.Get( - this.ResourceGroupName, - this.StorageAccountName); + ManagementPolicyResource policy = this.StorageClientTrack2.GetManagementPolicyResource( + this.ResourceGroupName, this.StorageAccountName, "default").Get(); - WriteObject(new PSManagementPolicy(managementPolicy, this.ResourceGroupName, this.StorageAccountName), true); + var result = new PSManagementPolicy(policy, this.ResourceGroupName, this.StorageAccountName); + WriteObject(result, true); } } } diff --git a/src/Storage/Storage.Management/StorageAccount/NewAzureStorageAccountManagementPolicyFilter.cs b/src/Storage/Storage.Management/StorageAccount/NewAzureStorageAccountManagementPolicyFilter.cs index ebc60cc76948..1f86cde882c7 100644 --- a/src/Storage/Storage.Management/StorageAccount/NewAzureStorageAccountManagementPolicyFilter.cs +++ b/src/Storage/Storage.Management/StorageAccount/NewAzureStorageAccountManagementPolicyFilter.cs @@ -14,8 +14,6 @@ using Microsoft.Azure.Commands.Management.Storage.Models; using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; -using Microsoft.Azure.Management.Storage; -using Microsoft.Azure.Management.Storage.Models; using System.Management.Automation; namespace Microsoft.Azure.Commands.Management.Storage diff --git a/src/Storage/Storage.Management/StorageAccount/NewAzureStorageAccountManagementPolicyRule.cs b/src/Storage/Storage.Management/StorageAccount/NewAzureStorageAccountManagementPolicyRule.cs index 1ec8df9d86a9..5814084564d8 100644 --- a/src/Storage/Storage.Management/StorageAccount/NewAzureStorageAccountManagementPolicyRule.cs +++ b/src/Storage/Storage.Management/StorageAccount/NewAzureStorageAccountManagementPolicyRule.cs @@ -14,8 +14,6 @@ using Microsoft.Azure.Commands.Management.Storage.Models; using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; -using Microsoft.Azure.Management.Storage; -using Microsoft.Azure.Management.Storage.Models; using Microsoft.WindowsAzure.Commands.Common.CustomAttributes; using System.Management.Automation; diff --git a/src/Storage/Storage.Management/StorageAccount/RemoveAzureStorageAccountManagementPolicy.cs b/src/Storage/Storage.Management/StorageAccount/RemoveAzureStorageAccountManagementPolicy.cs index 3ca9d1f78763..21aa7877b98a 100644 --- a/src/Storage/Storage.Management/StorageAccount/RemoveAzureStorageAccountManagementPolicy.cs +++ b/src/Storage/Storage.Management/StorageAccount/RemoveAzureStorageAccountManagementPolicy.cs @@ -12,11 +12,10 @@ // limitations under the License. // ---------------------------------------------------------------------------------- +using Azure; using Microsoft.Azure.Commands.Management.Storage.Models; using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; using Microsoft.Azure.Management.Internal.Resources.Utilities.Models; -using Microsoft.Azure.Management.Storage; -using Microsoft.Azure.Management.Storage.Models; using System.Management.Automation; namespace Microsoft.Azure.Commands.Management.Storage @@ -116,9 +115,8 @@ public override void ExecuteCmdlet() break; } - this.StorageClient.ManagementPolicies.Delete( - this.ResourceGroupName, - this.StorageAccountName); + this.StorageClientTrack2.GetManagementPolicyResource(this.ResourceGroupName, this.StorageAccountName, "default") + .Delete(WaitUntil.Completed); if (PassThru.IsPresent) { diff --git a/src/Storage/Storage.Management/StorageAccount/RestoreAzStorageBlobRange.cs b/src/Storage/Storage.Management/StorageAccount/RestoreAzStorageBlobRange.cs index b8e3a9473b2c..d0aa229636d2 100644 --- a/src/Storage/Storage.Management/StorageAccount/RestoreAzStorageBlobRange.cs +++ b/src/Storage/Storage.Management/StorageAccount/RestoreAzStorageBlobRange.cs @@ -12,15 +12,18 @@ // limitations under the License. // ---------------------------------------------------------------------------------- +using Azure; using Microsoft.Azure.Commands.Management.Storage.Models; using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; using Microsoft.Azure.Management.Internal.Resources.Utilities.Models; -using Microsoft.Azure.Management.Storage; -using Microsoft.Azure.Management.Storage.Models; using Microsoft.Rest.Azure; +using Microsoft.WindowsAzure.Commands.Utilities.Common; using System; +using System.Collections.Generic; using System.Management.Automation; using System.Threading.Tasks; +using Track2 = Azure.ResourceManager.Storage; +using Track2Models = Azure.ResourceManager.Storage.Models; namespace Microsoft.Azure.Commands.Management.Storage { @@ -79,7 +82,7 @@ public class RestoreAzureStorageBlobRangeCommand : StorageAccountBaseCmdlet [Parameter(Mandatory = true, HelpMessage = "The Time to Restore Blob.")] [ValidateNotNull] - public DateTime TimeToRestore { get; set; } + public DateTimeOffset TimeToRestore { get; set; } [Parameter(Mandatory = false, HelpMessage = "The blob range to Restore.")] [ValidateNotNull] @@ -118,50 +121,116 @@ public override void ExecuteCmdlet() if (ShouldProcess(this.StorageAccountName, "Restore Blob Range")) { - if (waitForComplete) - { - Task> beginTask = this.StorageClient.StorageAccounts.BeginRestoreBlobRangesWithHttpMessagesAsync( - this.ResourceGroupName, - this.StorageAccountName, - this.TimeToRestore, - PSBlobRestoreRange.ParseBlobRestoreRanges(this.BlobRestoreRange)); - - beginTask.Wait(); - - AzureOperationResponse response = beginTask.Result; + Track2.StorageAccountResource account = this.StorageClientTrack2.GetStorageAccount(this.ResourceGroupName, this.StorageAccountName); + var restoreLro = account.RestoreBlobRanges( + WaitUntil.Started, + new Track2Models.BlobRestoreContent( + this.TimeToRestore.ToUniversalTime(), + PSBlobRestoreRange.ParseBlobRestoreRanges(this.BlobRestoreRange))); + + // This is a temporary workaround of SDK issue https://github.com/Azure/azure-sdk-for-net/issues/29060 + // The workaround is to get the raw response and parse it into the output desired + // The Blob restore status should be got from SDK directly once the issue is fixed + Dictionary temp = restoreLro.GetRawResponse().Content.ToObjectFromJson() as Dictionary; - WriteWarning(string.Format("Restore blob ranges with Id '{0}' started. Restore blob ranges time to complete is dependent on the size of the restore.", response.Body is null ? "" : response.Body.RestoreId)); + if (waitForComplete) + { + if (temp == null) + { + throw new InvalidJobStateException("Could not fetch the Blob restore response."); + } + PSBlobRestoreStatus blobRestoreStatus = ParseRestoreRawResponse(temp); + if (blobRestoreStatus.RestoreId != null) + { + WriteWarning(string.Format("Restore blob ranges with Id '{0}' started. Restore blob ranges time to complete is dependent on the size of the restore.", blobRestoreStatus.RestoreId)); + } + else + { + WriteWarning(string.Format("Could not fetch the Restore Id.")); + } - Task> waitTask = ((StorageManagementClient)this.StorageClient).GetPostOrDeleteOperationResultAsync(response, null, new System.Threading.CancellationToken()); try { - waitTask.Wait(); + var result = restoreLro.WaitForCompletion().Value; + WriteObject(new PSBlobRestoreStatus(result)); } catch (System.AggregateException ex) when (ex.InnerException is CloudException) { - throw new InvalidJobStateException(string.Format("Blob ranges restore failed with information: '{0}'.", ((CloudException)ex.InnerException).Response.Content)); + throw new InvalidJobStateException(string.Format("Blob ranges restore failed with information: '{0}'.", ex.ToString())); } - - AzureOperationResponse result = waitTask.Result; - - WriteObject(new PSBlobRestoreStatus(result.Body)); - } else { - BlobRestoreStatus status = this.StorageClient.StorageAccounts.BeginRestoreBlobRanges( - this.ResourceGroupName, - this.StorageAccountName, - this.TimeToRestore, - PSBlobRestoreRange.ParseBlobRestoreRanges(this.BlobRestoreRange)); - - WriteObject(new PSBlobRestoreStatus(status)); - if (status != null && status.Status == BlobRestoreProgressStatus.Failed) + if (temp == null) + { + throw new InvalidJobStateException("Could not fetch the Blob restore response."); + } + + PSBlobRestoreStatus blobRestoreStatus = ParseRestoreRawResponse(temp); + + if (blobRestoreStatus.Status != null) + { + if (blobRestoreStatus.Status == Track2Models.BlobRestoreProgressStatus.Failed.ToString()) + { + throw new InvalidJobStateException("Blob ranges restore failed."); + } + } + else { - throw new InvalidJobStateException("Blob ranges restore failed."); + WriteWarning(string.Format("Could not fetch the status.")); } + WriteObject(blobRestoreStatus); } } } + + private PSBlobRestoreStatus ParseRestoreRawResponse(Dictionary response) + { + response.TryGetValue("restoreId", out object restoreId); + response.TryGetValue("status", out object jobStatus); + response.TryGetValue("parameters", out object parameters); + + PSBlobRestoreParameters blobRestoreParameters; + Dictionary paramMap = parameters as Dictionary; + + if (paramMap == null) + { + blobRestoreParameters = null; + } + else + { + blobRestoreParameters = new PSBlobRestoreParameters(); + paramMap.TryGetValue("timetoRestore", out object timeToRestore); + DateTimeOffset.TryParse(timeToRestore?.ToString(), out DateTimeOffset parseDate); + blobRestoreParameters.TimeToRestore = parseDate; + + paramMap.TryGetValue("blobRanges", out object ranges); + List blobRestoreRanges = new List(); + + object[] rangesList = ranges as object[]; + foreach (object range in rangesList) + { + Dictionary rangeMap = range as Dictionary; + + rangeMap.TryGetValue("startRange", out object startRange); + rangeMap.TryGetValue("endRange", out object endRange); + + PSBlobRestoreRange blobRestoreRange = new PSBlobRestoreRange + { + StartRange = startRange?.ToString(), + EndRange = endRange?.ToString() + }; + + blobRestoreRanges.Add(blobRestoreRange); + } + blobRestoreParameters.BlobRanges = blobRestoreRanges.ToArray(); + } + + return new PSBlobRestoreStatus( + status: jobStatus?.ToString(), + failureReason: null, + restoreId: restoreId?.ToString(), + parameters: blobRestoreParameters); + } } } diff --git a/src/Storage/Storage.Management/StorageAccount/SetAzureStorageAccountManagementPolicy.cs b/src/Storage/Storage.Management/StorageAccount/SetAzureStorageAccountManagementPolicy.cs index 28ef1b36de10..8e7b64c8d91f 100644 --- a/src/Storage/Storage.Management/StorageAccount/SetAzureStorageAccountManagementPolicy.cs +++ b/src/Storage/Storage.Management/StorageAccount/SetAzureStorageAccountManagementPolicy.cs @@ -12,16 +12,16 @@ // limitations under the License. // ---------------------------------------------------------------------------------- +using Azure.ResourceManager.Storage; using Microsoft.Azure.Commands.Management.Storage.Models; using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; -using Microsoft.Azure.Management.Storage; -using Microsoft.Azure.Management.Storage.Models; -using System.Management.Automation; -using Newtonsoft.Json.Linq; -using System.Globalization; -using System.Collections.Generic; using Microsoft.Azure.Management.Internal.Resources.Utilities.Models; using Microsoft.WindowsAzure.Commands.Common.CustomAttributes; +using Newtonsoft.Json.Linq; +using System.Collections.Generic; +using System.Globalization; +using System.Management.Automation; +using Track2 = Azure.ResourceManager.Storage; namespace Microsoft.Azure.Commands.Management.Storage { @@ -62,7 +62,7 @@ public class SetAzureStorageAccountManagementPolicyCommand : StorageAccountBaseC Position = 0, Mandatory = true, HelpMessage = "Resource Group Name.", - ParameterSetName = AccountNamePolicyRuleParameterSet)] + ParameterSetName = AccountNamePolicyRuleParameterSet)] [Parameter( Position = 0, Mandatory = true, @@ -155,7 +155,7 @@ public override void ExecuteCmdlet() base.ExecuteCmdlet(); if (ShouldProcess(this.StorageAccountName, "Set Storage Account Management Policy")) { - if ((this.ParameterSetName == AccountObjectPolicyRuleParameterSet) + if ((this.ParameterSetName == AccountObjectPolicyRuleParameterSet) || (this.ParameterSetName == AccountObjectPolicyObjectParameterSet)) { this.ResourceGroupName = StorageAccount.ResourceGroupName; @@ -168,35 +168,43 @@ public override void ExecuteCmdlet() this.ResourceGroupName = accountResource.ResourceGroupName; this.StorageAccountName = accountResource.ResourceName; } - ManagementPolicy managementPolicy; + + ManagementPolicyResource managementPolicyResource; + Track2.ManagementPolicyData data; switch (this.ParameterSetName) { case AccountObjectPolicyRuleParameterSet: case AccountNamePolicyRuleParameterSet: case AccountResourceIdPolicyRuleParameterSet: - managementPolicy = this.StorageClient.ManagementPolicies.CreateOrUpdate( - this.ResourceGroupName, - this.StorageAccountName, - new ManagementPolicySchema( - //this.version, - PSManagementPolicyRule.ParseManagementPolicyRules(this.Rule))); + + data = new Track2.ManagementPolicyData + { + Rules = PSManagementPolicyRule.ParseManagementPolicyRules(this.Rule) + }; + + managementPolicyResource = this.StorageClientTrack2 + .GetManagementPolicyResource(this.ResourceGroupName, this.StorageAccountName, "default") + .CreateOrUpdate(global::Azure.WaitUntil.Completed, data).Value; break; case AccountObjectPolicyObjectParameterSet: case AccountNamePolicyObjectParameterSet: case AccountResourceIdPolicyObjectParameterSet: - managementPolicy = this.StorageClient.ManagementPolicies.CreateOrUpdate( - this.ResourceGroupName, - this.StorageAccountName, - new ManagementPolicySchema( - //this.Policy.Version, - PSManagementPolicyRule.ParseManagementPolicyRules(this.Policy.Rules))); + + data = new Track2.ManagementPolicyData() + { + Rules = PSManagementPolicyRule.ParseManagementPolicyRules(this.Policy.Rules) + }; + + managementPolicyResource = this.StorageClientTrack2 + .GetManagementPolicyResource(this.ResourceGroupName, this.StorageAccountName, "default") + .CreateOrUpdate(global::Azure.WaitUntil.Completed, data).Value; break; default: throw new PSArgumentException(string.Format(CultureInfo.InvariantCulture, "Invalid ParameterSet: {0}", this.ParameterSetName)); } - WriteObject(new PSManagementPolicy(managementPolicy, this.ResourceGroupName, this.StorageAccountName), true); + WriteObject(new PSManagementPolicy(managementPolicyResource, this.ResourceGroupName, this.StorageAccountName), true); } } } diff --git a/src/Storage/Storage.Management/StorageAccount/StorageAccountBaseCmdlet.cs b/src/Storage/Storage.Management/StorageAccount/StorageAccountBaseCmdlet.cs index 68c2bbac4aba..221d25154a6f 100644 --- a/src/Storage/Storage.Management/StorageAccount/StorageAccountBaseCmdlet.cs +++ b/src/Storage/Storage.Management/StorageAccount/StorageAccountBaseCmdlet.cs @@ -15,16 +15,19 @@ using Azure.Core; using Azure.ResourceManager; using Azure.ResourceManager.Resources; -using Track2 = Azure.ResourceManager.Storage; -using Track2Models = Azure.ResourceManager.Storage.Models; using Microsoft.Azure.Commands.Management.Storage.Models; using Microsoft.Azure.Commands.ResourceManager.Common; using Microsoft.Azure.Management.Storage; using Microsoft.Azure.Management.Storage.Models; +using Microsoft.Azure.Storage; +using Microsoft.Azure.Storage.Auth; using Microsoft.WindowsAzure.Commands.Utilities.Common; using System; using System.Collections.Generic; +using System.Management.Automation; using StorageModels = Microsoft.Azure.Management.Storage.Models; +using Track2 = Azure.ResourceManager.Storage; +using Track2Models = Azure.ResourceManager.Storage.Models; namespace Microsoft.Azure.Commands.Management.Storage { @@ -194,6 +197,7 @@ protected static Track2Models.AccessTier ParseAccessTier(string accessTier) protected static Track2Models.Encryption ParseEncryption(bool storageEncryption = false, bool keyVaultEncryption = false, string keyName = null, string keyVersion = null, string keyVaultUri = null) { + // TODO: Fix the KeySource value. It should be null or empty. Input KeyVault for a placeholder. Track2Models.Encryption accountEncryption = new Track2Models.Encryption(Track2Models.KeySource.MicrosoftKeyvault); @@ -212,6 +216,31 @@ protected static Track2Models.Encryption ParseEncryption(bool storageEncryption return accountEncryption; } + public static CloudStorageAccount GetCloudStorageAccount(Track2.StorageAccountResource storageAccountResource) + { + Uri blobEndpoint = storageAccountResource.Data.PrimaryEndpoints.Blob != null ? new Uri(storageAccountResource.Data.PrimaryEndpoints.Blob) : null; + Uri queueEndpoint = storageAccountResource.Data.PrimaryEndpoints.Queue != null ? new Uri(storageAccountResource.Data.PrimaryEndpoints.Queue) : null; + Uri tableEndpoint = storageAccountResource.Data.PrimaryEndpoints.Table != null ? new Uri(storageAccountResource.Data.PrimaryEndpoints.Table) : null; + Uri fileEndpoint = storageAccountResource.Data.PrimaryEndpoints.File != null ? new Uri(storageAccountResource.Data.PrimaryEndpoints.File) : null; + string key = null; + if (storageAccountResource.GetKeys()?.Value?.Keys != null && storageAccountResource.GetKeys().Value.Keys.Count > 0) + { + key = storageAccountResource.GetKeys().Value.Keys[0].Value; + } else + { + throw new InvalidJobStateException("Could not fetch storage account keys to build storage account context."); + } + StorageCredentials storageCredentials = new Azure.Storage.Auth.StorageCredentials(storageAccountResource.Data.Name, key); + CloudStorageAccount cloudStorageAccount = new CloudStorageAccount( + storageCredentials, + new StorageUri(blobEndpoint), + new StorageUri(queueEndpoint), + new StorageUri(tableEndpoint), + new StorageUri(fileEndpoint)); + + return cloudStorageAccount; + } + protected void WriteStorageAccount(Track2.StorageAccountResource storageAccountResource) { WriteObject(PSStorageAccount.Create(storageAccountResource, this.StorageClientTrack2)); diff --git a/src/Storage/Storage.Management/Track2StorageManagementClient.cs b/src/Storage/Storage.Management/Track2StorageManagementClient.cs index 18390527fbd7..a603dfa2e62a 100644 --- a/src/Storage/Storage.Management/Track2StorageManagementClient.cs +++ b/src/Storage/Storage.Management/Track2StorageManagementClient.cs @@ -52,6 +52,18 @@ public Pageable ListStorageAccounts() => string.Format("/subscriptions/{0}", _subscription))) .GetStorageAccounts(); + /// + /// Check if a storage account name is available + /// + public CheckNameAvailabilityResult CheckNameAvailability(StorageAccountCheckNameAvailabilityContent content) => + GetSubscription(_subscription).CheckStorageAccountNameAvailability(content); + + /// + /// get Storage usages + /// + public Pageable GetStorageUsages(string location) => + GetSubscription(_subscription).GetUsagesByLocation(location); + /// /// List accounts from Resource group /// @@ -82,6 +94,23 @@ public StorageAccountResource CreateStorageAccount(string resourceGroup, string public Track2.StorageAccountResource UpdateStorageAccount(string resourcegroup, string storageAccountName, StorageAccountPatch patch) => GetStorageAccount(resourcegroup, storageAccountName).Update(patch); + /// + /// Get Blob inventory policy resource + public Track2.BlobInventoryPolicyResource GetBlobInventoryPolicyResource(string resourceGroupName, string storageAccountName, string policyName) => + _armClient.GetBlobInventoryPolicyResource(Track2.BlobInventoryPolicyResource.CreateResourceIdentifier(_subscription, resourceGroupName, storageAccountName, policyName)); + + /// + /// Get immutability policy resource + /// + public Track2.ImmutabilityPolicyResource GetImmutabilityPolicyResource(string resourceGroupName, string storageAccountName, string containerName) => + _armClient.GetImmutabilityPolicyResource(Track2.ImmutabilityPolicyResource.CreateResourceIdentifier(_subscription, resourceGroupName, storageAccountName, containerName)); + + /// + /// Get management policy resource + /// + public Track2.ManagementPolicyResource GetManagementPolicyResource(string resourceGroupName, string storageAccountName, string policyName) => + _armClient.GetManagementPolicyResource(Track2.ManagementPolicyResource.CreateResourceIdentifier(_subscription, resourceGroupName, storageAccountName, policyName)); + /// /// Get BlobServiceResource with subscription, resource group name, and storage account name /// @@ -100,12 +129,6 @@ public Track2.BlobContainerResource GetBlobContainerResource(string resourceGrou public Track2.BlobContainerCollection GetBlobContainers(string resourceGroupName, string storageAccountName) => GetBlobServiceResource(resourceGroupName, storageAccountName).GetBlobContainers(); - /// - /// Get a blob container under a resource group and storage account with container name - /// - public Track2.BlobContainerResource GetBlobContainer(string resourceGroupName, string storageAccountName, string containerName) => - GetBlobContainerResource(resourceGroupName, storageAccountName, containerName).Get(); - /// /// Update a blob container /// @@ -119,33 +142,31 @@ public Track2.BlobContainerResource CreateBlobContainer(string resourceGroupName GetBlobContainers(resourceGroupName, storageAccountName).CreateOrUpdate(WaitUntil.Completed, containerName, data).Value; /// - /// Get immutability policy of a blob container - /// - public Track2.ImmutabilityPolicyResource GetImmutabilityPolicy(string resourceGroupName, string storageAccountName, string containerName, string etag) => - GetBlobContainerResource(resourceGroupName, storageAccountName, containerName).GetImmutabilityPolicy().Get(etag); - - /// - /// Lock immutability policy of a blob container + /// Get file share resource /// - public Track2.ImmutabilityPolicyResource LockImmutabilityPolicy(string resourceGroupName, string storageAccountName, string containerName, string etag) => - GetBlobContainerResource(resourceGroupName, storageAccountName, containerName).GetImmutabilityPolicy().LockImmutabilityPolicy(etag); + public Track2.FileShareResource GetFileShareResource(string resourceGroupName, string storageAccountName, string shareName) => + _armClient.GetFileShareResource(FileShareResource.CreateResourceIdentifier(_subscription, resourceGroupName, storageAccountName, shareName)); /// - /// Create immutability policy for a blob container + /// Get file service resource /// - public Track2.ImmutabilityPolicyResource CreateImmutabilityPolicy(string resourceGroupName, string storageAccountName, string containerName, ImmutabilityPolicyData data, string etag) => - GetBlobContainerResource(resourceGroupName, storageAccountName, containerName).GetImmutabilityPolicy().CreateOrUpdate(WaitUntil.Completed, data, etag).Value; + public Track2.FileServiceResource GetFileServiceResource(string resourceGroupName, string storageAccountName) => + _armClient.GetFileServiceResource(FileServiceResource.CreateResourceIdentifier(_subscription, resourceGroupName, storageAccountName)); /// - /// Extend immutability policy for a blob container + /// Get encryption scope resource /// - public Track2.ImmutabilityPolicyResource ExtendImmutabilityPolicy(string resourceGroupName, string storageAccountName, string containerName, ImmutabilityPolicyData data, string etag) => - GetBlobContainerResource(resourceGroupName, storageAccountName, containerName).GetImmutabilityPolicy().ExtendImmutabilityPolicy(etag, data); + public Track2.EncryptionScopeResource GetEncryptionScopeResource(string resourceGroupName, string accountName, string encryptionScopeName) => + _armClient.GetEncryptionScopeResource(EncryptionScopeResource.CreateResourceIdentifier(_subscription, resourceGroupName, accountName, encryptionScopeName)); /// - /// Delete immutability policy for a blob container + /// Get object replication policy resource /// - public Track2.ImmutabilityPolicyResource DeleteImmutabilityPolicy(string resourceGroupName, string storageAccountName, string containerName, string etag) => - GetBlobContainerResource(resourceGroupName, storageAccountName, containerName).GetImmutabilityPolicy().Delete(WaitUntil.Completed, etag).Value; + /// + /// + /// + /// + public Track2.ObjectReplicationPolicyResource GetObjectReplicationPolicyResource(string resourceGroupName, string storageAccountName, string policyId) => + _armClient.GetObjectReplicationPolicyResource(ObjectReplicationPolicyResource.CreateResourceIdentifier(_subscription, resourceGroupName, storageAccountName, policyId)); } } \ No newline at end of file