-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into jettlin-issue_101
- Loading branch information
Showing
56 changed files
with
18,158 additions
and
448 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package synq_aws | ||
|
||
import ( | ||
// "errors" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/dynamodb" | ||
) | ||
|
||
// TODO : Figure out how to test this. AWS does not make this really something they want us to test locally... | ||
func GenerateSession(region string) *session.Session { | ||
// Just generate a session and return it | ||
return session.Must(session.NewSession(&aws.Config{Region: aws.String(region)})) | ||
} | ||
|
||
// Writes output to the database, uses current time as the end time | ||
func LogResult(sess *session.Session, id string, response string) (int, error) { | ||
// create the service object from the session | ||
svc := dynamodb.New(sess) | ||
|
||
// create the item input object | ||
// NOTE : Cannot insert items that are not already present! | ||
input := &dynamodb.UpdateItemInput{ | ||
ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{ | ||
":r": { S: aws.String(response) }, | ||
":n": { S: aws.String(time.Now().String()) }, | ||
":id": { S: aws.String(id) }, | ||
}, | ||
Key: map[string]*dynamodb.AttributeValue{ | ||
"id": { S: aws.String(id) }, | ||
}, | ||
TableName: aws.String("message_logs"), | ||
ReturnValues: aws.String("ALL_NEW"), | ||
UpdateExpression: aws.String("set api_result = :r, end_time = :n"), | ||
ConditionExpression: aws.String("id = :id"), | ||
} | ||
|
||
// determine if there is an error | ||
_, err := svc.UpdateItem(input) | ||
if err != nil { | ||
return 400, err // return error | ||
} | ||
|
||
// return 200 OK response | ||
return 200, nil | ||
} | ||
|
||
// create a log line | ||
func CreateLog(sess *session.Session, worker string, token string, datatype string, data string) (string, error) { | ||
// generate the UUID and get the service | ||
id := uuid.New().String() | ||
svc := dynamodb.New(sess) | ||
|
||
// build the inupts | ||
input := &dynamodb.PutItemInput{ | ||
Item: map[string]*dynamodb.AttributeValue{ | ||
"id": { S: aws.String(id) }, | ||
"start_time": { S: aws.String(time.Now().String()) }, | ||
"end_time": { S: aws.String("in progress") }, | ||
"token": { S: aws.String(token) }, | ||
"datatype": { S: aws.String(datatype) }, | ||
"data": { S: aws.String(data) }, | ||
"worker": { S: aws.String(worker) }, | ||
"api_result": { S: aws.String("in progress") }, | ||
}, | ||
ReturnConsumedCapacity: aws.String("TOTAL"), | ||
TableName: aws.String("message_logs"), | ||
} | ||
|
||
// Insert the item into the db | ||
_, err := svc.PutItem(input) | ||
if err != nil { | ||
// if there was an error return the error | ||
return "ERROR", err | ||
} | ||
|
||
// return the id and success if no error | ||
return id, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package synq_aws | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"net/http" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/aws/request" | ||
"github.com/aws/aws-sdk-go/service/dynamodb" | ||
) | ||
|
||
func setupDB() *session.Session { | ||
os.Setenv("AWS_ACCESS_KEY_ID", "derf") | ||
os.Setenv("AWS_SECRET_ACCESS_KEY", "derf") | ||
|
||
sess := session.New(&aws.Config{Region: aws.String("us-east-2")}) | ||
sess.Handlers.Send.Clear() | ||
sess.Handlers.Send.PushFront(func(r *request.Request) { | ||
code := 403 | ||
resp, _ := ioutil.ReadFile("../sample/aws/error_messages.xml") | ||
|
||
switch p := r.Params.(type) { | ||
case *dynamodb.UpdateItemInput: | ||
if *p.Key["id"].S == "good" { | ||
code = 200 | ||
resp = []byte("{}") | ||
} | ||
case *dynamodb.PutItemInput: | ||
if *p.Item["worker"].S == "good" { | ||
code = 200 | ||
resp = []byte("{}") | ||
} | ||
} | ||
|
||
r.HTTPResponse = &http.Response{ | ||
StatusCode: code, | ||
Body: ioutil.NopCloser(bytes.NewReader(resp)), | ||
Header: http.Header{"X-Amzn-Requestid": []string{"123454232"}}, | ||
} | ||
}) | ||
|
||
return sess | ||
} | ||
|
||
func TestLogResultSuccess(t *testing.T) { | ||
assert := require.New(t) | ||
sess := setupDB() | ||
resp, err := LogResult(sess, "good", "200 OK") | ||
|
||
assert.Equal(resp, 200) | ||
assert.Nil(err) | ||
} | ||
|
||
func TestLogResultFailure(t *testing.T) { | ||
assert := require.New(t) | ||
sess := setupDB() | ||
resp, err := LogResult(sess, "bad", "200 OK") | ||
|
||
assert.Equal(resp, 400) | ||
assert.NotNil(err) | ||
} | ||
|
||
func TestCreateRowSuccess(t *testing.T) { | ||
assert := require.New(t) | ||
sess := setupDB() | ||
resp, err := CreateLog(sess, "good", "good", "asset", "something") | ||
|
||
assert.NotEqual(resp, "") | ||
assert.Nil(err) | ||
} | ||
|
||
func TestCreateRowFailure(t *testing.T) { | ||
assert := require.New(t) | ||
sess := setupDB() | ||
resp, err := CreateLog(sess, "bad", "good", "asset", "something") | ||
|
||
assert.Equal(resp, "ERROR") | ||
assert.NotNil(err) | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.