|
| 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 | + |
| 16 | +using Microsoft.Azure.Commands.Aks.Models; |
| 17 | +using Microsoft.Azure.Commands.Aks.Properties; |
| 18 | +using Microsoft.Azure.Commands.ResourceManager.Common.ArgumentCompleters; |
| 19 | +using Microsoft.Azure.Management.ContainerService; |
| 20 | +using Microsoft.Azure.Management.ContainerService.Models; |
| 21 | +using Microsoft.Azure.Management.Internal.Resources.Utilities.Models; |
| 22 | +using Microsoft.WindowsAzure.Commands.Common; |
| 23 | + |
| 24 | +using System; |
| 25 | +using System.Collections.Generic; |
| 26 | +using System.Management.Automation; |
| 27 | +using System.Text; |
| 28 | + |
| 29 | +namespace Microsoft.Azure.Commands.Aks |
| 30 | +{ |
| 31 | + [Cmdlet("Set", ResourceManager.Common.AzureRMConstants.AzurePrefix + "AksClusterCredential", SupportsShouldProcess = true, DefaultParameterSetName = GroupNameParameterSet)] |
| 32 | + [OutputType(typeof(bool))] |
| 33 | + public class SetAzureRmAksCredential : KubeCmdletBase |
| 34 | + { |
| 35 | + private const string IdParameterSet = "IdParameterSet"; |
| 36 | + private const string GroupNameParameterSet = "GroupNameParameterSet"; |
| 37 | + private const string InputObjectParameterSet = "InputObjectParameterSet"; |
| 38 | + |
| 39 | + [Parameter(Mandatory = true, |
| 40 | + ParameterSetName = InputObjectParameterSet, |
| 41 | + ValueFromPipeline = true, |
| 42 | + HelpMessage = "A PSKubernetesCluster object, normally passed through the pipeline.")] |
| 43 | + [ValidateNotNullOrEmpty] |
| 44 | + public PSKubernetesCluster InputObject { get; set; } |
| 45 | + |
| 46 | + [Parameter(Mandatory = true, |
| 47 | + ParameterSetName = IdParameterSet, |
| 48 | + Position = 0, |
| 49 | + ValueFromPipelineByPropertyName = true, |
| 50 | + HelpMessage = "Id of a managed Kubernetes cluster")] |
| 51 | + [ValidateNotNullOrEmpty] |
| 52 | + [Alias("ResourceId")] |
| 53 | + public string Id { get; set; } |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Resource group name |
| 57 | + /// </summary> |
| 58 | + [Parameter( |
| 59 | + Position = 0, |
| 60 | + Mandatory = true, |
| 61 | + ParameterSetName = GroupNameParameterSet, |
| 62 | + HelpMessage = "Resource group name")] |
| 63 | + [ResourceGroupCompleter()] |
| 64 | + [ValidateNotNullOrEmpty] |
| 65 | + public string ResourceGroupName { get; set; } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Cluster name |
| 69 | + /// </summary> |
| 70 | + [Parameter( |
| 71 | + Mandatory = true, |
| 72 | + Position = 1, |
| 73 | + ParameterSetName = GroupNameParameterSet, |
| 74 | + HelpMessage = "Name of your managed Kubernetes cluster")] |
| 75 | + [ValidateNotNullOrEmpty] |
| 76 | + public string Name { get; set; } |
| 77 | + |
| 78 | + [Parameter( |
| 79 | + Mandatory = true, |
| 80 | + ParameterSetName = InputObjectParameterSet, |
| 81 | + HelpMessage = "The client id and client secret associated with the service principal.")] |
| 82 | + [Parameter( |
| 83 | + Mandatory = true, |
| 84 | + ParameterSetName = GroupNameParameterSet, |
| 85 | + HelpMessage = "The client id and client secret associated with the service principal.")] |
| 86 | + [Parameter( |
| 87 | + Mandatory = true, |
| 88 | + ParameterSetName = IdParameterSet, |
| 89 | + HelpMessage = "The client id and client secret associated with the service principal.")] |
| 90 | + public PSCredential ServicePrincipalIdAndSecret { get; set; } |
| 91 | + |
| 92 | + [Parameter(Mandatory = false)] |
| 93 | + public SwitchParameter PassThru { get; set; } |
| 94 | + |
| 95 | + [Parameter(Mandatory = false, HelpMessage = "Run cmdlet in the background")] |
| 96 | + public SwitchParameter AsJob { get; set; } |
| 97 | + |
| 98 | + [Parameter(Mandatory = false, HelpMessage = "Remove managed Kubernetes cluster without prompt")] |
| 99 | + public SwitchParameter Force { get; set; } |
| 100 | + |
| 101 | + public override void ExecuteCmdlet() |
| 102 | + { |
| 103 | + base.ExecuteCmdlet(); |
| 104 | + |
| 105 | + switch (ParameterSetName) |
| 106 | + { |
| 107 | + case IdParameterSet: |
| 108 | + { |
| 109 | + var resource = new ResourceIdentifier(Id); |
| 110 | + ResourceGroupName = resource.ResourceGroupName; |
| 111 | + Name = resource.ResourceName; |
| 112 | + break; |
| 113 | + } |
| 114 | + case InputObjectParameterSet: |
| 115 | + { |
| 116 | + var resource = new ResourceIdentifier(InputObject.Id); |
| 117 | + ResourceGroupName = resource.ResourceGroupName; |
| 118 | + Name = resource.ResourceName; |
| 119 | + break; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + var msg = $"{Name} in {ResourceGroupName}"; |
| 124 | + |
| 125 | + ConfirmAction(Force.IsPresent, |
| 126 | + Resources.ResetTheCredentialOfAksCluster, |
| 127 | + Resources.ResetingTheCredentialOfAksCluster, |
| 128 | + msg, |
| 129 | + () => |
| 130 | + { |
| 131 | + RunCmdLet(() => |
| 132 | + { |
| 133 | + ManagedClusterServicePrincipalProfile servicePrincipalProfile = new ManagedClusterServicePrincipalProfile() |
| 134 | + { |
| 135 | + ClientId = ServicePrincipalIdAndSecret.UserName, |
| 136 | + Secret = ServicePrincipalIdAndSecret.Password.ConvertToString() |
| 137 | + }; |
| 138 | + Client.ManagedClusters.ResetServicePrincipalProfile(ResourceGroupName, Name, servicePrincipalProfile); |
| 139 | + if (PassThru) |
| 140 | + { |
| 141 | + WriteObject(true); |
| 142 | + } |
| 143 | + }); |
| 144 | + }); |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments