Skip to content

Commit 7a22c7f

Browse files
fix:get aws leases in order (#430)
* fix:get aws leases in order * Changelog updated
1 parent 25a4530 commit 7a22c7f

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
## next
22

3+
- Add new secondary index 'PrincipalIdLastModifiedOn' for Lease table with range key as LastModifiedOn to get the records sort by last-modified
4+
- Update pkg/data/leases.go queryLeases method to use new IndexName PrincipalIdLastModifiedOn instead of existing IndexName PrincipalId in to get leases in order
5+
6+
## v0.33.9
7+
38
- Upgrade to Go version 1.17
49
- Upgrade Ubuntu version on Azure DevOps Agent
510
- Fix Go dependency errors in pipeline

modules/dynamodb.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ resource "aws_dynamodb_table" "leases" {
8787
write_capacity = var.leases_table_wcu
8888
}
8989

90+
global_secondary_index {
91+
name = "PrincipalIdLastModifiedOn"
92+
hash_key = "PrincipalId"
93+
range_key = "LastModifiedOn"
94+
projection_type = "ALL"
95+
read_capacity = var.leases_table_rcu
96+
write_capacity = var.leases_table_wcu
97+
}
98+
9099
# AWS Account ID
91100
attribute {
92101
name = "AccountId"
@@ -114,6 +123,12 @@ resource "aws_dynamodb_table" "leases" {
114123
type = "S"
115124
}
116125

126+
# Last ModifiedOn
127+
attribute {
128+
name = "LastModifiedOn"
129+
type = "N"
130+
}
131+
117132
tags = var.global_tags
118133
/*
119134
Other attributes:

pkg/data/leases.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package data
22

33
import (
4+
"strings"
5+
46
"github.com/Optum/dce/pkg/errors"
57
"github.com/Optum/dce/pkg/lease"
68
"github.com/aws/aws-sdk-go/aws"
79
"github.com/aws/aws-sdk-go/service/dynamodb"
810
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbattribute"
911
"github.com/aws/aws-sdk-go/service/dynamodb/expression"
10-
"strings"
1112
)
1213

1314
// queryLeases for doing a query against dynamodb
@@ -36,6 +37,7 @@ func (a *Lease) queryLeases(query *lease.Lease, keyName string, index string) (*
3637
FilterExpression: expr.Filter(),
3738
ExpressionAttributeNames: expr.Names(),
3839
ExpressionAttributeValues: expr.Values(),
40+
ScanIndexForward: aws.Bool(false),
3941
}
4042

4143
queryInput.SetLimit(*query.Limit)
@@ -125,7 +127,7 @@ func (a *Lease) List(query *lease.Lease) (*lease.Leases, error) {
125127
if query.ID != nil {
126128
outputs, err = a.queryLeases(query, "Id", "LeaseId")
127129
} else if query.PrincipalID != nil {
128-
outputs, err = a.queryLeases(query, "PrincipalId", "PrincipalId")
130+
outputs, err = a.queryLeases(query, "PrincipalId", "PrincipalIdLastModifiedOn")
129131
} else if query.Status != nil {
130132
outputs, err = a.queryLeases(query, "LeaseStatus", "LeaseStatus")
131133
} else {

0 commit comments

Comments
 (0)