|
8 | 8 | "github.com/aws/aws-sdk-go/aws/awserr"
|
9 | 9 | "github.com/aws/aws-sdk-go/aws/session"
|
10 | 10 | "github.com/aws/aws-sdk-go/service/dynamodb"
|
| 11 | + "github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute" |
11 | 12 | "github.com/gruntwork-io/go-commons/errors"
|
12 | 13 | "github.com/gruntwork-io/go-commons/retry"
|
13 | 14 | "github.com/sirupsen/logrus"
|
@@ -314,6 +315,51 @@ func GetLockStatus(options *Options) (*dynamodb.GetItemOutput, error) {
|
314 | 315 | return item, nil
|
315 | 316 | }
|
316 | 317 |
|
| 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 | + |
317 | 363 | func getDynamoDBClient(options *Options) (*dynamodb.DynamoDB, error) {
|
318 | 364 | if options.AwsSession == nil {
|
319 | 365 | return NewDynamoDb(options.AwsRegion)
|
|
0 commit comments