public TClient CreateClient <TClient>(IAzureContextContainer container, IAzureSubscription subscription, string endpoint) where TClient : ServiceClient <TClient> { var profile = container as AzureSMProfile; if (subscription == null) { throw new ArgumentException(Commands.Common.Properties.Resources.InvalidDefaultSubscription); } if (profile == null) { profile = new AzureSMProfile(Path.Combine(AzureSession.Instance.ProfileDirectory, AzureSession.Instance.ProfileFile)); } SubscriptionCloudCredentials creds = new TokenCloudCredentials(subscription.Id.ToString(), "fake_token"); if (HttpMockServer.GetCurrentMode() != HttpRecorderMode.Playback) { ProfileClient profileClient = new ProfileClient(profile); AzureContext context = new AzureContext( subscription, profileClient.GetAccount(subscription.GetAccount()), profileClient.GetEnvironmentOrDefault(subscription.GetEnvironment()) ); creds = AzureSession.Instance.AuthenticationFactory.GetSubscriptionCloudCredentials(context); } Uri endpointUri = profile.EnvironmentTable[subscription.GetEnvironment()].GetEndpointAsUri(endpoint); return(CreateCustomClient <TClient>(creds, endpointUri)); }
public IAzureSubscription AddOrSetSubscription(IAzureSubscription subscription) { if (subscription == null) { throw new ArgumentNullException("subscription", Resources.SubscriptionNeedsToBeSpecified); } if (subscription.GetEnvironment() == null) { throw new ArgumentNullException("subscription.Environment", Resources.EnvironmentNeedsToBeSpecified); } // Validate environment GetEnvironmentOrDefault(subscription.GetEnvironment()); if (Profile.SubscriptionTable.ContainsKey(subscription.GetId())) { Profile.SubscriptionTable[subscription.GetId()] = MergeSubscriptionProperties(subscription, Profile.SubscriptionTable[subscription.GetId()]); } else { Debug.Assert(!string.IsNullOrEmpty(subscription.GetAccount())); if (!Profile.AccountTable.ContainsKey(subscription.GetAccount())) { throw new KeyNotFoundException(string.Format("The specified account {0} does not exist in profile accounts", subscription.GetAccount())); } Profile.SubscriptionTable[subscription.GetId()] = subscription; } return(Profile.SubscriptionTable[subscription.GetId()]); }
public TClient CreateClient <TClient>(IAzureContextContainer profile, IAzureSubscription subscription, string endpoint) where TClient : ServiceClient <TClient> { #if !NETSTANDARD if (subscription == null) { throw new ArgumentException(Microsoft.Azure.Commands.ResourceManager.Common.Properties.Resources.InvalidDefaultSubscription); } if (profile == null) { profile = new AzureSMProfile(Path.Combine(AzureSession.Instance.ProfileDirectory, AzureSession.Instance.ProfileFile)); } SubscriptionCloudCredentials creds = new TokenCloudCredentials(subscription.Id.ToString(), "fake_token"); if (HttpMockServer.GetCurrentMode() != HttpRecorderMode.Playback) { ProfileClient profileClient = new ProfileClient(profile as AzureSMProfile); AzureContext context = new AzureContext( subscription, profileClient.GetAccount(subscription.GetAccount()), profileClient.GetEnvironmentOrDefault(subscription.GetEnvironment()) ); creds = AzureSession.Instance.AuthenticationFactory.GetSubscriptionCloudCredentials(context); } Uri endpointUri = profile.Environments.FirstOrDefault((e) => e.Name.Equals(subscription.GetEnvironment(), StringComparison.OrdinalIgnoreCase)).GetEndpointAsUri(endpoint); return(CreateCustomClient <TClient>(creds, endpointUri)); #else throw new NotSupportedException("AzureSMProfile is not supported in Azure PS on .Net Core."); #endif }
public PSAzureSubscription(IAzureSubscription subscription, AzureSMProfile profile) { SubscriptionId = subscription.Id.ToString(); SubscriptionName = subscription.Name; Environment = subscription.GetEnvironment(); DefaultAccount = subscription.GetAccount(); Accounts = profile.AccountTable.Values.Where(a => a.HasSubscription(subscription.GetId())).ToArray(); IsDefault = subscription.IsPropertySet(AzureSubscription.Property.Default); IsCurrent = profile.Context != null && profile.Context.Subscription.Id == subscription.Id; CurrentStorageAccountName = subscription.GetProperty(AzureSubscription.Property.StorageAccount); TenantId = subscription.GetTenant(); }
internal Subscription(IAzureSubscription azureSubscription) { if (azureSubscription == null) { throw new ArgumentNullException(); } ProfileClient client = new ProfileClient(new AzureSMProfile(Path.Combine(AzureSession.Instance.ProfileDirectory, AzureSession.Instance.ProfileFile))); var environment = client.GetEnvironmentOrDefault(azureSubscription.GetEnvironment()); this.SubscriptionName = azureSubscription.Name; this.SubscriptionId = azureSubscription.Id.ToString(); this.ServiceEndpoint = new Uri(String.Format("{0}/{1}/services/systemcenter/vmm", environment.GetEndpoint(AzureEnvironment.Endpoint.ServiceManagement).TrimEnd(new[] { '/' }), SubscriptionId)); this.Certificate = FileUtilities.DataStore.GetCertificate(azureSubscription.GetAccount()); this.CredentialType = CredentialType.UseCertificate; }
/// <summary> /// Creates the client. /// </summary> /// <typeparam name="TClient">The type of the client.</typeparam> /// <param name="profile">The profile.</param> /// <param name="subscription">The subscription.</param> /// <param name="endpoint">The endpoint.</param> /// <returns></returns> /// <exception cref="System.ApplicationException"></exception> /// <exception cref="System.ArgumentException"> /// accountName /// or /// environment /// </exception> public virtual TClient CreateClient <TClient>(IAzureContextContainer profile, IAzureSubscription subscription, string endpoint) where TClient : ServiceClient <TClient> { if (subscription == null) { throw new ApplicationException(Resources.InvalidDefaultSubscription); } var account = profile.Accounts.FirstOrDefault((a) => string.Equals(a.Id, (subscription.GetAccount()), StringComparison.OrdinalIgnoreCase)); if (null == account) { throw new ArgumentException(string.Format("Account with name '{0}' does not exist.", subscription.GetAccount()), "accountName"); } var environment = profile.Environments.FirstOrDefault((e) => string.Equals(e.Name, subscription.GetEnvironment(), StringComparison.OrdinalIgnoreCase)); if (null == environment) { throw new ArgumentException(string.Format(Resources.EnvironmentNotFound, subscription.GetEnvironment())); } AzureContext context = new AzureContext(subscription, account, environment); var client = CreateClient <TClient>(context, endpoint); foreach (IClientAction action in GetActions()) { action.Apply <TClient>(client, profile, endpoint); } return(client); }
/// <summary> /// Executes the set subscription cmdlet operation. /// </summary> public override void ExecuteCmdlet() { IAzureSubscription subscription = null; if (!string.IsNullOrEmpty(SubscriptionId) && string.IsNullOrEmpty(SubscriptionName)) { subscription = ProfileClient.GetSubscription(new Guid(SubscriptionId)); Environment = subscription.GetEnvironment(); } else if (string.IsNullOrEmpty(SubscriptionId) && !string.IsNullOrEmpty(SubscriptionName)) { subscription = ProfileClient.GetSubscription(SubscriptionName); Environment = subscription.GetEnvironment(); } else { subscription = new AzureSubscription(); subscription.Id = SubscriptionId; subscription.Name = SubscriptionName; } IAzureEnvironment environment = ProfileClient.GetEnvironment(Environment, ServiceEndpoint, ResourceManagerEndpoint); if (environment == null) { var profileClient = new ProfileClient(Profile); environment = profileClient.GetEnvironment(Environment, ServiceEndpoint, ResourceManagerEndpoint); } if (environment == null) { throw new ArgumentException("ServiceEndpoint and ResourceManagerEndpoint values do not " + "match existing environment. Please use Environment parameter."); } else { subscription.SetEnvironment(environment.Name); } if (ServiceEndpoint != null || ResourceManagerEndpoint != null) { WriteWarning("Please use Environment parameter to specify subscription environment. This " + "warning will be converted into an error in the upcoming release."); } if (Certificate != null) { ProfileClient.ImportCertificate(Certificate); subscription.SetAccount(Certificate.Thumbprint); AzureAccount account = new AzureAccount { Id = Certificate.Thumbprint, Type = AzureAccount.AccountType.Certificate }; account.SetOrAppendProperty(AzureAccount.Property.Subscriptions, subscription.Id.ToString()); ProfileClient.AddOrSetAccount(account); if (subscription.GetAccount() == null) { subscription.SetAccount(account.Id); } } if (subscription.GetAccount() == null) { throw new ArgumentException("Certificate is required for creating a new subscription."); } if (!string.IsNullOrEmpty(CurrentStorageAccountName) || Context != null) { ProfileClient.GetAccount(subscription.GetAccount()); if (Profile.Context != null && Profile.Context.Subscription != null && Profile.Context.Subscription.Id == subscription.Id) { GeneralUtilities.ClearCurrentStorageAccount(); } var context = new AzureContext(subscription, ProfileClient.GetAccount(subscription.GetAccount()), ProfileClient.GetEnvironmentOrDefault(subscription.GetEnvironment())); if (Context != null) { context.SetCurrentStorageAccount(Context); } else { var client = AzureSession.Instance.ClientFactory.CreateClient <StorageManagementClient>(context, AzureEnvironment.Endpoint.ServiceManagement); var account = StorageUtilities.GenerateCloudStorageAccount(client, CurrentStorageAccountName); context.SetCurrentStorageAccount(account.ToString(true)); } } subscription = ProfileClient.AddOrSetSubscription(subscription); if (PassThru) { WriteObject(subscription); } }
private PSAzureSubscriptionExtended ConstructPsAzureSubscriptionExtended(IAzureSubscription subscription, IClientFactory clientFactory) { using (var client = clientFactory.CreateClient <ManagementClient>(Profile, subscription, AzureEnvironment.Endpoint.ServiceManagement)) { var response = client.Subscriptions.Get(); var environment = ProfileClient.GetEnvironmentOrDefault(subscription.GetEnvironment()); var account = ProfileClient.Profile.AccountTable[subscription.GetAccount()]; bool isCert = account.Type == AzureAccount.AccountType.Certificate; var psAzureSubscription = new PSAzureSubscription(subscription, ProfileClient.Profile); PSAzureSubscriptionExtended result = new PSAzureSubscriptionExtended(psAzureSubscription) { AccountAdminLiveEmailId = response.AccountAdminLiveEmailId, ActiveDirectoryUserId = subscription.GetAccount(), CurrentCoreCount = response.CurrentCoreCount, CurrentHostedServices = response.CurrentHostedServices, CurrentDnsServers = response.CurrentDnsServers, CurrentLocalNetworkSites = response.CurrentLocalNetworkSites, CurrentStorageAccounts = response.CurrentStorageAccounts, CurrentVirtualNetworkSites = response.CurrentVirtualNetworkSites, MaxCoreCount = response.MaximumCoreCount, MaxDnsServers = response.MaximumDnsServers, MaxHostedServices = response.MaximumHostedServices, MaxLocalNetworkSites = response.MaximumLocalNetworkSites, MaxStorageAccounts = response.MaximumStorageAccounts, MaxVirtualNetworkSites = response.MaximumVirtualNetworkSites, ServiceAdminLiveEmailId = response.ServiceAdminLiveEmailId, SubscriptionRealName = response.SubscriptionName, SubscriptionStatus = response.SubscriptionStatus.ToString(), ServiceEndpoint = environment.GetEndpoint(AzureEnvironment.Endpoint.ServiceManagement), ResourceManagerEndpoint = environment.GetEndpoint(AzureEnvironment.Endpoint.ResourceManager), IsDefault = subscription.GetProperty(AzureSubscription.Property.Default) != null, Account = account, Certificate = isCert ? AzureSession.Instance.DataStore.GetCertificate(subscription.GetAccount()) : null, CurrentStorageAccountName = subscription.GetProperty(AzureSubscription.Property.StorageAccount) }; return(result); } }