public AzureContext(AzureSubscription subscription, AzureAccount account, AzureEnvironment environment, AzureTenant tenant) { Subscription = subscription; Account = account; Environment = environment; Tenant = tenant; }
private static AzureTenant CreateTenantFromString(string tenantOrDomain, string accessTokenTenantId) { AzureTenant result = new AzureTenant(); Guid id; if (Guid.TryParse(tenantOrDomain, out id)) { result.Id = id; } else { result.Id = Guid.Parse(accessTokenTenantId); result.Domain = tenantOrDomain; } return result; }
private List<AzureTenant> ListAccountTenants(AzureAccount account, AzureEnvironment environment, SecureString password, ShowDialog promptBehavior) { List<AzureTenant> result = new List<AzureTenant>(); try { var commonTenantToken = AcquireAccessToken(account, environment, AuthenticationFactory.CommonAdTenant, password, promptBehavior); using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>( new TokenCloudCredentials(commonTenantToken.AccessToken), environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager))) { //TODO: Fix subscription client to not require subscriptionId result = account.MergeTenants(subscriptionClient.Tenants.List().TenantIds, commonTenantToken); } } catch { WriteWarningMessage(string.Format(Microsoft.Azure.Commands.Profile.Properties.Resources.UnableToAqcuireToken, AuthenticationFactory.CommonAdTenant)); if (account.IsPropertySet(AzureAccount.Property.Tenants)) { result = account.GetPropertyAsArray(AzureAccount.Property.Tenants) .Select( ti => { var tenant = new AzureTenant(); Guid guid; if(Guid.TryParse(ti, out guid)) { tenant.Id = guid; tenant.Domain = AccessTokenExtensions.GetDomain(account.Id); } else { tenant.Domain = ti; } return tenant; }).ToList(); } } return result; }
private bool TryGetTenantSubscription(IAccessToken accessToken, AzureAccount account, AzureEnvironment environment, string tenantId, string subscriptionId, string subscriptionName, out AzureSubscription subscription, out AzureTenant tenant) { using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>( new TokenCloudCredentials(accessToken.AccessToken), environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager))) { Subscriptions.Models.Subscription subscriptionFromServer = null; try { if (subscriptionId != null) { subscriptionFromServer = subscriptionClient.Subscriptions.Get(subscriptionId).Subscription; } else { var subscriptions = (subscriptionClient.Subscriptions.List().Subscriptions ?? new List<Microsoft.Azure.Subscriptions.Models.Subscription>()) .Where(s => "enabled".Equals(s.State, StringComparison.OrdinalIgnoreCase) || "warned".Equals(s.State, StringComparison.OrdinalIgnoreCase)); if (subscriptions.Any()) { if (subscriptionName != null) { subscriptionFromServer = subscriptions.FirstOrDefault( s => s.DisplayName.Equals(subscriptionName, StringComparison.OrdinalIgnoreCase)); } else { if (subscriptions.Count() > 1) { WriteWarningMessage(string.Format( "TenantId '{0}' contains more than one active subscription. First one will be selected for further use. " + "To select another subscription, use Set-AzureRmContext.", tenantId)); } subscriptionFromServer = subscriptions.First(); } } } } catch (CloudException ex) { WriteWarningMessage(ex.Message); } if (subscriptionFromServer != null) { subscription = new AzureSubscription { Id = new Guid(subscriptionFromServer.SubscriptionId), Account = accessToken.UserId, Environment = environment.Name, Name = subscriptionFromServer.DisplayName, Properties = new Dictionary<AzureSubscription.Property, string> { { AzureSubscription.Property.Tenants, accessToken.TenantId } } }; tenant = new AzureTenant(); tenant.Id = new Guid(accessToken.TenantId); tenant.Domain = accessToken.GetDomain(); return true; } subscription = null; if (accessToken != null && accessToken.TenantId != null) { tenant = new AzureTenant(); tenant.Id = Guid.Parse(accessToken.TenantId); if (accessToken.UserId != null) { var domain = accessToken.UserId.Split(new[] { '@' }, StringSplitOptions.RemoveEmptyEntries); if (domain.Length == 2) { tenant.Domain = domain[1]; } } return true; } tenant = null; return false; } }
private AzureTenant CreateTenant(string tenantIdOrDomain) { var tenant = new AzureTenant(); Guid tenantIdGuid; if (Guid.TryParse(tenantIdOrDomain, out tenantIdGuid)) { tenant.Id = tenantIdGuid; } else { tenant.Domain = tenantIdOrDomain; } return tenant; }
public void CanConvertValidAzureTenants(string domain) { var oldTenant = new AzureTenant() { Domain = domain, Id = Guid.NewGuid(), }; var tenant = (PSAzureTenant) oldTenant; Assert.Equal(oldTenant.Domain, tenant.Domain); Assert.Equal(oldTenant.Id.ToString(), tenant.TenantId); Assert.NotNull(tenant.ToString()); }
private void SetupAzureEnvironmentFromEnvironmentVariables(AzureModule mode) { TestEnvironment currentEnvironment = null; if (mode == AzureModule.AzureResourceManager) { currentEnvironment = new CSMTestEnvironmentFactory().GetTestEnvironment(); } else { currentEnvironment = new RDFETestEnvironmentFactory().GetTestEnvironment(); } if (currentEnvironment.UserName == null) { currentEnvironment.UserName = "******"; } SetAuthenticationFactory(mode, currentEnvironment); AzureEnvironment environment = new AzureEnvironment { Name = testEnvironmentName }; Debug.Assert(currentEnvironment != null); environment.Endpoints[AzureEnvironment.Endpoint.ActiveDirectory] = currentEnvironment.Endpoints.AADAuthUri.AbsoluteUri; environment.Endpoints[AzureEnvironment.Endpoint.Gallery] = currentEnvironment.Endpoints.GalleryUri.AbsoluteUri; environment.Endpoints[AzureEnvironment.Endpoint.ServiceManagement] = currentEnvironment.BaseUri.AbsoluteUri; environment.Endpoints[AzureEnvironment.Endpoint.ResourceManager] = currentEnvironment.Endpoints.ResourceManagementUri.AbsoluteUri; if (!ProfileClient.Profile.Environments.ContainsKey(testEnvironmentName)) { ProfileClient.AddOrSetEnvironment(environment); } if (!AzureRmProfileProvider.Instance.Profile.Environments.ContainsKey(testEnvironmentName)) { AzureRmProfileProvider.Instance.Profile.Environments[testEnvironmentName] = environment; } if (currentEnvironment.SubscriptionId != null) { testSubscription = new AzureSubscription() { Id = new Guid(currentEnvironment.SubscriptionId), Name = testSubscriptionName, Environment = testEnvironmentName, Account = currentEnvironment.UserName, Properties = new Dictionary<AzureSubscription.Property, string> { {AzureSubscription.Property.Default, "True"}, { AzureSubscription.Property.StorageAccount, Environment.GetEnvironmentVariable("AZURE_STORAGE_ACCOUNT") }, } }; testAccount = new AzureAccount() { Id = currentEnvironment.UserName, Type = AzureAccount.AccountType.User, Properties = new Dictionary<AzureAccount.Property, string> { {AzureAccount.Property.Subscriptions, currentEnvironment.SubscriptionId}, } }; ProfileClient.Profile.Subscriptions[testSubscription.Id] = testSubscription; ProfileClient.Profile.Accounts[testAccount.Id] = testAccount; ProfileClient.SetSubscriptionAsDefault(testSubscription.Name, testSubscription.Account); var testTenant = new AzureTenant() { Id = Guid.NewGuid() }; if (!string.IsNullOrEmpty(currentEnvironment.Tenant)) { Guid tenant; if (Guid.TryParse(currentEnvironment.Tenant, out tenant)) { testTenant.Id = tenant; } } AzureRmProfileProvider.Instance.Profile.Context = new AzureContext(testSubscription, testAccount, environment, testTenant); } }
/// <summary> /// Creates new instance of AzureContext. /// </summary> /// <param name="account">The azure account object</param> /// <param name="environment">The azure environment object</param> /// <param name="tenant">The azure tenant object</param> public AzureContext(AzureAccount account, AzureEnvironment environment, AzureTenant tenant) : this(null, account, environment, tenant) { }
private bool TryGetTenantSubscription(IAccessToken accessToken, AzureAccount account, AzureEnvironment environment, string tenantId, string subscriptionId, string subscriptionName, out AzureSubscription subscription, out AzureTenant tenant) { using (var subscriptionClient = AzureSession.ClientFactory.CreateCustomClient<SubscriptionClient>( new TokenCloudCredentials(accessToken.AccessToken), environment.GetEndpointAsUri(AzureEnvironment.Endpoint.ResourceManager))) { Subscriptions.Models.Subscription subscriptionFromServer = null; try { if (subscriptionId != null) { subscriptionFromServer = subscriptionClient.Subscriptions.Get(subscriptionId).Subscription; } else { var subscriptions = subscriptionClient.Subscriptions.List().Subscriptions; if (subscriptions != null && subscriptions.Any()) { if (subscriptionName != null) { subscriptionFromServer = subscriptions.FirstOrDefault(s => s.DisplayName.Equals(subscriptionName, StringComparison.OrdinalIgnoreCase)); } else { if (subscriptions.Count > 1) { WriteWarningMessage(string.Format( "Tenant '{0}' contains more than one subscription. First one will be selected for further use. " + "To select another subscription, use Set-AzureRmContext.", tenantId)); } subscriptionFromServer = subscriptions.First(); } } } } catch (CloudException ex) { WriteWarningMessage(ex.Message); } if (subscriptionFromServer != null) { subscription = new AzureSubscription { Id = new Guid(subscriptionFromServer.SubscriptionId), Account = accessToken.UserId, Environment = environment.Name, Name = subscriptionFromServer.DisplayName, Properties = new Dictionary<AzureSubscription.Property, string> { { AzureSubscription.Property.Tenants, accessToken.TenantId } } }; account.Properties[AzureAccount.Property.Tenants] = accessToken.TenantId; tenant = new AzureTenant(); tenant.Id = new Guid(accessToken.TenantId); tenant.Domain = accessToken.GetDomain(); return true; } subscription = null; tenant = null; return false; } }