|
| 1 | +package aws |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + |
| 8 | + cbuilder "github.com/scribd/go-sdk/internal/pkg/configuration/builder" |
| 9 | +) |
| 10 | + |
| 11 | +type ( |
| 12 | + HTTPClient struct { |
| 13 | + // MaxIdleConns, if non-zero, controls the maximum idle |
| 14 | + // (keep-alive) connections to keep per-host. |
| 15 | + MaxIdleConns int `mapstructure:"max_idle_conns"` |
| 16 | + } |
| 17 | + |
| 18 | + StaticConfig struct { |
| 19 | + // AccessKeyID is the AWS access key ID. |
| 20 | + AccessKeyID string `mapstructure:"access_key_id"` |
| 21 | + // SecretAccessKey is the AWS secret access key. |
| 22 | + SecretAccessKey string `mapstructure:"secret_access_key"` |
| 23 | + // SessionToken is the AWS session token. |
| 24 | + SessionToken string `mapstructure:"session_token"` |
| 25 | + } |
| 26 | + |
| 27 | + AssumeRoleConfig struct { |
| 28 | + // ARN is the ARN of the role to assume. |
| 29 | + ARN string `mapstructure:"arn"` |
| 30 | + } |
| 31 | + |
| 32 | + CredentialsConfig struct { |
| 33 | + // Static is the configuration for static credentials. |
| 34 | + Static StaticConfig `mapstructure:"static"` |
| 35 | + // AssumeRole is the configuration for assuming a role. |
| 36 | + AssumeRole AssumeRoleConfig `mapstructure:"assume_role"` |
| 37 | + } |
| 38 | + |
| 39 | + AWSConfig struct { |
| 40 | + // Region is the region to send requests to. |
| 41 | + Region string `mapstructure:"region"` |
| 42 | + // HTTPClient is the configuration for the HttpClient AWS the SDK's API clients will use to invoke HTTP requests. |
| 43 | + HTTPClient HTTPClient `mapstructure:"http_client"` |
| 44 | + } |
| 45 | + |
| 46 | + S3Config struct { |
| 47 | + // Region is the region to send requests to. |
| 48 | + Region string `mapstructure:"region"` |
| 49 | + // HTTPClient is the configuration for the HttpClient AWS the SDK's API clients will use to invoke HTTP requests. |
| 50 | + HTTPClient HTTPClient `mapstructure:"http_client"` |
| 51 | + // Credentials is the configuration for the AWS credentials. |
| 52 | + Credentials CredentialsConfig `mapstructure:"credentials"` |
| 53 | + } |
| 54 | + |
| 55 | + SagemakerRuntimeConfig struct { |
| 56 | + // Region is the region to send requests to. |
| 57 | + Region string `mapstructure:"region"` |
| 58 | + // HTTPClient is the configuration for the HttpClient AWS the SDK's API clients will use to invoke HTTP requests. |
| 59 | + HTTPClient HTTPClient `mapstructure:"http_client"` |
| 60 | + // Credentials is the configuration for the AWS credentials. |
| 61 | + Credentials CredentialsConfig `mapstructure:"credentials"` |
| 62 | + } |
| 63 | + |
| 64 | + SFNConfig struct { |
| 65 | + // Region is the region to send requests to. |
| 66 | + Region string `mapstructure:"region"` |
| 67 | + // HTTPClient is the configuration for the HttpClient AWS the SDK's API clients will use to invoke HTTP requests. |
| 68 | + HTTPClient HTTPClient `mapstructure:"http_client"` |
| 69 | + // Credentials is the configuration for the AWS credentials. |
| 70 | + Credentials CredentialsConfig `mapstructure:"credentials"` |
| 71 | + } |
| 72 | + |
| 73 | + SQSConfig struct { |
| 74 | + // Region is the region to send requests to. |
| 75 | + Region string `mapstructure:"region"` |
| 76 | + // HTTPClient is the configuration for the HttpClient AWS the SDK's API clients will use to invoke HTTP requests. |
| 77 | + HTTPClient HTTPClient `mapstructure:"http_client"` |
| 78 | + // Credentials is the configuration for the AWS credentials. |
| 79 | + Credentials CredentialsConfig `mapstructure:"credentials"` |
| 80 | + } |
| 81 | + |
| 82 | + Config struct { |
| 83 | + // AWSConfig is the configuration for the AWS SDK. |
| 84 | + AWSConfig AWSConfig `mapstructure:"config"` |
| 85 | + // CredentialsConfig is the configuration for the AWS credentials. |
| 86 | + CredentialsConfig CredentialsConfig `mapstructure:"credentials"` |
| 87 | + // S3 is the configuration for the S3 clients. |
| 88 | + S3 map[string]S3Config `mapstructure:"s3"` |
| 89 | + // SagemakerRuntime is the configuration for the SagemakerRuntime clients. |
| 90 | + SagemakerRuntime map[string]SagemakerRuntimeConfig `mapstructure:"sagemakerruntime"` |
| 91 | + // SFN is the configuration for the Sfn clients. |
| 92 | + SFN map[string]SFNConfig `mapstructure:"sfn"` |
| 93 | + // SQS is the configuration for the Sqs clients. |
| 94 | + SQS map[string]SQSConfig `mapstructure:"sqs"` |
| 95 | + } |
| 96 | +) |
| 97 | + |
| 98 | +func NewConfig() (*Config, error) { |
| 99 | + config := &Config{} |
| 100 | + viperBuilder := cbuilder.New("aws") |
| 101 | + |
| 102 | + appName := strings.ReplaceAll(os.Getenv("APP_SETTINGS_NAME"), "-", "_") |
| 103 | + viperBuilder.SetDefault("aws", fmt.Sprintf("%s_%s", appName, os.Getenv("APP_ENV"))) |
| 104 | + |
| 105 | + vConf, err := viperBuilder.Build() |
| 106 | + if err != nil { |
| 107 | + return config, err |
| 108 | + } |
| 109 | + |
| 110 | + if err = vConf.Unmarshal(config); err != nil { |
| 111 | + return config, fmt.Errorf("unable to decode into struct: %s", err.Error()) |
| 112 | + } |
| 113 | + |
| 114 | + return config, nil |
| 115 | +} |
0 commit comments