Skip to content

Commit d84d757

Browse files
author
Hovsep
committed
Merge pull request #926 from hovsepm/env
[#102921534] Create Environment cmdlets for AzureRM Profile module
2 parents 17051f1 + 4a9548d commit d84d757

File tree

11 files changed

+1084
-2
lines changed

11 files changed

+1084
-2
lines changed

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

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
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;
2223
using System.Collections.Generic;
24+
using System.Diagnostics;
2325
using System.Linq;
2426
using System.Management.Automation;
2527
using System.Security;
@@ -140,6 +142,98 @@ public bool TryGetSubscription(string tenantId, string subscriptionId, out Azure
140142
tenantId, subscriptionId, null, ShowDialog.Never, out subscription, out tenant);
141143
}
142144

145+
public AzureEnvironment AddOrSetEnvironment(AzureEnvironment environment)
146+
{
147+
if (environment == null)
148+
{
149+
throw new ArgumentNullException("environment", Resources.EnvironmentNeedsToBeSpecified);
150+
}
151+
152+
if (AzureEnvironment.PublicEnvironments.ContainsKey(environment.Name))
153+
{
154+
throw new ArgumentException(Resources.ChangingDefaultEnvironmentNotSupported, "environment");
155+
}
156+
157+
if (_profile.Environments.ContainsKey(environment.Name))
158+
{
159+
_profile.Environments[environment.Name] =
160+
MergeEnvironmentProperties(environment, _profile.Environments[environment.Name]);
161+
}
162+
else
163+
{
164+
_profile.Environments[environment.Name] = environment;
165+
}
166+
167+
return _profile.Environments[environment.Name];
168+
}
169+
170+
public List<AzureEnvironment> ListEnvironments(string name)
171+
{
172+
var result = new List<AzureEnvironment>();
173+
174+
if (string.IsNullOrWhiteSpace(name))
175+
{
176+
result.AddRange(_profile.Environments.Values);
177+
}
178+
else if (_profile.Environments.ContainsKey(name))
179+
{
180+
result.Add(_profile.Environments[name]);
181+
}
182+
183+
return result;
184+
}
185+
186+
public AzureEnvironment RemoveEnvironment(string name)
187+
{
188+
if (string.IsNullOrEmpty(name))
189+
{
190+
throw new ArgumentNullException("name", Resources.EnvironmentNameNeedsToBeSpecified);
191+
}
192+
if (AzureEnvironment.PublicEnvironments.ContainsKey(name))
193+
{
194+
throw new ArgumentException(Resources.RemovingDefaultEnvironmentsNotSupported, "name");
195+
}
196+
197+
if (_profile.Environments.ContainsKey(name))
198+
{
199+
var environment = _profile.Environments[name];
200+
_profile.Environments.Remove(name);
201+
return environment;
202+
}
203+
else
204+
{
205+
throw new ArgumentException(string.Format(Resources.EnvironmentNotFound, name), "name");
206+
}
207+
}
208+
209+
private AzureEnvironment MergeEnvironmentProperties(AzureEnvironment environment1, AzureEnvironment environment2)
210+
{
211+
if (environment1 == null || environment2 == null)
212+
{
213+
throw new ArgumentNullException("environment1");
214+
}
215+
if (!string.Equals(environment1.Name, environment2.Name, StringComparison.InvariantCultureIgnoreCase))
216+
{
217+
throw new ArgumentException("Environment names do not match.");
218+
}
219+
AzureEnvironment mergedEnvironment = new AzureEnvironment
220+
{
221+
Name = environment1.Name
222+
};
223+
224+
// Merge all properties
225+
foreach (AzureEnvironment.Endpoint property in Enum.GetValues(typeof(AzureEnvironment.Endpoint)))
226+
{
227+
string propertyValue = environment1.GetEndpoint(property) ?? environment2.GetEndpoint(property);
228+
if (propertyValue != null)
229+
{
230+
mergedEnvironment.Endpoints[property] = propertyValue;
231+
}
232+
}
233+
234+
return mergedEnvironment;
235+
}
236+
143237
private bool TryGetTenantSubscription(
144238
AzureAccount account,
145239
AzureEnvironment environment,

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@
183183
</ItemGroup>
184184
<ItemGroup>
185185
<Compile Include="AzureRMProfileTests.cs" />
186+
<Compile Include="EnvironmentCmdletTests.cs" />
186187
<Compile Include="MockSubscriptionClientFactory.cs" />
187188
<Compile Include="ProfileController.cs" />
188189
<Compile Include="SubscriptionCmdletTests.cs" />

0 commit comments

Comments
 (0)