Skip to content

Commit e03df33

Browse files
authored
Implement ScanLocks function to support reporting (#60)
1 parent 2eb7181 commit e03df33

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

lock/lock.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/aws/aws-sdk-go/aws/awserr"
99
"github.com/aws/aws-sdk-go/aws/session"
1010
"github.com/aws/aws-sdk-go/service/dynamodb"
11+
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
1112
"github.com/gruntwork-io/go-commons/errors"
1213
"github.com/gruntwork-io/go-commons/retry"
1314
"github.com/sirupsen/logrus"
@@ -314,6 +315,51 @@ func GetLockStatus(options *Options) (*dynamodb.GetItemOutput, error) {
314315
return item, nil
315316
}
316317

318+
type Lock struct {
319+
// In the DynamoDB lock table, "LockID" will be the key and the deployment URL will be the value
320+
ID string `json:"LockID"`
321+
}
322+
323+
// ScanLocks will perform a scan operation on the indicated DynamoDB table. This operation is useful
324+
// in certain cases, for example when we want to generate a report of all currently active ref arch
325+
// deployments tracked in our lock table. It returns a slice of strings representing a list of lock
326+
// IDs on the table that are currently held
327+
func ScanLocks(options *Options) ([]string, error) {
328+
client, err := getDynamoDBClient(options)
329+
if err != nil {
330+
options.Logger.Errorf("Error authenticating to AWS: %s\n", err)
331+
return nil, err
332+
}
333+
334+
scanParams := &dynamodb.ScanInput{
335+
// Use a consistent read for our scan operation. Though this is more expensive,
336+
// we do this because we're using our results for report output, so we want to
337+
// see the latest consistent status
338+
ConsistentRead: aws.Bool(true),
339+
TableName: aws.String(options.LockTable),
340+
}
341+
342+
result, err := client.Scan(scanParams)
343+
if err != nil {
344+
options.Logger.Errorf("Error scanning Lock Table: %s\n", err)
345+
}
346+
347+
// Create a slice to hold the locks we fetch from DynamoDB
348+
retrievedLocks := make([]string, len(result.Items))
349+
350+
//Unmarshal the returned items into strings for easy printing
351+
for _, heldLock := range result.Items {
352+
lock := Lock{}
353+
err := dynamodbattribute.UnmarshalMap(heldLock, &lock)
354+
if err != nil {
355+
options.Logger.Errorf("Error unmarshalling deployment from DynamoDB: %s", err)
356+
}
357+
retrievedLocks = append(retrievedLocks, lock.ID)
358+
}
359+
360+
return retrievedLocks, nil
361+
}
362+
317363
func getDynamoDBClient(options *Options) (*dynamodb.DynamoDB, error) {
318364
if options.AwsSession == nil {
319365
return NewDynamoDb(options.AwsRegion)

0 commit comments

Comments
 (0)