Skip to content

Commit

Permalink
fix:get aws leases in order (#430)
Browse files Browse the repository at this point in the history
* fix:get aws leases in order

* Changelog updated
  • Loading branch information
anushaakkiraju authored Dec 22, 2022
1 parent 25a4530 commit 7a22c7f
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
## next

- Add new secondary index 'PrincipalIdLastModifiedOn' for Lease table with range key as LastModifiedOn to get the records sort by last-modified
- Update pkg/data/leases.go queryLeases method to use new IndexName PrincipalIdLastModifiedOn instead of existing IndexName PrincipalId in to get leases in order

## v0.33.9

- Upgrade to Go version 1.17
- Upgrade Ubuntu version on Azure DevOps Agent
- Fix Go dependency errors in pipeline
Expand Down
15 changes: 15 additions & 0 deletions modules/dynamodb.tf
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,15 @@ resource "aws_dynamodb_table" "leases" {
write_capacity = var.leases_table_wcu
}

global_secondary_index {
name = "PrincipalIdLastModifiedOn"
hash_key = "PrincipalId"
range_key = "LastModifiedOn"
projection_type = "ALL"
read_capacity = var.leases_table_rcu
write_capacity = var.leases_table_wcu
}

# AWS Account ID
attribute {
name = "AccountId"
Expand Down Expand Up @@ -114,6 +123,12 @@ resource "aws_dynamodb_table" "leases" {
type = "S"
}

# Last ModifiedOn
attribute {
name = "LastModifiedOn"
type = "N"
}

tags = var.global_tags
/*
Other attributes:
Expand Down
6 changes: 4 additions & 2 deletions pkg/data/leases.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package data

import (
"strings"

"github.com/Optum/dce/pkg/errors"
"github.com/Optum/dce/pkg/lease"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
"github.com/aws/aws-sdk-go/service/dynamodb/expression"
"strings"
)

// queryLeases for doing a query against dynamodb
Expand Down Expand Up @@ -36,6 +37,7 @@ func (a *Lease) queryLeases(query *lease.Lease, keyName string, index string) (*
FilterExpression: expr.Filter(),
ExpressionAttributeNames: expr.Names(),
ExpressionAttributeValues: expr.Values(),
ScanIndexForward: aws.Bool(false),
}

queryInput.SetLimit(*query.Limit)
Expand Down Expand Up @@ -125,7 +127,7 @@ func (a *Lease) List(query *lease.Lease) (*lease.Leases, error) {
if query.ID != nil {
outputs, err = a.queryLeases(query, "Id", "LeaseId")
} else if query.PrincipalID != nil {
outputs, err = a.queryLeases(query, "PrincipalId", "PrincipalId")
outputs, err = a.queryLeases(query, "PrincipalId", "PrincipalIdLastModifiedOn")
} else if query.Status != nil {
outputs, err = a.queryLeases(query, "LeaseStatus", "LeaseStatus")
} else {
Expand Down

0 comments on commit 7a22c7f

Please sign in to comment.