Exemplo n.º 1
0
        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));
            }
        }
Exemplo n.º 2
0
        /// <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);
        }
Exemplo n.º 3
0
        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));
            }
        }
Exemplo n.º 4
0
        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));;
            }
        }
Exemplo n.º 5
0
        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));
            }
        }
        /// <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);
        }
Exemplo n.º 7
0
        /// <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);
        }
Exemplo n.º 8
0
        /// <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);
        }
Exemplo n.º 9
0
        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));
            }
        }
        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));
            }
        }
Exemplo n.º 11
0
        /// <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);
        }
Exemplo n.º 12
0
        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));
        }
Exemplo n.º 13
0
        public async Task <ActionResult> UpdateAsync(string offerName, string name, [FromBody] ArmTemplateParameter armTemplateParameter)
        {
            AADAuthHelper.VerifyUserAccess(this.HttpContext, _logger, true);
            if (armTemplateParameter == null)
            {
                throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(ArmTemplateParameter).Name),
                                                      UserErrorCode.PayloadNotProvided);
            }

            if (!name.Equals(armTemplateParameter.Name))
            {
                throw new LunaBadRequestUserException(LoggingUtils.ComposeNameMismatchErrorMessage(typeof(ArmTemplateParameter).Name),
                                                      UserErrorCode.NameMismatch);
            }

            _logger.LogInformation(LoggingUtils.ComposeUpdateResourceMessage(typeof(ArmTemplateParameter).Name,
                                                                             name,
                                                                             JsonSerializer.Serialize(armTemplateParameter)));

            await _armTemplateParameterService.UpdateAsync(offerName, name, armTemplateParameter);

            return(Ok(armTemplateParameter));
        }
Exemplo n.º 14
0
        /// <summary>
        /// Updates a customMeter.
        /// </summary>
        /// <param name="offerName">The offer name of the customMeter.</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 <CustomMeter> UpdateAsync(string offerName, 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(offerName, meterName);

            if ((meterName != customMeter.MeterName) || (offerName != customMeter.OfferName))
            {
                throw new LunaBadRequestUserException(LoggingUtils.ComposeNameMismatchErrorMessage(typeof(CustomMeter).Name),
                                                      UserErrorCode.NameMismatch);
            }

            // Copy over the changes
            customMeterDb.Copy(customMeter);

            var offer = await _offerService.GetAsync(offerName);

            var connector = await _telemetryDataconnectorService.GetAsync(customMeter.TelemetryDataConnectorName);

            customMeterDb.TelemetryDataConnectorId = connector.Id;
            customMeterDb.OfferId = offer.Id;

            // 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);
        }