summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/goamz/goamz/dynamodb/stream.go
blob: 57f3a145f2cfd13b0c40efa93979d7d5a563889b (plain)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package dynamodb

import (
	"encoding/json"
	"errors"
	"fmt"
	"reflect"

	simplejson "github.com/bitly/go-simplejson"
)

type Stream struct {
	Server *Server
	Arn    string
}

type StreamListItemT struct {
	StreamArn   string
	StreamLabel string
	TableName   string
}

type SequenceNumberRangeT struct {
	EndingSequenceNumber   string
	StartingSequenceNumber string
}

type ShardT struct {
	ParentShardId       string
	SequenceNumberRange SequenceNumberRangeT
	ShardId             string
}

type StreamDescriptionT struct {
	CreationDateTime     float64
	KeySchema            []KeySchemaT
	LastEvaluatedShardId string
	Shards               []ShardT
	StreamArn            string
	StreamLabel          string
	StreamStatus         string
	StreamViewType       string
	TableName            string
}

type RecordT struct {
	AwsRegion    string
	EventID      string
	EventName    string
	EventSource  string
	EventVersion string
	StreamRecord *StreamRecordT
}

type StreamRecordT struct {
	Keys           map[string]*Attribute
	NewImage       map[string]*Attribute
	OldImage       map[string]*Attribute
	SequenceNumber string
	StreamViewType string
	SizeBytes      int64
}

type listStreamsResponse struct {
	Streams []StreamListItemT
}

type describeStreamResponse struct {
	StreamDescription StreamDescriptionT
}

var ErrNoRecords = errors.New("No records")

func (s *Server) ListStreams(startArn string) ([]StreamListItemT, error) {
	return s.LimitedListTableStreams("", startArn, 0)
}

func (s *Server) LimitedListStreams(startArn string, limit int64) ([]StreamListItemT, error) {
	return s.LimitedListTableStreams("", startArn, limit)
}

func (s *Server) ListTableStreams(table, startArn string) ([]StreamListItemT, error) {
	return s.LimitedListTableStreams(table, startArn, 0)
}

func (s *Server) LimitedListTableStreams(table, startArn string, limit int64) ([]StreamListItemT, error) {
	query := NewEmptyQuery()

	if len(table) != 0 {
		query.addTableByName(table)
	}

	if len(startArn) != 0 {
		query.AddExclusiveStartStreamArn(startArn)
	}

	if limit > 0 {
		query.AddLimit(limit)
	}

	jsonResponse, err := s.queryServer(streamsTarget("ListStreams"), query)
	if err != nil {
		return nil, err
	}

	var r listStreamsResponse
	err = json.Unmarshal(jsonResponse, &r)
	if err != nil {
		return nil, err
	}

	return r.Streams, nil
}

func (s *Server) DescribeStream(arn, startShardId string) (*StreamDescriptionT, error) {
	return s.LimitedDescribeStream(arn, startShardId, 0)
}

func (s *Server) LimitedDescribeStream(arn, startShardId string, limit int64) (*StreamDescriptionT, error) {
	query := NewEmptyQuery()
	query.AddStreamArn(arn)

	if len(startShardId) != 0 {
		query.AddExclusiveStartShardId(startShardId)
	}

	if limit > 0 {
		query.AddLimit(limit)
	}

	jsonResponse, err := s.queryServer(streamsTarget("DescribeStream"), query)
	if err != nil {
		return nil, err
	}

	var r describeStreamResponse
	err = json.Unmarshal(jsonResponse, &r)
	if err != nil {
		return nil, err
	}

	return &r.StreamDescription, nil
}

func (s *Server) NewStream(streamArn string) *Stream {
	return &Stream{s, streamArn}
}

func (s *Stream) DescribeStream(startShardId string) (*StreamDescriptionT, error) {
	return s.Server.DescribeStream(s.Arn, startShardId)
}

func (s *Stream) LimitedDescribeStream(startShardId string, limit int64) (*StreamDescriptionT, error) {
	return s.Server.LimitedDescribeStream(s.Arn, startShardId, limit)
}

func (s *Server) GetShardIterator(streamArn, shardId, shardIteratorType, sequenceNumber string) (string, error) {
	query := NewEmptyQuery()
	query.AddStreamArn(streamArn)
	query.AddShardId(shardId)
	query.AddShardIteratorType(shardIteratorType)

	if len(sequenceNumber) != 0 {
		query.AddSequenceNumber(sequenceNumber)
	}

	jsonResponse, err := s.queryServer(streamsTarget("GetShardIterator"), query)

	if err != nil {
		return "unknown", err
	}

	json, err := simplejson.NewJson(jsonResponse)

	if err != nil {
		return "unknown", err
	}

	return json.Get("ShardIterator").MustString(), nil
}

