public async Task <ActionResult> CreateOrUpdateAsync(string name, [FromBody] TelemetryDataConnector connector) { AADAuthHelper.VerifyUserAccess(this.HttpContext, _logger, true); if (connector == null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(nameof(connector)), UserErrorCode.PayloadNotProvided); } if (!name.Equals(connector.Name)) { throw new LunaBadRequestUserException(LoggingUtils.ComposeNameMismatchErrorMessage(nameof(name)), UserErrorCode.NameMismatch); } if (await _telemetryDataConnectorService.ExistsAsync(name)) { await _telemetryDataConnectorService.UpdateAsync(name, connector); return(Ok(connector)); } else { await _telemetryDataConnectorService.CreateAsync(name, connector); return(CreatedAtRoute(nameof(GetAsync) + nameof(TelemetryDataConnector), new { name }, connector)); } }
/// <summary> /// Updates a TelemetryDataConnector. /// </summary> /// <param name="name">The name of the TelemetryDataConnector.</param> /// <param name="connector">The updated TelemetryDataConnector.</param> /// <returns>The updated TelemetryDataConnector.</returns> public async Task <TelemetryDataConnector> UpdateAsync(string name, TelemetryDataConnector connector) { if (connector is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(TelemetryDataConnector).Name), UserErrorCode.PayloadNotProvided); } _logger.LogInformation(LoggingUtils.ComposeUpdateResourceMessage(typeof(TelemetryDataConnector).Name, name, payload: JsonSerializer.Serialize(connector))); if (!await ExistsAsync(name)) { throw new LunaConflictUserException(LoggingUtils.ComposeNotFoundErrorMessage(typeof(TelemetryDataConnector).Name, name)); } // Get the customMeter that matches the meterName provided var connectorDb = await GetAsync(name); // Copy over the changes connectorDb.Copy(connector); // Update connector values and save changes in db _context.TelemetryDataConnectors.Update(connectorDb); await _context._SaveChangesAsync(); _logger.LogInformation(LoggingUtils.ComposeResourceUpdatedMessage(typeof(TelemetryDataConnector).Name, name)); return(connector); }
/// <summary> /// Updates a customMeter. /// </summary> /// <param name="meterName">The name of the customMeter to update.</param> /// <param name="customMeter">The updated customMeter.</param> /// <returns>The updated customMeter.</returns> public async Task <CustomMeter> UpdateAsync(string meterName, CustomMeter customMeter) { if (customMeter is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(CustomMeter).Name), UserErrorCode.PayloadNotProvided); } _logger.LogInformation(LoggingUtils.ComposeUpdateResourceMessage(typeof(CustomMeter).Name, customMeter.MeterName, payload: JsonSerializer.Serialize(customMeter))); // Get the customMeter that matches the meterName provided var customMeterDb = await GetAsync(meterName); // Check if (the meterName has been updated) && // (a customMeter with the same new name does not already exist) if ((meterName != customMeter.MeterName) && (await ExistsAsync(customMeter.MeterName))) { throw new LunaBadRequestUserException(LoggingUtils.ComposeNameMismatchErrorMessage(typeof(CustomMeter).Name), UserErrorCode.NameMismatch); } // Copy over the changes customMeterDb.Copy(customMeter); // Update customMeterDb values and save changes in db _context.CustomMeters.Update(customMeterDb); await _context._SaveChangesAsync(); _logger.LogInformation(LoggingUtils.ComposeResourceUpdatedMessage(typeof(CustomMeter).Name, customMeter.MeterName)); return(customMeterDb); }
public async Task <ActionResult> CreateOrUpdateAsync(string offerName, string planName, Guid tenantId, [FromBody] RestrictedUser restrictedUser) { AADAuthHelper.VerifyUserAccess(this.HttpContext, _logger, true); if (restrictedUser == null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(nameof(restrictedUser)), UserErrorCode.PayloadNotProvided); } if (!tenantId.Equals(restrictedUser.TenantId)) { throw new LunaBadRequestUserException(LoggingUtils.ComposeNameMismatchErrorMessage(typeof(RestrictedUser).Name), UserErrorCode.NameMismatch); } if (await _restrictedUserService.ExistsAsync(offerName, planName, tenantId)) { _logger.LogInformation($"Update resticted user {tenantId} in plan {planName} in offer {offerName} with payload {JsonSerializer.Serialize(restrictedUser)}."); await _restrictedUserService.UpdateAsync(offerName, planName, restrictedUser); return(Ok(restrictedUser)); } else { _logger.LogInformation($"Create resticted user {tenantId} in plan {planName} in offer {offerName} with payload {JsonSerializer.Serialize(restrictedUser)}."); await _restrictedUserService.CreateAsync(offerName, planName, restrictedUser); return(CreatedAtRoute(nameof(GetAsync) + nameof(RestrictedUser), new { offerName = offerName, planName = planName, tenantId = tenantId }, restrictedUser)); } }
/// <summary> /// Updates a webhook within an offer. /// </summary> /// <param name="offerName">The name of the offer.</param> /// <param name="webhookName">The name of the webhook to update.</param> /// <param name="webhook">The new webhook.</param> /// <returns>The updated webhook.</returns> public async Task <Webhook> UpdateAsync(string offerName, string webhookName, Webhook webhook) { if (webhook is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(Webhook).Name), UserErrorCode.PayloadNotProvided); } // Check if (the webhookName has been updated) && // (a webhook with the same new webhookName does not already exist) if ((webhookName != webhook.WebhookName) && (await ExistsAsync(offerName, webhook.WebhookName))) { throw new LunaNotFoundUserException(LoggingUtils.ComposeNotFoundErrorMessage(typeof(Webhook).Name, webhook.WebhookName, offerName: offerName)); } _logger.LogInformation(LoggingUtils.ComposeUpdateResourceMessage(typeof(Webhook).Name, webhook.WebhookName, offerName: offerName, payload: JsonSerializer.Serialize(webhook))); // Get the webhook that matches the webhookName provided var webhookDb = await GetAsync(offerName, webhookName); // Copy over the changes webhookDb.Copy(webhook); // Update webhook values and save changes in db _context.Webhooks.Update(webhookDb); await _context._SaveChangesAsync(); _logger.LogInformation(LoggingUtils.ComposeResourceUpdatedMessage(nameof(Webhook), webhookName, offerName: offerName)); await UpdateWebhookParameters(await _offerService.GetAsync(offerName), webhookDb, webhookDb.Id); return(webhook); }
/// <summary> /// Creates a webhook within an offer. /// </summary> /// <param name="offerName">The name of the offer.</param> /// <param name="webhook">The webhook to create.</param> /// <returns>The created webhook.</returns> public async Task <Webhook> CreateAsync(string offerName, Webhook webhook) { if (webhook is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(Webhook).Name), UserErrorCode.PayloadNotProvided); } // Check that the offer does not already have a webhook with the same webhookName if (await ExistsAsync(offerName, webhook.WebhookName)) { throw new LunaConflictUserException(LoggingUtils.ComposeAlreadyExistsErrorMessage(typeof(Webhook).Name, webhook.WebhookName, offerName: offerName)); } _logger.LogInformation(LoggingUtils.ComposeCreateResourceMessage(typeof(Webhook).Name, webhook.WebhookName, offerName: offerName, payload: JsonSerializer.Serialize(webhook))); // Get the offer associated with the offerName provided var offer = await _offerService.GetAsync(offerName); // Set the FK to offer webhook.OfferId = offer.Id; // Add webhook to db _context.Webhooks.Add(webhook); await _context._SaveChangesAsync(); _logger.LogInformation(LoggingUtils.ComposeResourceCreatedMessage(typeof(Webhook).Name, webhook.WebhookName, offerName: offerName)); await CreateWebhookParameters(offerName, webhook); return(webhook); }
public async Task <ActionResult> CreateOrUpdateAsync(string offerName, string name, [FromBody] IpConfig ipConfig) { AADAuthHelper.VerifyUserAccess(this.HttpContext, _logger, true); if (ipConfig == null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(nameof(ipConfig)), UserErrorCode.PayloadNotProvided); } if (!name.Equals(ipConfig.Name)) { throw new LunaBadRequestUserException(LoggingUtils.ComposeNameMismatchErrorMessage(typeof(IpConfig).Name), UserErrorCode.NameMismatch); } if (await _ipConfigService.ExistsAsync(offerName, name)) { _logger.LogInformation($"Update IPConfig {name} in offer {offerName} with payload {JsonSerializer.Serialize(ipConfig)}"); await _ipConfigService.UpdateAsync(offerName, name, ipConfig); return(Ok(ipConfig)); } else { _logger.LogInformation($"Create IPConfig {name} in offer {offerName} with payload {JsonSerializer.Serialize(ipConfig)}"); await _ipConfigService.CreateAsync(offerName, ipConfig); return(CreatedAtRoute(nameof(GetAsync) + nameof(IpConfig), new { offerName = offerName, name = name }, ipConfig)); } }
public async Task <ActionResult> CreateOrUpdateAsync(string offerName, string parameterName, [FromBody] OfferParameter offerParameter) { AADAuthHelper.VerifyUserAccess(this.HttpContext, _logger, true); if (offerParameter == null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(nameof(offerParameter)), UserErrorCode.PayloadNotProvided); } if (!parameterName.Equals(offerParameter.ParameterName)) { throw new LunaBadRequestUserException(LoggingUtils.ComposeNameMismatchErrorMessage(typeof(OfferParameter).Name), UserErrorCode.NameMismatch); } if (await _offerParameterService.ExistsAsync(offerName, parameterName)) { _logger.LogInformation($"Update offer parameter {parameterName} in offer {offerName} with payload {JsonSerializer.Serialize(offerParameter)}."); await _offerParameterService.UpdateAsync(offerName, parameterName, offerParameter); return(Ok(offerParameter)); } else { _logger.LogInformation($"Create offer parameter {parameterName} in offer {offerName} with payload {JsonSerializer.Serialize(offerParameter)}."); await _offerParameterService.CreateAsync(offerName, offerParameter); return(CreatedAtRoute(nameof(GetAsync) + nameof(OfferParameter), new { offerName = offerName, parameterName = offerParameter.ParameterName }, offerParameter)); } }
public async Task <ActionResult> CreateOrUpdateAsync(string offerName, string planName, string meterName, [FromBody] CustomMeterDimension customMeterDimension) { AADAuthHelper.VerifyUserAccess(this.HttpContext, _logger, true); if (customMeterDimension == null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(nameof(customMeterDimension)), UserErrorCode.PayloadNotProvided); } if (!planName.Equals(customMeterDimension.PlanName)) { throw new LunaBadRequestUserException(LoggingUtils.ComposeNameMismatchErrorMessage(nameof(offerName)), UserErrorCode.NameMismatch); } if (!meterName.Equals(customMeterDimension.MeterName)) { throw new LunaBadRequestUserException(LoggingUtils.ComposeNameMismatchErrorMessage(nameof(meterName)), UserErrorCode.NameMismatch); } if (await _customMeterDimensionService.ExistsAsync(offerName, planName, meterName)) { await _customMeterDimensionService.UpdateAsync(offerName, planName, meterName, customMeterDimension); return(Ok(customMeterDimension)); } else { await _customMeterDimensionService.CreateAsync(offerName, planName, meterName, customMeterDimension); return(CreatedAtRoute(nameof(GetAsync) + nameof(CustomMeterDimension), new { offerName = offerName, planName = planName, meterName = meterName }, customMeterDimension));; } }
/// <summary> /// Updates a customMeterDimension. /// </summary> /// <param name="id">The id of the customMeterDimension to update.</param> /// <param name="customMeterDimension">The updated customMeterDimension.</param> /// <returns>The updated customMeterDimension.</returns> public async Task <CustomMeterDimension> UpdateAsync(long id, CustomMeterDimension customMeterDimension) { if (customMeterDimension is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(CustomMeterDimension).Name), UserErrorCode.PayloadNotProvided); } _logger.LogInformation(LoggingUtils.ComposeUpdateResourceMessage( typeof(CustomMeterDimension).Name, customMeterDimension.Id.ToString(), planName: customMeterDimension.Plan.PlanName, payload: JsonSerializer.Serialize(customMeterDimension))); // Get the customMeterDimension that matches the id provided var customMeterDimensionDb = await GetAsync(id); // update the FK to customMeter in case the meterName has been changed customMeterDimensionDb.MeterId = (await _customMeterService.GetAsync(customMeterDimension.MeterName)).Id; // Copy over the changes customMeterDimensionDb.Copy(customMeterDimension); // Update customMeterDimensionDb values and save changes in db _context.CustomMeterDimensions.Update(customMeterDimensionDb); await _context._SaveChangesAsync(); _logger.LogInformation(LoggingUtils.ComposeResourceUpdatedMessage( typeof(CustomMeterDimension).Name, customMeterDimension.Id.ToString(), planName: customMeterDimension.Plan.PlanName)); return(customMeterDimensionDb); }
/// <summary> /// Updates an aadSecretTmp object within an offer. /// </summary> /// <param name="offerName">The name of the offer.</param> /// <param name="name">The name of the aadSecretTmp object to update.</param> /// <param name="aadSecretTmp">The updated aadSecretTmp object.</param> /// <returns>The updated aadSecretTmp object.</returns> public async Task <AadSecretTmp> UpdateAsync(string offerName, string name, AadSecretTmp aadSecretTmp) { if (aadSecretTmp is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(AadSecretTmp).Name), UserErrorCode.PayloadNotProvided); } // Check if (the name has been updated) && // (an aadSecretTmp with the same new name does not already exist) if ((name != aadSecretTmp.Name) && (await ExistsAsync(offerName, aadSecretTmp.Name))) { throw new LunaBadRequestUserException(LoggingUtils.ComposeNameMismatchErrorMessage(typeof(AadSecretTmp).Name), UserErrorCode.NameMismatch); } _logger.LogInformation(LoggingUtils.ComposeUpdateResourceMessage(typeof(AadSecretTmp).Name, name, offerName: offerName, payload: JsonSerializer.Serialize(aadSecretTmp))); // Get the aadSecretTmp that matches the name provided var aadSecretTmpDb = await GetAsync(offerName, name); // Copy over the changes aadSecretTmpDb.Copy(aadSecretTmp); // Update aadSecretTmpDb values and save changes in db _context.AadSecretTmps.Update(aadSecretTmpDb); await _context._SaveChangesAsync(); _logger.LogInformation(LoggingUtils.ComposeResourceUpdatedMessage(typeof(AadSecretTmp).Name, name, offerName: offerName)); return(aadSecretTmpDb); }
/// <summary> /// Creates an aadSecretTmp object within an offer. /// </summary> /// <param name="offerName">The name of the offer.</param> /// <param name="aadSecretTmp">The aadSecretTmp object to create.</param> /// <returns>The created aadSecretTmp object.</returns> public async Task <AadSecretTmp> CreateAsync(string offerName, AadSecretTmp aadSecretTmp) { if (aadSecretTmp is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(AadSecretTmp).Name), UserErrorCode.PayloadNotProvided); } // Check that the offer does not already have an aadSecretTmp with the same name if (await ExistsAsync(offerName, aadSecretTmp.Name)) { throw new LunaConflictUserException(LoggingUtils.ComposeAlreadyExistsErrorMessage(typeof(AadSecretTmp).Name, aadSecretTmp.Name, offerName: offerName)); } _logger.LogInformation(LoggingUtils.ComposeCreateResourceMessage(typeof(AadSecretTmp).Name, aadSecretTmp.Name, offerName: offerName, payload: JsonSerializer.Serialize(aadSecretTmp))); // Get the offer associated with the offerName provided var offer = await _offerService.GetAsync(offerName); // Set the FK to offer aadSecretTmp.OfferId = offer.Id; // Add aadSecretTmp to db _context.AadSecretTmps.Add(aadSecretTmp); await _context._SaveChangesAsync(); _logger.LogInformation(LoggingUtils.ComposeResourceCreatedMessage(typeof(AadSecretTmp).Name, aadSecretTmp.Name, offerName: offerName)); return(aadSecretTmp); }
/// <summary> /// Creates a restrictedUser within a plan within an offer. /// </summary> /// <param name="offerName">The name of the offer.</param> /// <param name="planUniqueName">The name of the plan.</param> /// <param name="restrictedUser">The restrictedUser to create.</param> /// <returns>The created restrictedUser.</returns> public async Task <RestrictedUser> CreateAsync(string offerName, string planUniqueName, RestrictedUser restrictedUser) { if (restrictedUser is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(RestrictedUser).Name), UserErrorCode.PayloadNotProvided); } _logger.LogInformation(LoggingUtils.ComposeCreateResourceMessage(typeof(RestrictedUser).Name, restrictedUser.TenantId.ToString(), offerName: offerName, planName: planUniqueName, payload: JsonSerializer.Serialize(restrictedUser))); // Get the plan associated with the offerName and planUniqueName provided var plan = await _planService.GetAsync(offerName, planUniqueName); // Set the FK to plan restrictedUser.PlanId = plan.Id; // Reset the PK (should not be modified in request) restrictedUser.Id = 0; // Add restrictedUser to db _context.RestrictedUsers.Add(restrictedUser); await _context._SaveChangesAsync(); _logger.LogInformation(LoggingUtils.ComposeResourceCreatedMessage(typeof(RestrictedUser).Name, restrictedUser.TenantId.ToString(), offerName: offerName, planName: planUniqueName)); return(restrictedUser); }
/// <summary> /// Updates an offer. /// </summary> /// <param name="offerName">The name of the offer to update.</param> /// <param name="offer">The updated offer.</param> /// <returns>The updated offer.</returns> public async Task <Offer> UpdateAsync(string offerName, Offer offer) { if (offer is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(Offer).Name), UserErrorCode.PayloadNotProvided); } _logger.LogInformation(LoggingUtils.ComposeUpdateResourceMessage(typeof(Offer).Name, offer.OfferName, payload: JsonSerializer.Serialize(offer))); // Get the offer that matches the offerName provided var offerDb = await GetAsync(offerName); // Check if (the offerName has been updated) && // (an offer with the same new name does not already exist) if ((offerName != offer.OfferName) && (await ExistsAsync(offer.OfferName))) { throw new LunaBadRequestUserException(LoggingUtils.ComposeNameMismatchErrorMessage(typeof(Offer).Name), UserErrorCode.NameMismatch); } // Copy over the changes offerDb.Copy(offer); // Update the offer last updated time offerDb.LastUpdatedTime = DateTime.UtcNow; // Update offerDb values and save changes in db _context.Offers.Update(offerDb); await _context._SaveChangesAsync(); _logger.LogInformation(LoggingUtils.ComposeResourceUpdatedMessage(typeof(Offer).Name, offer.OfferName)); return(offerDb); }
public async Task <SubscriptionParameter> CreateAsync(SubscriptionParameter subscriptionParameter) { if (subscriptionParameter is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(SubscriptionParameter).Name), UserErrorCode.PayloadNotProvided); } // Check that the offer does not already have an armTemplateParameter with the same name if (await ExistsAsync(subscriptionParameter.SubscriptionId, subscriptionParameter.Name)) { throw new LunaConflictUserException(LoggingUtils.ComposeAlreadyExistsErrorMessage(typeof(SubscriptionParameter).Name, subscriptionParameter.Name)); } if (!await _subscriptionService.ExistsAsync(subscriptionParameter.SubscriptionId)) { throw new ArgumentException("Subscription doesn't exist."); } _logger.LogInformation(LoggingUtils.ComposeCreateResourceMessage(typeof(SubscriptionParameter).Name, subscriptionParameter.Name, payload: JsonSerializer.Serialize(subscriptionParameter))); // Add armTemplateParameter to db _context.SubscriptionParameters.Add(subscriptionParameter); await _context._SaveChangesAsync(); _logger.LogInformation(LoggingUtils.ComposeResourceCreatedMessage(typeof(SubscriptionParameter).Name, subscriptionParameter.Name)); return(subscriptionParameter); }
public async Task <ActionResult> CreateOrUpdateAsync(string offerName, string templateName, [FromBody] object armTemplateJSON) { AADAuthHelper.VerifyUserAccess(this.HttpContext, _logger, true); if (armTemplateJSON == null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(ArmTemplate).Name), UserErrorCode.PayloadNotProvided); } ArmTemplate armTemplate = null; if (await _armTemplateService.ExistsAsync(offerName, templateName)) { // Update. Do not log armtemplatejson _logger.LogInformation(LoggingUtils.ComposeUpdateResourceMessage(typeof(ArmTemplateParameter).Name, templateName)); armTemplate = await _armTemplateService.UpdateAsync(offerName, templateName, armTemplateJSON); return(Ok(armTemplate)); } else { // Update. Do not log armtemplatejson _logger.LogInformation(LoggingUtils.ComposeCreateResourceMessage(typeof(ArmTemplateParameter).Name, templateName)); armTemplate = await _armTemplateService.CreateAsync(offerName, templateName, armTemplateJSON); return(CreatedAtRoute(nameof(GetAsync) + nameof(ArmTemplate), new { OfferName = offerName, TemplateName = templateName }, armTemplate)); } }
/// <summary> /// Updates a plan within an offer. /// </summary> /// <param name="offerName">The name of the offer.</param> /// <param name="planUniqueName">The name of the plan to update.</param> /// <param name="plan">The updated plan.</param> /// <returns>The updated plan.</returns> public async Task <Plan> UpdateAsync(string offerName, string planUniqueName, Plan plan) { if (plan is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(Plan).Name), UserErrorCode.PayloadNotProvided); } // Check if (the planUniqueName has been updated) && // (a plan with the same new planUniqueName does not already exist) if ((planUniqueName != plan.PlanName) && (await ExistsAsync(offerName, plan.PlanName))) { throw new LunaBadRequestUserException(LoggingUtils.ComposeNameMismatchErrorMessage(typeof(Plan).Name), UserErrorCode.NameMismatch); } _logger.LogInformation(LoggingUtils.ComposeUpdateResourceMessage(typeof(Plan).Name, planUniqueName, offerName: offerName, payload: JsonSerializer.Serialize(plan))); var dbPlan = await GetAsync(offerName, planUniqueName); dbPlan.Copy(plan); dbPlan = await SetArmTemplateAndWebhookIds(offerName, dbPlan); _context.Plans.Update(dbPlan); await _context._SaveChangesAsync(); _logger.LogInformation(LoggingUtils.ComposeResourceUpdatedMessage(typeof(Plan).Name, planUniqueName, offerName: offerName)); return(dbPlan); }
/// <summary> /// Creates a plan within an offer. /// </summary> /// <param name="offerName">The name of the offer.</param> /// <param name="plan">The plan to create.</param> /// <returns>The created plan.</returns> public async Task <Plan> CreateAsync(string offerName, Plan plan) { if (plan is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(Plan).Name), UserErrorCode.PayloadNotProvided); } // Check that the offer does not already have an plan with the same planUniqueName if (await ExistsAsync(offerName, plan.PlanName)) { throw new LunaConflictUserException(LoggingUtils.ComposeAlreadyExistsErrorMessage(typeof(Plan).Name, plan.PlanName, offerName: offerName)); } _logger.LogInformation(LoggingUtils.ComposeCreateResourceMessage(typeof(Plan).Name, plan.PlanName, offerName: offerName, payload: JsonSerializer.Serialize(plan))); // Get the offer associated with the offerName provided var offer = await _offerService.GetAsync(offerName); // Set the FK to offer plan.OfferId = offer.Id; plan = await SetArmTemplateAndWebhookIds(offerName, plan); // Add plan to db _context.Plans.Add(plan); await _context._SaveChangesAsync(); _logger.LogInformation(LoggingUtils.ComposeResourceCreatedMessage(typeof(Plan).Name, plan.PlanName, offerName: offerName)); return(plan); }
/// <summary> /// Creates a customMeter. /// </summary> /// <param name="offerName">The offer name of the customMeter.</param> /// <param name="meterName">The name of the customMeter.</param> /// <param name="customMeter">The customMeter to create.</param> /// <returns>The created customMeter.</returns> public async Task <CustomMeter> CreateAsync(string offerName, string meterName, CustomMeter customMeter) { if (customMeter is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(CustomMeter).Name), UserErrorCode.PayloadNotProvided); } _logger.LogInformation(LoggingUtils.ComposeCreateResourceMessage(typeof(CustomMeter).Name, customMeter.MeterName, offerName: offerName, payload: JsonSerializer.Serialize(customMeter))); // Check that an customMeter with the same name does not already exist if (await ExistsAsync(offerName, meterName)) { throw new LunaConflictUserException(LoggingUtils.ComposeAlreadyExistsErrorMessage(typeof(CustomMeter).Name, customMeter.MeterName, offerName: offerName)); } var offer = await _offerService.GetAsync(offerName); var connector = await _telemetryDataconnectorService.GetAsync(customMeter.TelemetryDataConnectorName); customMeter.TelemetryDataConnectorId = connector.Id; customMeter.OfferId = offer.Id; using (var transaction = await _context.BeginTransactionAsync()) { // Not using subscriptionService here to avoid circular reference List <Subscription> subscriptionList = _context.Subscriptions.Where(s => s.OfferId == offer.Id && (s.Status == FulfillmentState.Subscribed.ToString() || s.Status == FulfillmentState.Suspended.ToString() || s.Status == FulfillmentState.PendingFulfillmentStart.ToString())).ToList(); _context.CustomMeters.Add(customMeter); await _context._SaveChangesAsync(); // Add customMeter to db foreach (var sub in subscriptionList) { bool isEnabled = sub.Status == FulfillmentState.Subscribed.ToString() || sub.Status == FulfillmentState.Suspended.ToString(); _context.SubscriptionCustomMeterUsages.Add(new SubscriptionCustomMeterUsage(customMeter.Id, sub.SubscriptionId, isEnabled)); } await _context._SaveChangesAsync(); transaction.Commit(); } _logger.LogInformation(LoggingUtils.ComposeResourceCreatedMessage(typeof(CustomMeter).Name, customMeter.MeterName, offerName: offerName)); return(customMeter); }
public async Task <ActionResult> ResolveToken([FromBody] string token) { if (string.IsNullOrEmpty(token)) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(nameof(token)), UserErrorCode.PayloadNotProvided); } // Do not log token content! _logger.LogInformation($"Resolve token for a subscription."); MarketplaceSubscription resolvedSubscription = await _fulfillmentManager.ResolveSubscriptionAsync(token); return(Ok(resolvedSubscription)); }
public async Task <ActionResult> CreateOrUpdateAsync(Guid subscriptionId, [FromBody] Subscription subscription) { if (subscription == null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(nameof(subscription)), UserErrorCode.PayloadNotProvided); } if (!subscriptionId.Equals(subscription.SubscriptionId)) { throw new LunaBadRequestUserException(LoggingUtils.ComposeNameMismatchErrorMessage(typeof(Subscription).Name), UserErrorCode.NameMismatch); } if (await _subscriptionService.ExistsAsync(subscriptionId)) { _logger.LogInformation($"Update subscription {subscriptionId} with payload {JsonSerializer.Serialize(subscription)}."); var sub = await _subscriptionService.GetAsync(subscriptionId); AADAuthHelper.VerifyUserAccess(this.HttpContext, _logger, false, sub.Owner); if (!sub.OfferName.Equals(subscription.OfferName, StringComparison.InvariantCultureIgnoreCase)) { throw new LunaBadRequestUserException("Offer name of an existing subscription can not be changed.", UserErrorCode.InvalidParameter); } if (!string.IsNullOrEmpty(subscription.Owner) && !sub.Owner.Equals(subscription.Owner, StringComparison.InvariantCultureIgnoreCase)) { throw new LunaBadRequestUserException("Owner name of an existing subscription can not be changed.", UserErrorCode.InvalidParameter); } if (sub.PlanName.Equals(subscription.PlanName, StringComparison.InvariantCultureIgnoreCase)) { throw new LunaConflictUserException($"The subscription {subscription.SubscriptionId} is already in plan {subscription.PlanName}."); } // Update existing subscription await _fulfillmentManager.RequestUpdateSubscriptionAsync(subscriptionId, subscription.PlanName); return(Ok(await _subscriptionService.GetAsync(subscriptionId))); } else { _logger.LogInformation($"Create subscription {subscriptionId} with payload {JsonSerializer.Serialize(subscription)}."); // Create a new subscription AADAuthHelper.VerifyUserAccess(this.HttpContext, _logger, false, subscription.Owner); await _subscriptionService.CreateAsync(subscription); return(CreatedAtRoute(nameof(GetAsync) + nameof(Subscription), new { subscriptionId = subscription.SubscriptionId }, subscription)); } }
/// <summary> /// Updates a customMeter. /// </summary> /// <param name="subscriptionId">The subscription id.</param> /// <param name="meterName">The name of the customMeter to update.</param> /// <param name="customMeter">The updated customMeter.</param> /// <returns>The updated customMeter.</returns> public async Task <SubscriptionCustomMeterUsage> UpdateAsync(Guid subscriptionId, string meterName, SubscriptionCustomMeterUsage subscriptionCustomMeterUsage) { if (subscriptionCustomMeterUsage is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(SubscriptionCustomMeterUsage).Name), UserErrorCode.PayloadNotProvided); } var subscription = await _subscriptionService.GetAsync(subscriptionId); var customMeter = await _customMeterService.GetAsync(subscription.OfferName, meterName); _logger.LogInformation(LoggingUtils.ComposeUpdateResourceMessage( typeof(SubscriptionCustomMeterUsage).Name, customMeter.MeterName, payload: JsonSerializer.Serialize(customMeter), subscriptionId: subscriptionId)); // Get the customMeter that matches the meterName provided var subscriptionCustomMeterUsageDb = await GetAsync(subscriptionId, meterName); // Check if (the meterName has been updated) && // (a customMeter with the same new name does not already exist) if (subscriptionCustomMeterUsageDb.SubscriptionId != subscriptionCustomMeterUsage.SubscriptionId || subscriptionCustomMeterUsageDb.MeterId != subscriptionCustomMeterUsage.MeterId) { throw new LunaBadRequestUserException(LoggingUtils.ComposeNameMismatchErrorMessage(typeof(SubscriptionCustomMeterUsage).Name), UserErrorCode.NameMismatch); } // Copy over the changes subscriptionCustomMeterUsageDb.Copy(subscriptionCustomMeterUsage); // Update customMeterDb values and save changes in db _context.SubscriptionCustomMeterUsages.Update(subscriptionCustomMeterUsageDb); await _context._SaveChangesAsync(); _logger.LogInformation(LoggingUtils.ComposeResourceUpdatedMessage( typeof(SubscriptionCustomMeterUsage).Name, customMeter.MeterName, subscriptionId: subscriptionId)); return(subscriptionCustomMeterUsageDb); }
/// <summary> /// Creates a webhookParameter object within an offer. /// </summary> /// <param name="offerName">The name of the offer.</param> /// <param name="webhookId">The id of the webhook that the parameter is associated with.</param> /// <param name="webhookParameter">The webhookParameter to create.</param> /// <returns>The created webhookParameter.</returns> public async Task <WebhookParameter> CreateAsync(string offerName, long webhookId, WebhookParameter webhookParameter) { if (webhookParameter is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(WebhookParameter).Name), UserErrorCode.PayloadNotProvided); } if (ExpressionEvaluationUtils.ReservedParameterNames.Contains(webhookParameter.Name)) { _logger.LogInformation($"Webhook {webhookId} is referencing system parameter {webhookParameter.Name}"); return(webhookParameter); //throw new LunaConflictUserException(LoggingUtils.ComposeAlreadyExistsErrorMessage(typeof(WebhookParameter).Name, // webhookParameter.Name, offerName: offerName)); } _logger.LogInformation(LoggingUtils.ComposeCreateResourceMessage(typeof(WebhookParameter).Name, webhookParameter.Name, offerName: offerName, payload: JsonSerializer.Serialize(webhookParameter))); // Get the offer associated with the offerName provided var offer = await _offerService.GetAsync(offerName); // Check if the WebhookParameter already exists if (await ExistsAsync(offerName, webhookParameter.Name)) { // Just create a new join entry to keep track of the fact that this Webhook is using this parameter WebhookParameter webhookParameterDb = await GetAsync(offerName, webhookParameter.Name); await _webhookWebhookParameterService.CreateJoinEntryAsync(webhookId, webhookParameterDb.Id); return(webhookParameterDb); } // Set the FK to offer webhookParameter.OfferId = offer.Id; // Add webhookParameter to db _context.WebhookParameters.Add(webhookParameter); await _context._SaveChangesAsync(); await _webhookWebhookParameterService.CreateJoinEntryAsync(webhookId, webhookParameter.Id); _logger.LogInformation(LoggingUtils.ComposeResourceCreatedMessage(typeof(WebhookParameter).Name, webhookParameter.Name, offerName: offerName)); return(webhookParameter); }
public async Task <ActionResult> UpdateAsync(string offerName, string name, [FromBody] WebhookParameter webhookParameter) { AADAuthHelper.VerifyUserAccess(this.HttpContext, _logger, true); if (webhookParameter == null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(nameof(webhookParameter)), UserErrorCode.PayloadNotProvided); } if (!name.Equals(webhookParameter.Name)) { throw new LunaBadRequestUserException(LoggingUtils.ComposeNameMismatchErrorMessage(typeof(WebhookParameter).Name), UserErrorCode.NameMismatch); } _logger.LogInformation($"Update webhook parameter {name} in offer {offerName} with payload {JsonSerializer.Serialize(webhookParameter)}."); await _webhookParameterService.UpdateAsync(offerName, name, webhookParameter); return(Ok(webhookParameter)); }
/// <summary> /// Creates an armTemplateParameter object within an offer. /// </summary> /// <param name="offerName">The name of the offer.</param> /// <param name="armTemplateId">The id of the armTemplate that the parameter is associated with.</param> /// <param name="armTemplateParameter">The armTemplateParameter to create.</param> /// <returns>The created armTemplateParameter.</returns> public async Task <ArmTemplateParameter> CreateAsync(string offerName, long armTemplateId, ArmTemplateParameter armTemplateParameter) { if (armTemplateParameter is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(ArmTemplateParameter).Name), UserErrorCode.PayloadNotProvided); } if (ExpressionEvaluationUtils.ReservedParameterNames.Contains(armTemplateParameter.Name)) { _logger.LogInformation($"ARM template {armTemplateId} is referencing system parameter {armTemplateParameter.Name}."); return(armTemplateParameter); //throw new LunaConflictUserException(LoggingUtils.ComposeAlreadyExistsErrorMessage(typeof(ArmTemplateParameter).Name, // armTemplateParameter.Name, offerName: offerName)); } Offer offer = await _offerService.GetAsync(offerName); // Check if the ArmTemplateParameter already exists if (await ExistsAsync(offerName, armTemplateParameter.Name)) { // Just create a new join entry to keep track of the fact that this ArmTempate is using this parameter ArmTemplateParameter armTemplateParameterDb = await GetAsync(offerName, armTemplateParameter.Name); await _armTemplateArmTemplateParameterService.CreateJoinEntryAsync(armTemplateId, armTemplateParameterDb.Id); return(armTemplateParameterDb); } armTemplateParameter.OfferId = offer.Id; _context.ArmTemplateParameters.Add(armTemplateParameter); await _context._SaveChangesAsync(); await _armTemplateArmTemplateParameterService.CreateJoinEntryAsync(armTemplateId, armTemplateParameter.Id); _logger.LogInformation(LoggingUtils.ComposeResourceCreatedMessage(typeof(ArmTemplateParameter).Name, armTemplateParameter.Name, offerName: offerName)); return(armTemplateParameter); }
/// <summary> /// Updates a restrictedUser /// </summary> /// <param name="offerName">The offer name.</param> /// <param name="planName">The plan name.</param> /// <param name="restrictedUser">The updated restrictedUser.</param> /// <returns>The updated restrictedUser.</returns> public async Task <RestrictedUser> UpdateAsync(string offerName, string planName, RestrictedUser restrictedUser) { if (restrictedUser is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(RestrictedUser).Name), UserErrorCode.PayloadNotProvided); } _logger.LogInformation(LoggingUtils.ComposeUpdateResourceMessage(typeof(RestrictedUser).Name, restrictedUser.TenantId.ToString(), offerName: offerName, planName: planName, payload: JsonSerializer.Serialize(restrictedUser))); var dbUser = await GetAsync(offerName, planName, restrictedUser.TenantId); dbUser.Copy(restrictedUser); // Update restrictedUserDb values and save changes in db _context.RestrictedUsers.Update(dbUser); await _context._SaveChangesAsync(); _logger.LogInformation(LoggingUtils.ComposeResourceUpdatedMessage(typeof(RestrictedUser).Name, restrictedUser.TenantId.ToString(), offerName: offerName, planName: planName)); return(dbUser); }
/// <summary> /// Creates a customMeterDimension within a plan within an offer. /// </summary> /// <param name="offerName">The name of the offer.</param> /// <param name="planUniqueName">The name of the plan.</param> /// <param name="customMeterDimension">The customMeterDimension object to create.</param> /// <returns>The created customMeterDimension.</returns> public async Task <CustomMeterDimension> CreateAsync(string offerName, string planUniqueName, CustomMeterDimension customMeterDimension) { if (customMeterDimension is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(CustomMeterDimension).Name), UserErrorCode.PayloadNotProvided); } _logger.LogInformation(LoggingUtils.ComposeCreateResourceMessage( typeof(CustomMeterDimension).Name, customMeterDimension.Id.ToString(), offerName: offerName, planName: planUniqueName, payload: JsonSerializer.Serialize(customMeterDimension))); // Get the plan associated with the offerName and planUniqueName provided var plan = await _planService.GetAsync(offerName, planUniqueName); // Set the FK to plan customMeterDimension.PlanId = plan.Id; // Set the FK to customMeter customMeterDimension.MeterId = (await _customMeterService.GetAsync(customMeterDimension.MeterName)).Id; // Reset the PK (should not be modified in request) customMeterDimension.Id = 0; // Add customMeterDimension to db _context.CustomMeterDimensions.Add(customMeterDimension); await _context._SaveChangesAsync(); _logger.LogInformation(LoggingUtils.ComposeResourceCreatedMessage( typeof(ArmTemplateParameter).Name, customMeterDimension.Id.ToString(), offerName: offerName, planName: planUniqueName)); return(customMeterDimension); }
/// <summary> /// Uploads the given armTemplate as a JSON file in blob storage and records the URI to the /// created resrouce in the db. /// </summary> /// <param name="offerName">The name of the offer.</param> /// <param name="templateName">The name of the ARM template.</param> /// <param name="armTemplateJSON">The ARM Template's raw JSON data.</param> /// <returns>The created armTemplate db record.</returns> public async Task <ArmTemplate> UpdateAsync(string offerName, string templateName, object armTemplateJSON) { if (armTemplateJSON is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(ArmTemplate).Name), UserErrorCode.PayloadNotProvided); } // Check that the offer does already have an armTemplate with the same templateName if (!await ExistsAsync(offerName, templateName)) { throw new LunaNotFoundUserException(LoggingUtils.ComposeNotFoundErrorMessage(typeof(ArmTemplate).Name, templateName, offerName: offerName)); } ArmTemplate armTemplate = await GetAsync(offerName, templateName); // Get the offer associated with the offerName provided var offer = await _offerService.GetAsync(offerName); // Get the container name associated with the offer var containerName = offer.ContainerName.ToString(); // Upload the armTemplateJSON as a file in blob storage and get the URL to the created resource var url = await uploadToBlobStorageAsync(containerName, GetArmTemplateFileName(templateName), armTemplateJSON.ToString()); _logger.LogInformation($"Arm template {templateName} in offer {offerName} is uploaded to {url}."); armTemplate.TemplateFilePath = url; // Add armTemplate to db _context.ArmTemplates.Update(armTemplate); await _context._SaveChangesAsync(); await UpdateArmTemplateParameters(offer, armTemplateJSON, armTemplate.Id); return(armTemplate); }
/// <summary> /// Creates a subscriptionCustomMeterUsage. /// </summary> /// <param name="subscriptionId">The subscription id.</param> /// <param name="meterName">The name of the SubscriptionCustomMeterUsage to update.</param> /// <param name="subscriptionCustomMeterUsage">The subscriptionCustomMeterUsage to create.</param> /// <returns>The created subscriptionCustomMeterUsage.</returns> public async Task <SubscriptionCustomMeterUsage> CreateAsync( Guid subscriptionId, string meterName, SubscriptionCustomMeterUsage subscriptionCustomMeterUsage) { if (subscriptionCustomMeterUsage is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(SubscriptionCustomMeterUsage).Name), UserErrorCode.PayloadNotProvided); } _logger.LogInformation(LoggingUtils.ComposeCreateResourceMessage( typeof(SubscriptionCustomMeterUsage).Name, subscriptionCustomMeterUsage.MeterId.ToString(), subscriptionId: subscriptionCustomMeterUsage.SubscriptionId, payload: JsonSerializer.Serialize(subscriptionCustomMeterUsage))); var subscription = await _subscriptionService.GetAsync(subscriptionId); var customMeter = await _customMeterService.GetAsync(subscription.OfferName, meterName); // Check that an SubscriptionCustomMeterUsage with the same name does not already exist if (await ExistsAsync(subscriptionId, meterName)) { throw new LunaConflictUserException(LoggingUtils.ComposeAlreadyExistsErrorMessage( typeof(SubscriptionCustomMeterUsage).Name, customMeter.MeterName, subscriptionId: subscriptionId)); } // Add customMeter to db _context.SubscriptionCustomMeterUsages.Add(subscriptionCustomMeterUsage); await _context._SaveChangesAsync(); _logger.LogInformation(LoggingUtils.ComposeResourceCreatedMessage(typeof(SubscriptionCustomMeterUsage).Name, meterName, subscriptionId: subscriptionId)); return(subscriptionCustomMeterUsage); }
/// <summary> /// Updates an ArmTemplateParameter within an offer by name. /// </summary> /// <param name="offerName">The name of the offer.</param> /// <param name="parameterName">The name of the ArmTemplateParameter to update.</param> /// <param name="armTemplateParameter">The updated ArmTemplateParameter object.</param> /// <returns>The updated ArmTemplateParameter object.</returns> public async Task <ArmTemplateParameter> UpdateAsync(string offerName, string parameterName, ArmTemplateParameter armTemplateParameter) { if (armTemplateParameter is null) { throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(ArmTemplateParameter).Name), UserErrorCode.PayloadNotProvided); } _logger.LogInformation(LoggingUtils.ComposeUpdateResourceMessage(typeof(ArmTemplateParameter).Name, armTemplateParameter.Name, offerName: offerName, payload: JsonSerializer.Serialize(armTemplateParameter))); Offer offer = await _offerService.GetAsync(offerName); ArmTemplateParameter armTemplateParameterDb = await GetAsync(offerName, parameterName); armTemplateParameterDb.Copy(armTemplateParameter); armTemplateParameterDb.OfferId = offer.Id; _context.ArmTemplateParameters.Update(armTemplateParameterDb); await _context._SaveChangesAsync(); _logger.LogInformation(LoggingUtils.ComposeResourceUpdatedMessage(typeof(ArmTemplateParameter).Name, armTemplateParameter.Name, offerName: offerName)); return(armTemplateParameterDb); }