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

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

            return(AzureDeploymentException.ResolveResourceErrors(errorToken));
        }
示例#2
0
        public async Task <IEnumerable <string> > ValidateSubscriptionTemplateAsync(AzureDeploymentTemplate deploymentTemplate, Guid subscriptionId, string location, bool throwOnError = false)
        {
            if (deploymentTemplate is null)
            {
                throw new ArgumentNullException(nameof(deploymentTemplate));
            }

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

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

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

            try
            {
                _ = await azureSessionService.Environment.ResourceManagerEndpoint
                    .AppendPathSegment(deploymentResourceId)
                    .SetQueryParam("api-version", "2019-10-01")
                    .WithOAuthBearerToken(token)
                    .PostJsonAsync(payload)
                    .ConfigureAwait(false);

                return(null);
            }
            catch (FlurlHttpException exc) when(exc.Call.HttpStatus == System.Net.HttpStatusCode.BadRequest)
            {
                var validationResultJson = await exc.Call.Response.Content.ReadAsStringAsync().ConfigureAwait(false);

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

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

                return(validationResultResourceErrors);
            }
        }