Ejemplo n.º 1
0
        public async Task <CustomMeteringRequestResult> RecordBatchUsageAsync(Guid requestId, Guid correlationId, IEnumerable <Usage> usage, CancellationToken cancellationToken)
        {
            var requestUrl = FluentUriBuilder
                             .Start(_baseUri)
                             .AddPath("batchUsageEvent")
                             .AddQuery(DefaultApiVersionParameterName, _apiVersion)
                             .Uri;

            requestId     = requestId == default ? Guid.NewGuid() : requestId;
            correlationId = correlationId == default ? Guid.NewGuid() : correlationId;
            var bearerToken = await _keyVaultHelper.GetBearerToken(
                _options.AzureActiveDirectory,
                _options.ClientService.AuthenticationResourceId);

            var response = await this.SendRequestAndReturnResult(
                HttpMethod.Post,
                requestUrl,
                requestId,
                correlationId,
                bearerToken,
                null,
                JsonConvert.SerializeObject(usage),
                cancellationToken);

            _logger.LogInformation($"RecordBatchUsageAsync response: {response}");

            switch (response.StatusCode)
            {
            case HttpStatusCode.OK:
                return(await HttpRequestResult.ParseAsync <CustomMeteringBatchSuccessResult>(response));

            case HttpStatusCode.Forbidden:
                return(await HttpRequestResult.ParseAsync <CustomMeteringForbiddenResult>(response));

            case HttpStatusCode.Conflict:
                return(await HttpRequestResult.ParseAsync <CustomMeteringConflictResult>(response));

            case HttpStatusCode.BadRequest:
                return(await HttpRequestResult.ParseAsync <CustomMeteringBadRequestResult>(response));

            default:
                throw new ApplicationException($"Unknown response from the API {await response.Content.ReadAsStringAsync()}");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Deploy to a resource group
        /// </summary>
        /// <remarks>Default to "Complete" deployment</remarks>
        /// <param name="requestId"></param>
        /// <param name="correlationId"></param>
        /// <param name="subscriptionId">ISV Azure subscription id</param>
        /// <param name="resourceGroup">ISV Azure resource group</param>
        /// <param name="deploymentName">Deployment name</param>
        /// <param name="templatePath">Link to template file</param>
        /// <param name="parametersPath">Link to parameter file</param>
        /// <param name="rollbackToLastSuccessful">If true, the rolllback property will be set in the request body</param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task <DeploymentExtendedResult> PutDeploymentAsync(
            Guid requestId,
            Guid correlationId,
            string subscriptionId,
            string resourceGroup,
            string deploymentName,
            string templatePath                 = default,
            object template                     = default,
            string parametersPath               = default,
            object parameters                   = default,
            bool rollbackToLastSuccessful       = default,
            CancellationToken cancellationToken = default)
        {
            try
            {
                if ((templatePath == null) == (template == null))
                {
                    throw new LunaBadRequestUserException(LoggingUtils.ComposeBadXorArgumentMessage("templatePath", "template"), UserErrorCode.InvalidParameter);
                }
                if ((parametersPath == null) == (parameters == null))
                {
                    throw new LunaBadRequestUserException(LoggingUtils.ComposeBadXorArgumentMessage("parametersPath", "parameters"), UserErrorCode.InvalidParameter);
                }

                var requestUrl = GetRequestUri(subscriptionId, resourceGroup, deploymentName);

                var body = rollbackToLastSuccessful ?
                           new DeploymentRequestBody
                {
                    Properties = new DeploymentProperties
                    {
                        Mode         = nameof(DeploymentMode.Complete),
                        TemplateLink = new TemplateLink {
                            Uri = templatePath
                        },
                        Template          = template,
                        Parameters        = parameters,
                        OnErrorDeployment = new OnErrorDeployment {
                            Type = nameof(DeploymentRollback.LastSuccessful)
                        }
                    }
                } :
                new DeploymentRequestBody
                {
                    Properties = new DeploymentProperties
                    {
                        Mode         = nameof(DeploymentMode.Complete),
                        TemplateLink = new TemplateLink {
                            Uri = templatePath
                        },
                        Template   = template,
                        Parameters = parameters
                    }
                }
                ;

                var requestBody = JsonConvert.SerializeObject(body);
                var bearerToken = await _keyVaultHelper.GetBearerToken(
                    _options.AzureActiveDirectory,
                    _options.ClientService.AuthenticationResourceId);

                var response = await SendRequestAndReturnResult(
                    HttpMethod.Put,
                    requestUrl,
                    requestId,
                    correlationId,
                    bearerToken,
                    null,
                    requestBody,
                    cancellationToken
                    );

                return(await DeploymentRequestResult.ParseAsync <DeploymentExtendedResult>(response));
            }
            catch (LunaServerException e)
            {
                throw new LunaProvisioningException("Cannot deploy template.", e.IsRetryable, ProvisioningState.ArmTemplatePending, e);
            }
        }
Ejemplo n.º 3
0
        public async Task <FulfillmentRequestResult> ActivateSubscriptionAsync(
            Guid subscriptionId,
            ActivatedSubscriptionResult subscriptionDetails,
            Guid requestId,
            Guid correlationId,
            CancellationToken cancellationToken = default)
        {
            try
            {
                var requestUrl = FluentUriBuilder
                                 .Start(_baseUri)
                                 .AddPath("subscriptions")
                                 .AddPath(subscriptionId.ToString())
                                 .AddPath("activate")
                                 .AddQuery(DefaultApiVersionParameterName, _apiVersion)
                                 .Uri;

                requestId     = requestId == default ? Guid.NewGuid() : requestId;
                correlationId = correlationId == default ? Guid.NewGuid() : correlationId;
                var bearerToken = await _keyVaultHelper.GetBearerToken(
                    _options.AzureActiveDirectory,
                    _options.ClientService.AuthenticationResourceId);

                var response = await SendRequestAndReturnResult(
                    HttpMethod.Post,
                    requestUrl,
                    requestId,
                    correlationId,
                    bearerToken,
                    null,
                    JsonConvert.SerializeObject(subscriptionDetails),
                    cancellationToken);

                return(await FulfillmentRequestResult.ParseAsync <FulfillmentRequestResult>(response));
            }
            catch (LunaServerException e)
            {
                throw new LunaFulfillmentException($"Cannot activate subscription {subscriptionId}.", e);
            }
        }