From 31169ba38492353d4b9402992b1a098d7f801de9 Mon Sep 17 00:00:00 2001 From: David Gamba Date: Thu, 12 Mar 2020 23:26:31 -0600 Subject: [PATCH 1/3] Add go.mod support --- go.mod | 9 +++++++++ go.sum | 23 +++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 go.mod create mode 100644 go.sum diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..6554c3d --- /dev/null +++ b/go.mod @@ -0,0 +1,9 @@ +module github.com/evalphobia/aws-sdk-go-wrapper + +go 1.14 + +require ( + github.com/aws/aws-sdk-go v1.29.23 + github.com/stretchr/testify v1.5.1 + golang.org/x/time v0.0.0-20191024005414-555d28b269f0 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..2b44781 --- /dev/null +++ b/go.sum @@ -0,0 +1,23 @@ +github.com/aws/aws-sdk-go v1.29.23 h1:wtiGLOzxAP755OfuVTDIy/NbUIYEDxbIbBEDfNhUpeU= +github.com/aws/aws-sdk-go v1.29.23/go.mod h1:1KvfttTE3SPKMpo8g2c6jL3ZKfXtFvKscTgahTma5Xg= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= +github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM= +github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= +github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/time v0.0.0-20191024005414-555d28b269f0 h1:/5xXl8Y5W96D+TtHSlonuFqGHIWVuyCkGJLwGh9JJFs= +golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= From 4fc15dd7855875ec4b5102302bd318e602049b80 Mon Sep 17 00:00:00 2001 From: David Gamba Date: Fri, 13 Mar 2020 00:16:25 -0600 Subject: [PATCH 2/3] Core: Add config.NewSession method This will enable separation of session creation from new resource instantiation. It also allows callers with custom session needs to bring their own session. --- config/config.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/config/config.go b/config/config.go index 3afb88c..d7ca5a7 100644 --- a/config/config.go +++ b/config/config.go @@ -1,8 +1,11 @@ package config import ( + "fmt" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/credentials" + "github.com/aws/aws-sdk-go/aws/ec2metadata" "github.com/aws/aws-sdk-go/aws/session" "github.com/evalphobia/aws-sdk-go-wrapper/private/pointers" @@ -10,6 +13,30 @@ import ( const defaultRegion = "us-east-1" +// NewSession - Creates a new AWS session given a profile to load from your local creds and a region. +// If region is empty, will get the region from the instance metadata. +func NewSession(profile, region string) (*session.Session, error) { + if region == "" { + svc := ec2metadata.New(session.New()) + doc, err := svc.GetInstanceIdentityDocument() + if err != nil { + return nil, fmt.Errorf("Failed to stablish AWS session: %w", err) + } + if len(doc.AvailabilityZone) > 0 { + region = doc.AvailabilityZone[:len(doc.AvailabilityZone)-1] + } + } + sess, err := session.NewSessionWithOptions(session.Options{ + SharedConfigState: session.SharedConfigEnable, + Config: aws.Config{Region: aws.String(region)}, + Profile: profile, + }) + if err != nil { + return sess, fmt.Errorf("Failed to stablish AWS session: %w", err) + } + return sess, nil +} + // Config has AWS settings. type Config struct { AccessKey string From dbaa8f59428fe99bb07701320641a2a786abfd3b Mon Sep 17 00:00:00 2001 From: David Gamba Date: Fri, 13 Mar 2020 00:19:21 -0600 Subject: [PATCH 3/3] SQS: Allow creating a SQS instance with a custom session --- sqs/client.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/sqs/client.go b/sqs/client.go index 97a72c5..9a245fe 100644 --- a/sqs/client.go +++ b/sqs/client.go @@ -6,6 +6,7 @@ import ( "strings" "sync" + "github.com/aws/aws-sdk-go/aws/session" SDK "github.com/aws/aws-sdk-go/service/sqs" "github.com/evalphobia/aws-sdk-go-wrapper/config" @@ -45,6 +46,15 @@ func New(conf config.Config) (*SQS, error) { return svc, nil } +func NewFromSession(sess *session.Session) (*SQS, error) { + svc := &SQS{ + client: SDK.New(sess), + logger: log.DefaultLogger, + queues: make(map[string]*Queue), + } + return svc, nil +} + // SetLogger sets logger. func (svc *SQS) SetLogger(logger log.Logger) { svc.logger = logger