Skip to content

Commit

Permalink
Merge branch 'master' into jettlin-issue_101
Browse files Browse the repository at this point in the history
  • Loading branch information
jessica-christie committed Apr 19, 2018
2 parents 0f53a58 + ed05f31 commit 2c72eaa
Show file tree
Hide file tree
Showing 56 changed files with 18,158 additions and 448 deletions.
9 changes: 9 additions & 0 deletions metadata/synq_metadata.go
Expand Up @@ -42,4 +42,13 @@ type ImageData struct {
type AkkaXMLAsset struct {
MetaData
Images []ImageData `json:"images"`
Rights []VideoRights `json:"rights"`
}

// NOTE : This is probably not an all inclusive list of rights but reflects what we have samples for
type VideoRights struct {
ValidFrom string `json:"valid_from"`
ValidTo string `json:"valid_to"`
Unlimited bool `json:"unlimited"`
Devices []string `json:"devices"`
}
82 changes: 82 additions & 0 deletions synq_aws/dynamodb.go
@@ -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
}
85 changes: 85 additions & 0 deletions synq_aws/dynamodb_test.go
@@ -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)
}
92 changes: 0 additions & 92 deletions synq_aws/messages.go

This file was deleted.

0 comments on commit 2c72eaa

Please sign in to comment.