예제 #1
0
        private async Task <JObject> GetDeploymentJsonAsync()
        {
            if (deploymentJsonFinal != null)
            {
                return(deploymentJsonFinal);
            }

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

                var json = await azureSessionService.Environment.ResourceManagerEndpoint
                           .AppendPathSegment(ResourceId)
                           .SetQueryParam("api-version", "2019-05-01")
                           .WithOAuthBearerToken(token)
                           .GetJObjectAsync()
                           .ConfigureAwait(false);

                if (GetState(json).IsFinalState())
                {
                    deploymentJsonFinal = json;
                }

                return(json);
            }
            catch (FlurlHttpException exc) when(exc.Call.HttpStatus == System.Net.HttpStatusCode.NotFound)
            {
                return(null);
            }
        }
예제 #2
0
    public async Task <IAzureDeployment> GetAzureDeploymentAsync(string resourceId)
    {
        if (string.IsNullOrWhiteSpace(resourceId))
        {
            throw new ArgumentException($"Argument '{nameof(resourceId)}' must not NULL or WHITESPACE", nameof(resourceId));
        }

        if (!resourceId.Contains("/providers/Microsoft.Resources/deployments/", StringComparison.OrdinalIgnoreCase))
        {
            throw new ArgumentException($"Argument '{nameof(resourceId)}' must be a valid deployment resource Id", nameof(resourceId));
        }

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

        var response = await azureSessionService.Environment.ResourceManagerEndpoint
                       .AppendPathSegment(resourceId)
                       .SetQueryParam("api-version", "2019-05-01")
                       .AllowHttpStatus(HttpStatusCode.NotFound)
                       .WithOAuthBearerToken(token)
                       .GetAsync(completionOption: System.Net.Http.HttpCompletionOption.ResponseHeadersRead)
                       .ConfigureAwait(false);

        return(response.IsSuccessStatusCode()
            ? new AzureDeployment(resourceId, azureSessionService)
            : null);
    }
예제 #3
0
        private async Task <string> GetDefaultDomainAsync()
        {
            var token = await azureSessionService
                        .AcquireTokenAsync(AzureEndpoint.GraphEndpoint)
                        .ConfigureAwait(false);

            var json = await azureSessionService.Environment.GraphEndpoint
                       .AppendPathSegment($"{azureSessionService.Options.TenantId}/tenantDetails")
                       .SetQueryParam("api-version", "1.6")
                       .WithOAuthBearerToken(token)
                       .GetJObjectAsync()
                       .ConfigureAwait(false);

            return(json.SelectToken("$.value[0].verifiedDomains[?(@.default == true)].name")?.ToString());
        }
예제 #4
0
        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.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);

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

            return(new AzureDeployment(deploymentResourceId, azureSessionService));
        }
예제 #5
0
        public async Task <AuthenticationHeaderValue> GetAuthenticationHeaderAsync(CancellationToken cancellationToken)
        {
            var token = await azureSessionService.AcquireTokenAsync(azureEndpoint).ConfigureAwait(false);

            return(new AuthenticationHeaderValue("Bearer", token));
        }