Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Elio Capelati Jr eliocapelati
file 215 lines (180 sloc) 5.14 kb
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 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
package dynamodb

import (
"bytes"
"encoding/json"
"github.com/bmizerany/aws4"
"io/ioutil"
"log"
"net/http"
"net/http/httputil"
"time"
)

type Region struct {
name string
endpoint string
}

func (r *Region) url() string {
return "https://" + r.endpoint
}

var (
USEast1 *Region = &Region{"us-east-1", "dynamodb.us-east-1.amazonaws.com"}
USWest1 *Region = &Region{"us-west-1", "dynamodb.us-west-1.amazonaws.com"}
USWest2 *Region = &Region{"us-west-2", "dynamodb.us-west-2.amazonaws.com"}
EUWest1 *Region = &Region{"eu-west-1", "dynamodb.eu-west-1.amazonaws.com"}
APNorthEast1 *Region = &Region{"ap-northeast-1", "dynamodb.ap-northeast-1.amazonaws.com"}
APSouthEast1 *Region = &Region{"ap-southeast-1", "dynamodb.ap-southeast-1.amazonaws.com"}
APSouthEast2 *Region = &Region{"ap-southeast-2", "dynamodb.ap-southeast-2.amazonaws.com"}
SAEast1 *Region = &Region{"sa-east-1", "dynamodb.sa-east-1.amazonaws.com"}
)

type Table struct {
name string
region *Region
keys *aws4.Keys
service *aws4.Service
debugMode bool // causes requests to be dumped
}

func NewTable(name string, region *Region, awsAccessKeyId string,
awsSecretAccessKey string, debugMode bool) *Table {

k := &aws4.Keys{AccessKey: awsAccessKeyId, SecretKey: awsSecretAccessKey}
s := &aws4.Service{Name: "dynamodb", Region: region.name}
return &Table{name, region, k, s, debugMode}
}

func (t *Table) DeleteItem(key interface{}) (map[string]interface{}, float64, error) {
k, err := NewField(key)
if err != nil {
return nil, 0, err
}

r := new(DeleteItemRequest)
r.TableName = t.name
r.Key = Key{HashKeyElement: k}
r.ReturnValues = "ALL_OLD"

rawResp, err := t.doDynamoRequest("DeleteItem", r)
if err != nil {
return nil, 0, err
}

type DeleteItemResponse struct {
Attributes map[string]Field
ConsumedCapacityUnits float64
}

resp := new(DeleteItemResponse)
err = json.Unmarshal(rawResp, resp)
if err != nil {
return nil, 0, err
}
return fieldMapToValues(resp.Attributes), resp.ConsumedCapacityUnits, nil
}

func (t *Table) UpdateItem(key interface{}, item map[string]interface{}) (map[string]interface{}, float64, error) {
k, err := NewField(key)
if err != nil {
return nil, 0, err
}
attrs, err := valuesToAttributeMap(item)
if err != nil {
return nil, 0, err
}

r := new(UpdateItemRequest)
r.TableName = t.name
r.Key = Key{HashKeyElement: k}
r.AttributeUpdates = attrs
r.ReturnValues = "UPDATED_OLD"

rawResp, err := t.doDynamoRequest("UpdateItem", r)
if err != nil {
return nil, 0, err
}

type UpdateItemResponse struct {
Attributes map[string]Field
ConsumedCapacityUnits float64
}

resp := new(UpdateItemResponse)
err = json.Unmarshal(rawResp, resp)
if err != nil {
return nil, 0, err
}
return fieldMapToValues(resp.Attributes), resp.ConsumedCapacityUnits, nil
}

func (t *Table) Query(key interface{}, consistent bool) ([]map[string]interface{}, error) {
k, err := NewField(key)
if err != nil {
return nil, err
}

r := new(QueryRequest)
r.TableName = t.name
r.HashKeyValue = k
r.ConsistentRead = consistent

rawResp, err := t.doDynamoRequest("Query", r)
if err != nil {
return nil, err
}
resp := new(QueryResponse)
err = json.Unmarshal(rawResp, &resp)
if err != nil {
return nil, err
}

items := make([]map[string]interface{}, len(resp.Items))
for i, item := range resp.Items {
items[i] = item.Map()
}
return items, nil
}

func (t *Table) GetItem(key interface{}, consistent bool) (map[string]interface{}, error) {
k, err := NewField(key)
if err != nil {
return nil, err
}

r := new(GetItemRequest)
r.TableName = t.name
r.Key = Key{HashKeyElement: k}
r.ConsistentRead = consistent

rawResp, err := t.doDynamoRequest("GetItem", r)
if err != nil {
return nil, err
}
resp := new(GetItemResponse)
err = json.Unmarshal(rawResp, &resp)
if err != nil {
return nil, err
}

return resp.Item.Map(), nil
}

func (t *Table) doDynamoRequest(operation string, body interface{}) ([]byte, error) {
var b bytes.Buffer
if err := json.NewEncoder(&b).Encode(body); err != nil {
return nil, err
}

req, err := http.NewRequest("POST", t.region.url(), &b)
if err != nil {
return nil, err
}

req.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
req.Header.Set("X-Amz-Target", "DynamoDB_20111205."+operation)
req.Header.Set("Content-Type", "application/x-amz-json-1.0")
req.Header.Set("Connection", "Keep-Alive")

err = t.service.Sign(t.keys, req)
if err != nil {
return nil, err
}

if t.debugMode {
out, err := httputil.DumpRequestOut(req, true)
if err != nil {
return nil, err
}
log.Println(string(out))
}

resp, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}

defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
return respBody, RequestError{Status: resp.Status, Message: string(respBody)}
}

return respBody, err
}

type RequestError struct {
Status string
Message string
}

func (r RequestError) Error() string {
return "Status: " + r.Status + ", Message: " + r.Message
}
Something went wrong with that request. Please try again.