-
Notifications
You must be signed in to change notification settings - Fork 0
/
credentialmodel_dynamo.go
128 lines (113 loc) · 3.5 KB
/
credentialmodel_dynamo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// dynamodDB credential data model
package credentialstore
import (
"log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/dynamodb"
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
"github.com/satori/go.uuid"
)
type CredentialRecord struct {
credentialName string
credentialType string
credentialData string
}
// using dynamodbiface so mocking can be used for unit testing
type MyDynamo struct {
Db dynamodbiface.DynamoDBAPI
}
var Dyna *MyDynamo
var describeTableOutput *dynamodb.DescribeTableOutput = nil
// configure dynamodb connection
func ConfigureDynamoDB() error {
Dyna = new(MyDynamo)
awsSession, err := session.NewSession(&aws.Config{Region: aws.String(GetConfig().region)})
if err != nil {
return err
}
var svc *dynamodb.DynamoDB = dynamodb.New(awsSession)
Dyna.Db = dynamodbiface.DynamoDBAPI(svc)
return nil
}
// initialize dynamoDB with schema
func InitModel() error {
model := &dynamodb.CreateTableInput{
AttributeDefinitions: []*dynamodb.AttributeDefinition {
{
AttributeName: aws.String("credentialId"),
AttributeType: aws.String("S"),
},
},
KeySchema: []*dynamodb.KeySchemaElement {
{
AttributeName: aws.String("credentialId"),
KeyType: aws.String("HASH"),
},
},
ProvisionedThroughput: &dynamodb.ProvisionedThroughput {
ReadCapacityUnits: aws.Int64(5),
WriteCapacityUnits: aws.Int64(5),
},
TableName: aws.String("cred"),
}
// see if table exists
if nil == describeTableOutput {
describeTableOutput, err := Dyna.Db.DescribeTable(&dynamodb.DescribeTableInput{TableName: model.TableName})
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case dynamodb.ErrCodeResourceNotFoundException:
// table does not exist, create
result, err := Dyna.Db.CreateTable(model)
if err != nil {
log.Print(err.Error())
return err
}
log.Print(result)
default:
log.Print(err.Error())
}
}
} else {
log.Print(describeTableOutput)
}
}
return nil
}
// create a new credential record in the DB
// returns the new record ID
func CreateCred(cred CredentialRecord) (string, error) {
err := InitModel()
var newId string
if nil != err {
return "", err
}
newId = uuid.NewV4().String()
newCred := &dynamodb.PutItemInput{
Item: map[string]*dynamodb.AttributeValue{
"credentialID": {
S: aws.String(newId),
},
"credentialData": {
S: aws.String(cred.credentialData),
},
"name": {
S: aws.String(cred.credentialName),
},
"credType": {
S: aws.String(cred.credentialType),
},
},
ReturnConsumedCapacity: aws.String("TOTAL"),
TableName: aws.String("cred"),
}
result, err := Dyna.Db.PutItem(newCred)
if err != nil {
log.Print(err.Error())
} else {
log.Print(result)
}
return newId, err
}