-
Notifications
You must be signed in to change notification settings - Fork 6
/
resource_topics.go
executable file
·333 lines (285 loc) · 8.97 KB
/
resource_topics.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package cplatform
import (
"context"
"fmt"
"log"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
confluent "github.com/OneMount/gonfluent"
)
func topics() *schema.Resource {
return &schema.Resource{
CreateContext: topicsCreate,
DeleteContext: topicsDelete,
ReadContext: topicsRead,
UpdateContext: topicsUpdate,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
},
"partitions": {
Type: schema.TypeInt,
Required: true,
ForceNew: false,
Description: "Number of partitions.",
},
"replication_factor": {
Type: schema.TypeInt,
ForceNew: false,
Optional: true,
Description: "Number of replication factors.",
},
"config": {
Type: schema.TypeMap,
Optional: true,
Computed: true,
Description: "A map of string k/v attributes.",
Elem: schema.TypeString,
},
"cluster_id": {
Type: schema.TypeString,
ForceNew: true,
Required: true,
Description: "The ID of cluster",
},
},
}
}
func topicsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*confluent.Client)
clusterId := d.Get("cluster_id").(string)
topicName := d.Id()
//topicName := d.Get("topic_name").(string)
topic, err := c.GetTopic(clusterId, topicName)
if err != nil {
log.Printf("[ERROR] Error getting topics %s from Confluent", err)
d.SetId("")
return diag.FromErr(err)
}
log.Printf("[DEBUG] Setting the state from Confluent %v", topic)
err = d.Set("name", topic.Name)
if err != nil {
return diag.FromErr(err)
}
return diag.FromErr(err)
}
func topicsCreate(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[INFO] Creating topic" + d.Get("name").(string))
c := meta.(*confluent.Client)
topicName := d.Get("name").(string)
if err := topicCreateFunc(c, d); err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] Creat topic" + d.Get("name").(string) + "successfully!")
d.SetId(topicName)
return diags
}
func topicCreateFunc(c *confluent.Client, d *schema.ResourceData) error {
clusterId:= d.Get("cluster_id").(string)
topicName := d.Get("name").(string)
partitionsCount := d.Get("partitions").(int)
replicationFactor := d.Get("replication_factor").(int)
config := d.Get("config").(map[string]interface{})
if confluentPlacementConstraintsIsPresent(d) {
replicationFactor = 0
}
var topicConfigs []confluent.TopicConfig
for key, value := range config {
switch value := value.(type) {
case string:
topicConfigs = append(topicConfigs, confluent.TopicConfig{
Name: key,
Value: value,
})
}
}
err = c.CreateTopic(clusterId, topicName, partitionsCount, replicationFactor, topicConfigs, nil)
if err != nil {
return err
}
d.SetId(topicName)
return nil
}
func topicsDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[INFO] Delete topic" + d.Get("name").(string))
c := meta.(*confluent.Client)
clusterId:= d.Get("cluster_id").(string)
topicName := d.Id()
if err := c.DeleteTopic(clusterId, topicName); err != nil {
return diag.FromErr(err)
}
if err := waitForTopicDelete(ctx, c, d.Id(), clusterId); err != nil {
return diag.FromErr(err)
}
log.Printf("[INFO] Topic" + d.Get("name").(string) + " deleted!")
return diags
}
func topicsUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*confluent.Client)
clusterId:= d.Get("cluster_id").(string)
if err != nil {
return diag.FromErr(err)
}
t := confluent.Topic{
Name: d.Id(),
}
// update replica count of existing partitions before adding new ones
if d.HasChange("replication_factor") {
if !(confluentPlacementConstraintsIsPresent(d)) {
oi, ni := d.GetChange("replication_factor")
oldRF := oi.(int)
newRF := ni.(int)
t.ReplicationFactor = int16(newRF)
log.Printf("[INFO] Updating replication_factor from %d to %d", oldRF, newRF)
err := c.UpdateReplicationsFactor(t)
if err != nil {
return diag.FromErr(err)
}
if err := waitForRFUpdate(ctx, c, d.Id()); err != nil {
return diag.FromErr(err)
}
if err := waitForTopicRefresh(ctx, c, d.Id(), clusterId, t); err != nil {
return diag.FromErr(err)
}
}
}
if d.HasChange("partitions") {
// update should only be called when we're increasing partitions
oi, ni := d.GetChange("partitions")
oldPartitions := oi.(int)
newPartitions := ni.(int)
if newPartitions < oldPartitions {
return diag.FromErr(fmt.Errorf("cannot decrease the number of partitions of topic"))
}
log.Printf("[INFO] Updating partitions from %d to %d", oldPartitions, newPartitions)
t.Partitions = int32(newPartitions)
if err := c.UpdatePartitions(t); err != nil {
return diag.FromErr(err)
}
if err := waitForTopicRefresh(ctx, c, d.Id(), clusterId, t); err != nil {
return diag.FromErr(err)
}
}
if d.HasChange("config") {
config := d.Get("config").(map[string]interface{})
var topicConfigs []confluent.TopicConfig
for key, value := range config {
switch value := value.(type) {
case string:
topicConfigs = append(topicConfigs, confluent.TopicConfig{
Name: key,
Value: value,
})
}
}
if err := c.UpdateTopicConfigs(clusterId, d.Id(),topicConfigs); err != nil {
return diag.FromErr(err)
}
}
if err := waitForTopicRefresh(ctx, c, d.Id(), clusterId, t); err != nil {
return diag.FromErr(err)
}
return nil
}
func confluentPlacementConstraintsIsPresent(d *schema.ResourceData) bool {
config := d.Get("config").(map[string]interface{})
for config["confluent.placement.constraints"] != nil {
return true
}
return false
}
func waitForRFUpdate(ctx context.Context, c *confluent.Client, topic string) error {
refresh := func() (interface{}, string, error) {
isRFUpdating, err := c.IsReplicationFactorUpdating(topic)
if err != nil {
return nil, "Error", err
} else if isRFUpdating {
return nil, "Updating", nil
} else {
return "not-nil", "Ready", nil
}
}
stateConf := &resource.StateChangeConf{
Pending: []string{"Updating"},
Target: []string{"Ready"},
Refresh: refresh,
Timeout: 120 * time.Second,
Delay: 1 * time.Second,
PollInterval: 1 * time.Second,
MinTimeout: 2 * time.Second,
}
if _, err := stateConf.WaitForStateContext(ctx); err != nil {
return fmt.Errorf(
"error waiting for topic (%s) replication_factor to update: %s",
topic, err)
}
return nil
}
func waitForTopicDelete(ctx context.Context,c *confluent.Client, topic, clusterId string) error {
stateConf := &resource.StateChangeConf{
Pending: []string{"Updating"},
Target: []string{"Ready"},
Refresh: topicDeleteRefreshFunc(c, topic, clusterId),
Timeout: 120 * time.Second,
Delay: 1 * time.Second,
PollInterval: 1 * time.Second,
MinTimeout: 2 * time.Second,
}
if _, err := stateConf.WaitForStateContext(ctx); err != nil {
return fmt.Errorf(
"error waiting for topic (%s) to become ready: %s",
topic, err)
}
return nil
}
func waitForTopicRefresh(ctx context.Context,c *confluent.Client, topic, clusterId string, expected confluent.Topic) error {
stateConf := &resource.StateChangeConf{
Pending: []string{"Updating"},
Target: []string{"Ready"},
Refresh: topicRefreshFunc(c, topic, clusterId, expected),
Timeout: 120 * time.Second,
Delay: 1 * time.Second,
PollInterval: 1 * time.Second,
MinTimeout: 2 * time.Second,
}
if _, err := stateConf.WaitForStateContext(ctx); err != nil {
return fmt.Errorf(
"error waiting for topic (%s) to become ready: %s",
topic, err)
}
return nil
}
func topicRefreshFunc(c *confluent.Client, topic, clusterId string, expected confluent.Topic) resource.StateRefreshFunc {
return func() (result interface{}, s string, err error) {
log.Printf("[DEBUG] waiting for topic to update %s", topic)
actual, err := c.GetTopic(clusterId, topic)
if err != nil {
log.Printf("[ERROR] could not read topic %s, %s", topic, err)
return actual, "Error", err
}
p, err := c.GetTopicPartitions(clusterId, topic)
if err != nil {
log.Printf("[ERROR] could not read topic %s, %s", topic, err)
return actual, "Error", err
}
if int16(actual.ReplicationFactor) == expected.ReplicationFactor && int32(len(p)) == expected.Partitions {
return actual, "Ready", nil
}
return nil, fmt.Sprintf("%v != %v", "", ""), nil
}
}
func topicDeleteRefreshFunc(c *confluent.Client, topic, clusterId string) resource.StateRefreshFunc {
return func() (result interface{}, s string, err error) {
log.Printf("[DEBUG] waiting for topic to delete %s", topic)
actual, err := c.GetTopic(clusterId, topic)
if err != nil && strings.Contains(err.Error(), "404") {
return actual, "Ready", nil
}
return nil, fmt.Sprintf("cannot delete topic: %v", topic), nil
}
}