Esempio n. 1
0
    public static async Task Restore(IConfiguration configuration, HttpClient httpClient)
    {
        var accessToken = await AuthorizationUtils.GetBearerToken(configuration);

        var restoreUrl = RestoreUrlPattern
                         .Replace("{subscriptionId}", configuration[SubscriptionId])
                         .Replace("{resourceGroupName}", configuration[ResourceGroupName])
                         .Replace("{serviceName}", configuration[ServiceName])
                         .Replace("{api-version}", configuration[ApiVersion]);

        var restoreName = $"{configuration[ServiceName]}/backup";

        var contentPayload = $@"{{
            ""storageAccount"": ""{configuration[StorageAccount]}"",
            ""containerName"": ""{configuration[ContainerName]}"",
            ""backupName"": ""{restoreName}"",
            ""accessType"": ""SystemAssignedManagedIdentity""
        }}";

        var content = new StringContent(contentPayload, Encoding.UTF8, "application/json");
        var request = new HttpRequestMessage(HttpMethod.Post, restoreUrl)
        {
            Content = content
        };

        request.Headers.Add("Authorization", $"Bearer {accessToken}");

        var response = await httpClient.SendAsync(request);

        try
        {
            response.EnsureSuccessStatusCode();
        }
        catch (HttpRequestException)
        {
            var responseContent = await response.Content.ReadAsStringAsync();

            var errorModel = JsonSerializer.Deserialize <ApimErrorResponse>(
                responseContent,
                new JsonSerializerOptions {
                PropertyNameCaseInsensitive = true
            });

            Console.WriteLine("Error ({0}) with message ({1})", errorModel.Error.Code, errorModel.Error.Message);
            Console.WriteLine();
            throw;
        }

        Console.WriteLine("Location to check operation status: {0}", response.Headers.Location);
    }