/// <summary> /// Create new resource group /// </summary> /// <param name="parametersDictionary">List of {parameter-value} pairs</param> /// <param name="location"></param> /// <returns></returns> public async Task <ResourceGroup> CreateResourceGroup(Dictionary <string, string> tagsDictionary, string resourceGroupName, string subscriptionId, string location) { var resourceGroup = new ResourceGroup() { Location = location, Name = resourceGroupName, Tags = tagsDictionary, }; var restApiClient = new RestApiClient(); var endpointUrl = string.Format(UriConstants.CreateResourceGroupUri, subscriptionId, resourceGroup.Name); var result = await restApiClient.CallPutAsync <ResourceGroup, ResourceGroup>(resourceGroup, endpointUrl, await ServicePrincipal.GetAccessToken()); Log.Info(TemplateHelper.ToJson(result)); if (result.Success) { return(result.Result); } throw new ServiceCatalogException(result.Message); }
public async Task <RedirectToRouteResult> Deploy() { try { Log.Info("Read parameters from request data."); var templateIdStr = Request.Form["TemplateId"]; int templateId; if (!int.TryParse(templateIdStr, out templateId)) { throw new ServiceCatalogException("Couldn't read template ID from request."); } Log.Info($"Template ID: {templateId}"); var subscriptionId = Request.Form["SelectedSubscriptionId"]; var location = WebConfigurationManager.AppSettings[ConfigurationConstants.DefaultLocation]; var context = new WebAppContext(); var template = context.TemplateJsons.FirstOrDefault(tj => tj.TemplateId == templateId); if (template == null) { throw new ServiceCatalogException("Couldn't locate template with specified template ID."); } Log.Info("Parse parameters from request data"); var paramsDict = new Dictionary <string, string>(); var tagsDict = new Dictionary <string, string>() { { "Deploy", "ServiceCatalog" }, }; FillParametersAndTagsDictionaries(template.TemplateJson, ref paramsDict, ref tagsDict); Log.Info($"Is Manage Template: {template.IsManageTemplate}"); var jobId = Guid.NewGuid().ToString(); if (template.IsManageTemplate) { Log.Info($"Job Id: {jobId}"); paramsDict["jobid"] = jobId; } var deploymentsId = Guid.NewGuid(); Log.Info("Start deployments - {0}", deploymentsId); var resourceGroupName = template.IsManageTemplate ? "automation-account-resource-group" : Request.Form["SelectedResourceGroupName"]; if (string.IsNullOrWhiteSpace(subscriptionId) || string.IsNullOrWhiteSpace(resourceGroupName)) { throw new ServiceCatalogException("You should specify both Subscription and Resource Group."); } Log.Info($"Subscription ID: {subscriptionId}"); Log.Info($"Resource group name: {resourceGroupName}"); var resourceGroup = await GetOrCreateResourceGroup(resourceGroupName, subscriptionId, location, tagsDict); var parametersObj = TemplateHelper.PrepareTemplateParametersWithValues(template.TemplateJson, paramsDict); Log.Info($"Parameters: {parametersObj}"); var deployment = new Deployment { Properties = new DeploymentProperties { Mode = DeploymentMode.Incremental, Template = JObject.Parse(template.TemplateJson), Parameters = parametersObj, DebugSetting = new DebugSetting() { DetailLevel = "requestContent, responseContent" } } }; var deploymentId = Guid.NewGuid().ToString(); var subscriptions = await new SubscriptionController().GetSubscriptions(); var subscription = subscriptions.FirstOrDefault(s => s.SubscriptionId == subscriptionId); if (template.IsManageTemplate) { using (WebAppContext webAppContext = new WebAppContext()) { Job job = new Job() { Id = jobId, Owner = System.Web.HttpContext.Current.User.Identity.Name }; webAppContext.Jobs.Add(job); webAppContext.SaveChanges(); } } else { using (WebAppContext webAppContext = new WebAppContext()) { DeploymentViewModel deploymentViewModel = new DeploymentViewModel() { DeploymentName = deploymentId, TemplateVersion = template.TemplateJsonVersion, Owner = System.Web.HttpContext.Current.User.Identity.Name, TemplateName = template.TemplateName, Timestamp = DateTime.Now, SubscriptionId = subscription?.SubscriptionId, SubscriptionName = subscription?.DisplayName }; webAppContext.Deployments.Add(deploymentViewModel); webAppContext.SaveChanges(); } } // Preparing endpoint URL for deployment var endpointUrl = string.Format(UriConstants.CreateDeploymentsUri, subscriptionId, resourceGroup.Name, deploymentId); Log.Info($"Sending request to API: {endpointUrl}"); // Start deployment call async var client = new RestApiClient(); var result = await client.CallPutAsync <Deployment, Deployment>(deployment, endpointUrl, await ServicePrincipal.GetAccessToken()); Log.Info(TemplateHelper.ToJson($"Request result: {TemplateHelper.ToJson(result)}")); ViewBag.AsyncOperationUrl = result.AsyncOperationUrl; ViewBag.OperationResultUrl = endpointUrl; ViewBag.FileLogName = $"{DateTime.Today:yyyy-MM-dd}.log"; return(template.IsManageTemplate ? RedirectToAction("RunBooksView", "RunBooks") : RedirectToAction("DeploymentsView", "Deployments")); } catch (Exception exception) { ViewBag.ErrorMessage = "Error"; ViewBag.ErrorDetails = exception.Message; Log.Error(exception); return(RedirectToAction("DeploymentsView", "Deployments")); } }