|
| 1 | +package aws |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/aws/aws-sdk-go-v2/aws" |
| 9 | + awscfg "github.com/aws/aws-sdk-go-v2/config" |
| 10 | + "github.com/aws/aws-sdk-go-v2/credentials" |
| 11 | + "github.com/aws/aws-sdk-go-v2/credentials/stscreds" |
| 12 | + "github.com/aws/aws-sdk-go-v2/service/s3" |
| 13 | + "github.com/aws/aws-sdk-go-v2/service/sagemakerruntime" |
| 14 | + "github.com/aws/aws-sdk-go-v2/service/sts" |
| 15 | +) |
| 16 | + |
| 17 | +type ( |
| 18 | + Builder struct { |
| 19 | + config *Config |
| 20 | + } |
| 21 | +) |
| 22 | + |
| 23 | +func NewBuilder(c *Config) *Builder { |
| 24 | + return &Builder{config: c} |
| 25 | +} |
| 26 | + |
| 27 | +func (b *Builder) LoadConfig( |
| 28 | + ctx context.Context, opts ...func(options *awscfg.LoadOptions) error) (aws.Config, error) { |
| 29 | + defaultOpts := []func(options *awscfg.LoadOptions) error{ |
| 30 | + awscfg.WithRegion(b.config.AWSConfig.Region), |
| 31 | + awscfg.WithHTTPClient(createHttpClient(&b.config.AWSConfig.HTTPClient)), |
| 32 | + } |
| 33 | + |
| 34 | + opts = append(defaultOpts, opts...) |
| 35 | + |
| 36 | + return awscfg.LoadDefaultConfig(ctx, opts...) |
| 37 | +} |
| 38 | + |
| 39 | +func (b *Builder) NewS3Service( |
| 40 | + cfg aws.Config, serviceName string, opts ...func(options *s3.Options)) (*s3.Client, error) { |
| 41 | + s3Cfg, ok := b.config.S3[serviceName] |
| 42 | + if !ok { |
| 43 | + return nil, fmt.Errorf("s3 config for service %s not found", serviceName) |
| 44 | + } |
| 45 | + defaultOpts := []func(*s3.Options){ |
| 46 | + func(options *s3.Options) { |
| 47 | + if s3Cfg.Region != "" { |
| 48 | + options.Region = s3Cfg.Region |
| 49 | + } |
| 50 | + if credentialsSet(&s3Cfg.Credentials) { |
| 51 | + options.Credentials = getCredentialsProvider(cfg, &s3Cfg.Credentials) |
| 52 | + } |
| 53 | + if s3Cfg.HTTPClient.MaxIdleConns > 0 { |
| 54 | + options.HTTPClient = createHttpClient(&s3Cfg.HTTPClient) |
| 55 | + } |
| 56 | + }, |
| 57 | + } |
| 58 | + |
| 59 | + opts = append(defaultOpts, opts...) |
| 60 | + |
| 61 | + return s3.NewFromConfig(cfg, opts...), nil |
| 62 | +} |
| 63 | + |
| 64 | +func (b *Builder) NewSagemakerRuntimeService( |
| 65 | + cfg aws.Config, |
| 66 | + serviceName string, opts ...func(options *sagemakerruntime.Options)) (*sagemakerruntime.Client, error) { |
| 67 | + sagemakerRuntimeCfg, ok := b.config.SagemakerRuntime[serviceName] |
| 68 | + if !ok { |
| 69 | + return nil, fmt.Errorf("sagemaker runtime config for service %s not found", serviceName) |
| 70 | + } |
| 71 | + defaultOpts := []func(*sagemakerruntime.Options){ |
| 72 | + func(options *sagemakerruntime.Options) { |
| 73 | + if sagemakerRuntimeCfg.Region != "" { |
| 74 | + options.Region = sagemakerRuntimeCfg.Region |
| 75 | + } |
| 76 | + if credentialsSet(&sagemakerRuntimeCfg.Credentials) { |
| 77 | + options.Credentials = getCredentialsProvider(cfg, &sagemakerRuntimeCfg.Credentials) |
| 78 | + } |
| 79 | + if sagemakerRuntimeCfg.HTTPClient.MaxIdleConns > 0 { |
| 80 | + options.HTTPClient = createHttpClient(&sagemakerRuntimeCfg.HTTPClient) |
| 81 | + } |
| 82 | + }, |
| 83 | + } |
| 84 | + |
| 85 | + opts = append(defaultOpts, opts...) |
| 86 | + |
| 87 | + return sagemakerruntime.NewFromConfig(cfg, opts...), nil |
| 88 | +} |
| 89 | + |
| 90 | +func (b *Builder) NewSFNService( |
| 91 | + cfg aws.Config, |
| 92 | + serviceName string, opts ...func(options *sagemakerruntime.Options)) (*sagemakerruntime.Client, error) { |
| 93 | + sfnCfg, ok := b.config.SFN[serviceName] |
| 94 | + if !ok { |
| 95 | + return nil, fmt.Errorf("sfn config for service %s not found", serviceName) |
| 96 | + } |
| 97 | + defaultOpts := []func(*sagemakerruntime.Options){ |
| 98 | + func(options *sagemakerruntime.Options) { |
| 99 | + if sfnCfg.Region != "" { |
| 100 | + options.Region = sfnCfg.Region |
| 101 | + } |
| 102 | + if credentialsSet(&sfnCfg.Credentials) { |
| 103 | + options.Credentials = getCredentialsProvider(cfg, &sfnCfg.Credentials) |
| 104 | + } |
| 105 | + if sfnCfg.HTTPClient.MaxIdleConns > 0 { |
| 106 | + options.HTTPClient = createHttpClient(&sfnCfg.HTTPClient) |
| 107 | + } |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + opts = append(defaultOpts, opts...) |
| 112 | + |
| 113 | + return sagemakerruntime.NewFromConfig(cfg, opts...), nil |
| 114 | +} |
| 115 | + |
| 116 | +func (b *Builder) NewSQSService( |
| 117 | + cfg aws.Config, serviceName string, opts ...func(options *s3.Options)) (*s3.Client, error) { |
| 118 | + sqsCfg, ok := b.config.SQS[serviceName] |
| 119 | + if !ok { |
| 120 | + return nil, fmt.Errorf("sqs config for service %s not found", serviceName) |
| 121 | + } |
| 122 | + defaultOpts := []func(*s3.Options){ |
| 123 | + func(options *s3.Options) { |
| 124 | + if sqsCfg.Region != "" { |
| 125 | + options.Region = sqsCfg.Region |
| 126 | + } |
| 127 | + if credentialsSet(&sqsCfg.Credentials) { |
| 128 | + options.Credentials = getCredentialsProvider(cfg, &sqsCfg.Credentials) |
| 129 | + } |
| 130 | + if sqsCfg.HTTPClient.MaxIdleConns > 0 { |
| 131 | + options.HTTPClient = createHttpClient(&sqsCfg.HTTPClient) |
| 132 | + } |
| 133 | + }, |
| 134 | + } |
| 135 | + |
| 136 | + opts = append(defaultOpts, opts...) |
| 137 | + |
| 138 | + return s3.NewFromConfig(cfg, opts...), nil |
| 139 | +} |
| 140 | + |
| 141 | +func createHttpClient(cfg *HTTPClient) *http.Client { |
| 142 | + defaultRoundTripper := http.DefaultTransport |
| 143 | + defaultTransport := defaultRoundTripper.(*http.Transport) |
| 144 | + |
| 145 | + httpTransport := defaultTransport.Clone() |
| 146 | + httpTransport.MaxIdleConnsPerHost = cfg.MaxIdleConns |
| 147 | + |
| 148 | + return &http.Client{Transport: httpTransport} |
| 149 | +} |
| 150 | + |
| 151 | +func credentialsSet(cfg *CredentialsConfig) bool { |
| 152 | + return cfg.Static.AccessKeyID != "" && |
| 153 | + cfg.Static.SecretAccessKey != "" || |
| 154 | + cfg.AssumeRole.ARN != "" |
| 155 | +} |
| 156 | + |
| 157 | +func getCredentialsProvider( |
| 158 | + awsConf aws.Config, cfg *CredentialsConfig) aws.CredentialsProvider { |
| 159 | + if cfg.AssumeRole.ARN != "" { |
| 160 | + return aws.NewCredentialsCache( |
| 161 | + stscreds.NewAssumeRoleProvider( |
| 162 | + sts.NewFromConfig(awsConf), |
| 163 | + cfg.AssumeRole.ARN)) |
| 164 | + } |
| 165 | + return credentials.NewStaticCredentialsProvider( |
| 166 | + cfg.Static.AccessKeyID, |
| 167 | + cfg.Static.SecretAccessKey, |
| 168 | + cfg.Static.SessionToken) |
| 169 | +} |
0 commit comments