Skip to content

Commit

Permalink
Merge pull request Optum#71 from Optum/feature/lease-migration
Browse files Browse the repository at this point in the history
Feature/lease migration
  • Loading branch information
nathanagood authored Oct 24, 2019
2 parents d48602a + 60bc6ce commit 1b5101d
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 12 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
- Simplified lease status model to include only two statuses: Inactive and Active.
- Changed check_budget to update_lease_status and added check for expiration date.
- Changed SQS and SNS notifications for lease status change to be triggered by lease status change in DB.
- Added defaults for leases; if ID isn't specified upon save in the DB a new one will be assigned, and if
the expiration date isn't defined the environment variable `DEFAULT_LEASE_LENGTH_IN_DAYS` will be used and
if that is not defined, a default of seven (7) days will be used.
- Added migration for the leases to all be set to Inactive if they're anything but Active.
- Lease added SNS topic updates principal policy
- Refactored lease API controller and methods to organize methods into files.

Expand Down
17 changes: 17 additions & 0 deletions pkg/common/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,23 @@ func RequireEnvInt(env string) int {
return intVal
}

// RequireEnvIntWithDefault returns an environment that is required to be an integer
func RequireEnvIntWithDefault(env string, defaultValue int) int {
val, ok := os.LookupEnv(env)

if !ok {
return defaultValue
}

intVal, err := strconv.Atoi(val)

if err != nil {
return defaultValue
}

return intVal
}

