forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_alicloud_ots.go
183 lines (160 loc) · 5.33 KB
/
service_alicloud_ots.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
package alicloud
import (
"strings"
"time"
"fmt"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ots"
"github.com/aliyun/aliyun-tablestore-go-sdk/tablestore"
"github.com/hashicorp/terraform/helper/resource"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
type OtsService struct {
client *connectivity.AliyunClient
}
func (s *OtsService) getPrimaryKeyType(primaryKeyType string) tablestore.PrimaryKeyType {
var keyType tablestore.PrimaryKeyType
t := PrimaryKeyTypeString(primaryKeyType)
switch t {
case IntegerType:
keyType = tablestore.PrimaryKeyType_INTEGER
case StringType:
keyType = tablestore.PrimaryKeyType_STRING
case BinaryType:
keyType = tablestore.PrimaryKeyType_BINARY
}
return keyType
}
func (s *OtsService) DescribeOtsTable(instanceName, tableName string) (table *tablestore.DescribeTableResponse, err error) {
describeTableReq := new(tablestore.DescribeTableRequest)
describeTableReq.TableName = tableName
err = resource.Retry(5*time.Minute, func() *resource.RetryError {
if _, e := s.DescribeOtsInstance(instanceName); e != nil {
return resource.NonRetryableError(e)
}
raw, e := s.client.WithTableStoreClient(instanceName, func(tableStoreClient *tablestore.TableStoreClient) (interface{}, error) {
return tableStoreClient.DescribeTable(describeTableReq)
})
if e != nil {
if strings.HasSuffix(e.Error(), SuffixNoSuchHost) {
return resource.RetryableError(fmt.Errorf("RetryTimeout. Failed to describe table with error: %s", e))
}
if strings.HasPrefix(e.Error(), OTSObjectNotExist) {
return resource.NonRetryableError(GetNotFoundErrorFromString(GetNotFoundMessage("OTS Table", tableName)))
}
return resource.NonRetryableError(fmt.Errorf("Failed to describe table with error: %#v", e))
}
table, _ = raw.(*tablestore.DescribeTableResponse)
if table == nil || table.TableMeta == nil || table.TableMeta.TableName != tableName {
return resource.NonRetryableError(GetNotFoundErrorFromString(GetNotFoundMessage("OTS Table", tableName)))
}
return nil
})
return
}
func (s *OtsService) DeleteOtsTable(instanceName, tableName string) (bool, error) {
deleteReq := new(tablestore.DeleteTableRequest)
deleteReq.TableName = tableName
_, err := s.client.WithTableStoreClient(instanceName, func(tableStoreClient *tablestore.TableStoreClient) (interface{}, error) {
return tableStoreClient.DeleteTable(deleteReq)
})
if err != nil {
if NotFoundError(err) {
return true, nil
}
return false, err
}
describ, err := s.DescribeOtsTable(instanceName, tableName)
if err != nil {
if NotFoundError(err) {
return true, nil
}
return false, err
}
if describ.TableMeta != nil {
return false, err
}
return true, err
}
// Convert tablestore.PrimaryKeyType to PrimaryKeyTypeString
func (s *OtsService) convertPrimaryKeyType(t tablestore.PrimaryKeyType) PrimaryKeyTypeString {
var typeString PrimaryKeyTypeString
switch t {
case tablestore.PrimaryKeyType_INTEGER:
typeString = IntegerType
case tablestore.PrimaryKeyType_BINARY:
typeString = BinaryType
case tablestore.PrimaryKeyType_STRING:
typeString = StringType
}
return typeString
}
func (s *OtsService) DescribeOtsInstance(name string) (inst ots.InstanceInfo, err error) {
req := ots.CreateGetInstanceRequest()
req.InstanceName = name
req.Method = "GET"
raw, err := s.client.WithOtsClient(func(otsClient *ots.Client) (interface{}, error) {
return otsClient.GetInstance(req)
})
// OTS instance not found error code is "NotFound"
if err != nil {
return
}
resp, _ := raw.(*ots.GetInstanceResponse)
if resp == nil || resp.InstanceInfo.InstanceName != name {
return inst, GetNotFoundErrorFromString(GetNotFoundMessage("OTS Instance", name))
}
return resp.InstanceInfo, nil
}
func (s *OtsService) DescribeOtsInstanceVpc(name string) (inst ots.VpcInfo, err error) {
req := ots.CreateListVpcInfoByInstanceRequest()
req.Method = "GET"
req.InstanceName = name
raw, err := s.client.WithOtsClient(func(otsClient *ots.Client) (interface{}, error) {
return otsClient.ListVpcInfoByInstance(req)
})
if err != nil {
return inst, err
}
resp, _ := raw.(*ots.ListVpcInfoByInstanceResponse)
if resp == nil || resp.TotalCount < 1 {
return inst, GetNotFoundErrorFromString(GetNotFoundMessage("OTS Instance VPC", name))
}
return resp.VpcInfos.VpcInfo[0], nil
}
func (s *OtsService) WaitForOtsInstance(name string, status Status, timeout int) error {
if timeout <= 0 {
timeout = DefaultTimeout
}
for {
inst, err := s.DescribeOtsInstance(name)
if err != nil {
return err
}
if inst.Status == convertOtsInstanceStatus(status) {
break
}
timeout = timeout - DefaultIntervalShort
if timeout <= 0 {
return GetTimeErrorFromString(GetTimeoutMessage("OTS Instance", string(status)))
}
time.Sleep(DefaultIntervalShort * time.Second)
}
return nil
}
func (s *OtsService) DescribeOtsInstanceTypes() (types []string, err error) {
req := ots.CreateListClusterTypeRequest()
req.Method = requests.GET
raw, err := s.client.WithOtsClient(func(otsClient *ots.Client) (interface{}, error) {
return otsClient.ListClusterType(req)
})
if err != nil {
err = fmt.Errorf("Failed to list instance type with error: %#v", err)
return
}
resp, _ := raw.(*ots.ListClusterTypeResponse)
if resp != nil {
return resp.ClusterTypeInfos.ClusterType, nil
}
return
}