/// <summary> /// Gets all armTemplates within an offer. /// </summary> /// <param name="offerName">The name of the offer.</param> /// <returns>A list of armTemplates.</returns> public async Task <List <ArmTemplate> > GetAllAsync(string offerName) { _logger.LogInformation(LoggingUtils.ComposeGetAllResourcesMessage(typeof(ArmTemplate).Name, offerName: offerName)); // Get the offer associated with the offerName provided var offer = await _offerService.GetAsync(offerName); // Get all armTemplates with a FK to the offer var armTemplates = await _context.ArmTemplates.Where(a => a.OfferId == offer.Id).ToListAsync(); foreach (var armTemplate in armTemplates) { // Generate Sas key armTemplate.TemplateFilePath = await _storageUtility.GetFileReferenceWithSasKeyAsync(armTemplate.TemplateFilePath); } _logger.LogInformation(LoggingUtils.ComposeReturnCountMessage(typeof(ArmTemplate).Name, armTemplates.Count())); return(armTemplates); }
public async Task <Subscription> DeployArmTemplateAsync(Guid subscriptionId) { Subscription subscription = await _context.Subscriptions.FindAsync(subscriptionId); ValidateSubscriptionAndInputState(subscription); try { if (subscription.ResourceGroup == null) { // If resource group is not created, transit to ProvisioningPending state to create resource group return(await TransitToNextState(subscription, ProvisioningState.ProvisioningPending)); } Offer offer = await FindOfferById(subscription.OfferId); Plan plan = await FindPlanById(subscription.PlanId); string deploymentName = $"{plan.PlanName}{offer.OfferName}{new Random().Next(0, 9999).ToString("D4")}"; // deployment name cannot exceed 64 characters, otherwise returns 400 string isvSubscriptionId = offer.HostSubscription.ToString(); string templatePath = await GetTemplatePath(plan, subscription.ProvisioningType); if (templatePath == null) { // If template is not specified, do nothing and transit to WebhookPending state return(await TransitToNextState(subscription, ProvisioningState.WebhookPending)); } // Reevaluate parameters if not subscribe. If it is subscribe, the parameters are evaluated when creating resource group if (!subscription.ProvisioningType.Equals(nameof(ProvisioningType.Subscribe))) { await EvaluateParameters(offer, plan, subscription); } templatePath = await _storageUtility.GetFileReferenceWithSasKeyAsync(templatePath); JObject parameters = new JObject(); using (WebClient client = new WebClient()) { string content = client.DownloadString(templatePath); Context context = await SetContext(offer.OfferName, subscription.Owner, subscriptionId, plan.PlanName, subscription.ProvisioningType); var paramList = ARMTemplateHelper.GetArmTemplateParameters(content); foreach (var param in paramList) { JProperty value = new JProperty("value", context.Parameters[param.Key]); parameters.Add(param.Key, new JObject(value)); } } _logger.LogInformation( LoggingUtils.ComposeHttpClientLogMessage( _provisioningClient.GetType().Name, nameof(_provisioningClient.PutDeploymentAsync), subscriptionId)); var result = await _provisioningClient.PutDeploymentAsync( Guid.NewGuid(), Guid.NewGuid(), isvSubscriptionId, subscription.ResourceGroup, deploymentName, parameters : parameters, templatePath : templatePath); subscription.DeploymentName = result.Name; _logger.LogInformation($"Running ARM deployment {deploymentName} for subscription {isvSubscriptionId} in resource group {subscription.ResourceGroup}."); return(await TransitToNextState(subscription, ProvisioningState.ArmTemplateRunning)); } catch (Exception e) { return(await HandleExceptions(subscription, e)); } }