@@ -2,14 +2,15 @@ package lock
22
33import (
44 "fmt"
5+ "time"
6+
57 "github.com/aws/aws-sdk-go/aws"
68 "github.com/aws/aws-sdk-go/aws/awserr"
79 "github.com/aws/aws-sdk-go/aws/session"
810 "github.com/aws/aws-sdk-go/service/dynamodb"
911 "github.com/gruntwork-io/go-commons/errors"
1012 "github.com/gruntwork-io/go-commons/retry"
1113 "github.com/sirupsen/logrus"
12- "time"
1314)
1415
1516const (
@@ -40,6 +41,10 @@ type Options struct {
4041 SleepBetweenRetries time.Duration
4142 // The logger to use for the lock
4243 Logger * logrus.Logger
44+
45+ // Custom session to use to authenticate to AWS in the SDK. If nil, constructs the session based on the default
46+ // authentication chain in the SDK.
47+ AwsSession * session.Session
4348}
4449
4550type TimeoutExceeded struct {
@@ -79,23 +84,13 @@ func NewAuthenticatedSession(awsRegion string) (*session.Session, error) {
7984 return sess , nil
8085}
8186
82- // NewDynamoDb returns an authenticated client object for accessing DynamoDb
83- func NewDynamoDb (awsRegion string ) (* dynamodb.DynamoDB , error ) {
84- sess , err := NewAuthenticatedSession (awsRegion )
85- if err != nil {
86- return nil , err
87- }
88- dynamodbSvc := dynamodb .New (sess )
89- return dynamodbSvc , nil
90- }
91-
9287// AcquireLock will attempt to acquire a lock in DynamoDB table while taking the configuration options into account.
9388// We are using DynamoDB to create a table to help us track the lock status of different resources.
9489// The acquiring of a lock attempts to create a table. The intention is that we have 1 table per resource in a single region.
9590// This would allow the locking mechanism to flexibly decide if a resource is locked or not. For test cases where the AWS resource
9691// is multi-region, or global, the configuration of which regions to use should reflect that.
9792func AcquireLock (options * Options ) error {
98- client , err := NewDynamoDb (options . AwsRegion )
93+ client , err := getDynamoDBClient (options )
9994 if err != nil {
10095 options .Logger .Errorf ("Error authenticating to AWS: %s\n " , err )
10196 return err
@@ -156,7 +151,7 @@ func acquireLock(options *Options, client *dynamodb.DynamoDB) error {
156151// ReleaseLock will attempt to release the lock defined by the provided lock string in the configured lock table for the
157152// configured region
158153func ReleaseLock (options * Options ) error {
159- client , err := NewDynamoDb (options . AwsRegion )
154+ client , err := getDynamoDBClient (options )
160155 if err != nil {
161156 options .Logger .Errorf ("Error authenticating to AWS: %s\n " , err )
162157 return err
@@ -297,7 +292,7 @@ func lockTableExistsAndIsActive(tableName string, client *dynamodb.DynamoDB) (bo
297292// GetLockStatus attempts to acquire the lock and check if the expected item is there
298293// If there's the expected Item with the correct `LockString` value - then the status is `locked`, if the item is not there - then the status is `not locked`
299294func GetLockStatus (options * Options ) (* dynamodb.GetItemOutput , error ) {
300- client , err := NewDynamoDb (options . AwsRegion )
295+ client , err := getDynamoDBClient (options )
301296 if err != nil {
302297 options .Logger .Errorf ("Error authenticating to AWS: %s\n " , err )
303298 return nil , err
@@ -318,3 +313,20 @@ func GetLockStatus(options *Options) (*dynamodb.GetItemOutput, error) {
318313
319314 return item , nil
320315}
316+
317+ func getDynamoDBClient (options * Options ) (* dynamodb.DynamoDB , error ) {
318+ if options .AwsSession == nil {
319+ return NewDynamoDb (options .AwsRegion )
320+ }
321+ return dynamodb .New (options .AwsSession ), nil
322+ }
323+
324+ // NewDynamoDb returns an authenticated client object for accessing DynamoDb
325+ func NewDynamoDb (awsRegion string ) (* dynamodb.DynamoDB , error ) {
326+ sess , err := NewAuthenticatedSession (awsRegion )
327+ if err != nil {
328+ return nil , err
329+ }
330+ dynamodbSvc := dynamodb .New (sess )
331+ return dynamodbSvc , nil
332+ }
0 commit comments