Exemplo n.º 1
0
        public Task <CloudBlobClient> GetAuthorizedClientAsync(IntegrationDataStoreConfiguration integrationDataStoreConfiguration, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(integrationDataStoreConfiguration.StorageAccountConnection))
            {
                throw new IntegrationDataStoreClientInitializerException(Resources.InvalidConnectionSettings, HttpStatusCode.BadRequest);
            }

            if (!CloudStorageAccount.TryParse(integrationDataStoreConfiguration.StorageAccountConnection, out CloudStorageAccount cloudAccount))
            {
                throw new IntegrationDataStoreClientInitializerException(Resources.InvalidConnectionSettings, HttpStatusCode.BadRequest);
            }

            CloudBlobClient blobClient;

            try
            {
                blobClient = cloudAccount.CreateCloudBlobClient();
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Failed to create a Cloud Blob Client");

                throw new IntegrationDataStoreClientInitializerException(Resources.InvalidConnectionSettings, HttpStatusCode.BadRequest);
            }

            return(Task.FromResult(blobClient));
        }
        public async Task <CloudBlobClient> GetAuthorizedClientAsync(IntegrationDataStoreConfiguration integrationDataStoreConfiguration, CancellationToken cancellationToken)
        {
            if (string.IsNullOrWhiteSpace(integrationDataStoreConfiguration.StorageAccountUri))
            {
                throw new IntegrationDataStoreClientInitializerException(Resources.InvalidStorageUri, HttpStatusCode.BadRequest);
            }

            if (!Uri.TryCreate(integrationDataStoreConfiguration.StorageAccountUri, UriKind.Absolute, out Uri storageAccountUri))
            {
                throw new IntegrationDataStoreClientInitializerException(Resources.InvalidStorageUri, HttpStatusCode.BadRequest);
            }

            string accessToken;

            try
            {
                accessToken = await _accessTokenProvider.GetAccessTokenForResourceAsync(storageAccountUri, cancellationToken);
            }
            catch (AccessTokenProviderException atp)
            {
                _logger.LogError(atp, "Unable to get access token");

                throw new IntegrationDataStoreClientInitializerException(Resources.CannotGetAccessToken, HttpStatusCode.Unauthorized);
            }

#pragma warning disable CA2000 // Dispose objects before losing scope
            StorageCredentials storageCredentials = new StorageCredentials(new TokenCredential(accessToken));
#pragma warning restore CA2000 // Dispose objects before losing scope
            return(new CloudBlobClient(storageAccountUri, storageCredentials));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Customer can use this DataStore to integrate with other Azure services for data purpose.
        /// </summary>
        /// <param name="fhirServerBuilder">Service builder for FHIR server</param>
        /// <param name="configuration">Configuration for FHIR server</param>
        public static IFhirServerBuilder AddAzureIntegrationDataStoreClient(this IFhirServerBuilder fhirServerBuilder, IConfiguration configuration)
        {
            EnsureArg.IsNotNull(fhirServerBuilder, nameof(fhirServerBuilder));

            var integrationDataStoreConfiguration = new IntegrationDataStoreConfiguration();

            configuration.GetSection(IntegrationDataStoreConfigurationName).Bind(integrationDataStoreConfiguration);

            if (!string.IsNullOrWhiteSpace(integrationDataStoreConfiguration.StorageAccountUri))
            {
                fhirServerBuilder.Services.Add <AzureAccessTokenClientInitializerV2>()
                .Transient()
                .AsService <IIntegrationDataStoreClientInitilizer <CloudBlobClient> >();

                fhirServerBuilder.Services.Add <AzureAccessTokenProvider>()
                .Transient()
                .AsService <IAccessTokenProvider>();
            }
            else
            {
                fhirServerBuilder.Services.Add <AzureConnectionStringClientInitializerV2>()
                .Transient()
                .AsService <IIntegrationDataStoreClientInitilizer <CloudBlobClient> >();
            }

            fhirServerBuilder.Services.Add <AzureBlobIntegrationDataStoreClient>()
            .Transient()
            .AsImplementedInterfaces();

            return(fhirServerBuilder);
        }
Exemplo n.º 4
0
        private static AsyncRetryPolicy CreateExtendedRetryPolicy(IntegrationDataStoreConfiguration integrationDataStoreConfiguration)
        {
            return(Policy.Handle <StorageException>()
                   .WaitAndRetryAsync(
                       retryCount: integrationDataStoreConfiguration.MaxRetryCount,
                       sleepDurationProvider: (_) => TimeSpan.FromSeconds(integrationDataStoreConfiguration.RetryInternalInSecondes),
                       onRetryAsync: (e, _, _, ctx) =>
            {
                if (integrationDataStoreConfiguration.MaxWaitTimeInSeconds == -1)
                {
                    // no timeout
                    return Task.CompletedTask;
                }

                if (ctx.TryGetValue(RetryEndTimeContextKey, out var endTimeObj))
                {
                    if (DateTime.UtcNow >= (DateTime)endTimeObj)
                    {
                        ExceptionDispatchInfo.Throw(e);
                    }

                    // otherwise, we have enough time to retry
                }
                else
                {
                    ctx.Add(RetryEndTimeContextKey, DateTime.UtcNow.AddSeconds(integrationDataStoreConfiguration.MaxWaitTimeInSeconds));
                }

                return Task.CompletedTask;
            }));
        }
Exemplo n.º 5
0
        public AzureConnectionStringClientInitializerV2(IOptions <IntegrationDataStoreConfiguration> integrationDataStoreConfiguration, ILogger <AzureConnectionStringClientInitializerV2> logger)
        {
            EnsureArg.IsNotNull(integrationDataStoreConfiguration?.Value, nameof(integrationDataStoreConfiguration));
            EnsureArg.IsNotNull(logger, nameof(logger));

            _integrationDataStoreConfiguration = integrationDataStoreConfiguration.Value;
            _logger = logger;
        }
        public AzureAccessTokenClientInitializerV2(
            IAccessTokenProvider accessTokenProvider,
            IOptions <IntegrationDataStoreConfiguration> integrationDataStoreConfiguration,
            ILogger <AzureAccessTokenClientInitializerV2> logger)
        {
            EnsureArg.IsNotNull(accessTokenProvider, nameof(accessTokenProvider));
            EnsureArg.IsNotNull(integrationDataStoreConfiguration?.Value, nameof(integrationDataStoreConfiguration));
            EnsureArg.IsNotNull(logger, nameof(logger));

            _accessTokenProvider = accessTokenProvider;
            _integrationDataStoreConfiguration = integrationDataStoreConfiguration.Value;
            _logger = logger;
        }