-
-
Notifications
You must be signed in to change notification settings - Fork 179
/
updatetable.go
221 lines (193 loc) · 6.1 KB
/
updatetable.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
package dynamo
import (
"context"
"errors"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
"github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
)
// UpdateTable is a request to change a table's settings.
// See: http://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_UpdateTable.html
type UpdateTable struct {
table Table
r, w int64 // throughput
billingMode types.BillingMode
disableStream bool
streamView StreamView
updateIdx map[string]Throughput
createIdx []Index
deleteIdx []string
ads []types.AttributeDefinition
err error
}
// UpdateTable makes changes to this table's settings.
func (table Table) UpdateTable() *UpdateTable {
return &UpdateTable{
table: table,
updateIdx: make(map[string]Throughput),
}
}
// OnDemand sets this table to use on-demand (pay per request) billing mode if enabled is true.
// If enabled is false, this table will be changed to provisioned billing mode.
func (ut *UpdateTable) OnDemand(enabled bool) *UpdateTable {
if enabled {
ut.billingMode = types.BillingModePayPerRequest
} else {
ut.billingMode = types.BillingModeProvisioned
}
return ut
}
// Provision sets this table's read and write throughput capacity.
func (ut *UpdateTable) Provision(read, write int64) *UpdateTable {
ut.r, ut.w = read, write
return ut
}
// ProvisionIndex updates a global secondary index's read and write throughput capacity.
func (ut *UpdateTable) ProvisionIndex(name string, read, write int64) *UpdateTable {
ut.updateIdx[name] = Throughput{Read: read, Write: write}
return ut
}
// CreateIndex adds a new secondary global index.
// You must specify the index name, keys, key types, projection.
// If this table is not on-demand you must also specify throughput.
func (ut *UpdateTable) CreateIndex(index Index) *UpdateTable {
if index.Name == "" {
ut.err = errors.New("dynamo: update table: missing index name")
}
if index.HashKey == "" {
ut.err = errors.New("dynamo: update table: missing hash key")
}
if index.HashKeyType == "" {
ut.err = errors.New("dynamo: update table: missing hash key type")
}
if index.RangeKey != "" && index.RangeKeyType == "" {
ut.err = errors.New("dynamo: update table: missing range key type")
}
if index.ProjectionType == "" {
ut.err = errors.New("dynamo: update table: missing projection type")
}
ut.addAD(index.HashKey, index.HashKeyType)
if index.RangeKey != "" {
ut.addAD(index.RangeKey, index.RangeKeyType)
}
ut.createIdx = append(ut.createIdx, index)
return ut
}
// DeleteIndex deletes the specified index.
func (ut *UpdateTable) DeleteIndex(name string) *UpdateTable {
ut.deleteIdx = append(ut.deleteIdx, name)
return ut
}
// Stream enables streaming and sets the stream view type.
func (ut *UpdateTable) Stream(view StreamView) *UpdateTable {
ut.streamView = view
return ut
}
// DisableStream disables this table's stream.
func (ut *UpdateTable) DisableStream() *UpdateTable {
ut.disableStream = true
return ut
}
// Run executes this request and describes the table.
func (ut *UpdateTable) Run(ctx context.Context) (Description, error) {
if ut.err != nil {
return Description{}, ut.err
}
input := ut.input()
var result *dynamodb.UpdateTableOutput
err := ut.table.db.retry(ctx, func() error {
var err error
result, err = ut.table.db.client.UpdateTable(ctx, input)
return err
})
if err != nil {
return Description{}, err
}
return newDescription(result.TableDescription), nil
}
func (ut *UpdateTable) input() *dynamodb.UpdateTableInput {
input := &dynamodb.UpdateTableInput{
TableName: aws.String(ut.table.Name()),
AttributeDefinitions: ut.ads,
BillingMode: ut.billingMode,
}
if ut.r != 0 || ut.w != 0 {
input.ProvisionedThroughput = &types.ProvisionedThroughput{
ReadCapacityUnits: &ut.r,
WriteCapacityUnits: &ut.w,
}
}
if ut.disableStream {
input.StreamSpecification = &types.StreamSpecification{
StreamEnabled: aws.Bool(false),
}
} else if ut.streamView != "" {
input.StreamSpecification = &types.StreamSpecification{
StreamEnabled: aws.Bool(true),
StreamViewType: types.StreamViewType(ut.streamView),
}
}
for index, thru := range ut.updateIdx {
up := types.GlobalSecondaryIndexUpdate{Update: &types.UpdateGlobalSecondaryIndexAction{
IndexName: aws.String(index),
ProvisionedThroughput: &types.ProvisionedThroughput{
ReadCapacityUnits: aws.Int64(thru.Read),
WriteCapacityUnits: aws.Int64(thru.Write),
},
}}
input.GlobalSecondaryIndexUpdates = append(input.GlobalSecondaryIndexUpdates, up)
}
for _, index := range ut.createIdx {
up := types.GlobalSecondaryIndexUpdate{Create: createIndexAction(index)}
input.GlobalSecondaryIndexUpdates = append(input.GlobalSecondaryIndexUpdates, up)
}
for _, del := range ut.deleteIdx {
up := types.GlobalSecondaryIndexUpdate{Delete: &types.DeleteGlobalSecondaryIndexAction{
IndexName: aws.String(del),
}}
input.GlobalSecondaryIndexUpdates = append(input.GlobalSecondaryIndexUpdates, up)
}
return input
}
func (ut *UpdateTable) addAD(name string, typ KeyType) {
for _, ad := range ut.ads {
if *ad.AttributeName == name {
return
}
}
ut.ads = append(ut.ads, types.AttributeDefinition{
AttributeName: &name,
AttributeType: types.ScalarAttributeType(typ),
})
}
func createIndexAction(index Index) *types.CreateGlobalSecondaryIndexAction {
ks := []types.KeySchemaElement{
{
AttributeName: &index.HashKey,
KeyType: types.KeyTypeHash,
},
}
if index.RangeKey != "" {
ks = append(ks, types.KeySchemaElement{
AttributeName: &index.RangeKey,
KeyType: types.KeyTypeRange,
})
}
add := &types.CreateGlobalSecondaryIndexAction{
IndexName: &index.Name,
KeySchema: ks,
Projection: &types.Projection{
ProjectionType: types.ProjectionType(index.ProjectionType),
},
}
if index.Throughput.Read > 0 && index.Throughput.Write > 0 {
add.ProvisionedThroughput = &types.ProvisionedThroughput{
ReadCapacityUnits: aws.Int64(index.Throughput.Read),
WriteCapacityUnits: aws.Int64(index.Throughput.Write),
}
}
if index.ProjectionType == IncludeProjection {
add.Projection.NonKeyAttributes = index.ProjectionAttribs
}
return add
}