|
| 1 | +using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; |
| 2 | +using Microsoft.Azure.Commands.Synapse.Common; |
| 3 | +using Microsoft.Azure.Commands.Synapse.Models; |
| 4 | +using Microsoft.Azure.Commands.Synapse.Properties; |
| 5 | +using Microsoft.Azure.Management.Internal.Resources.Utilities.Models; |
| 6 | +using Microsoft.WindowsAzure.Commands.Utilities.Common; |
| 7 | +using System.Management.Automation; |
| 8 | +using System; |
| 9 | + |
| 10 | +namespace Microsoft.Azure.Commands.Synapse.Commands |
| 11 | +{ |
| 12 | + [Cmdlet(VerbsCommon.Remove, ResourceManager.Common.AzureRMConstants.AzureRMPrefix + SynapseConstants.SynapsePrefix + SynapseConstants.SqlPool+ SynapseConstants.RestorePoint, DefaultParameterSetName = DeleteByNameParameterSet, SupportsShouldProcess = true)] |
| 13 | + [OutputType(typeof(bool))] |
| 14 | + public class RemoveAzureSynapseSqlPoolRestorePoint : SynapseManagementCmdletBase |
| 15 | + { |
| 16 | + private const string DeleteByNameParameterSet = "DeleteByNameParameterSet"; |
| 17 | + private const string DeleteByParentObjectParameterSet = "DeleteByParentObjectParameterSet"; |
| 18 | + private const string DeleteByInputObjectParameterSet = "DeleteByInputObjectParameterSet"; |
| 19 | + private const string DeleteByResourceIdParameterSet = "DeleteByResourceIdParameterSet"; |
| 20 | + |
| 21 | + [Parameter(Mandatory = false, ParameterSetName = DeleteByNameParameterSet, HelpMessage = HelpMessages.ResourceGroupName)] |
| 22 | + [ResourceGroupCompleter] |
| 23 | + [ValidateNotNullOrEmpty] |
| 24 | + public string ResourceGroupName { get; set; } |
| 25 | + |
| 26 | + [Parameter(Mandatory = true, ParameterSetName = DeleteByNameParameterSet, HelpMessage = HelpMessages.WorkspaceName)] |
| 27 | + [ResourceNameCompleter(ResourceTypes.Workspace, nameof(ResourceGroupName))] |
| 28 | + [ValidateNotNullOrEmpty] |
| 29 | + public string WorkspaceName { get; set; } |
| 30 | + |
| 31 | + [Parameter(Mandatory = true, ParameterSetName = DeleteByNameParameterSet, HelpMessage = HelpMessages.SqlPoolName)] |
| 32 | + [ResourceNameCompleter(ResourceTypes.SqlPool, nameof(WorkspaceName))] |
| 33 | + [ValidateNotNullOrEmpty] |
| 34 | + public string SqlPoolName { get; set; } |
| 35 | + |
| 36 | + [Parameter(Mandatory = true, ParameterSetName = DeleteByParentObjectParameterSet, HelpMessage = HelpMessages.SqlPoolRestorePointName)] |
| 37 | + [Parameter(Mandatory = true, ParameterSetName = DeleteByNameParameterSet, HelpMessage = HelpMessages.SqlPoolRestorePointName)] |
| 38 | + [ResourceNameCompleter( |
| 39 | + ResourceTypes.SqlPoolRestorePoint, |
| 40 | + nameof(ResourceGroupName), |
| 41 | + nameof(WorkspaceName), |
| 42 | + nameof(SqlPoolName))] |
| 43 | + [ValidateNotNullOrEmpty] |
| 44 | + public DateTime RestorePointCreationDate { get; set; } |
| 45 | + |
| 46 | + [Parameter(ValueFromPipeline = true, ParameterSetName = DeleteByParentObjectParameterSet, |
| 47 | + Mandatory = true, HelpMessage = HelpMessages.SqlPoolObject)] |
| 48 | + [ValidateNotNull] |
| 49 | + public PSSynapseSqlPool SqlPoolObject { get; set; } |
| 50 | + |
| 51 | + [Parameter(ValueFromPipeline = true, ParameterSetName = DeleteByInputObjectParameterSet, |
| 52 | + Mandatory = true, HelpMessage = HelpMessages.SqlPoolRestorePointObject)] |
| 53 | + [ValidateNotNull] |
| 54 | + public PSRestorePoint InputObject { get; set; } |
| 55 | + |
| 56 | + [Parameter(ValueFromPipelineByPropertyName = true, ParameterSetName = DeleteByResourceIdParameterSet, |
| 57 | + Mandatory = true, HelpMessage = HelpMessages.SqlPoolRestorePointResourceId)] |
| 58 | + [ValidateNotNullOrEmpty] |
| 59 | + public string ResourceId { get; set; } |
| 60 | + |
| 61 | + [Parameter(Mandatory = false, HelpMessage = HelpMessages.PassThru)] |
| 62 | + public SwitchParameter PassThru { get; set; } |
| 63 | + |
| 64 | + [Parameter(Mandatory = false, HelpMessage = HelpMessages.AsJob)] |
| 65 | + public SwitchParameter AsJob { get; set; } |
| 66 | + |
| 67 | + [Parameter(Mandatory = false, HelpMessage = HelpMessages.Force)] |
| 68 | + public SwitchParameter Force { get; set; } |
| 69 | + |
| 70 | + public override void ExecuteCmdlet() |
| 71 | + { |
| 72 | + if (this.IsParameterBound(c => c.SqlPoolObject)) |
| 73 | + { |
| 74 | + var resourceIdentifier = new ResourceIdentifier(this.SqlPoolObject.Id); |
| 75 | + this.ResourceGroupName = resourceIdentifier.ResourceGroupName; |
| 76 | + this.WorkspaceName = resourceIdentifier.ParentResource; |
| 77 | + this.WorkspaceName = this.WorkspaceName.Substring(this.WorkspaceName.LastIndexOf('/') + 1); |
| 78 | + this.SqlPoolName = resourceIdentifier.ResourceName; |
| 79 | + } |
| 80 | + |
| 81 | + if (this.IsParameterBound(c => c.InputObject)) |
| 82 | + { |
| 83 | + var dateString = this.InputObject.Id.Substring(this.InputObject.Id.LastIndexOf('/') + 1); |
| 84 | + this.RestorePointCreationDate = DateTime.FromFileTime(Convert.ToInt64(dateString)); |
| 85 | + var resourceId = this.InputObject.Id.Substring(0, this.InputObject.Id.Substring(0, this.InputObject.Id.LastIndexOf("/")).LastIndexOf("/")); |
| 86 | + var resourceIdentifier = new ResourceIdentifier(resourceId); |
| 87 | + this.ResourceGroupName = resourceIdentifier.ResourceGroupName; |
| 88 | + this.WorkspaceName = resourceIdentifier.ParentResource; |
| 89 | + this.WorkspaceName = this.WorkspaceName.Substring(this.WorkspaceName.LastIndexOf('/') + 1); |
| 90 | + this.SqlPoolName = resourceIdentifier.ResourceName; |
| 91 | + } |
| 92 | + |
| 93 | + if (this.IsParameterBound(c => c.ResourceId)) |
| 94 | + { |
| 95 | + var dateString = this.ResourceId.Substring(this.ResourceId.LastIndexOf('/') + 1); |
| 96 | + this.RestorePointCreationDate = DateTime.FromFileTime(Convert.ToInt64(dateString)); |
| 97 | + this.ResourceId = this.ResourceId.Substring(0,this.ResourceId.Substring(0, this.ResourceId.LastIndexOf("/")).LastIndexOf("/")); |
| 98 | + var resourceIdentifier = new ResourceIdentifier(this.ResourceId); |
| 99 | + this.ResourceGroupName = resourceIdentifier.ResourceGroupName; |
| 100 | + this.WorkspaceName = resourceIdentifier.ParentResource; |
| 101 | + this.WorkspaceName = this.WorkspaceName.Substring(this.WorkspaceName.LastIndexOf('/') + 1); |
| 102 | + this.SqlPoolName = resourceIdentifier.ResourceName; |
| 103 | + } |
| 104 | + |
| 105 | + if (string.IsNullOrEmpty(this.ResourceGroupName)) |
| 106 | + { |
| 107 | + this.ResourceGroupName = this.SynapseAnalyticsClient.GetResourceGroupByWorkspaceName(this.WorkspaceName); |
| 108 | + } |
| 109 | + |
| 110 | + ConfirmAction( |
| 111 | + Force.IsPresent, |
| 112 | + string.Format(Resources.RemoveSynapseSqlPoolRestorePoint, this.RestorePointCreationDate), |
| 113 | + string.Format(Resources.RemovingSynapseSqlPoolRestorePoint, this.RestorePointCreationDate, this.ResourceGroupName, this.WorkspaceName, this.SqlPoolName), |
| 114 | + this.RestorePointCreationDate.ToFileTimeUtc().ToString(), |
| 115 | + () => |
| 116 | + { |
| 117 | + this.SynapseAnalyticsClient.DeleteSqlPoolRestorePoint(this.ResourceGroupName, this.WorkspaceName, this.SqlPoolName, this.RestorePointCreationDate.ToFileTimeUtc().ToString()); |
| 118 | + if (this.PassThru.IsPresent) |
| 119 | + { |
| 120 | + WriteObject(true); |
| 121 | + } |
| 122 | + }); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments