private async Task SaveEnrolledSubscriptions(ActivationInfo activationInfo) { foreach (var enrolledSubscription in activationInfo.EnrolledSubscriptions) { Run.WithProgressBackOff(5, 1, 5, () => { AzureResourceManagerUtil.GrantRoleToServicePrincipalOnSubscription(activationInfo.HostSubscription.ServicePrincipalId, enrolledSubscription.SubscriptionId); }); var subscriptionFromDb = this.coreRepository.GetSubscription(enrolledSubscription.SubscriptionId); Subscription subscriptionToSave = null; if (subscriptionFromDb == null) { subscriptionToSave = new Subscription { Id = enrolledSubscription.SubscriptionId, IsEnrolled = true, ConnectedOn = DateTime.Now, DisplayName = enrolledSubscription.SubscriptionName, OrganizationId = enrolledSubscription.OrganizationId, StorageName = storageName, ConnectedBy = ClaimsPrincipal.Current.Identity.Name, }; } else { subscriptionToSave = subscriptionFromDb; subscriptionToSave.IsEnrolled = true; } await this.coreRepository.SaveSubscription(subscriptionToSave); } }
private async Task SaveHostSubscription(ActivationInfo activationInfo) { if (string.IsNullOrEmpty(activationInfo.HostSubscription.ServicePrincipalId)) { var organizations = AzureResourceManagerUtil.GetUserOrganizations(); var selectedOrganization = organizations.Where(o => o.Id == activationInfo.HostSubscription.OrganizationId).FirstOrDefault(); activationInfo.HostSubscription.ServicePrincipalId = selectedOrganization.ObjectIdOfCloudSenseServicePrincipal; } Run.WithProgressBackOff(5, 1, 5, () => { AzureResourceManagerUtil.GrantRoleToServicePrincipalOnSubscription(activationInfo.HostSubscription.ServicePrincipalId, activationInfo.HostSubscription.SubscriptionId); }); // Create Resource Group to hold Storage Account string resourceGroup = Config.DefaultResourceGroup; var json = await AzureResourceManagerUtil.GetStorageProvider(activationInfo.HostSubscription.SubscriptionId); JObject storageProvider = Newtonsoft.Json.JsonConvert.DeserializeObject <JObject>(json); var client = Utils.GetResourceManagementClient(activationInfo.HostSubscription.SubscriptionId); string location = storageProvider["resourceTypes"][0]["locations"][0].ToString(); var rg = new ResourceGroup(location); var result = await client.ResourceGroups.CreateOrUpdateAsync(resourceGroup, rg, new CancellationToken()); // Create Storage Account this.storageName = await AzureResourceManagerUtil.CreateServiceCatalogMetadataStorageAccount(activationInfo.HostSubscription.SubscriptionId, resourceGroup); string key = await AzureResourceManagerUtil.GetStorageAccountKeysArm(activationInfo.HostSubscription.SubscriptionId, this.storageName); BlobHelpers.CreateInitialTablesAndBlobContainers(storageName, key); CacheDetails(activationInfo.HostSubscription.SubscriptionId, key, storageName, activationInfo.HostSubscription.OrganizationId); var orgGroups = AzureADGraphApiUtil.GetAllGroupsForOrganization(activationInfo.Organization.Id); ContributorGroup[] contributorGroups = new ContributorGroup[1]; contributorGroups[0] = new ContributorGroup(); contributorGroups[0].Id = orgGroups[0].Id; contributorGroups[0].Name = orgGroups[0].Name; var jsonContributorGroups = JsonConvert.SerializeObject(contributorGroups); await this.coreRepository.SaveSubscription(new Subscription { Id = activationInfo.HostSubscription.SubscriptionId, IsConnected = true, ConnectedOn = DateTime.Now, ContributorGroups = jsonContributorGroups, DisplayName = activationInfo.HostSubscription.SubscriptionName, OrganizationId = activationInfo.HostSubscription.OrganizationId, StorageName = storageName, ConnectedBy = ClaimsPrincipal.Current.Identity.Name, }); }
public async Task SaveEnrolledSubscriptions(SubscriptionsViewModel subscriptionsVM) { var tenantId = ClaimsPrincipal.Current.TenantId(); var organization = await this.coreRepository.GetOrganization(tenantId); foreach (var subscription in subscriptionsVM.Subscriptions) { var subscriptionFromDb = this.coreRepository.GetSubscription(subscription.Id); var isCurrentlyEnrolled = (subscriptionFromDb != null && subscriptionFromDb.IsEnrolled); var isCurrentlyConnectedHost = (subscriptionFromDb != null && subscriptionFromDb.IsConnected); var isSwitchingEnrollmentOn = (subscription.IsEnrolled && !isCurrentlyEnrolled); var isSwitchingEnrollmentOff = (!subscription.IsEnrolled && isCurrentlyEnrolled); var isSwitchingHostingOn = (subscription.IsConnected && !isCurrentlyConnectedHost); var isSwitchingHostingOff = (!subscription.IsConnected && isCurrentlyConnectedHost); if (isSwitchingEnrollmentOn || isSwitchingHostingOn) { Run.WithProgressBackOff(5, 1, 5, () => { AzureResourceManagerUtil.GrantRoleToServicePrincipalOnSubscription(organization.ObjectIdOfCloudSenseServicePrincipal, subscription.Id); }); } if (isSwitchingEnrollmentOff || isSwitchingHostingOff) { if (subscriptionsVM.Subscriptions.Count == 1) { throw new ValidationException("There must be at least one enrolled and one hosting subscription."); } int numberOfEnrolledSubscriptions = subscriptionsVM.Subscriptions.Count(s => s.IsEnrolled); if (numberOfEnrolledSubscriptions == 0) { throw new ValidationException("There must be at least one enrolled subscription."); } subscription.ContributorGroups = null; AzureResourceManagerUtil.RevokeRoleFromServicePrincipalOnSubscription(organization.ObjectIdOfCloudSenseServicePrincipal, subscription.Id); } if (isSwitchingHostingOn) { // Create Resource Group to hold Storage Account var client = Utils.GetResourceManagementClient(subscription.Id); var rg = new ResourceGroup(subscriptionsVM.Location); var result = await client.ResourceGroups.CreateOrUpdateAsync(subscriptionsVM.ResourceGroup, rg, new CancellationToken()); // Create Storage Account var storageName = await AzureResourceManagerUtil.CreateServiceCatalogMetadataStorageAccount(subscription.Id, subscriptionsVM.ResourceGroup); string key = await AzureResourceManagerUtil.GetStorageAccountKeysArm(subscription.Id, storageName); BlobHelpers.CreateInitialTablesAndBlobContainers(storageName, key); subscription.StorageName = storageName; subscription.ConnectedOn = DateTime.Now; subscription.ConnectedBy = ClaimsPrincipal.Current.Identity.Name; CacheDetails(subscription.Id, key, storageName, subscription.OrganizationId); } if (subscription.ConnectedOn.IsMinDate()) { subscription.ConnectedOn = DateTime.Now; } await this.coreRepository.SaveSubscription(subscription); } }