Skip to content

[#102921534] Create Environment cmdlets for AzureRM Profile module #926

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions src/Common/Commands.ResourceManager.Common/RMProfileClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
using Microsoft.Azure.Common.Authentication;
using Microsoft.Azure.Common.Authentication.Factories;
using Microsoft.Azure.Common.Authentication.Models;
using Microsoft.Azure.Common.Authentication.Properties;
using Microsoft.Azure.Subscriptions;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Management.Automation;
using System.Security;
Expand Down Expand Up @@ -140,6 +142,98 @@ public bool TryGetSubscription(string tenantId, string subscriptionId, out Azure
tenantId, subscriptionId, null, ShowDialog.Never, out subscription, out tenant);
}

public AzureEnvironment AddOrSetEnvironment(AzureEnvironment environment)
{
if (environment == null)
{
throw new ArgumentNullException("environment", Resources.EnvironmentNeedsToBeSpecified);
}

if (AzureEnvironment.PublicEnvironments.ContainsKey(environment.Name))
{
throw new ArgumentException(Resources.ChangingDefaultEnvironmentNotSupported, "environment");
}

if (_profile.Environments.ContainsKey(environment.Name))
{
_profile.Environments[environment.Name] =
MergeEnvironmentProperties(environment, _profile.Environments[environment.Name]);
}
else
{
_profile.Environments[environment.Name] = environment;
}

return _profile.Environments[environment.Name];
}

public List<AzureEnvironment> ListEnvironments(string name)
{
var result = new List<AzureEnvironment>();

if (string.IsNullOrWhiteSpace(name))
{
result.AddRange(_profile.Environments.Values);
}
else if (_profile.Environments.ContainsKey(name))
{
result.Add(_profile.Environments[name]);
}

return result;
}

public AzureEnvironment RemoveEnvironment(string name)
{
if (string.IsNullOrEmpty(name))
{
throw new ArgumentNullException("name", Resources.EnvironmentNameNeedsToBeSpecified);
}
if (AzureEnvironment.PublicEnvironments.ContainsKey(name))
{
throw new ArgumentException(Resources.RemovingDefaultEnvironmentsNotSupported, "name");
}

if (_profile.Environments.ContainsKey(name))
{
var environment = _profile.Environments[name];
_profile.Environments.Remove(name);
return environment;
}
else
{
throw new ArgumentException(string.Format(Resources.EnvironmentNotFound, name), "name");
}
}

private AzureEnvironment MergeEnvironmentProperties(AzureEnvironment environment1, AzureEnvironment environment2)
{
if (environment1 == null || environment2 == null)
{
throw new ArgumentNullException("environment1");
}
if (!string.Equals(environment1.Name, environment2.Name, StringComparison.InvariantCultureIgnoreCase))
{
throw new ArgumentException("Environment names do not match.");
}
AzureEnvironment mergedEnvironment = new AzureEnvironment
{
Name = environment1.Name
};

// Merge all properties
foreach (AzureEnvironment.Endpoint property in Enum.GetValues(typeof(AzureEnvironment.Endpoint)))
{
string propertyValue = environment1.GetEndpoint(property) ?? environment2.GetEndpoint(property);
if (propertyValue != null)
{
mergedEnvironment.Endpoints[property] = propertyValue;
}
}

return mergedEnvironment;
}

private bool TryGetTenantSubscription(
AzureAccount account,
AzureEnvironment environment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AzureRMProfileTests.cs" />
<Compile Include="EnvironmentCmdletTests.cs" />
<Compile Include="MockSubscriptionClientFactory.cs" />
<Compile Include="ProfileController.cs" />
<Compile Include="SubscriptionCmdletTests.cs" />
Expand Down
Loading