func (s *Stream) GetShardIterator(shardId, shardIteratorType, sequenceNumber string) (string, error) {
	return s.Server.GetShardIterator(s.Arn, shardId, shardIteratorType, sequenceNumber)
}

func (s *Server) GetRecords(shardIterator string) (string, []*RecordT, error) {
	return s.LimitedGetRecords(shardIterator, 0)
}

func (s *Server) LimitedGetRecords(shardIterator string, limit int64) (string, []*RecordT, error) {
	query := NewEmptyQuery()
	query.AddShardIterator(shardIterator)

	if limit > 0 {
		query.AddLimit(limit)
	}

	jsonResponse, err := s.queryServer(streamsTarget("GetRecords"), query)
	if err != nil {
		return "", nil, err
	}

	jsonParsed, err := simplejson.NewJson(jsonResponse)
	if err != nil {
		return "", nil, err
	}

	nextShardIt := ""
	nextShardItJson, ok := jsonParsed.CheckGet("NextShardIterator")
	if ok {
		nextShardIt, err = nextShardItJson.String()
		if err != nil {
			message := fmt.Sprintf("Unexpected response %s", jsonResponse)
			return "", nil, errors.New(message)
		}
	}

	recordsJson, ok := jsonParsed.CheckGet("Records")
	if !ok {
		return nextShardIt, nil, ErrNoRecords
	}

	recordsArray, err := recordsJson.Array()
	if err != nil {
		message := fmt.Sprintf("Unexpected response %s", jsonResponse)
		return nextShardIt, nil, errors.New(message)
	}

	var records []*RecordT
	for _, record := range recordsArray {
		if recordMap, ok := record.(map[string]interface{}); ok {
			r := parseRecord(recordMap)
			records = append(records, r)
		}
	}

	return nextShardIt, records, nil
}

func (s *Stream) GetRecords(shardIterator string) (string, []*RecordT, error) {
	return s.Server.GetRecords(shardIterator)
}

func (s *Stream) LimitedGetRecords(shardIterator string, limit int64) (string, []*RecordT, error) {
	return s.Server.LimitedGetRecords(shardIterator, limit)
}

func parseRecord(r map[string]interface{}) *RecordT {
	record := RecordT{}
	rValue := reflect.ValueOf(&record)

	keys := []string{"awsRegion", "eventID", "eventName", "eventSource", "eventVersion"}
	for i, key := range keys {
		if value, ok := r[key]; ok {
			if valueStr, ok := value.(string); ok {
				rValue.Elem().Field(i).SetString(valueStr)
			}
		}
	}

	if streamRecord, ok := r["dynamodb"]; ok {
		if streamRecordMap, ok := streamRecord.(map[string]interface{}); ok {
			record.StreamRecord = parseStreamRecord(streamRecordMap)
		}
	}

	return &record
}

func parseStreamRecord(s map[string]interface{}) *StreamRecordT {
	sr := StreamRecordT{}
	rValue := reflect.ValueOf(&sr)

	attrKeys := []string{"Keys", "NewImage", "OldImage"}
	numAttrKeys := len(attrKeys)
	for i, key := range attrKeys {
		if value, ok := s[key]; ok {
			if valueMap, ok := value.(map[string]interface{}); ok {
				attrs := parseAttributes(valueMap)
				rValue.Elem().Field(i).Set(reflect.ValueOf(attrs))
			}
		}
	}

	strKeys := []string{"SequenceNumber", "StreamViewType"}
	numStrKeys := len(strKeys)
	for i, key := range strKeys {
		if value, ok := s[key]; ok {
			if valueStr, ok := value.(string); ok {
				rValue.Elem().Field(i + numAttrKeys).SetString(valueStr)
			}
		}
	}

	intKeys := []string{"SizeBytes"}
	for i, key := range intKeys {
		if value, ok := s[key]; ok {
			if valueNumber, ok := value.(json.Number); ok {
				if valueInt, err := valueNumber.Int64(); err == nil {
					rValue.Elem().Field(i + numAttrKeys + numStrKeys).SetInt(valueInt)
				}
			}
		}
	}

	return &sr
}