|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +using System; |
| 5 | +using System.Threading; |
| 6 | +using System.Threading.Tasks; |
| 7 | +using Azure.Core; |
| 8 | +using Azure.Storage.DataMovement.Models; |
| 9 | + |
| 10 | +namespace Azure.Storage.DataMovement.Blobs |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// Provider for a <see cref="StorageResource"/> configured for an Azure Blob Storage resource. |
| 14 | + /// </summary> |
| 15 | + public class BlobStorageResourceProvider |
| 16 | + { |
| 17 | + internal DataTransferProperties Properties { get; } |
| 18 | + internal bool MakesSource { get; } |
| 19 | + internal BlobStorageResources.ResourceType ResourceType { get; } |
| 20 | + |
| 21 | + internal BlobStorageResourceProvider( |
| 22 | + DataTransferProperties properties, |
| 23 | + bool asSource, |
| 24 | + BlobStorageResources.ResourceType resourceType) |
| 25 | + { |
| 26 | + Argument.AssertNotNull(properties, nameof(properties)); |
| 27 | + if (resourceType == BlobStorageResources.ResourceType.Unknown) |
| 28 | + { |
| 29 | + throw BadResourceTypeException(resourceType); |
| 30 | + } |
| 31 | + |
| 32 | + Properties = properties; |
| 33 | + MakesSource = asSource; |
| 34 | + ResourceType = resourceType; |
| 35 | + } |
| 36 | + |
| 37 | + /// <summary> |
| 38 | + /// Creates the configured <see cref="StorageResource"/> instance using the given <see cref="StorageSharedKeyCredential"/>. |
| 39 | + /// </summary> |
| 40 | + /// <returns></returns> |
| 41 | + public async Task<StorageResource> MakeResourceAsync(StorageSharedKeyCredential credential, CancellationToken cancellationToken = default) |
| 42 | + { |
| 43 | + return ResourceType switch |
| 44 | + { |
| 45 | + BlobStorageResources.ResourceType.BlockBlob => await BlockBlobStorageResource |
| 46 | + .RehydrateResourceAsync(Properties, MakesSource, credential, cancellationToken) |
| 47 | + .ConfigureAwait(false), |
| 48 | + BlobStorageResources.ResourceType.PageBlob => await PageBlobStorageResource |
| 49 | + .RehydrateResourceAsync(Properties, MakesSource, credential, cancellationToken) |
| 50 | + .ConfigureAwait(false), |
| 51 | + BlobStorageResources.ResourceType.AppendBlob => await AppendBlobStorageResource |
| 52 | + .RehydrateResourceAsync(Properties, MakesSource, credential, cancellationToken) |
| 53 | + .ConfigureAwait(false), |
| 54 | + BlobStorageResources.ResourceType.BlobContainer => await BlobStorageResourceContainer |
| 55 | + .RehydrateResourceAsync(Properties, MakesSource, credential, cancellationToken) |
| 56 | + .ConfigureAwait(false), |
| 57 | + _ => throw BadResourceTypeException(ResourceType) |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + /// <summary> |
| 62 | + /// Creates the configured <see cref="StorageResource"/> instance using the given <see cref="AzureSasCredential"/>. |
| 63 | + /// </summary> |
| 64 | + /// <returns></returns> |
| 65 | + public async Task<StorageResource> MakeResourceAsync(AzureSasCredential credential, CancellationToken cancellationToken = default) |
| 66 | + { |
| 67 | + return ResourceType switch |
| 68 | + { |
| 69 | + BlobStorageResources.ResourceType.BlockBlob => await BlockBlobStorageResource |
| 70 | + .RehydrateResourceAsync(Properties, MakesSource, credential, cancellationToken) |
| 71 | + .ConfigureAwait(false), |
| 72 | + BlobStorageResources.ResourceType.PageBlob => await PageBlobStorageResource |
| 73 | + .RehydrateResourceAsync(Properties, MakesSource, credential, cancellationToken) |
| 74 | + .ConfigureAwait(false), |
| 75 | + BlobStorageResources.ResourceType.AppendBlob => await AppendBlobStorageResource |
| 76 | + .RehydrateResourceAsync(Properties, MakesSource, credential, cancellationToken) |
| 77 | + .ConfigureAwait(false), |
| 78 | + BlobStorageResources.ResourceType.BlobContainer => await BlobStorageResourceContainer |
| 79 | + .RehydrateResourceAsync(Properties, MakesSource, credential, cancellationToken) |
| 80 | + .ConfigureAwait(false), |
| 81 | + _ => throw BadResourceTypeException(ResourceType) |
| 82 | + }; |
| 83 | + } |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// Creates the configured <see cref="StorageResource"/> instance using the given <see cref="TokenCredential"/>. |
| 87 | + /// </summary> |
| 88 | + /// <returns></returns> |
| 89 | + public async Task<StorageResource> MakeResourceAsync(TokenCredential credential, CancellationToken cancellationToken = default) |
| 90 | + { |
| 91 | + return ResourceType switch |
| 92 | + { |
| 93 | + BlobStorageResources.ResourceType.BlockBlob => await BlockBlobStorageResource |
| 94 | + .RehydrateResourceAsync(Properties, MakesSource, credential, cancellationToken) |
| 95 | + .ConfigureAwait(false), |
| 96 | + BlobStorageResources.ResourceType.PageBlob => await PageBlobStorageResource |
| 97 | + .RehydrateResourceAsync(Properties, MakesSource, credential, cancellationToken) |
| 98 | + .ConfigureAwait(false), |
| 99 | + BlobStorageResources.ResourceType.AppendBlob => await AppendBlobStorageResource |
| 100 | + .RehydrateResourceAsync(Properties, MakesSource, credential, cancellationToken) |
| 101 | + .ConfigureAwait(false), |
| 102 | + BlobStorageResources.ResourceType.BlobContainer => await BlobStorageResourceContainer |
| 103 | + .RehydrateResourceAsync(Properties, MakesSource, credential, cancellationToken) |
| 104 | + .ConfigureAwait(false), |
| 105 | + _ => throw BadResourceTypeException(ResourceType) |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + /// <summary> |
| 110 | + /// Creates the configured <see cref="StorageResource"/> instance using no credential. |
| 111 | + /// </summary> |
| 112 | + /// <returns></returns> |
| 113 | + public async Task<StorageResource> MakeResourceAsync(CancellationToken cancellationToken = default) |
| 114 | + { |
| 115 | + return ResourceType switch |
| 116 | + { |
| 117 | + BlobStorageResources.ResourceType.BlockBlob => await BlockBlobStorageResource |
| 118 | + .RehydrateResourceAsync(Properties, MakesSource, cancellationToken) |
| 119 | + .ConfigureAwait(false), |
| 120 | + BlobStorageResources.ResourceType.PageBlob => await PageBlobStorageResource |
| 121 | + .RehydrateResourceAsync(Properties, MakesSource, cancellationToken) |
| 122 | + .ConfigureAwait(false), |
| 123 | + BlobStorageResources.ResourceType.AppendBlob => await AppendBlobStorageResource |
| 124 | + .RehydrateResourceAsync(Properties, MakesSource, cancellationToken) |
| 125 | + .ConfigureAwait(false), |
| 126 | + BlobStorageResources.ResourceType.BlobContainer => await BlobStorageResourceContainer |
| 127 | + .RehydrateResourceAsync(Properties, MakesSource, cancellationToken) |
| 128 | + .ConfigureAwait(false), |
| 129 | + _ => throw BadResourceTypeException(ResourceType) |
| 130 | + }; |
| 131 | + } |
| 132 | + |
| 133 | + private static ArgumentException BadResourceTypeException(BlobStorageResources.ResourceType resourceType) |
| 134 | + => new ArgumentException( |
| 135 | + $"No support for resource type {Enum.GetName(typeof(BlobStorageResources.ResourceType), resourceType)}."); |
| 136 | + } |
| 137 | +} |
0 commit comments