|
| 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 Microsoft.Azure.Commands.ResourceManager.Common; |
| 16 | +using Microsoft.Azure.Common.Authentication.Models; |
| 17 | +using System; |
| 18 | +using System.Globalization; |
| 19 | +using System.Management.Automation; |
| 20 | + |
| 21 | +namespace Microsoft.Azure.Commands.Profile |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// Cmdlet to set Azure Environment in Profile. |
| 25 | + /// </summary> |
| 26 | + [Cmdlet(VerbsCommon.Set, "AzureRMEnvironment")] |
| 27 | + [OutputType(typeof(AzureEnvironment))] |
| 28 | + public class SetAzureRMEnvironmentCommand : AzureRMCmdlet |
| 29 | + { |
| 30 | + [Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true)] |
| 31 | + public string Name { get; set; } |
| 32 | + |
| 33 | + [Parameter(Position = 1, Mandatory = false, ValueFromPipelineByPropertyName = true)] |
| 34 | + public string PublishSettingsFileUrl { get; set; } |
| 35 | + |
| 36 | + [Parameter(Position = 2, Mandatory = false, ValueFromPipelineByPropertyName = true)] |
| 37 | + [Alias("ServiceManagement", "ServiceManagementUrl")] |
| 38 | + public string ServiceEndpoint { get; set; } |
| 39 | + |
| 40 | + [Parameter(Position = 3, Mandatory = false, ValueFromPipelineByPropertyName = true)] |
| 41 | + public string ManagementPortalUrl { get; set; } |
| 42 | + |
| 43 | + [Parameter(Position = 4, Mandatory = false, HelpMessage = "The storage endpoint")] |
| 44 | + [Alias("StorageEndpointSuffix")] |
| 45 | + public string StorageEndpoint { get; set; } |
| 46 | + |
| 47 | + [Parameter(Position = 5, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "Active directory endpoint")] |
| 48 | + [Alias("AdEndpointUrl", "ActiveDirectory", "ActiveDirectoryAuthority")] |
| 49 | + public string ActiveDirectoryEndpoint { get; set; } |
| 50 | + |
| 51 | + [Parameter(Position = 6, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The cloud service endpoint")] |
| 52 | + [Alias("ResourceManager", "ResourceManagerUrl")] |
| 53 | + public string ResourceManagerEndpoint { get; set; } |
| 54 | + |
| 55 | + [Parameter(Position = 7, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The public gallery endpoint")] |
| 56 | + [Alias("Gallery", "GalleryUrl")] |
| 57 | + public string GalleryEndpoint { get; set; } |
| 58 | + |
| 59 | + [Parameter(Position = 8, Mandatory = false, ValueFromPipelineByPropertyName = true, |
| 60 | + HelpMessage = "Identifier of the target resource that is the recipient of the requested token.")] |
| 61 | + public string ActiveDirectoryServiceEndpointResourceId { get; set; } |
| 62 | + |
| 63 | + [Parameter(Position = 9, Mandatory = false, ValueFromPipelineByPropertyName = true, |
| 64 | + HelpMessage = "The AD Graph Endpoint.")] |
| 65 | + [Alias("Graph", "GraphUrl")] |
| 66 | + public string GraphEndpoint { get; set; } |
| 67 | + |
| 68 | + [Parameter(Position = 10, Mandatory = false, ValueFromPipelineByPropertyName = true, |
| 69 | + HelpMessage = "Dns suffix of Azure Key Vault service. Example is vault-int.azure-int.net")] |
| 70 | + public string AzureKeyVaultDnsSuffix { get; set; } |
| 71 | + |
| 72 | + [Parameter(Position = 11, Mandatory = false, ValueFromPipelineByPropertyName = true, |
| 73 | + HelpMessage = "Resource identifier of Azure Key Vault data service that is the recipient of the requested token.")] |
| 74 | + public string AzureKeyVaultServiceEndpointResourceId { get; set; } |
| 75 | + |
| 76 | + [Parameter(Position = 12, Mandatory = false, ValueFromPipelineByPropertyName = true, |
| 77 | + HelpMessage = "Dns suffix of Traffic Manager service.")] |
| 78 | + public string TrafficManagerDnsSuffix { get; set; } |
| 79 | + |
| 80 | + [Parameter(Position = 13, Mandatory = false, ValueFromPipelineByPropertyName = true, |
| 81 | + HelpMessage = "Dns suffix of Sql databases created in this environment.")] |
| 82 | + public string SqlDatabaseDnsSuffix { get; set; } |
| 83 | + |
| 84 | + [Parameter(Position = 14, Mandatory = false, ValueFromPipelineByPropertyName = true, |
| 85 | + HelpMessage = "Determines whether to enable ADFS authentication, or to use AAD authentication instead. This value is normally true only for Azure Stack endpoints.")] |
| 86 | + [Alias("OnPremise")] |
| 87 | + public SwitchParameter EnableAdfsAuthentication { get; set; } |
| 88 | + |
| 89 | + [Parameter(Position = 15, Mandatory = false, ValueFromPipelineByPropertyName = true, |
| 90 | + HelpMessage = "The default tenant for this environment.")] |
| 91 | + public string AdTenant { get; set; } |
| 92 | + |
| 93 | + protected override void ProcessRecord() |
| 94 | + { |
| 95 | + var profileClient = new RMProfileClient(AzureRMCmdlet.DefaultProfile); |
| 96 | + |
| 97 | + |
| 98 | + if ((Name == "AzureCloud") || (Name == "AzureChinaCloud")) |
| 99 | + { |
| 100 | + throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, |
| 101 | + "Cannot change built-in environment {0}.", Name)); |
| 102 | + } |
| 103 | + |
| 104 | + var newEnvironment = new AzureEnvironment { Name = Name, OnPremise = EnableAdfsAuthentication }; |
| 105 | + if (AzureRMCmdlet.DefaultProfile.Environments.ContainsKey(Name)) |
| 106 | + { |
| 107 | + newEnvironment = AzureRMCmdlet.DefaultProfile.Environments[Name]; |
| 108 | + } |
| 109 | + SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.PublishSettingsFileUrl, PublishSettingsFileUrl); |
| 110 | + SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.ServiceManagement, ServiceEndpoint); |
| 111 | + SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.ResourceManager, ResourceManagerEndpoint); |
| 112 | + SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.ManagementPortalUrl, ManagementPortalUrl); |
| 113 | + SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.StorageEndpointSuffix, StorageEndpoint); |
| 114 | + SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.ActiveDirectory, ActiveDirectoryEndpoint); |
| 115 | + SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId, ActiveDirectoryServiceEndpointResourceId); |
| 116 | + SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.Gallery, GalleryEndpoint); |
| 117 | + SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.Graph, GraphEndpoint); |
| 118 | + SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.AzureKeyVaultDnsSuffix, AzureKeyVaultDnsSuffix); |
| 119 | + SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId, AzureKeyVaultServiceEndpointResourceId); |
| 120 | + SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.TrafficManagerDnsSuffix, TrafficManagerDnsSuffix); |
| 121 | + SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.SqlDatabaseDnsSuffix, SqlDatabaseDnsSuffix); |
| 122 | + SetEndpointIfProvided(newEnvironment, AzureEnvironment.Endpoint.AdTenant, AdTenant); |
| 123 | + |
| 124 | + profileClient.AddOrSetEnvironment(newEnvironment); |
| 125 | + |
| 126 | + WriteObject(newEnvironment); |
| 127 | + } |
| 128 | + |
| 129 | + private void SetEndpointIfProvided(AzureEnvironment newEnvironment, AzureEnvironment.Endpoint endpoint, string property) |
| 130 | + { |
| 131 | + if (!string.IsNullOrEmpty(property)) |
| 132 | + { |
| 133 | + newEnvironment.Endpoints[endpoint] = property; |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments