|
| 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 | +using System; |
| 16 | +using System.Management.Automation; |
| 17 | +using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; |
| 18 | +using Microsoft.Azure.Commands.ServiceFabric.Common; |
| 19 | +using Microsoft.Azure.Commands.ServiceFabric.Models.ManagedClusters; |
| 20 | + |
| 21 | +namespace Microsoft.Azure.Commands.ServiceFabric.Commands |
| 22 | +{ |
| 23 | + [Cmdlet(VerbsCommon.Remove, ResourceManager.Common.AzureRMConstants.AzurePrefix + Constants.ServiceFabricPrefix + "ManagedClusterApplicationTypeVersion", DefaultParameterSetName = ByResourceGroup, SupportsShouldProcess = true), OutputType(typeof(bool))] |
| 24 | + public class RemoveAzServiceFabricManagedClusterApplicationTypeVersion : ManagedApplicationCmdletBase |
| 25 | + { |
| 26 | + private const string ByResourceGroup = "ByResourceGroup"; |
| 27 | + private const string ByInputObject = "ByInputObject"; |
| 28 | + private const string ByResourceId = "ByResourceId"; |
| 29 | + |
| 30 | + #region Parameters |
| 31 | + [Parameter(Mandatory = true, Position = 0, ParameterSetName = ByResourceGroup, ValueFromPipelineByPropertyName = true, |
| 32 | + HelpMessage = "Specify the name of the resource group.")] |
| 33 | + [ResourceGroupCompleter] |
| 34 | + [ValidateNotNullOrEmpty] |
| 35 | + public override string ResourceGroupName { get; set; } |
| 36 | + |
| 37 | + [Parameter(Mandatory = true, Position = 1, ParameterSetName = ByResourceGroup, ValueFromPipelineByPropertyName = true, |
| 38 | + HelpMessage = "Specify the name of the cluster.")] |
| 39 | + [ResourceNameCompleter(Constants.ManagedClustersFullType, nameof(ResourceGroupName))] |
| 40 | + [ValidateNotNullOrEmpty] |
| 41 | + public override string ClusterName { get; set; } |
| 42 | + |
| 43 | + [Parameter(Mandatory = true, ParameterSetName = ByResourceGroup, HelpMessage = "Specify the name of the managed application type.")] |
| 44 | + [ValidateNotNullOrEmpty] |
| 45 | + [Alias("ApplicationTypeName")] |
| 46 | + public string Name { get; set; } |
| 47 | + |
| 48 | + [Parameter(Mandatory = true, ParameterSetName = ByResourceGroup, HelpMessage = "Specify the managed application type version.")] |
| 49 | + [ValidateNotNullOrEmpty] |
| 50 | + [Alias("ApplicationTypeVersion")] |
| 51 | + public string Version { get; set; } |
| 52 | + |
| 53 | + [Parameter(Mandatory = true, ParameterSetName = ByResourceId, ValueFromPipelineByPropertyName = true, |
| 54 | + HelpMessage = "Arm ResourceId of the managed application type version.")] |
| 55 | + [ResourceIdCompleter(Constants.ManagedClustersFullType)] |
| 56 | + [ValidateNotNullOrEmpty] |
| 57 | + public string ResourceId { get; set; } |
| 58 | + |
| 59 | + [Parameter(Mandatory = true, ParameterSetName = ByInputObject, ValueFromPipeline = true, |
| 60 | + HelpMessage = "The managed application type version resource.")] |
| 61 | + public PSManagedApplicationTypeVersion InputObject { get; set; } |
| 62 | + |
| 63 | + [Parameter(Mandatory = false, ParameterSetName = ByResourceGroup)] |
| 64 | + [Parameter(Mandatory = false, ParameterSetName = ByInputObject)] |
| 65 | + [Parameter(Mandatory = false, ParameterSetName = ByResourceId)] |
| 66 | + public SwitchParameter PassThru { get; set; } |
| 67 | + |
| 68 | + [Parameter(Mandatory = false, ParameterSetName = ByResourceGroup, HelpMessage = "Remove without prompt.")] |
| 69 | + [Parameter(Mandatory = false, ParameterSetName = ByInputObject, HelpMessage = "Remove without prompt.")] |
| 70 | + [Parameter(Mandatory = false, ParameterSetName = ByResourceId, HelpMessage = "Remove without prompt.")] |
| 71 | + public SwitchParameter Force { get; set; } |
| 72 | + |
| 73 | + [Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background and return a Job to track progress.")] |
| 74 | + public SwitchParameter AsJob { get; set; } |
| 75 | + #endregion |
| 76 | + |
| 77 | + public override void ExecuteCmdlet() |
| 78 | + { |
| 79 | + switch (ParameterSetName) |
| 80 | + { |
| 81 | + case ByInputObject: |
| 82 | + if (string.IsNullOrEmpty(this.InputObject?.Id)) |
| 83 | + { |
| 84 | + throw new ArgumentException("ResourceId is null."); |
| 85 | + } |
| 86 | + this.ResourceId = InputObject.Id; |
| 87 | + SetParametersByResourceId(this.ResourceId); |
| 88 | + break; |
| 89 | + case ByResourceId: |
| 90 | + SetParametersByResourceId(this.ResourceId); |
| 91 | + break; |
| 92 | + case ByResourceGroup: |
| 93 | + // intentionally left empty |
| 94 | + break; |
| 95 | + default: |
| 96 | + throw new PSArgumentException("Invalid ParameterSetName"); |
| 97 | + } |
| 98 | + |
| 99 | + var resourceMessage = $"Managed ApplicationType '{this.Name}' in resource group '{this.ResourceGroupName}', cluster name {this.ClusterName}"; |
| 100 | + if (ShouldProcess(target: this.Version, action: $"Remove {resourceMessage}")) |
| 101 | + { |
| 102 | + ConfirmAction(Force.IsPresent, |
| 103 | + "Do you want to remove the managed application type version? Please remove all managed applications under this resource before running this command.", |
| 104 | + "Removing managed application type version.", |
| 105 | + resourceMessage, |
| 106 | + () => |
| 107 | + { |
| 108 | + try |
| 109 | + { |
| 110 | + var beginRequestResponse = this.SfrpMcClient.ApplicationTypeVersions.BeginDeleteWithHttpMessagesAsync( |
| 111 | + this.ResourceGroupName, |
| 112 | + this.ClusterName, |
| 113 | + this.Name, |
| 114 | + this.Version).GetAwaiter().GetResult(); |
| 115 | + |
| 116 | + this.PollLongRunningOperation(beginRequestResponse); |
| 117 | + if (PassThru) |
| 118 | + { |
| 119 | + WriteObject(true); |
| 120 | + } |
| 121 | + } |
| 122 | + catch (Exception ex) |
| 123 | + { |
| 124 | + this.PrintSdkExceptionDetail(ex); |
| 125 | + throw; |
| 126 | + } |
| 127 | + }); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + private void SetParametersByResourceId(string resourceId) |
| 132 | + { |
| 133 | + this.GetParametersByResourceId(resourceId, Constants.applicationTypeVersionProvider, out string resourceGroup, out string resourceName, out string parentResourceName, out string grandParentResourceName); |
| 134 | + this.ResourceGroupName = resourceGroup; |
| 135 | + this.Name = resourceName; |
| 136 | + this.ClusterName = parentResourceName; |
| 137 | + this.Version = grandParentResourceName; |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments