|
16 | 16 | using Microsoft.Azure.Common.Authentication;
|
17 | 17 | using Microsoft.Azure.Common.Authentication.Factories;
|
18 | 18 | using Microsoft.Azure.Common.Authentication.Models;
|
| 19 | +using Microsoft.Azure.Common.Authentication.Properties; |
19 | 20 | using Microsoft.Azure.Subscriptions;
|
20 | 21 | using Microsoft.IdentityModel.Clients.ActiveDirectory;
|
21 | 22 | using System;
|
22 | 23 | using System.Collections.Generic;
|
| 24 | +using System.Diagnostics; |
23 | 25 | using System.Linq;
|
24 | 26 | using System.Management.Automation;
|
25 | 27 | using System.Security;
|
@@ -140,6 +142,98 @@ public bool TryGetSubscription(string tenantId, string subscriptionId, out Azure
|
140 | 142 | tenantId, subscriptionId, null, ShowDialog.Never, out subscription, out tenant);
|
141 | 143 | }
|
142 | 144 |
|
| 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 | + |
143 | 237 | private bool TryGetTenantSubscription(
|
144 | 238 | AzureAccount account,
|
145 | 239 | AzureEnvironment environment,
|
|
0 commit comments