Support SQS ChainProvider by default.

Unless the credpath query param is provided, the SQS credidentails
will be automatically chosen from one of the following:

- ~/.aws/credidentials
- Environment variables
- EC2 Role
This commit is contained in:
tidwall 2019-03-11 10:49:25 -07:00
parent 3e64497a51
commit ec57aaee1a
2 changed files with 28 additions and 29 deletions

View File

@ -95,6 +95,7 @@ type Endpoint struct {
CredPath string CredPath string
CredProfile string CredProfile string
QueueName string QueueName string
CreateQueue bool
} }
NATS struct { NATS struct {
Host string Host string
@ -502,10 +503,15 @@ func parseEndpoint(s string) (Endpoint, error) {
endpoint.SQS.CredPath = val[0] endpoint.SQS.CredPath = val[0]
case "credprofile": case "credprofile":
endpoint.SQS.CredProfile = val[0] endpoint.SQS.CredProfile = val[0]
case "createqueue":
switch strings.ToLower(val[0]) {
case "0", "false":
default:
endpoint.SQS.CreateQueue = true
}
} }
} }
} }
// Throw error if we not provide any queue name // Throw error if we not provide any queue name
if endpoint.SQS.QueueName == "" { if endpoint.SQS.QueueName == "" {
return endpoint, errors.New("missing SQS queue name") return endpoint, errors.New("missing SQS queue name")

View File

@ -65,30 +65,22 @@ func (conn *SQSConn) Send(msg string) error {
conn.t = time.Now() conn.t = time.Now()
if conn.svc == nil && conn.session == nil { if conn.svc == nil && conn.session == nil {
var creds *credentials.Credentials
credPath := conn.ep.SQS.CredPath credPath := conn.ep.SQS.CredPath
if credPath != "" {
credProfile := conn.ep.SQS.CredProfile credProfile := conn.ep.SQS.CredProfile
var sess *session.Session if credProfile == "" {
if credPath != "" && credProfile != "" { credProfile = "default"
sess = session.Must(session.NewSession(&aws.Config{
Region: aws.String(conn.ep.SQS.Region),
Credentials: credentials.NewSharedCredentials(credPath, credProfile),
MaxRetries: aws.Int(5),
}))
} else if credPath != "" {
sess = session.Must(session.NewSession(&aws.Config{
Region: aws.String(conn.ep.SQS.Region),
Credentials: credentials.NewSharedCredentials(credPath, "default"),
MaxRetries: aws.Int(5),
}))
} else {
sess = session.Must(session.NewSession(&aws.Config{
Region: aws.String(conn.ep.SQS.Region),
MaxRetries: aws.Int(5),
}))
} }
// Create a SQS service client. creds = credentials.NewSharedCredentials(credPath, credProfile)
}
sess := session.Must(session.NewSession(&aws.Config{
Region: aws.String(conn.ep.SQS.Region),
Credentials: creds,
MaxRetries: aws.Int(5),
}))
svc := sqs.New(sess) svc := sqs.New(sess)
if conn.ep.SQS.CreateQueue {
svc.CreateQueue(&sqs.CreateQueueInput{ svc.CreateQueue(&sqs.CreateQueueInput{
QueueName: aws.String(conn.ep.SQS.QueueName), QueueName: aws.String(conn.ep.SQS.QueueName),
Attributes: map[string]*string{ Attributes: map[string]*string{
@ -96,6 +88,7 @@ func (conn *SQSConn) Send(msg string) error {
"MessageRetentionPeriod": aws.String("86400"), "MessageRetentionPeriod": aws.String("86400"),
}, },
}) })
}
conn.session = sess conn.session = sess
conn.svc = svc conn.svc = svc
} }