func RequireEnvStringSlice(env string, sep string) []string {
val := RequireEnv(env)
list := strings.Split(val, sep)
Expand Down
26 changes: 21 additions & 5 deletions pkg/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
"time"

guuid "github.com/google/uuid"

"github.com/Optum/Redbox/pkg/common"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
Expand All @@ -30,6 +32,8 @@ type DB struct {
AccountTableName string
// Name of the RedboxLease table
LeaseTableName string
// Default expiry time, in days, of the lease
DefaultLeaseLengthInDays int
// Use Consistend Reads when scanning or querying. When possbile.
ConsistendRead bool
}
Expand Down Expand Up @@ -364,6 +368,16 @@ func (db *DB) PutAccount(account RedboxAccount) error {
// the lease that was added
func (db *DB) PutLease(lease RedboxLease) (
*RedboxLease, error) {

// apply some reasonable DEFAULTS to the lease before saving it.
if len(lease.ID) == 0 {
lease.ID = guuid.New().String()
}

if lease.ExpiresOn == 0 {
lease.ExpiresOn = time.Now().AddDate(0, 0, db.DefaultLeaseLengthInDays).Unix()
}

item, err := dynamodbattribute.MarshalMap(lease)
if err != nil {
return nil, err
Expand Down Expand Up @@ -760,12 +774,13 @@ func unmarshalLease(dbResult map[string]*dynamodb.AttributeValue) (*RedboxLease,
//
// Elsewhere, you should generally use `db.NewFromEnv()`
//
func New(client *dynamodb.DynamoDB, accountTableName string, leaseTableName string) *DB {
func New(client *dynamodb.DynamoDB, accountTableName string, leaseTableName string, defaultLeaseLengthInDays int) *DB {
return &DB{
Client: client,
AccountTableName: accountTableName,
LeaseTableName: leaseTableName,
ConsistendRead: false,
Client: client,
AccountTableName: accountTableName,
LeaseTableName: leaseTableName,
DefaultLeaseLengthInDays: defaultLeaseLengthInDays,
ConsistendRead: false,
}
}

Expand All @@ -789,5 +804,6 @@ func NewFromEnv() (*DB, error) {
),
common.RequireEnv("ACCOUNT_DB"),
common.RequireEnv("LEASE_DB"),
common.RequireEnvIntWithDefault("DEFAULT_LEASE_LENGTH_IN_DAYS", 7),
), nil
}
17 changes: 16 additions & 1 deletion scripts/migrations/v0.18.0_db_add_leaseId/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"time"

"github.com/Optum/Redbox/pkg/common"
data "github.com/Optum/Redbox/pkg/db"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
Expand All @@ -41,6 +42,7 @@ type RedboxLeaseMod struct {
PrincipalID string `json:"PrincipalId"`
ID string `json:"Id"`
LeaseStatus string
ExpiresOn int64
CreatedOn int64
LastModifiedOn int64
LeaseStatusModifiedOn int64
Expand Down Expand Up @@ -72,6 +74,13 @@ func migrationV18(input *migrationV18Input) (int64, error) {
for _, item := range leases {
fmt.Printf("AccountId: %s\n", item.AccountID)
leaseID := guuid.New()
expiresOn := time.Date(2019, time.October, 27, 0, 0, 0, 0, time.Local).Unix()
// There are only two statuses for lease now--either Active or Inactive. If
// it's not Active, any other status is now considered Inactive.
updatedLeaseStatus := item.LeaseStatus
if updatedLeaseStatus != string(data.Active) {
updatedLeaseStatus = string(data.Inactive)
}
result, err := input.dynDB.UpdateItem(
&dynamodb.UpdateItemInput{
// Query in Lease Table
Expand All @@ -86,11 +95,17 @@ func migrationV18(input *migrationV18Input) (int64, error) {
},
},
// Set Id to a new unique id
UpdateExpression: aws.String("set Id=:id"),
UpdateExpression: aws.String("set Id=:id, ExpiresOn=:expiresOn, LeaseStatus=:leaseStatus"),
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
":id": {
N: aws.String(leaseID.String()),
},
":expiresOn": {
N: aws.String(string(expiresOn)),
},
":leaseStatus": {
N: aws.String(updatedLeaseStatus),
},
},
// Return the updated record
ReturnValues: aws.String("ALL_NEW"),
Expand Down
1 change: 1 addition & 0 deletions tests/acceptance/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func TestApi(t *testing.T) {
),
tfOut["dynamodb_table_account_name"].(string),
tfOut["redbox_lease_db_table_name"].(string),
7,
)

// Configure the usage service
Expand Down
15 changes: 9 additions & 6 deletions tests/acceptance/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func TestDb(t *testing.T) {
),
tfOut["dynamodb_table_account_name"].(string),
tfOut["redbox_lease_db_table_name"].(string),
7,
)
// Set consistent reads to improve testing without a bunch of sleeps
// for eventual consistency
Expand Down Expand Up @@ -528,12 +529,14 @@ func TestDb(t *testing.T) {
uuidThree := uuid.New().String()
uuidFour := uuid.New().String()

expiryDate := time.Now().AddDate(0, 0, 30).Unix()

// Create some leases in the DB
for _, lease := range []db.RedboxLease{
{ID: uuidOne, AccountID: "1", PrincipalID: "pid", LeaseStatus: db.Active, LeaseStatusReason: db.LeaseActive},
{ID: uuidTwo, AccountID: "2", PrincipalID: "pid", LeaseStatus: db.Inactive, LeaseStatusReason: db.LeaseExpired},
{ID: uuidThree, AccountID: "3", PrincipalID: "pid", LeaseStatus: db.Active, LeaseStatusReason: db.LeaseActive},
{ID: uuidFour, AccountID: "4", PrincipalID: "pid", LeaseStatus: db.Inactive, LeaseStatusReason: db.LeaseDestroyed},
{ID: uuidOne, AccountID: "1", PrincipalID: "pid", LeaseStatus: db.Active, LeaseStatusReason: db.LeaseActive, ExpiresOn: expiryDate},
{ID: uuidTwo, AccountID: "2", PrincipalID: "pid", LeaseStatus: db.Inactive, LeaseStatusReason: db.LeaseExpired, ExpiresOn: expiryDate},
{ID: uuidThree, AccountID: "3", PrincipalID: "pid", LeaseStatus: db.Active, LeaseStatusReason: db.LeaseActive, ExpiresOn: expiryDate},
{ID: uuidFour, AccountID: "4", PrincipalID: "pid", LeaseStatus: db.Inactive, LeaseStatusReason: db.LeaseDestroyed, ExpiresOn: expiryDate},
} {
_, err := dbSvc.PutLease(lease)
require.Nil(t, err)
Expand All @@ -543,8 +546,8 @@ func TestDb(t *testing.T) {
res, err := dbSvc.FindLeasesByStatus(db.Inactive)
require.Nil(t, err)
require.Equal(t, []*db.RedboxLease{
{ID: uuidTwo, AccountID: "2", PrincipalID: "pid", LeaseStatus: db.Inactive, LeaseStatusReason: db.LeaseExpired},
{ID: uuidFour, AccountID: "4", PrincipalID: "pid", LeaseStatus: db.Inactive, LeaseStatusReason: db.LeaseDestroyed},
{ID: uuidTwo, AccountID: "2", PrincipalID: "pid", LeaseStatus: db.Inactive, LeaseStatusReason: db.LeaseExpired, ExpiresOn: expiryDate},
{ID: uuidFour, AccountID: "4", PrincipalID: "pid", LeaseStatus: db.Inactive, LeaseStatusReason: db.LeaseDestroyed, ExpiresOn: expiryDate},
}, res)
})

Expand Down
1 change: 1 addition & 0 deletions tests/acceptance/provisioner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func TestProvisioner(t *testing.T) {
),
tfOut["dynamodb_table_account_name"].(string),
tfOut["redbox_lease_db_table_name"].(string),
7,
)

// Configure the Provisioner service
Expand Down

0 comments on commit 1b5101d

Please sign in to comment.