forked from aliyun/terraform-provider-alicloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_alicloud_datahub_topic.go
234 lines (206 loc) · 7.52 KB
/
resource_alicloud_datahub_topic.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
package alicloud
import (
"fmt"
"strings"
"time"
"github.com/aliyun/aliyun-datahub-sdk-go/datahub"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-alicloud/alicloud/connectivity"
)
func resourceAlicloudDatahubTopic() *schema.Resource {
return &schema.Resource{
Create: resourceAliyunDatahubTopicCreate,
Read: resourceAliyunDatahubTopicRead,
Update: resourceAliyunDatahubTopicUpdate,
Delete: resourceAliyunDatahubTopicDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"project_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateDatahubProjectName,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return strings.ToLower(new) == strings.ToLower(old)
},
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateDatahubTopicName,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return strings.ToLower(new) == strings.ToLower(old)
},
},
"shard_count": {
Type: schema.TypeInt,
Optional: true,
Default: 1,
ValidateFunc: validateIntegerInRange(1, 10),
},
"life_cycle": {
Type: schema.TypeInt,
Optional: true,
Default: 3,
ValidateFunc: validateIntegerInRange(1, 7),
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return strings.ToLower(old) != "" && strings.ToLower(new) != strings.ToLower(old)
},
},
"comment": {
Type: schema.TypeString,
Optional: true,
Default: "topic added by terraform",
ValidateFunc: validateStringLengthInRange(0, 255),
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return strings.ToLower(new) == strings.ToLower(old)
},
},
"record_type": {
Type: schema.TypeString,
Optional: true,
Default: "TUPLE",
ValidateFunc: validateAllowedStringValue([]string{string(datahub.TUPLE), string(datahub.BLOB)}),
},
"record_schema": {
Type: schema.TypeMap,
Elem: schema.TypeString,
Optional: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return d.Get("record_type") != string(datahub.TUPLE)
},
},
"create_time": {
Type: schema.TypeString, //converted from UTC(uint64) value
Computed: true,
},
"last_modify_time": {
Type: schema.TypeString, //converted from UTC(uint64) value
Computed: true,
},
},
}
}
func resourceAliyunDatahubTopicCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
t := &datahub.Topic{
ProjectName: d.Get("project_name").(string),
TopicName: d.Get("name").(string),
ShardCount: d.Get("shard_count").(int),
Lifecycle: d.Get("life_cycle").(int),
Comment: d.Get("comment").(string),
}
recordType := d.Get("record_type").(string)
if recordType == string(datahub.TUPLE) {
t.RecordType = datahub.TUPLE
recordSchema := d.Get("record_schema").(map[string]interface{})
if len(recordSchema) == 0 {
recordSchema = getDefaultRecordSchemainMap()
}
t.RecordSchema = getRecordSchema(recordSchema)
} else if recordType == string(datahub.BLOB) {
t.RecordType = datahub.BLOB
}
_, err := client.WithDataHubClient(func(dataHubClient *datahub.DataHub) (interface{}, error) {
return nil, dataHubClient.CreateTopic(t)
})
if err != nil {
return fmt.Errorf("failed to create topic'%s/%s' with error: %s", t.ProjectName, t.TopicName, err)
}
d.SetId(strings.ToLower(fmt.Sprintf("%s%s%s", t.ProjectName, COLON_SEPARATED, t.TopicName)))
return resourceAliyunDatahubTopicRead(d, meta)
}
func parseId2(d *schema.ResourceData, meta interface{}) (projectName, topicName string, err error) {
split := strings.Split(d.Id(), COLON_SEPARATED)
if len(split) != 2 {
err = fmt.Errorf("you should use resource alicloud_datahub_topic's new field 'project_name' and 'name' to re-import this resource.")
return
} else {
projectName = split[0]
topicName = split[1]
return
}
}
func resourceAliyunDatahubTopicRead(d *schema.ResourceData, meta interface{}) error {
projectName, topicName, err := parseId2(d, meta)
if err != nil {
return err
}
client := meta.(*connectivity.AliyunClient)
raw, err := client.WithDataHubClient(func(dataHubClient *datahub.DataHub) (interface{}, error) {
return dataHubClient.GetTopic(projectName, topicName)
})
if err != nil {
if isDatahubNotExistError(err) {
d.SetId("")
return nil
}
return fmt.Errorf("failed to access topic '%s/%s' with error: %s", projectName, topicName, err)
}
topic, _ := raw.(*datahub.Topic)
d.SetId(strings.ToLower(fmt.Sprintf("%s%s%s", topic.ProjectName, COLON_SEPARATED, topic.TopicName)))
d.Set("name", topic.TopicName)
d.Set("project_name", topic.ProjectName)
d.Set("shard_count", topic.ShardCount)
d.Set("life_cycle", topic.Lifecycle)
d.Set("comment", topic.Comment)
d.Set("record_type", topic.RecordType.String())
d.Set("record_schema", topic.RecordSchema.String())
d.Set("create_time", datahub.Uint64ToTimeString(topic.CreateTime))
d.Set("last_modify_time", datahub.Uint64ToTimeString(topic.LastModifyTime))
return nil
}
func resourceAliyunDatahubTopicUpdate(d *schema.ResourceData, meta interface{}) error {
projectName, topicName, err := parseId2(d, meta)
if err != nil {
return err
}
client := meta.(*connectivity.AliyunClient)
// Currently, life_cycle can not be modified and it will be fixed in the next future.
if d.HasChange("life_cycle") || d.HasChange("comment") {
lifeCycle := d.Get("life_cycle").(int)
topicComment := d.Get("comment").(string)
_, err := client.WithDataHubClient(func(dataHubClient *datahub.DataHub) (interface{}, error) {
return nil, dataHubClient.UpdateTopic(projectName, topicName, lifeCycle, topicComment)
})
if err != nil {
return fmt.Errorf("failed to update topic '%s/%s' with error: %s", projectName, topicName, err)
}
}
return resourceAliyunDatahubTopicRead(d, meta)
}
func resourceAliyunDatahubTopicDelete(d *schema.ResourceData, meta interface{}) error {
projectName, topicName, err := parseId2(d, meta)
if err != nil {
return err
}
client := meta.(*connectivity.AliyunClient)
return resource.Retry(3*time.Minute, func() *resource.RetryError {
_, err := client.WithDataHubClient(func(dataHubClient *datahub.DataHub) (interface{}, error) {
return dataHubClient.GetTopic(projectName, topicName)
})
if err != nil {
if isDatahubNotExistError(err) {
return nil
}
if isRetryableDatahubError(err) {
return resource.RetryableError(fmt.Errorf("while deleting '%s/%s', failed to access it with error: %s", projectName, topicName, err))
}
return resource.NonRetryableError(fmt.Errorf("while deleting '%s/%s', failed to access it with error: %s", projectName, topicName, err))
}
_, err = client.WithDataHubClient(func(dataHubClient *datahub.DataHub) (interface{}, error) {
return nil, dataHubClient.DeleteTopic(projectName, topicName)
})
if err == nil || isDatahubNotExistError(err) {
return nil
}
if isRetryableDatahubError(err) {
return resource.RetryableError(fmt.Errorf("Deleting topic '%s/%s' timeout and got an error: %#v.", projectName, topicName, err))
}
return resource.NonRetryableError(fmt.Errorf("Deleting topic '%s/%s' timeout and got an error: %#v.", projectName, topicName, err))
})
}