github.com/cornelk/go-cloud@v0.17.1/aws/aws.go (about) 1 // Copyright 2018 The Go Cloud Development Kit Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // https://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // Package aws provides fundamental Wire providers for Amazon Web Services (AWS). 16 package aws // import "github.com/cornelk/go-cloud/aws" 17 18 import ( 19 "fmt" 20 "net/url" 21 "strconv" 22 23 "github.com/aws/aws-sdk-go/aws" 24 "github.com/aws/aws-sdk-go/aws/client" 25 "github.com/aws/aws-sdk-go/aws/credentials" 26 "github.com/aws/aws-sdk-go/aws/session" 27 "github.com/google/wire" 28 ) 29 30 // DefaultSession is a Wire provider set that provides a *session.Session using 31 // the default options. 32 var DefaultSession = wire.NewSet( 33 SessionConfig, 34 ConfigCredentials, 35 NewDefaultSession, 36 wire.Bind(new(client.ConfigProvider), new(*session.Session)), 37 ) 38 39 // NewDefaultSession returns a *session.Session using the default options. 40 func NewDefaultSession() (*session.Session, error) { 41 return session.NewSessionWithOptions(session.Options{SharedConfigState: session.SharedConfigEnable}) 42 } 43 44 // SessionConfig returns sess.Config. 45 func SessionConfig(sess *session.Session) *aws.Config { 46 return sess.Config 47 } 48 49 // ConfigCredentials returns cfg.Credentials. 50 func ConfigCredentials(cfg *aws.Config) *credentials.Credentials { 51 return cfg.Credentials 52 } 53 54 // ConfigOverrider implements client.ConfigProvider by overlaying a list of 55 // configurations over a base configuration provider. 56 type ConfigOverrider struct { 57 Base client.ConfigProvider 58 Configs []*aws.Config 59 } 60 61 // ClientConfig calls the base provider's ClientConfig method with co.Configs 62 // followed by the arguments given to ClientConfig. 63 func (co ConfigOverrider) ClientConfig(serviceName string, cfgs ...*aws.Config) client.Config { 64 cfgs = append(co.Configs[:len(co.Configs):len(co.Configs)], cfgs...) 65 return co.Base.ClientConfig(serviceName, cfgs...) 66 } 67 68 // ConfigFromURLParams returns an aws.Config initialized based on the URL 69 // parameters in q. It is intended to be used by URLOpeners for AWS services. 70 // https://docs.aws.amazon.com/sdk-for-go/api/aws/#Config 71 // 72 // It returns an error if q contains any unknown query parameters; callers 73 // should remove any query parameters they know about from q before calling 74 // ConfigFromURLParams. 75 // 76 // The following query options are supported: 77 // - region: The AWS region for requests; sets aws.Config.Region. 78 // - endpoint: The endpoint URL (hostname only or fully qualified URI); sets aws.Config.Endpoint. 79 // - disableSSL: A value of "true" disables SSL when sending requests; sets aws.Config.DisableSSL. 80 // - s3ForcePathStyle: A value of "true" forces the request to use path-style addressing; sets aws.Config.S3ForcePathStyle. 81 func ConfigFromURLParams(q url.Values) (*aws.Config, error) { 82 var cfg aws.Config 83 for param, values := range q { 84 value := values[0] 85 switch param { 86 case "region": 87 cfg.Region = aws.String(value) 88 case "endpoint": 89 cfg.Endpoint = aws.String(value) 90 case "disableSSL": 91 b, err := strconv.ParseBool(value) 92 if err != nil { 93 return nil, fmt.Errorf("invalid value for query parameter %q: %v", param, err) 94 } 95 cfg.DisableSSL = aws.Bool(b) 96 case "s3ForcePathStyle": 97 b, err := strconv.ParseBool(value) 98 if err != nil { 99 return nil, fmt.Errorf("invalid value for query parameter %q: %v", param, err) 100 } 101 cfg.S3ForcePathStyle = aws.Bool(b) 102 default: 103 return nil, fmt.Errorf("unknown query parameter %q", param) 104 } 105 } 106 return &cfg, nil 107 }