From d5f955e9486aa43e92b3e00c15c9f1ca3b5cf569 Mon Sep 17 00:00:00 2001 From: Mark Wolfe Date: Tue, 15 Dec 2015 07:59:51 +1100 Subject: [PATCH] Added wait routine for table creation. --- Makefile | 4 ++-- ds.go | 48 ++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 46 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 10236be..a2c7202 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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 diff --git a/ds.go b/ds.go index 92a9454..aae46ec 100644 --- a/ds.go +++ b/ds.go @@ -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" @@ -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 ( @@ -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() { @@ -88,7 +91,8 @@ func Setup() (err error) { fmt.Printf("res = %+v\n", res) - // TODO wait for table to be ACTIVE + err = waitForTable() + return } @@ -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 + } + } + +}