This repository has been archived by the owner on Aug 25, 2020. It is now read-only.
forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 27
/
service_alicloud_datahub.go
252 lines (217 loc) · 6.91 KB
/
service_alicloud_datahub.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
// Package alicloud common functions used by datahub
package alicloud
import (
"strings"
"time"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
"github.com/aliyun/aliyun-datahub-sdk-go/datahub"
)
type DatahubService struct {
client *connectivity.AliyunClient
}
func (s *DatahubService) DescribeDatahubProject(id string) (*datahub.Project, error) {
var requestInfo *datahub.DataHub
project := &datahub.Project{}
raw, err := s.client.WithDataHubClient(func(dataHubClient *datahub.DataHub) (interface{}, error) {
requestInfo = dataHubClient
return dataHubClient.GetProject(id)
})
if err != nil {
if isDatahubNotExistError(err) {
return project, WrapErrorf(err, NotFoundMsg, AliyunDatahubSdkGo)
}
return project, WrapErrorf(err, DefaultErrorMsg, id, "GetProject", AliyunDatahubSdkGo)
}
if debugOn() {
requestMap := make(map[string]string)
requestMap["ProjectName"] = id
addDebug("GetProject", raw, requestInfo, requestMap)
}
project, _ = raw.(*datahub.Project)
if project == nil {
return project, WrapErrorf(Error(GetNotFoundMessage("DatahubProject", id)), NotFoundMsg, ProviderERROR)
}
return project, nil
}
func (s *DatahubService) WaitForDatahubProject(id string, status Status, timeout int) error {
deadline := time.Now().Add(time.Duration(timeout) * time.Second)
for {
object, err := s.DescribeDatahubProject(id)
if err != nil {
if NotFoundError(err) {
if status == Deleted {
return nil
}
} else {
return WrapError(err)
}
}
if time.Now().After(deadline) {
return WrapErrorf(err, WaitTimeoutMsg, id, GetFunc(1), timeout, object.String(), id, ProviderERROR)
}
}
}
func (s *DatahubService) DescribeDatahubSubscription(id string) (*datahub.Subscription, error) {
subscription := &datahub.Subscription{}
parts, err := ParseResourceId(id, 3)
if err != nil {
return subscription, WrapError(err)
}
projectName, topicName, subId := parts[0], parts[1], parts[2]
var requestInfo *datahub.DataHub
raw, err := s.client.WithDataHubClient(func(dataHubClient *datahub.DataHub) (interface{}, error) {
requestInfo = dataHubClient
return dataHubClient.GetSubscription(projectName, topicName, subId)
})
if err != nil {
if isDatahubNotExistError(err) {
return subscription, WrapErrorf(err, NotFoundMsg, AliyunDatahubSdkGo)
}
return subscription, WrapErrorf(err, DefaultErrorMsg, id, "GetSubscription", AliyunDatahubSdkGo)
}
if debugOn() {
requestMap := make(map[string]string)
requestMap["ProjectName"] = projectName
requestMap["TopicName"] = topicName
requestMap["SubId"] = subId
addDebug("GetProject", raw, requestInfo, requestMap)
}
subscription, _ = raw.(*datahub.Subscription)
if subscription == nil || subscription.TopicName != topicName || subscription.SubId != subId {
return subscription, WrapErrorf(Error(GetNotFoundMessage("DatahubSubscription", id)), NotFoundMsg, ProviderERROR)
}
return subscription, nil
}
func (s *DatahubService) WaitForDatahubSubscription(id string, status Status, timeout int) error {
deadline := time.Now().Add(time.Duration(timeout) * time.Second)
parts, err := ParseResourceId(id, 3)
if err != nil {
return WrapError(err)
}
topicName, subId := parts[1], parts[2]
for {
object, err := s.DescribeDatahubSubscription(id)
if err != nil {
if NotFoundError(err) {
if status == Deleted {
return nil
}
} else {
return WrapError(err)
}
}
if object.TopicName == topicName && object.SubId == subId && status != Deleted {
return nil
}
if time.Now().After(deadline) {
return WrapErrorf(err, WaitTimeoutMsg, id, GetFunc(1), timeout, object.TopicName+":"+object.SubId, parts[1]+":"+parts[2], ProviderERROR)
}
}
}
func (s *DatahubService) DescribeDatahubTopic(id string) (*datahub.Topic, error) {
topic := &datahub.Topic{}
parts, err := ParseResourceId(id, 2)
if err != nil {
return topic, WrapError(err)
}
projectName, topicName := parts[0], parts[1]
var requestInfo *datahub.DataHub
raw, err := s.client.WithDataHubClient(func(dataHubClient *datahub.DataHub) (interface{}, error) {
requestInfo = dataHubClient
return dataHubClient.GetTopic(projectName, topicName)
})
if err != nil {
if isDatahubNotExistError(err) {
return topic, WrapErrorf(err, NotFoundMsg, AliyunDatahubSdkGo)
}
return topic, WrapErrorf(err, DefaultErrorMsg, id, "GetTopic", AliyunDatahubSdkGo)
}
if debugOn() {
requestMap := make(map[string]string)
requestMap["ProjectName"] = projectName
requestMap["TopicName"] = topicName
addDebug("GetTopic", raw, requestInfo, requestMap)
}
topic, _ = raw.(*datahub.Topic)
if topic == nil {
return topic, WrapErrorf(Error(GetNotFoundMessage("DatahubTopic", id)), NotFoundMsg, ProviderERROR)
}
return topic, nil
}
func (s *DatahubService) WaitForDatahubTopic(id string, status Status, timeout int) error {
deadline := time.Now().Add(time.Duration(timeout) * time.Second)
parts, err := ParseResourceId(id, 2)
if err != nil {
return WrapError(err)
}
projectName, topicName := parts[0], parts[1]
for {
object, err := s.DescribeDatahubTopic(id)
if err != nil {
if NotFoundError(err) {
if status == Deleted {
return nil
}
} else {
return WrapError(err)
}
}
if object.ProjectName == projectName && object.TopicName == topicName && status != Deleted {
return nil
}
if time.Now().After(deadline) {
return WrapErrorf(err, WaitTimeoutMsg, id, GetFunc(1), timeout, object.ProjectName+":"+object.TopicName, id, ProviderERROR)
}
}
}
func convUint64ToDate(t uint64) string {
return time.Unix(int64(t), 0).Format("2006-01-02 15:04:05")
}
func getNow() string {
return time.Now().Format("2006-01-02 15:04:05")
}
func getRecordSchema(typeMap map[string]interface{}) (recordSchema *datahub.RecordSchema) {
recordSchema = datahub.NewRecordSchema()
for k, v := range typeMap {
recordSchema.AddField(datahub.Field{Name: string(k), Type: datahub.FieldType(v.(string))})
}
return recordSchema
}
func isRetryableDatahubError(err error) bool {
if e, ok := err.(datahub.DatahubError); ok && e.StatusCode >= 500 {
return true
}
return false
}
// It is proactive defense to the case that SDK extends new datahub objects.
const (
DoesNotExist = "does not exist"
)
func isDatahubNotExistError(err error) bool {
return IsExpectedErrors(err, []string{datahub.NoSuchProject, datahub.NoSuchTopic, datahub.NoSuchShard, datahub.NoSuchSubscription, DoesNotExist})
}
func isTerraformTestingDatahubObject(name string) bool {
prefixes := []string{
"tf_testAcc",
"tf_test_",
"testAcc",
}
for _, prefix := range prefixes {
if strings.HasPrefix(strings.ToLower(name), strings.ToLower(prefix)) {
return true
}
}
return false
}
func getDefaultRecordSchemainMap() map[string]interface{} {
return map[string]interface{}{
"string_field": "STRING",
}
}
func recordSchemaToMap(fields []datahub.Field) map[string]string {
result := make(map[string]string)
for _, f := range fields {
result[f.Name] = f.Type.String()
}
return result
}