Skip to content

Commit 3d054b7

Browse files
author
Hovsep Mkrtchyan
committed
Azure Environment current work.
1 parent 390d35b commit 3d054b7

File tree

5 files changed

+313
-1
lines changed

5 files changed

+313
-1
lines changed

src/Common/Commands.ResourceManager.Common/RMProfileClient.cs

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
using Microsoft.Azure.Common.Authentication;
1717
using Microsoft.Azure.Common.Authentication.Factories;
1818
using Microsoft.Azure.Common.Authentication.Models;
19+
using Microsoft.Azure.Common.Authentication.Properties;
1920
using Microsoft.Azure.Subscriptions;
2021
using Microsoft.IdentityModel.Clients.ActiveDirectory;
2122
using System;
@@ -140,6 +141,59 @@ public bool TryGetSubscription(string tenantId, string subscriptionId, out Azure
140141
tenantId, subscriptionId, null, ShowDialog.Never, out subscription, out tenant);
141142
}
142143

144+
public AzureEnvironment AddOrSetEnvironment(AzureEnvironment environment)
145+
{
146+
if (environment == null)
147+
{
148+
throw new ArgumentNullException("environment", Resources.EnvironmentNeedsToBeSpecified);
149+
}
150+
151+
if (AzureEnvironment.PublicEnvironments.ContainsKey(environment.Name))
152+
{
153+
throw new ArgumentException(Resources.ChangingDefaultEnvironmentNotSupported, "environment");
154+
}
155+
156+
if (_profile.Environments.ContainsKey(environment.Name))
157+
{
158+
_profile.Environments[environment.Name] =
159+
MergeEnvironmentProperties(environment, _profile.Environments[environment.Name]);
160+
}
161+
else
162+
{
163+
_profile.Environments[environment.Name] = environment;
164+
}
165+
166+
return _profile.Environments[environment.Name];
167+
}
168+
169+
private AzureEnvironment MergeEnvironmentProperties(AzureEnvironment environment1, AzureEnvironment environment2)
170+
{
171+
if (environment1 == null || environment2 == null)
172+
{
173+
throw new ArgumentNullException("environment1");
174+
}
175+
if (!string.Equals(environment1.Name, environment2.Name, StringComparison.InvariantCultureIgnoreCase))
176+
{
177+
throw new ArgumentException("Environment names do not match.");
178+
}
179+
AzureEnvironment mergedEnvironment = new AzureEnvironment
180+
{
181+
Name = environment1.Name
182+
};
183+
184+
// Merge all properties
185+
foreach (AzureEnvironment.Endpoint property in Enum.GetValues(typeof(AzureEnvironment.Endpoint)))
186+
{
187+
string propertyValue = environment1.GetEndpoint(property) ?? environment2.GetEndpoint(property);
188+
if (propertyValue != null)
189+
{
190+
mergedEnvironment.Endpoints[property] = propertyValue;
191+
}
192+
}
193+
194+
return mergedEnvironment;
195+
}
196+
143197
private bool TryGetTenantSubscription(
144198
AzureAccount account,
145199
AzureEnvironment environment,

src/Common/Commands.ResourceManager.Profile/Commands.ResourceManager.Profile.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@
132132
<Reference Include="System.Xml.Linq" />
133133
</ItemGroup>
134134
<ItemGroup>
135-
<Compile Include="GetAzureRMSubscription.cs" />
135+
<Compile Include="Environment\SetAzureRMEnvironment.cs" />
136+
<Compile Include="Environment\AddAzureRMEnvironment.cs" />
137+
<Compile Include="Subscription\GetAzureRMSubscription.cs" />
136138
<Compile Include="Account\LoginAzureRMAccount.cs" />
137139
<Compile Include="Context\GetAzureRMContext.cs" />
138140
<Compile Include="Context\SetAzureRMContext.cs" />
@@ -172,6 +174,7 @@
172174
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
173175
</EmbeddedResource>
174176
</ItemGroup>
177+
<ItemGroup />
175178
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
176179
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
177180
<Import Project="..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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.Management.Automation;
18+
19+
namespace Microsoft.Azure.Commands.Profile
20+
{
21+
/// <summary>
22+
/// Cmdlet to add Azure Environment to Profile.
23+
/// </summary>
24+
[Cmdlet(VerbsCommon.Add, "AzureRMEnvironment")]
25+
[OutputType(typeof(AzureEnvironment))]
26+
public class AddAzureRMEnvironmentCommand : AzureRMCmdlet
27+
{
28+
[Parameter(Position = 0, Mandatory = true, ValueFromPipelineByPropertyName = true)]
29+
public string Name { get; set; }
30+
31+
[Parameter(Position = 1, Mandatory = false, ValueFromPipelineByPropertyName = true)]
32+
public string PublishSettingsFileUrl { get; set; }
33+
34+
[Parameter(Position = 2, Mandatory = false, ValueFromPipelineByPropertyName = true)]
35+
[Alias("ServiceManagement", "ServiceManagementUrl")]
36+
public string ServiceEndpoint { get; set; }
37+
38+
[Parameter(Position = 3, Mandatory = false, ValueFromPipelineByPropertyName = true)]
39+
public string ManagementPortalUrl { get; set; }
40+
41+
[Parameter(Position = 4, Mandatory = false, HelpMessage = "The storage endpoint")]
42+
[Alias("StorageEndpointSuffix")]
43+
public string StorageEndpoint { get; set; }
44+
45+
[Parameter(Position = 5, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The URI for the Active Directory service for this environment")]
46+
[Alias("AdEndpointUrl", "ActiveDirectory", "ActiveDirectoryAuthority")]
47+
public string ActiveDirectoryEndpoint { get; set; }
48+
49+
[Parameter(Position = 6, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The cloud service endpoint")]
50+
[Alias("ResourceManager", "ResourceManagerUrl")]
51+
public string ResourceManagerEndpoint { get; set; }
52+
53+
[Parameter(Position = 7, Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The public gallery endpoint")]
54+
[Alias("Gallery", "GalleryUrl")]
55+
public string GalleryEndpoint { get; set; }
56+
57+
[Parameter(Position = 8, Mandatory = false, ValueFromPipelineByPropertyName = true,
58+
HelpMessage = "Identifier of the target resource that is the recipient of the requested token.")]
59+
public string ActiveDirectoryServiceEndpointResourceId { get; set; }
60+
61+
[Parameter(Position = 9, Mandatory = false, ValueFromPipelineByPropertyName = true,
62+
HelpMessage = "The AD Graph Endpoint.")]
63+
[Alias("Graph", "GraphUrl")]
64+
public string GraphEndpoint { get; set; }
65+
66+
[Parameter(Position = 10, Mandatory = false, ValueFromPipelineByPropertyName = true,
67+
HelpMessage = "Dns suffix of Azure Key Vault service. Example is vault-int.azure-int.net")]
68+
public string AzureKeyVaultDnsSuffix { get; set; }
69+
70+
[Parameter(Position = 11, Mandatory = false, ValueFromPipelineByPropertyName = true,
71+
HelpMessage = "Resource identifier of Azure Key Vault data service that is the recipient of the requested token.")]
72+
public string AzureKeyVaultServiceEndpointResourceId { get; set; }
73+
74+
[Parameter(Position = 12, Mandatory = false, ValueFromPipelineByPropertyName = true,
75+
HelpMessage = "Dns suffix of Traffic Manager service.")]
76+
public string TrafficManagerDnsSuffix { get; set; }
77+
78+
[Parameter(Position = 13, Mandatory = false, ValueFromPipelineByPropertyName = true,
79+
HelpMessage = "Dns suffix of Sql databases created in this environment.")]
80+
public string SqlDatabaseDnsSuffix { get; set; }
81+
82+
[Parameter(Position = 14, Mandatory = false, ValueFromPipelineByPropertyName = true,
83+
HelpMessage = "Enable ADFS authentication by disabling the authority validation")]
84+
[Alias("OnPremise")]
85+
public SwitchParameter EnableAdfsAuthentication { get; set; }
86+
87+
[Parameter(Position = 15, Mandatory = false, ValueFromPipelineByPropertyName = true,
88+
HelpMessage = "The default tenant for this environment.")]
89+
public string AdTenant { get; set; }
90+
91+
protected override void ProcessRecord()
92+
{
93+
var profileClient = new RMProfileClient(AzureRMCmdlet.DefaultProfile);
94+
95+
var newEnvironment = new AzureEnvironment
96+
{
97+
Name = Name,
98+
OnPremise = EnableAdfsAuthentication
99+
};
100+
101+
newEnvironment.Endpoints[AzureEnvironment.Endpoint.PublishSettingsFileUrl] = PublishSettingsFileUrl;
102+
newEnvironment.Endpoints[AzureEnvironment.Endpoint.ServiceManagement] = ServiceEndpoint;
103+
newEnvironment.Endpoints[AzureEnvironment.Endpoint.ResourceManager] = ResourceManagerEndpoint;
104+
newEnvironment.Endpoints[AzureEnvironment.Endpoint.ManagementPortalUrl] = ManagementPortalUrl;
105+
newEnvironment.Endpoints[AzureEnvironment.Endpoint.StorageEndpointSuffix] = StorageEndpoint;
106+
newEnvironment.Endpoints[AzureEnvironment.Endpoint.ActiveDirectory] = ActiveDirectoryEndpoint;
107+
newEnvironment.Endpoints[AzureEnvironment.Endpoint.ActiveDirectoryServiceEndpointResourceId] = ActiveDirectoryServiceEndpointResourceId;
108+
newEnvironment.Endpoints[AzureEnvironment.Endpoint.Gallery] = GalleryEndpoint;
109+
newEnvironment.Endpoints[AzureEnvironment.Endpoint.Graph] = GraphEndpoint;
110+
newEnvironment.Endpoints[AzureEnvironment.Endpoint.AzureKeyVaultDnsSuffix] = AzureKeyVaultDnsSuffix;
111+
newEnvironment.Endpoints[AzureEnvironment.Endpoint.AzureKeyVaultServiceEndpointResourceId] = AzureKeyVaultServiceEndpointResourceId;
112+
newEnvironment.Endpoints[AzureEnvironment.Endpoint.TrafficManagerDnsSuffix] = TrafficManagerDnsSuffix;
113+
newEnvironment.Endpoints[AzureEnvironment.Endpoint.SqlDatabaseDnsSuffix] = SqlDatabaseDnsSuffix;
114+
newEnvironment.Endpoints[AzureEnvironment.Endpoint.AdTenant] = AdTenant;
115+
WriteObject(profileClient.AddOrSetEnvironment(newEnvironment));
116+
}
117+
}
118+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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

Comments
 (0)