/
resource_alicloud_ots_secondary_index.go
294 lines (265 loc) · 8.61 KB
/
resource_alicloud_ots_secondary_index.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
package alicloud
import (
"fmt"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/aliyun/aliyun-tablestore-go-sdk/tablestore"
"github.com/aliyun/terraform-provider-alicloud/alicloud/connectivity"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourceAlicloudOtsSecondaryIndex() *schema.Resource {
return &schema.Resource{
Create: resourceAliyunOtsSecondaryIndexCreate,
Read: resourceAliyunOtsSecondaryIndexRead,
Delete: resourceAliyunOtsSecondaryIndexDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"instance_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateOTSInstanceName,
},
"table_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateOTSTableName,
},
"index_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateOTSIndexName,
},
"index_type": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice([]string{
string(Local), string(Global)}, false),
},
"include_base_data": {
Type: schema.TypeBool,
Required: true,
ForceNew: true,
},
"primary_keys": {
Type: schema.TypeList,
Required: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
MinItems: 1,
MaxItems: 4,
},
"defined_columns": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
MaxItems: 32,
},
},
}
}
func parseArgs(d *schema.ResourceData) *SecIndexResourceArgs {
args := &SecIndexResourceArgs{
instanceName: d.Get("instance_name").(string),
tableName: d.Get("table_name").(string),
includeBaseData: d.Get("include_base_data").(bool),
indexName: d.Get("index_name").(string),
indexType: SecondaryIndexTypeString(d.Get("index_type").(string)),
primaryKeys: Unique(Interface2StrSlice(d.Get("primary_keys").([]interface{}))),
}
if v, ok := d.GetOk("defined_columns"); ok && len(v.([]interface{})) > 0 {
args.definedColumns = Unique(Interface2StrSlice(v.([]interface{})))
}
return args
}
type SecIndexResourceArgs struct {
instanceName string
tableName string
includeBaseData bool
indexName string
indexType SecondaryIndexTypeString
primaryKeys []string
definedColumns []string
}
func resourceAliyunOtsSecondaryIndexCreate(d *schema.ResourceData, meta interface{}) error {
args := parseArgs(d)
client := meta.(*connectivity.AliyunClient)
otsService := OtsService{client}
// check table exists
tableResp, err := otsService.LoopWaitTable(args.instanceName, args.tableName)
if err != nil {
return WrapError(err)
}
// serverside arguments check
if err := checkArgs(tableResp, args); err != nil {
return err
}
// build request
idxType, err := ConvertSecIndexTypeString(args.indexType)
if err != nil {
return WrapError(err)
}
req := &tablestore.CreateIndexRequest{
MainTableName: args.tableName,
IncludeBaseData: args.includeBaseData,
IndexMeta: &tablestore.IndexMeta{
IndexName: args.indexName,
Primarykey: args.primaryKeys,
IndexType: idxType,
DefinedColumns: args.definedColumns,
},
}
var reqClient *tablestore.TableStoreClient
if err := resource.Retry(2*time.Minute, func() *resource.RetryError {
raw, err := client.WithTableStoreClient(args.instanceName, func(tableStoreClient *tablestore.TableStoreClient) (interface{}, error) {
reqClient = tableStoreClient
return tableStoreClient.CreateIndex(req)
})
defer func() {
addDebug("CreateTableSecondaryIndex", raw, reqClient, req)
}()
if err != nil {
if IsExpectedErrors(err, OtsSecondaryIndexIsTemporarilyUnavailable) {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
}); err != nil {
return WrapErrorf(err, DefaultErrorMsg, "alicloud_ots_secondary_index", "CreateIndex", AliyunTablestoreGoSdk)
}
d.SetId(ID(args.instanceName, args.tableName, args.indexName, string(args.indexType)))
return resourceAliyunOtsSecondaryIndexRead(d, meta)
}
func checkArgs(tableResp *tablestore.DescribeTableResponse, args *SecIndexResourceArgs) error {
dataPks := simplifyPK(tableResp.TableMeta.SchemaEntry)
dataCols := simplifyCol(tableResp.TableMeta.DefinedColumns)
allCards := append(dataPks, dataCols...)
if !IsSubCollection(args.primaryKeys, allCards) {
return WrapError(fmt.Errorf("some primary keys not exist in table: %s/%s", args.primaryKeys, args.tableName))
}
if !IsSubCollection(args.definedColumns, allCards) {
return WrapError(fmt.Errorf("some defined columns not exist in table: %s/%s", args.definedColumns, args.tableName))
}
if args.indexName == args.tableName {
return WrapError(fmt.Errorf("index name cannot be the same as table: %s/%s", args.indexName, args.tableName))
}
if args.indexType == Local && args.primaryKeys[0] != dataPks[0] {
return WrapError(fmt.Errorf("when using a local secondary index, the first primary key of the index must be "+
"the same as the first primary key of the data table: %s/%s", args.primaryKeys, dataPks))
}
if tableResp.TableOption.TimeToAlive != -1 {
return WrapError(fmt.Errorf("when creating a secondary index, the TimeToAlive of the table must be -1: %v", tableResp.TableOption.TimeToAlive))
}
if tableResp.TableOption.MaxVersion != 1 {
return WrapError(fmt.Errorf("when creating a secondary index, the table's MaxVersion must be 1: %v", tableResp.TableOption.MaxVersion))
}
return nil
}
func simplifyPK(keys []*tablestore.PrimaryKeySchema) []string {
var pks = make([]string, 0, len(keys))
for _, key := range keys {
pks = append(pks, *key.Name)
}
return pks
}
func simplifyCol(columns []*tablestore.DefinedColumnSchema) []string {
var cols = make([]string, 0, len(columns))
for _, col := range columns {
cols = append(cols, col.Name)
}
return cols
}
func simplifySecIndex(indexes []*tablestore.IndexMeta) []string {
var ii = make([]string, 0, len(indexes))
for _, idx := range indexes {
ii = append(ii, idx.IndexName)
}
return ii
}
func resourceAliyunOtsSecondaryIndexRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
otsService := OtsService{client}
idx, err := otsService.DescribeOtsSecondaryIndex(d.Id())
if err != nil {
if NotFoundError(err) {
return nil
}
return WrapError(err)
}
if idx == nil {
d.SetId("")
return nil
}
if err := d.Set("instance_name", idx.InstanceName); err != nil {
return WrapError(err)
}
if err := d.Set("table_name", idx.TableName); err != nil {
return WrapError(err)
}
if err := d.Set("index_name", idx.Index.IndexName); err != nil {
return WrapError(err)
}
indexType, err := ConvertSecIndexType(idx.Index.IndexType)
if err != nil {
return WrapError(err)
}
if err := d.Set("index_type", string(indexType)); err != nil {
return WrapError(err)
}
if err := d.Set("primary_keys", idx.Index.Primarykey); err != nil {
return WrapError(err)
}
if err := d.Set("defined_columns", idx.Index.DefinedColumns); err != nil {
return WrapError(err)
}
return nil
}
func resourceAliyunOtsSecondaryIndexDelete(d *schema.ResourceData, meta interface{}) error {
instanceName, tableName, indexName, _, err := ParseIndexId(d.Id())
if err != nil {
return WrapError(err)
}
client := meta.(*connectivity.AliyunClient)
req := &tablestore.DeleteIndexRequest{
MainTableName: tableName,
IndexName: indexName,
}
err = resource.Retry(2*time.Minute, func() *resource.RetryError {
var requestInfo *tablestore.TableStoreClient
raw, err := client.WithTableStoreClient(instanceName, func(tableStoreClient *tablestore.TableStoreClient) (interface{}, error) {
requestInfo = tableStoreClient
return tableStoreClient.DeleteIndex(req)
})
defer func() {
addDebug("DeleteTableSecondaryIndex", raw, requestInfo, req)
}()
if err != nil {
if IsExpectedErrors(err, OtsTableIsTemporarilyUnavailable) {
return resource.RetryableError(err)
}
return resource.NonRetryableError(err)
}
return nil
})
if err != nil {
if strings.HasPrefix(err.Error(), "OTSObjectNotExist") {
return nil
}
return WrapErrorf(err, DefaultErrorMsg, d.Id(), "DeleteTableSecondaryIndex", AliyunTablestoreGoSdk)
}
otsService := OtsService{client}
return WrapError(otsService.WaitForSecondaryIndex(instanceName, tableName, indexName, Deleted, DefaultTimeout))
}