-
Notifications
You must be signed in to change notification settings - Fork 154
/
resource_huaweicloud_dms_kafka_topic.go
246 lines (215 loc) · 6.6 KB
/
resource_huaweicloud_dms_kafka_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
235
236
237
238
239
240
241
242
243
244
245
246
package dms
import (
"context"
"fmt"
"log"
"strings"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/chnsz/golangsdk/openstack/dms/v2/kafka/topics"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/common"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
)
// ResourceDmsKafkaTopic implements the resource of "huaweicloud_dms_kafka_topic"
// @API Kafka POST /v2/{project_id}/instances/{instance_id}/topics/delete
// @API Kafka GET /v2/{project_id}/instances/{instance_id}/topics
// @API Kafka POST /v2/{project_id}/instances/{instance_id}/topics
// @API Kafka PUT /v2/{project_id}/instances/{instance_id}/topics
func ResourceDmsKafkaTopic() *schema.Resource {
return &schema.Resource{
CreateContext: resourceDmsKafkaTopicCreate,
ReadContext: resourceDmsKafkaTopicRead,
UpdateContext: resourceDmsKafkaTopicUpdate,
DeleteContext: resourceDmsKafkaTopicDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceDmsKafkaTopicImport,
},
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"instance_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"partitions": {
Type: schema.TypeInt,
Required: true,
},
"replicas": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
ForceNew: true,
},
"aging_time": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
},
"sync_replication": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
"sync_flushing": {
Type: schema.TypeBool,
Optional: true,
Computed: true,
},
},
}
}
func resourceDmsKafkaTopicCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
dmsV2Client, err := cfg.DmsV2Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating DMS client: %s", err)
}
createOpts := &topics.CreateOps{
Name: d.Get("name").(string),
Partition: d.Get("partitions").(int),
Replication: d.Get("replicas").(int),
RetentionTime: d.Get("aging_time").(int),
SyncReplication: d.Get("sync_replication").(bool),
SyncMessageFlush: d.Get("sync_flushing").(bool),
}
log.Printf("[DEBUG] Create Options: %#v", createOpts)
instanceID := d.Get("instance_id").(string)
v, err := topics.Create(dmsV2Client, instanceID, createOpts).Extract()
if err != nil {
return diag.Errorf("error creating DMS kafka topic: %s", err)
}
// use topic name as the resource ID
d.SetId(v.Name)
return resourceDmsKafkaTopicRead(ctx, d, meta)
}
func resourceDmsKafkaTopicRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
dmsV2Client, err := cfg.DmsV2Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating DMS client: %s", err)
}
instanceID := d.Get("instance_id").(string)
allTopics, err := topics.List(dmsV2Client, instanceID).Extract()
if err != nil {
return common.CheckDeletedDiag(d, err, "DMS kafka topic")
}
topicID := d.Id()
isFound := false
var found topics.Topic
for _, item := range allTopics {
if item.Name == topicID {
found = item
isFound = true
break
}
}
if !isFound {
d.SetId("")
return nil
}
log.Printf("[DEBUG] DMS kafka topic %s: %+v", d.Id(), found)
mErr := multierror.Append(nil,
d.Set("region", cfg.GetRegion(d)),
d.Set("name", found.Name),
d.Set("partitions", found.Partition),
d.Set("replicas", found.Replication),
d.Set("aging_time", found.RetentionTime),
d.Set("sync_replication", found.SyncReplication),
d.Set("sync_flushing", found.SyncMessageFlush),
)
if mErr.ErrorOrNil() != nil {
return diag.FromErr(mErr)
}
return nil
}
func resourceDmsKafkaTopicUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
dmsV2Client, err := cfg.DmsV2Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating DMS client: %s", err)
}
updateItem := topics.UpdateItem{
Name: d.Get("name").(string),
}
// Set value if it changed.
if d.HasChange("partitions") {
newPartition := d.Get("partitions").(int)
updateItem.Partition = &newPartition
}
if d.HasChange("aging_time") {
retentionTime := d.Get("aging_time").(int)
updateItem.RetentionTime = &retentionTime
}
if d.HasChange("sync_replication") {
syncReplication := d.Get("sync_replication").(bool)
updateItem.SyncReplication = &syncReplication
}
if d.HasChange("sync_flushing") {
syncMessageFlush := d.Get("sync_flushing").(bool)
updateItem.SyncMessageFlush = &syncMessageFlush
}
updateOpts := topics.UpdateOpts{
Topics: []topics.UpdateItem{
updateItem,
},
}
log.Printf("[DEBUG] Update Options: %#v", updateOpts)
instanceID := d.Get("instance_id").(string)
err = topics.Update(dmsV2Client, instanceID, updateOpts).Err
if err != nil {
return diag.Errorf("error updating DMS kafka topic: %s", err)
}
return resourceDmsKafkaTopicRead(ctx, d, meta)
}
func resourceDmsKafkaTopicDelete(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
dmsV2Client, err := cfg.DmsV2Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating DMS client: %s", err)
}
topicID := d.Id()
instanceID := d.Get("instance_id").(string)
response, err := topics.Delete(dmsV2Client, instanceID, []string{topicID}).Extract()
if err != nil {
return diag.Errorf("error deleting DMS kafka topic: %s", err)
}
var success bool
for _, item := range response {
if item.Name == topicID {
success = item.Success
break
}
}
if !success {
return diag.Errorf("error deleting DMS kafka topic")
}
d.SetId("")
return nil
}
// resourceDmsKafkaTopicImport query the rules from HuaweiCloud and imports them to Terraform.
// It is a common function in waf and is also called by other rule resources.
func resourceDmsKafkaTopicImport(_ context.Context, d *schema.ResourceData, _ interface{}) ([]*schema.ResourceData,
error) {
parts := strings.SplitN(d.Id(), "/", 2)
if len(parts) != 2 {
err := fmt.Errorf("invalid format specified for DMS kafka topic. Format must be <instance id>/<topic name>")
return nil, err
}
instanceID := parts[0]
topicID := parts[1]
d.SetId(topicID)
err := d.Set("instance_id", instanceID)
return []*schema.ResourceData{d}, err
}