Exemplo n.º 1
0
        public ConfigModel CreateBlob(ConfigModel model)
        {
            if (model == null)
            {
                throw new HttpRequestWithStatusException("Empty model", new HttpResponseException(HttpStatusCode.BadRequest));
            }

            if (string.IsNullOrWhiteSpace(model.ClientId) ||
                string.IsNullOrWhiteSpace(model.ClientSecret) ||
                string.IsNullOrWhiteSpace(model.TenantId) ||
                string.IsNullOrWhiteSpace(model.Subscription))
            {
                throw new HttpRequestWithStatusException("ClientId/ClientSecret/TenantId/Subscription is empty", new HttpResponseException(HttpStatusCode.BadRequest));
            }

            try
            {
                var azure = AzureClient.GetAzure(model.ClientId,
                                                 model.ClientSecret,
                                                 model.TenantId,
                                                 model.Subscription);

                var resourceGroupName = GetUniqueHash(model.TenantId + model.ClientId + CommonName);
                model.SelectedRegion = Region.USEast.Name;
                IResourceGroup resourceGroup;
                try
                {
                    resourceGroup = azure.ResourceGroups.GetByName(resourceGroupName);
                }
                catch (Exception e)
                {
                    resourceGroup =
                        ApiHelper.CreateResourceGroup(azure, resourceGroupName, model.SelectedRegion);
                }
                model.SelectedDeploymentRg = resourceGroupName;
                var storageAccountName = GetUniqueHash(model.TenantId + model.ClientId + resourceGroupName + CommonName);
                model.StorageAccountName = storageAccountName;
                var             storageAccounts    = azure.StorageAccounts.ListByResourceGroup(resourceGroupName);
                IStorageAccount storageAccountInfo = null;
                if (storageAccounts != null && storageAccounts.Count() > 0)
                {
                    storageAccountInfo = storageAccounts.FirstOrDefault(x =>
                                                                        x.Name.Equals(storageAccountName, StringComparison.OrdinalIgnoreCase));
                }

                if (storageAccountInfo == null)
                {
                    try
                    {
                        storageAccountInfo = ApiHelper.CreateStorageAccount(azure, resourceGroup.Name, model.SelectedRegion,
                                                                            storageAccountName);
                    }
                    catch (Exception e)
                    {
                        throw new HttpRequestWithStatusException("storage account not created",
                                                                 new HttpResponseException(HttpStatusCode.InternalServerError));
                    }
                }

                var    storageKeys       = storageAccountInfo.GetKeys();
                string storageConnection = string.Format(StorageConStringFormat,
                                                         model.StorageAccountName, storageKeys[0].Value);
                AddAppsettings(StorageConnectionString, storageConnection);
                var storageAccount = CloudStorageAccount.Parse(storageConnection);
                var blockBlob      = ApiHelper.CreateBlobContainer(storageAccount);
                var configString   = ApiHelper.ConvertConfigObjectToString(model);
                using (var ms = new MemoryStream())
                {
                    LoadStreamWithJson(ms, configString);
                    blockBlob.UploadFromStream(ms);
                }

                //else
                //{
                //    var azureSettings = JsonConvert.DeserializeObject<AzureSettings>(data);
                //    model = ConvertAzureSettingsConfigModel(azureSettings);
                //}

                var functionAppName =
                    GetUniqueHash(model.ClientId + model.TenantId + model.Subscription + CommonName);

                var azureFunctions =
                    azure.AppServices.FunctionApps.ListByResourceGroup(resourceGroupName);

                // try to give the read and write permissions
                if (azureFunctions != null && azureFunctions.Count() > 0)
                {
                    return(model);
                }
                if (!DeployAzureFunctions(model, functionAppName, storageConnection, resourceGroupName))
                {
                    throw new HttpRequestWithStatusException("Azure Functions are not deployed",
                                                             new HttpResponseException(HttpStatusCode.InternalServerError));
                }

                return(model);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
        public FaultInjectionResponseModel <ConfigModel> CreateBlob(ConfigModel model)
        {
            if (model == null)
            {
                throw new HttpRequestWithStatusException("Empty model", new HttpResponseException(HttpStatusCode.BadRequest));
            }

            if (string.IsNullOrWhiteSpace(model.ClientId) ||
                string.IsNullOrWhiteSpace(model.ClientSecret) ||
                string.IsNullOrWhiteSpace(model.TenantId) ||
                string.IsNullOrWhiteSpace(model.Subscription))
            {
                throw new HttpRequestWithStatusException("ClientId/ClientSecret/TenantId/Subscription is empty", new HttpResponseException(HttpStatusCode.BadRequest));
            }

            if (2 * model.RollbackFrequency > model.SchedulerFrequency)
            {
                throw new Exception("Power Cycle Schedule Frequency should be less than half the Scheduler Frequency, Please decrease the Power Cycle Schedule or increase the Scheduler Frequency value");
            }

            if (2 * model.RollbackFrequency <= model.SchedulerFrequency &&
                model.SchedulerFrequency > (model.CrawlerFrequency + model.MeanTime))
            {
                throw new HttpRequestWithStatusException("Scheduler Frequency should be less than the sum of Crawler Frequency & Mean Time between FI on VM, Please reduce the value of Scheduler Frequency", new HttpResponseException(HttpStatusCode.BadRequest));
            }

            try
            {
                var storageAccountName = ConfigurationManager.AppSettings[StorageAccountName];
                var resourceGroupName  = ConfigurationManager.AppSettings[ResourceGroupName];

                if (string.IsNullOrWhiteSpace(storageAccountName) || string.IsNullOrWhiteSpace(resourceGroupName))
                {
                    throw new ArgumentNullException("Storage account name or resource group name is null "
                                                    + "Storage Param: " + StorageAccountName + " Resource Param: " + ResourceGroupName);
                }

                var azure = AzureClient.GetAzure(model.ClientId,
                                                 model.ClientSecret,
                                                 model.TenantId,
                                                 model.Subscription);

                var resourceGroup = azure.ResourceGroups.GetByName(resourceGroupName);
                if (resourceGroup == null)
                {
                    throw new ArgumentNullException(ResourceGroupName, "Resource group information is empty: " + ResourceGroupName);
                }

                var             storageAccounts    = azure.StorageAccounts.ListByResourceGroup(resourceGroupName);
                IStorageAccount storageAccountInfo = null;
                if (storageAccounts != null && storageAccounts.Count() > 0)
                {
                    storageAccountInfo = storageAccounts.FirstOrDefault(x =>
                                                                        x.Name.Equals(storageAccountName, StringComparison.OrdinalIgnoreCase));
                }

                if (storageAccountInfo == null)
                {
                    throw new ArgumentNullException(StorageAccountName, "Storage account information is empty: " + storageAccountName);
                }

                var    storageKeys       = storageAccountInfo.GetKeys();
                string storageConnection = string.Format(StorageConStringFormat,
                                                         storageAccountName, storageKeys[0].Value);

                InsertOrUpdateAppsettings(StorageConnectionString, storageConnection);
                var storageAccount = CloudStorageAccount.Parse(storageConnection);
                var blockBlob      = ApiHelper.CreateBlobContainer(storageAccount);
                var configString   = ApiHelper.ConvertConfigObjectToString(model);
                using (var ms = new MemoryStream())
                {
                    LoadStreamWithJson(ms, configString);
                    blockBlob.UploadFromStream(ms);
                }

                var functionAppName = CommonName +
                                      GetUniqueHash(model.ClientId + model.TenantId + model.Subscription);

                // TODO: ask ? do we deploy the azure functions, whenever user do any changes in the  config?
                if (!DeployAzureFunctions(model, functionAppName, storageConnection, resourceGroupName))
                {
                    throw new HttpRequestWithStatusException("Azure Functions are not deployed",
                                                             new HttpResponseException(HttpStatusCode.InternalServerError));
                }


                // add the tenant id and application id into the configuration file to validate the user next time.
                InsertOrUpdateAppsettings(TenantIdString, model.TenantId);
                InsertOrUpdateAppsettings(ApplicationIdString, model.ClientId);

                return(new FaultInjectionResponseModel <ConfigModel>()
                {
                    Success = true,
                    SuccessMessage = "Deployment Completed Successfully",
                    Result = model
                });
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }