public static AzureSMProfile CreateAzureSMProfile(string storageAccount) { var profile = new AzureSMProfile(); var client = new ProfileClient(profile); var tenantId = Guid.NewGuid(); var subscriptionId = Guid.NewGuid(); var account = new AzureAccount { Id = "*****@*****.**", Type = AzureAccount.AccountType.User }; account.SetProperty(AzureAccount.Property.Tenants, tenantId.ToString()); account.SetProperty(AzureAccount.Property.Subscriptions, subscriptionId.ToString()); var subscription = new AzureSubscription() { Id = subscriptionId, Name = "Test Subscription 1", Environment = EnvironmentName.AzureCloud, Account = account.Id, }; subscription.SetProperty(AzureSubscription.Property.Tenants, tenantId.ToString()); subscription.SetProperty(AzureSubscription.Property.StorageAccount, storageAccount); client.AddOrSetAccount(account); client.AddOrSetSubscription(subscription); client.SetSubscriptionAsDefault(subscriptionId, account.Id); return profile; }
internal static AzureSubscription ToAzureSubscription(this Subscription other, AzureContext context) { var subscription = new AzureSubscription(); subscription.Account = context.Account != null ? context.Account.Id : null; subscription.Environment = context.Environment != null ? context.Environment.Name : EnvironmentName.AzureCloud; subscription.Id = new Guid(other.SubscriptionId); subscription.Name = other.DisplayName; subscription.State = other.State; subscription.SetProperty(AzureSubscription.Property.Tenants, context.Tenant.Id.ToString()); return subscription; }
private IEnumerable<AzureSubscription> ListServiceManagementSubscriptions(AzureAccount account, AzureEnvironment environment, SecureString password, ShowDialog promptBehavior, string[] tenants) { List<AzureSubscription> result = new List<AzureSubscription>(); if (!environment.IsEndpointSet(AzureEnvironment.Endpoint.ServiceManagement)) { return result; } foreach (var tenant in tenants) { try { var tenantAccount = new AzureAccount(); CopyAccount(account, tenantAccount); var tenantToken = AzureSession.AuthenticationFactory.Authenticate(tenantAccount, environment, tenant, password, ShowDialog.Never); if (string.Equals(tenantAccount.Id, account.Id, StringComparison.InvariantCultureIgnoreCase)) { tenantAccount = account; } tenantAccount.SetOrAppendProperty(AzureAccount.Property.Tenants, new string[] { tenant }); using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>( new TokenCloudCredentials(tenantToken.AccessToken), environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ServiceManagement))) { var subscriptionListResult = subscriptionClient.Subscriptions.List(); foreach (var subscription in subscriptionListResult.Subscriptions) { // only add the subscription if it's actually in this tenant if (subscription.ActiveDirectoryTenantId == tenant) { AzureSubscription psSubscription = new AzureSubscription { Id = new Guid(subscription.SubscriptionId), Name = subscription.SubscriptionName, Environment = environment.Name }; psSubscription.SetProperty(AzureSubscription.Property.Tenants, subscription.ActiveDirectoryTenantId); psSubscription.Account = tenantAccount.Id; tenantAccount.SetOrAppendProperty(AzureAccount.Property.Subscriptions, new string[] { psSubscription.Id.ToString() }); result.Add(psSubscription); } } } AddOrSetAccount(tenantAccount); } catch (CloudException cEx) { WriteOrThrowAadExceptionMessage(cEx); } catch (AadAuthenticationException aadEx) { WriteOrThrowAadExceptionMessage(aadEx); } } return result; }
private AzureSubscription MergeSubscriptionProperties(AzureSubscription subscription1, AzureSubscription subscription2) { if (subscription1 == null || subscription2 == null) { throw new ArgumentNullException("subscription1"); } if (subscription1.Id != subscription2.Id) { throw new ArgumentException("Subscription Ids do not match."); } AzureSubscription mergedSubscription = new AzureSubscription { Id = subscription1.Id, Name = subscription1.Name, Environment = subscription1.Environment, State = (subscription1.State != null && subscription1.State.Equals(subscription2.State, StringComparison.OrdinalIgnoreCase)) ? subscription1.State : null, Account = subscription1.Account ?? subscription2.Account }; // Merge all properties foreach (AzureSubscription.Property property in Enum.GetValues(typeof(AzureSubscription.Property))) { string propertyValue = subscription1.GetProperty(property) ?? subscription2.GetProperty(property); if (propertyValue != null) { mergedSubscription.Properties[property] = propertyValue; } } // Merge RegisteredResourceProviders var registeredProviders = subscription1.GetPropertyAsArray(AzureSubscription.Property.RegisteredResourceProviders) .Union(subscription2.GetPropertyAsArray(AzureSubscription.Property.RegisteredResourceProviders), StringComparer.CurrentCultureIgnoreCase); mergedSubscription.SetProperty(AzureSubscription.Property.RegisteredResourceProviders, registeredProviders.ToArray()); // Merge Tenants var tenants = subscription1.GetPropertyAsArray(AzureSubscription.Property.Tenants) .Union(subscription2.GetPropertyAsArray(AzureSubscription.Property.Tenants), StringComparer.CurrentCultureIgnoreCase); mergedSubscription.SetProperty(AzureSubscription.Property.Tenants, tenants.ToArray()); return mergedSubscription; }
public void AccountMatchingIgnoresCase() { var profile = new AzureSMProfile(); string accountName = "*****@*****.**"; string accountNameCase = "*****@*****.**"; var subscriptionId = Guid.NewGuid(); var tenantId = Guid.NewGuid(); var account = new AzureAccount { Id = accountName, Type = AzureAccount.AccountType.User }; account.SetProperty(AzureAccount.Property.Subscriptions, subscriptionId.ToString()); account.SetProperty(AzureAccount.Property.Tenants, tenantId.ToString()); var subscription = new AzureSubscription { Id = subscriptionId, Account = accountNameCase, Environment = EnvironmentName.AzureCloud }; subscription.SetProperty(AzureSubscription.Property.Default, "true"); subscription.SetProperty(AzureSubscription.Property.Tenants, tenantId.ToString()); profile.Accounts.Add(accountName, account); profile.Subscriptions.Add(subscriptionId, subscription); Assert.NotNull(profile.Context); Assert.NotNull(profile.Context.Account); Assert.NotNull(profile.Context.Environment); Assert.NotNull(profile.Context.Subscription); Assert.Equal(account, profile.Context.Account); Assert.Equal(subscription, profile.Context.Subscription); }
public void CanConvertValidAzureSubscriptions(string account, string name, string environment, string storageAccount, string expectedAccountName) { var oldSubscription = new AzureSubscription() { Account = account, Environment = environment, Id = Guid.NewGuid(), Name = name }; oldSubscription.SetProperty(AzureSubscription.Property.StorageAccount, storageAccount); oldSubscription.SetProperty(AzureSubscription.Property.Tenants, Guid.NewGuid().ToString()); var subscription = (PSAzureSubscription)oldSubscription; Assert.Equal(oldSubscription.Name, subscription.SubscriptionName); Assert.Equal(oldSubscription.Id.ToString(), subscription.SubscriptionId); Assert.Equal(oldSubscription.GetProperty(AzureSubscription.Property.Tenants), subscription.TenantId); Assert.Equal(expectedAccountName, subscription.CurrentStorageAccountName); Assert.Equal(storageAccount, subscription.CurrentStorageAccount); Assert.NotNull(subscription.ToString()); }
public AzureSubscription ToAzureSubscription(List<AzureEnvironment> envs) { AzureSubscription subscription = new AzureSubscription(); try { subscription.Id = new Guid(this.SubscriptionId); } catch (Exception ex) { throw new ArgumentException("Subscription ID is not a valid GUID.", ex); } subscription.Name = Name; // Logic to detect what is the subscription environment relies on having ManagementEndpoint (i.e. RDFE endpoint) set already on the subscription List<AzureEnvironment> allEnvs = envs.Union(AzureEnvironment.PublicEnvironments.Values).ToList(); AzureEnvironment env = allEnvs.FirstOrDefault(e => e.IsEndpointSetToValue(AzureEnvironment.Endpoint.ServiceManagement, this.ManagementEndpoint)); if (env != null) { subscription.Environment = env.Name; } else { subscription.Environment = EnvironmentName.AzureCloud; } if (!string.IsNullOrEmpty(this.ManagementCertificate)) { subscription.Account = this.ManagementCertificate; } if (!string.IsNullOrEmpty(this.ActiveDirectoryUserId)) { subscription.Account = this.ActiveDirectoryUserId; } if (!string.IsNullOrEmpty(this.ActiveDirectoryTenantId)) { subscription.SetProperty(AzureSubscription.Property.Tenants, ActiveDirectoryTenantId); } if (this.IsDefault) { subscription.SetProperty(AzureSubscription.Property.Default, "True"); } if (!string.IsNullOrEmpty(this.CloudStorageAccount)) { subscription.Properties.Add(AzureSubscription.Property.StorageAccount, this.CloudStorageAccount); } if (this.RegisteredResourceProviders.Count() > 0) { StringBuilder providers = new StringBuilder(); subscription.Properties.Add(AzureSubscription.Property.RegisteredResourceProviders, string.Join(",", RegisteredResourceProviders)); } return subscription; }