Beispiel #1
0
        /// <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);
        }
Beispiel #2
0
        /// <summary>
        /// Creates a subscription within a plan within an offer.
        /// </summary>
        /// <param name="subscription">The subscription to create.</param>
        /// <returns>The created subscription.</returns>
        public async Task <Subscription> CreateAsync(Subscription subscription)
        {
            if (subscription is null)
            {
                throw new LunaBadRequestUserException(LoggingUtils.ComposePayloadNotProvidedErrorMessage(typeof(Subscription).Name),
                                                      UserErrorCode.PayloadNotProvided);
            }

            if (await ExistsAsync(subscription.SubscriptionId))
            {
                throw new LunaConflictUserException(LoggingUtils.ComposeAlreadyExistsErrorMessage(typeof(Subscription).Name,
                                                                                                  subscription.SubscriptionId.ToString()));
            }
            _logger.LogInformation(LoggingUtils.ComposeCreateResourceMessage(typeof(Subscription).Name, subscription.Name, offerName: subscription.OfferName, planName: subscription.PlanName, payload: JsonSerializer.Serialize(subscription)));

            var offerParameters = await _offerParameterService.GetAllAsync(subscription.OfferName);

            foreach (var param in offerParameters)
            {
                // Check if value of all offer parameters are provided with correct type
                if (subscription.InputParameters.Where(x => x.Name.Equals(param.ParameterName) && x.Type.Equals(param.ValueType)).Count() < 1)
                {
                    throw new LunaBadRequestUserException($"Value of parameter {param.ParameterName} is not provided, or the type doesn't match.", UserErrorCode.ParameterNotProvided);
                }
            }

            // Get the offer associated with the offerName provided
            var offer = await _offerService.GetAsync(subscription.OfferName);


            // Get the plan associated with the planUniqueName provided
            var plan = await _planService.GetAsync(subscription.OfferName, subscription.PlanName);

            // Set the FK to offer
            subscription.OfferId = offer.Id;

            // Set the FK to plan
            subscription.PlanId = plan.Id;

            // Always set quantity to 1 to walkaround a marketplace service bug
            subscription.Quantity = 1;

            // Set the created time
            subscription.CreatedTime = DateTime.UtcNow;

            subscription.Status = nameof(FulfillmentState.PendingFulfillmentStart);

            subscription.ProvisioningStatus = nameof(ProvisioningState.ProvisioningPending);

            subscription.ProvisioningType = nameof(ProvisioningType.Subscribe);

            subscription.RetryCount = 0;

            List <CustomMeter> customMeterList = await _customMeterService.GetAllAsync(offer.OfferName);

            using (var transaction = await _context.BeginTransactionAsync())
            {
                // Add subscription to db
                _context.Subscriptions.Add(subscription);
                await _context._SaveChangesAsync();

                // Add subscription parameters
                foreach (var param in subscription.InputParameters)
                {
                    param.SubscriptionId = subscription.SubscriptionId;
                    _context.SubscriptionParameters.Add(param);
                }
                await _context._SaveChangesAsync();

                foreach (var meter in customMeterList)
                {
                    _context.SubscriptionCustomMeterUsages.Add(new SubscriptionCustomMeterUsage(meter.Id, subscription.SubscriptionId));
                }

                await _context._SaveChangesAsync();

                transaction.Commit();
            }
            _logger.LogInformation(LoggingUtils.ComposeResourceCreatedMessage(typeof(Subscription).Name, subscription.Name, offerName: subscription.OfferName, planName: subscription.PlanName));

            return(subscription);
        }