Esempio n. 1
0
        public static async Task <string> CreateServiceCatalogMetadataStorageAccount(string subscriptionId, string resourceGroupName)
        {
            string storageAccountName = "svccat" + BlobHelpers.RandomString(18);
            var    parameters         = new { storageAccountName = new { value = storageAccountName }, storageAccountType = new { value = "Standard_LRS" } };

            await CreateStorageAccountARM(subscriptionId, resourceGroupName, parameters);

            return(storageAccountName);
        }
Esempio n. 2
0
        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,
            });
        }
Esempio n. 3
0
        private async Task <string> SaveProductImageAsBlob(string productImage)
        {
            string productImagePath = null;

            //First save the template blob if it exists
            if (!string.IsNullOrEmpty(productImage))
            {
                string       base64prefix    = ";base64,";
                string       base64Substring = productImage.Substring(productImage.LastIndexOf(base64prefix) + base64prefix.Length);
                byte[]       bytes           = Convert.FromBase64String(base64Substring);
                var          imagePrefix     = "image/";
                int          mimeTypeIndexOf = productImage.IndexOf(imagePrefix);
                string       contentType     = productImage.Substring(mimeTypeIndexOf, productImage.IndexOf(base64prefix) - mimeTypeIndexOf);
                string       fileExtension   = contentType.Substring(imagePrefix.Length);
                MemoryStream ms       = new MemoryStream(bytes);
                string       imageUri = await BlobHelpers.SaveToBlobContainer(BlobContainers.ProductImages, ms, fileExtension, contentType, productImagePath);

                productImagePath = imageUri;
            }

            return(productImagePath);
        }
Esempio n. 4
0
        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);
            }
        }