Beispiel #1
0
    public async Task <IEnumerable <string> > GetErrorsAsync()
    {
        var json = await GetDeploymentJsonAsync()
                   .ConfigureAwait(false);

        var errorToken = json.SelectToken("$.properties.error");

        return(AzureDeploymentException.ResolveResourceErrors(errorToken));
    }
    public async Task <IAzureDeployment> DeploySubscriptionTemplateAsync(AzureDeploymentTemplate template, Guid subscriptionId, string location)
    {
        if (template is null)
        {
            throw new ArgumentNullException(nameof(template));
        }

        if (location is null)
        {
            throw new ArgumentNullException(nameof(location));
        }

        var deploymentId         = Guid.NewGuid();
        var deploymentResourceId = $"/subscriptions/{subscriptionId}/providers/Microsoft.Resources/deployments/{deploymentId}";

        var payload = await GetDeploymentPayloadAsync(deploymentId, template, location, DeploymentMode.Incremental)
                      .ConfigureAwait(false);

        var token = await azureSessionService
                    .AcquireTokenAsync()
                    .ConfigureAwait(false);

        try
        {
            _ = await azureSessionService.Environment.ResourceManagerEndpoint
                .AppendPathSegment(deploymentResourceId)
                .SetQueryParam("api-version", "2019-05-01")
                .WithOAuthBearerToken(token)
                .PutJsonAsync(payload)
                .ConfigureAwait(false);
        }
        catch (FlurlHttpException exc) when(exc.Call.HttpResponseMessage.StatusCode == System.Net.HttpStatusCode.BadRequest)
        {
            var validationResultJson = await exc.Call.Response.GetStringAsync().ConfigureAwait(false);

            var validationResultError          = JObject.Parse(validationResultJson).SelectToken("$..error");
            var validationResultResourceErrors = AzureDeploymentException.ResolveResourceErrors(validationResultError);

            throw new AzureDeploymentException($"Invalid deployment template: {string.Join(", ", validationResultResourceErrors)}", deploymentResourceId, validationResultResourceErrors.ToArray());
        }

        return(new AzureDeployment(deploymentResourceId, azureSessionService));
    }