-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
describetable.go
325 lines (286 loc) · 8.49 KB
/
describetable.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package dynamo
import (
"context"
"time"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
// Description contains information about a table.
type Description struct {
Name string
ARN string
Status Status
Created time.Time
// Attribute name of the hash key (a.k.a. partition key).
HashKey string
HashKeyType KeyType
// Attribute name of the range key (a.k.a. sort key) or blank if nonexistant.
RangeKey string
RangeKeyType KeyType
// Provisioned throughput for this table.
Throughput Throughput
// OnDemand is true if on-demand (pay per request) billing mode is enabled.
OnDemand bool
// The number of items of the table, updated every 6 hours.
Items int64
// The size of this table in bytes, updated every 6 hours.
Size int64
// Global secondary indexes.
GSI []Index
// Local secondary indexes.
LSI []Index
StreamEnabled bool
StreamView StreamView
LatestStreamARN string
LatestStreamLabel string
SSEDescription SSEDescription
}
func (d Description) Active() bool {
return d.Status == ActiveStatus
}
type Throughput struct {
// Read capacity units.
Read int64
// Write capacity units.
Write int64
// Time at which throughput was last increased for this table.
LastInc time.Time
// Time at which throughput was last decreased for this table.
LastDec time.Time
// The number of throughput decreases in this UTC calendar day.
DecsToday int64
}
type Index struct {
Name string
ARN string
Status Status
Backfilling bool // only for GSI
// Local is true when this index is a local secondary index, otherwise it is a global secondary index.
Local bool
// Attribute name of the hash key (a.k.a. partition key).
HashKey string
HashKeyType KeyType
// Attribute name of the range key (a.k.a. sort key) or blank if nonexistant.
RangeKey string
RangeKeyType KeyType
// The provisioned throughput for this index.
Throughput Throughput
Items int64
Size int64
ProjectionType IndexProjection
// Non-key attributes for this index's projection (if ProjectionType is IncludeProjection).
ProjectionAttribs []string
}
func newDescription(table *types.TableDescription) Description {
desc := Description{
Name: *table.TableName,
}
if table.TableArn != nil {
desc.ARN = *table.TableArn
}
if table.TableStatus != "" {
desc.Status = Status(table.TableStatus)
}
if table.CreationDateTime != nil {
desc.Created = *table.CreationDateTime
}
desc.HashKey, desc.RangeKey = schemaKeys(table.KeySchema)
desc.HashKeyType = lookupADType(table.AttributeDefinitions, desc.HashKey)
desc.RangeKeyType = lookupADType(table.AttributeDefinitions, desc.RangeKey)
if table.BillingModeSummary != nil && table.BillingModeSummary.BillingMode != "" {
desc.OnDemand = table.BillingModeSummary.BillingMode == types.BillingModePayPerRequest
}
if table.ProvisionedThroughput != nil {
desc.Throughput = newThroughput(table.ProvisionedThroughput)
}
if table.ItemCount != nil {
desc.Items = *table.ItemCount
}
if table.TableSizeBytes != nil {
desc.Size = *table.TableSizeBytes
}
for _, index := range table.GlobalSecondaryIndexes {
idx := Index{
Name: *index.IndexName,
ARN: *index.IndexArn,
Status: Status(index.IndexStatus),
Throughput: newThroughput(index.ProvisionedThroughput),
}
if index.Projection != nil && index.Projection.ProjectionType != "" {
idx.ProjectionType = IndexProjection(index.Projection.ProjectionType)
idx.ProjectionAttribs = index.Projection.NonKeyAttributes
}
if index.Backfilling != nil {
idx.Backfilling = *index.Backfilling
}
idx.HashKey, idx.RangeKey = schemaKeys(index.KeySchema)
idx.HashKeyType = lookupADType(table.AttributeDefinitions, idx.HashKey)
idx.RangeKeyType = lookupADType(table.AttributeDefinitions, idx.RangeKey)
if index.ItemCount != nil {
idx.Items = *index.ItemCount
}
if index.IndexSizeBytes != nil {
idx.Size = *index.IndexSizeBytes
}
desc.GSI = append(desc.GSI, idx)
}
for _, index := range table.LocalSecondaryIndexes {
idx := Index{
Status: ActiveStatus, // local secondary index is always active (technically, it has no status)
Local: true,
Throughput: desc.Throughput, // has the same throughput as the table
}
if index.IndexName != nil {
idx.Name = *index.IndexName
}
if index.IndexArn != nil {
idx.ARN = *index.IndexArn
}
if index.Projection != nil && index.Projection.ProjectionType != "" {
idx.ProjectionType = IndexProjection(index.Projection.ProjectionType)
idx.ProjectionAttribs = index.Projection.NonKeyAttributes
}
idx.HashKey, idx.RangeKey = schemaKeys(index.KeySchema)
idx.HashKeyType = lookupADType(table.AttributeDefinitions, idx.HashKey)
idx.RangeKeyType = lookupADType(table.AttributeDefinitions, idx.RangeKey)
if index.ItemCount != nil {
idx.Items = *index.ItemCount
}
if index.IndexSizeBytes != nil {
idx.Size = *index.IndexSizeBytes
}
desc.LSI = append(desc.LSI, idx)
}
if table.StreamSpecification != nil {
if table.StreamSpecification.StreamEnabled != nil {
desc.StreamEnabled = *table.StreamSpecification.StreamEnabled
}
if table.StreamSpecification.StreamViewType != "" {
desc.StreamView = StreamView(table.StreamSpecification.StreamViewType)
}
}
if table.LatestStreamArn != nil {
desc.LatestStreamARN = *table.LatestStreamArn
}
if table.LatestStreamLabel != nil {
desc.LatestStreamLabel = *table.LatestStreamLabel
}
if table.SSEDescription != nil {
sseDesc := SSEDescription{}
if table.SSEDescription.InaccessibleEncryptionDateTime != nil {
sseDesc.InaccessibleEncryptionDateTime = *table.SSEDescription.InaccessibleEncryptionDateTime
}
if table.SSEDescription.KMSMasterKeyArn != nil {
sseDesc.KMSMasterKeyARN = *table.SSEDescription.KMSMasterKeyArn
}
if table.SSEDescription.SSEType != "" {
sseDesc.SSEType = table.SSEDescription.SSEType
}
if table.SSEDescription.Status != "" {
sseDesc.Status = table.SSEDescription.Status
}
desc.SSEDescription = sseDesc
}
return desc
}
func (desc Description) keys(index string) map[string]struct{} {
keys := make(map[string]struct{})
keys[desc.HashKey] = struct{}{}
if desc.RangeKey != "" {
keys[desc.RangeKey] = struct{}{}
}
if index != "" {
for _, gsi := range desc.GSI {
if gsi.Name == index {
keys[gsi.HashKey] = struct{}{}
if gsi.RangeKey != "" {
keys[gsi.RangeKey] = struct{}{}
}
return keys
}
}
for _, lsi := range desc.LSI {
if lsi.Name == index {
keys[lsi.HashKey] = struct{}{}
if lsi.RangeKey != "" {
keys[lsi.RangeKey] = struct{}{}
}
return keys
}
}
return nil
}
return keys
}
// DescribeTable is a request for information about a table and its indexes.
// See: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DescribeTable.html
type DescribeTable struct {
table Table
}
// Describe begins a new request to describe this table.
func (table Table) Describe() *DescribeTable {
return &DescribeTable{table: table}
}
// Run executes this request and describe the table.
func (dt *DescribeTable) Run(ctx context.Context) (Description, error) {
input := dt.input()
var result *dynamodb.DescribeTableOutput
err := dt.table.db.retry(ctx, func() error {
var err error
result, err = dt.table.db.client.DescribeTable(ctx, input)
return err
})
if err != nil {
return Description{}, err
}
desc := newDescription(result.Table)
dt.table.db.storeDesc(desc)
return desc, nil
}
func (dt *DescribeTable) input() *dynamodb.DescribeTableInput {
name := dt.table.Name()
return &dynamodb.DescribeTableInput{
TableName: &name,
}
}
func newThroughput(td *types.ProvisionedThroughputDescription) Throughput {
if td == nil {
return Throughput{}
}
thru := Throughput{
Read: *td.ReadCapacityUnits,
Write: *td.WriteCapacityUnits,
}
if td.LastIncreaseDateTime != nil {
thru.LastInc = *td.LastIncreaseDateTime
}
if td.LastDecreaseDateTime != nil {
thru.LastDec = *td.LastDecreaseDateTime
}
if td.NumberOfDecreasesToday != nil {
thru.DecsToday = *td.NumberOfDecreasesToday
}
return thru
}
func schemaKeys(schema []types.KeySchemaElement) (hashKey, rangeKey string) {
for _, ks := range schema {
switch ks.KeyType {
case types.KeyTypeHash:
hashKey = *ks.AttributeName
case types.KeyTypeRange:
rangeKey = *ks.AttributeName
}
}
return
}
func lookupADType(ads []types.AttributeDefinition, name string) KeyType {
if name == "" {
return ""
}
for _, ad := range ads {
if *ad.AttributeName == name {
return KeyType(ad.AttributeType)
}
}
return ""
}