public async Task <ActionResult> CreateOrUpdateAsync(string offerName, [FromBody] Offer offer) { AADAuthHelper.VerifyUserAccess(this.HttpContext, _logger, true); if (offer == null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(nameof(offer)), UserErrorCode.PayloadNotProvided); } if (!offerName.Equals(offer.OfferName)) { throw new LunaBadRequestUserException(LoggingUtils.ComposeNameMismatchErrorMessage(typeof(Offer).Name), UserErrorCode.NameMismatch); } if (await _offerService.ExistsAsync(offerName)) { _logger.LogInformation($"Update offer {offerName} with payload {JsonConvert.SerializeObject(offer)}"); await _offerService.UpdateAsync(offerName, offer); return(Ok(offer)); } else { _logger.LogInformation($"Create offer {offerName} with payload {JsonConvert.SerializeObject(offer)}"); await _offerService.CreateAsync(offer); return(CreatedAtRoute(nameof(GetAsync) + nameof(Offer), new { offerName = offer.OfferName }, offer)); } }
/// <summary> /// Checks if an armTemplateParameter exists within an offer. /// </summary> /// <param name="offerName">The name of the offer.</param> /// <param name="name">The name of the armTemplateParameter to check exists.</param> /// <returns>True if exists, false otherwise.</returns> public async Task <bool> ExistsAsync(string offerName, string name) { _logger.LogInformation(LoggingUtils.ComposeCheckResourceExistsMessage(typeof(ArmTemplateParameter).Name, name, offerName: offerName)); if (!await _offerService.ExistsAsync(offerName)) { // Instead of throw NotFound exception, just return false. _logger.LogInformation(LoggingUtils.ComposeResourceExistsOrNotMessage(typeof(ArmTemplateParameter).Name, name, false, offerName: offerName)); return(false); } //Get the offer associated with the offerName provided var offer = await _offerService.GetAsync(offerName); // Check that only one armTemplateParameter with this name exists within the offer var count = await _context.ArmTemplateParameters .CountAsync(a => (a.OfferId == offer.Id) && (a.Name == name)); // More than one instance of an object with the same name exists, this should not happen if (count > 1) { throw new NotSupportedException(LoggingUtils.ComposeFoundDuplicatesErrorMessage(typeof(ArmTemplateParameter).Name, name, offerName: offerName)); } else if (count == 0) { _logger.LogInformation(LoggingUtils.ComposeResourceExistsOrNotMessage(typeof(ArmTemplateParameter).Name, name, false, offerName: offerName)); return(false); } else { _logger.LogInformation(LoggingUtils.ComposeResourceExistsOrNotMessage(typeof(ArmTemplateParameter).Name, name, true, offerName: offerName)); // count = 1 return(true); } }
/// <summary> /// Gets an armTemplate within an offer. /// </summary> /// <param name="offerName">The name of the offer.</param> /// <param name="templateName">The name of the armTemplate to get.</param> /// <param name="useSaSKey">Specify if use SaS key in the uri</param> /// <returns>The armTemplate.</returns> public async Task <ArmTemplate> GetAsync(string offerName, string templateName, bool useSaSKey = true) { if (!await _offerService.ExistsAsync(offerName)) { throw new LunaNotFoundUserException(LoggingUtils.ComposeNotFoundErrorMessage(typeof(Offer).Name, offerName)); } // Check that an armTemplate with the provided templateName exists within the given offer if (!(await ExistsAsync(offerName, templateName))) { throw new LunaNotFoundUserException(LoggingUtils.ComposeNotFoundErrorMessage(typeof(ArmTemplate).Name, templateName, offerName: offerName)); } _logger.LogInformation(LoggingUtils.ComposeGetSingleResourceMessage(typeof(ArmTemplate).Name, templateName, offerName: offerName)); // Get the offer associated with the offerName provided var offer = await _offerService.GetAsync(offerName); // Find the armTemplate that matches the templateName provided var armTemplate = await _context.ArmTemplates .SingleOrDefaultAsync(a => (a.OfferId == offer.Id) && (a.TemplateName == templateName)); _logger.LogInformation(LoggingUtils.ComposeReturnValueMessage(typeof(ArmTemplate).Name, templateName, JsonSerializer.Serialize(armTemplate), offerName: offerName)); if (useSaSKey) { // Generate Sas key armTemplate.TemplateFilePath = await _storageUtility.GetFileReferenceWithSasKeyAsync(armTemplate.TemplateFilePath); } _logger.LogInformation("Sas key generated."); return(armTemplate); }