using Amazon.SecretsManager.Extensions.Caching; using Amazon.SecretsManager.Model; using Microsoft.Extensions.Options; using Strata.SnowflakeLib; using Strata.SnowflakeLib.Configuration; using System.Diagnostics.CodeAnalysis; using System.Runtime.ExceptionServices; namespace Strata.DecisionSupport.Biz.Test.Integration { public class SnowflakeTestConnectionStringBuilderFactory : ISnowflakeConnectionStringBuilderFactory { private readonly SecretsManagerCache _secretsManagerCache; private readonly SnowflakeOptions _snowflakeOptions; /// /// Constructor that uses configuration values to generate the factory /// /// logger to allow output of message to our centralized logging system /// aws secrets manager cache injected during DI for faster lookup /// The options class that holds configuration, forcing the consumer of the nuget to have certain settings /// Claims accessor object to expose an interface to pull the identity and database information public SnowflakeTestConnectionStringBuilderFactory( [NotNull] SecretsManagerCache secretsManagerCache, //[NotNull] IOptions snowflakeOptions) { try { _secretsManagerCache = secretsManagerCache ?? throw new ArgumentNullException(nameof(secretsManagerCache)); _snowflakeOptions = snowflakeOptions?.Value ?? throw new ArgumentNullException(nameof(snowflakeOptions)); } catch (Exception ex) { throw; } } /// /// Method to get a snowflake connection string through secrets manager. /// /// The aws secret name to lookup the snowflake connection string /// The connection string builder for the connection string stored at the awws secret name public async Task CreateAsync(string secretName) { return await CreateAsync(secretName, Guid.Empty); } /// /// Method to get a snowflake connection string through secrets manager. /// /// The aws secret name to lookup the snowflake connection string /// The database guid used to make the connection /// The connection string builder for the connection string stored at the awws secret name public async Task CreateAsync(string secretName, Guid databaseGuid) { SnowflakeConnectionStringBuilder builder = null; try { var snowflakeConnectionString = await _secretsManagerCache.GetSecretString(secretName); builder = new SnowflakeConnectionStringBuilder(snowflakeConnectionString) { URL = _snowflakeOptions.Url, Account = _snowflakeOptions.Account, // IgnoreCase = true, // QueryPassthrough = true, Timeout = (int)TimeSpan.FromHours(10).TotalSeconds, // Other = "RetryStatusCodes=500" DatabaseGuid = databaseGuid }; } catch (ResourceNotFoundException e) { ExceptionDispatchInfo.Capture(e).Throw(); } catch (InvalidRequestException e) { ExceptionDispatchInfo.Capture(e).Throw(); } catch (InvalidParameterException e) { ExceptionDispatchInfo.Capture(e).Throw(); } catch (Exception e) { ExceptionDispatchInfo.Capture(e).Throw(); } return builder; } } }