forked from aliyun/terraform-provider-alicloud
/
resource_alicloud_datahub_subscription.go
188 lines (163 loc) · 6.09 KB
/
resource_alicloud_datahub_subscription.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
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 resourceAlicloudDatahubSubscription() *schema.Resource {
return &schema.Resource{
Create: resourceAliyunDatahubSubscriptionCreate,
Read: resourceAliyunDatahubSubscriptionRead,
Update: resourceAliyunDatahubSubscriptionUpdate,
Delete: resourceAliyunDatahubSubscriptionDelete,
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)
},
},
"topic_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)
},
},
"comment": {
Type: schema.TypeString,
Optional: true,
Default: "subscription added by terraform",
ValidateFunc: validateStringLengthInRange(0, 255),
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return strings.ToLower(new) == strings.ToLower(old)
},
},
"sub_id": {
Type: schema.TypeString,
Computed: true,
},
"create_time": {
Type: schema.TypeString, //uint64 value from sdk
Computed: true,
},
"last_modify_time": {
Type: schema.TypeString, //uint64 value from sdk
Computed: true,
},
},
}
}
func resourceAliyunDatahubSubscriptionCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*connectivity.AliyunClient)
projectName := d.Get("project_name").(string)
topicName := d.Get("topic_name").(string)
subComment := d.Get("comment").(string)
raw, err := client.WithDataHubClient(func(dataHubClient *datahub.DataHub) (interface{}, error) {
return dataHubClient.CreateSubscription(projectName, topicName, subComment)
})
if err != nil {
return fmt.Errorf("failed to create subscription under '%s/%s' with error: %s", projectName, topicName, err)
}
subId, _ := raw.(string)
d.SetId(fmt.Sprintf("%s%s%s%s%s", strings.ToLower(projectName), COLON_SEPARATED, strings.ToLower(topicName), COLON_SEPARATED, subId))
return resourceAliyunDatahubSubscriptionRead(d, meta)
}
func parseId3(d *schema.ResourceData, meta interface{}) (projectName, topicName, subId string, err error) {
split := strings.Split(d.Id(), COLON_SEPARATED)
if len(split) != 3 {
err = fmt.Errorf("you should use resource alicloud_datahub_subscription's new field 'project_name' and 'topic_name' to re-import this resource.")
return
} else {
projectName = split[0]
topicName = split[1]
subId = split[2]
}
return
}
func resourceAliyunDatahubSubscriptionRead(d *schema.ResourceData, meta interface{}) error {
projectName, topicName, subId, err := parseId3(d, meta)
if err != nil {
return err
}
client := meta.(*connectivity.AliyunClient)
raw, err := client.WithDataHubClient(func(dataHubClient *datahub.DataHub) (interface{}, error) {
return dataHubClient.GetSubscription(projectName, topicName, subId)
})
if err != nil {
if isDatahubNotExistError(err) {
d.SetId("")
return nil
}
return fmt.Errorf("failed to get subscription %s with error: %s", subId, err)
}
sub, _ := raw.(*datahub.Subscription)
d.SetId(fmt.Sprintf("%s%s%s%s%s", strings.ToLower(projectName), COLON_SEPARATED, strings.ToLower(sub.TopicName), COLON_SEPARATED, sub.SubId))
d.Set("project_name", projectName)
d.Set("topic_name", sub.TopicName)
d.Set("sub_id", sub.SubId)
d.Set("comment", sub.Comment)
d.Set("create_time", datahub.Uint64ToTimeString(sub.CreateTime))
d.Set("last_modify_time", datahub.Uint64ToTimeString(sub.LastModifyTime))
return nil
}
func resourceAliyunDatahubSubscriptionUpdate(d *schema.ResourceData, meta interface{}) error {
projectName, topicName, subId, err := parseId3(d, meta)
if err != nil {
return err
}
client := meta.(*connectivity.AliyunClient)
if d.HasChange("comment") {
subComment := d.Get("comment").(string)
_, err := client.WithDataHubClient(func(dataHubClient *datahub.DataHub) (interface{}, error) {
return nil, dataHubClient.UpdateSubscription(projectName, topicName, subId, subComment)
})
if err != nil {
return fmt.Errorf("failed to update subscription %s's comment with error: %s", subId, err)
}
}
return resourceAliyunDatahubSubscriptionRead(d, meta)
}
func resourceAliyunDatahubSubscriptionDelete(d *schema.ResourceData, meta interface{}) error {
projectName, topicName, subId, err := parseId3(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.GetSubscription(projectName, topicName, subId)
})
if err != nil {
if isDatahubNotExistError(err) {
return nil
}
if isRetryableDatahubError(err) {
return resource.RetryableError(fmt.Errorf("while deleting subscription '%s', failed to get it with error: %s", subId, err))
}
return resource.NonRetryableError(fmt.Errorf("while deleting subscription '%s', failed to get it with error: %s", subId, err))
}
_, err = client.WithDataHubClient(func(dataHubClient *datahub.DataHub) (interface{}, error) {
return nil, dataHubClient.DeleteSubscription(projectName, topicName, subId)
})
if err == nil || isDatahubNotExistError(err) {
return nil
}
if isRetryableDatahubError(err) {
return resource.RetryableError(fmt.Errorf("Deleting subscription '%s' timeout and got an error: %#v.", subId, err))
}
return resource.NonRetryableError(fmt.Errorf("Deleting subscription '%s' timeout.", subId))
})
}