public async Task <IList <AutomationAccount> > GetAutomationAccounts() { if (currSubscription.Name == null) { throw new Exception(Properties.Resources.SubscriptionNotSet); } // Get the token for the tenant on this subscription. azureARMAuthResult = AuthenticateHelper.RefreshTokenByAuthority(currSubscription.Authority, Properties.Settings.Default.appIdURI); subscriptionCreds = new TokenCloudCredentials(currSubscription.SubscriptionId, azureARMAuthResult.AccessToken); automationManagementClient = new AutomationManagementClient(subscriptionCreds, new Uri(Properties.Settings.Default.appIdURI)); // Add user agent string to indicate this is coming from the ISE automation client. ProductInfoHeaderValue ISEClientAgent = new ProductInfoHeaderValue(Constants.ISEUserAgent, Constants.ISEVersion); automationManagementClient.UserAgent.Add(ISEClientAgent); //TODO: does this belong here? if (accountResourceGroups == null) { accountResourceGroups = new Dictionary <AutomationAccount, ResourceGroupExtended>(); } else { accountResourceGroups.Clear(); } IList <AutomationAccount> result = new List <AutomationAccount>(); // Get ARM automation account resources var automationResources = await GetAutomationResources(); // Retrieve all of the automation accounts found foreach (var resource in automationResources.Resources) { CancellationTokenSource cts = new CancellationTokenSource(); cts.CancelAfter(TIMEOUT_MS); // Find the resource group name from the resource id. var startPosition = resource.Id.IndexOf("/resourceGroups/"); var endPosition = resource.Id.IndexOf("/", startPosition + 16); var resourceGroup = resource.Id.Substring(startPosition + 16, endPosition - startPosition - 16); AutomationAccountGetResponse account = await automationManagementClient.AutomationAccounts.GetAsync(resourceGroup, resource.Name, cts.Token); result.Add(account.AutomationAccount); var accountResourceGroup = new ResourceGroupExtended(); accountResourceGroup.Name = resourceGroup; accountResourceGroups.Add(account.AutomationAccount, accountResourceGroup); } return(result); }
public async Task <IList <SubscriptionObject> > GetSubscriptions() { if (azureADAuthResult == null) { throw new Exception(Properties.Resources.AzureADAuthResult); } // Common subscription object to host subscriptions from RDFE & ARM IList <SubscriptionObject> subscriptionList = new List <SubscriptionObject>(); subscriptionCredentials = new Microsoft.Azure.TokenCloudCredentials(azureADAuthResult.AccessToken); subscriptionClient = new Microsoft.Azure.Subscriptions.SubscriptionClient(subscriptionCredentials, new Uri(Properties.Settings.Default.appIdURI)); var cancelToken = new CancellationToken(); var tenants = subscriptionClient.Tenants.ListAsync(cancelToken).Result; // Get subscriptions for each tenant foreach (var tenant in tenants.TenantIds) { try { AuthenticationResult tenantTokenCreds = AuthenticateHelper.RefreshTokenByAuthority(tenant.TenantId, Properties.Settings.Default.appIdURI); subscriptionCredentials = new Microsoft.Azure.TokenCloudCredentials(tenantTokenCreds.AccessToken); var tenantSubscriptionClient = new Microsoft.Azure.Subscriptions.SubscriptionClient(subscriptionCredentials, new Uri(Properties.Settings.Default.appIdURI)); var subscriptionListResults = tenantSubscriptionClient.Subscriptions.ListAsync(cancelToken).Result; foreach (var subscription in subscriptionListResults.Subscriptions) { var subList = new SubscriptionObject(); subList.Name = subscription.DisplayName; subList.SubscriptionId = subscription.SubscriptionId; subList.Authority = tenant.TenantId; subscriptionList.Add(subList); } } catch (Exception ex) { // ignored } } return(subscriptionList); }
public async Task <IList <ResourceGroupExtended> > GetResourceGroups() { if (currSubscription.Name == null) { throw new Exception(Properties.Resources.SubscriptionNotSet); } // Get the token for the tenant on this subscription. var cloudtoken = AuthenticateHelper.RefreshTokenByAuthority(azureARMAuthResult.TenantId, Properties.Settings.Default.appIdURI); subscriptionCreds = new TokenCloudCredentials(currSubscription.SubscriptionId, cloudtoken.AccessToken); resourceManagementClient = new ResourceManagementClient(subscriptionCreds, new Uri(Properties.Settings.Default.appIdURI)); ResourceGroupListResult resourceGroupResult = await resourceManagementClient.ResourceGroups.ListAsync(null); return(resourceGroupResult.ResourceGroups); }
public async Task <IList <AutomationAccount> > GetAutomationAccounts() { if (currSubscription.Name == null) { throw new Exception(Properties.Resources.SubscriptionNotSet); } // Get the token for the tenant on this subscription. azureARMAuthResult = AuthenticateHelper.RefreshTokenByAuthority(currSubscription.Authority); subscriptionCreds = new TokenCloudCredentials(currSubscription.SubscriptionId, azureARMAuthResult.AccessToken); automationManagementClient = new AutomationManagementClient(subscriptionCreds); // Add user agent string to indicate this is coming from the ISE automation client. ProductInfoHeaderValue ISEClientAgent = new ProductInfoHeaderValue(Constants.ISEUserAgent, Constants.ISEVersion); automationManagementClient.UserAgent.Add(ISEClientAgent); //TODO: does this belong here? if (accountResourceGroups == null) { accountResourceGroups = new Dictionary <AutomationAccount, ResourceGroupExtended>(); } else { accountResourceGroups.Clear(); } IList <AutomationAccount> result = new List <AutomationAccount>(); IList <ResourceGroupExtended> resourceGroups = await this.GetResourceGroups(); foreach (ResourceGroupExtended resourceGroup in resourceGroups) { CancellationTokenSource cts = new CancellationTokenSource(); cts.CancelAfter(TIMEOUT_MS); AutomationAccountListResponse accountListResponse = await automationManagementClient.AutomationAccounts.ListAsync(resourceGroup.Name, cts.Token); foreach (AutomationAccount account in accountListResponse.AutomationAccounts) { result.Add(account); accountResourceGroups.Add(account, resourceGroup); } } return(result); }
public static async Task <Boolean> CreateStorageAccount(String authority, AutomationManagementClient automationManagementClient, string resourceGroupName, AutomationAccount account, string storageResourceGroup, string storageSubID, string storageAccount, string storageRGLocation) { try { // Get the token for the tenant on this subscription. var cloudtoken = AuthenticateHelper.RefreshTokenByAuthority(authority, Properties.Settings.Default.appIdURI); var subscriptionCreds = new TokenCloudCredentials(storageSubID, cloudtoken.AccessToken); var resourceManagementClient = new ResourceManagementClient(subscriptionCreds, new Uri(Properties.Settings.Default.appIdURI)); // Check if the resource group exists, otherwise create it. var rgExists = resourceManagementClient.ResourceGroups.CheckExistence(storageResourceGroup); if (!(rgExists.Exists)) { var resourceGroup = new ResourceGroup { Location = storageRGLocation }; await resourceManagementClient.ResourceGroups.CreateOrUpdateAsync(storageResourceGroup, resourceGroup); } // Create storage client and set subscription to work against var token = new Microsoft.Rest.TokenCredentials(cloudtoken.AccessToken); var storageManagementClient = new Microsoft.Azure.Management.Storage.StorageManagementClient(new Uri(Properties.Settings.Default.appIdURI), token); storageManagementClient.SubscriptionId = storageSubID; // Use Standard local replication as the sku since it is not critical to keep these modules replicated var storageParams = new StorageAccountCreateParameters() { Location = storageRGLocation, Kind = Kind.Storage, Sku = new Microsoft.Azure.Management.Storage.Models.Sku(SkuName.StandardLRS) }; // Create storage account CancellationToken cancelToken = new CancellationToken(); await storageManagementClient.StorageAccounts.CreateAsync(storageResourceGroup, storageAccount, storageParams, cancelToken); } catch (Exception Ex) { throw Ex; } return(true); }
/// <summary> /// Refreshes the token used to access azure automation. /// This is currently called from a timer that runs on the Constants.tokenRefreshInterval /// If it is about to expire (2 minutes from the next refresh, it will renew) /// </summary> public void RefreshAutomationClientwithNewToken() { // Get the token for the tenant on this subscription and check if it is about to expire. // If it is, refresh it if possible. if (currSubscription.Name == null) { return; } if (azureARMAuthResult.ExpiresOn.ToLocalTime() < DateTime.Now.AddMinutes(Constants.tokenRefreshInterval + 2)) { azureARMAuthResult = AuthenticateHelper.RefreshTokenByAuthority(currSubscription.Authority, Properties.Settings.Default.appIdURI); subscriptionCreds = new TokenCloudCredentials(currSubscription.SubscriptionId, azureARMAuthResult.AccessToken); automationManagementClient = new AutomationManagementClient(subscriptionCreds, new Uri(Properties.Settings.Default.appIdURI)); // Add user agent string to indicate this is coming from the ISE automation client. ProductInfoHeaderValue ISEClientAgent = new ProductInfoHeaderValue(Constants.ISEUserAgent, Constants.ISEVersion); automationManagementClient.UserAgent.Add(ISEClientAgent); } }
public async Task <ResourceListResult> GetAutomationResources() { if (currSubscription.Name == null) { throw new Exception(Properties.Resources.SubscriptionNotSet); } // Get the token for the tenant on this subscription. var cloudtoken = AuthenticateHelper.RefreshTokenByAuthority(azureARMAuthResult.TenantId, Properties.Settings.Default.appIdURI); subscriptionCreds = new TokenCloudCredentials(currSubscription.SubscriptionId, cloudtoken.AccessToken); resourceManagementClient = new ResourceManagementClient(subscriptionCreds, new Uri(Properties.Settings.Default.appIdURI)); // Only get automation account resources ResourceListParameters automationResourceParams = new ResourceListParameters(); automationResourceParams.ResourceType = "Microsoft.Automation/automationAccounts"; ResourceListResult resources = await resourceManagementClient.Resources.ListAsync(automationResourceParams); return(resources); }