Skip to content

Commit

Permalink
Added wait routine for table creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfeidau committed Dec 14, 2015
1 parent e9271a6 commit d5f955e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ VERSION=1.0.0
GO15VENDOREXPERIMENT := 1

vendor:
godep save -t -v ./...
godep save -d -t

build:
rm -rf build && mkdir build
Expand All @@ -22,4 +22,4 @@ release: build
tar -zcf release/$(NAME)_$(VERSION)_windows_$(ARCH).tgz -C build/Windows $(NAME).exe
gh-release create versent/$(NAME) $(VERSION) $(shell git rev-parse --abbrev-ref HEAD)

.PHONY: build test release
.PHONY: vendor build test release
48 changes: 44 additions & 4 deletions ds.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/base64"
"errors"
"fmt"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
Expand All @@ -15,11 +16,10 @@ const (
// Table the name of the dynamodb table
Table = "credential-store"

// Region the AWS region dynamodb table
Region = "us-west-2"

// KmsKey default KMS key alias name
KmsKey = "alias/credstash"

tableCreateTimeout = 30 * time.Second
)

var (
Expand All @@ -30,6 +30,9 @@ var (

// ErrHmacValidationFailed returned when the hmac signature validation fails
ErrHmacValidationFailed = errors.New("Secret HMAC validation failed")

// ErrTimeout timeout occured waiting for dynamodb table to create
ErrTimeout = errors.New("Timed out waiting for dynamodb table to become active")
)

func init() {
Expand Down Expand Up @@ -88,7 +91,8 @@ func Setup() (err error) {

fmt.Printf("res = %+v\n", res)

// TODO wait for table to be ACTIVE
err = waitForTable()

return
}

Expand Down Expand Up @@ -301,3 +305,39 @@ func decryptCredential(cred *Credential) (*DecryptedCredential, error) {

return &DecryptedCredential{Credential: cred, Secret: plainText}, nil
}

func waitForTable() error {

timeout := make(chan bool, 1)
go func() {
time.Sleep(tableCreateTimeout)
timeout <- true
}()

ticker := time.NewTicker(1 * time.Second)

defer ticker.Stop()

for {
select {
case <-ticker.C:
// a read from ch has occurred
res, err := dynamoSvc.DescribeTable(&dynamodb.DescribeTableInput{
TableName: aws.String(Table),
})

if err != nil {
return err
}

if *res.Table.TableStatus == "ACTIVE" {
return nil
}

case <-timeout:
// polling for table status has taken more than the timeout
return ErrTimeout
}
}

}

0 comments on commit d5f955e

Please sign in to comment.