-
-
Notifications
You must be signed in to change notification settings - Fork 128
/
resource_kafka_topic.go
323 lines (278 loc) · 8.5 KB
/
resource_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
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
package kafka
import (
"context"
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func kafkaTopicResource() *schema.Resource {
return &schema.Resource{
CreateContext: topicCreate,
ReadContext: topicRead,
UpdateContext: topicUpdate,
DeleteContext: topicDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
CustomizeDiff: customDiff,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The name of the topic.",
},
"partitions": {
Type: schema.TypeInt,
Required: true,
Description: "Number of partitions.",
ValidateFunc: validation.IntAtLeast(1),
},
"replication_factor": {
Type: schema.TypeInt,
Required: true,
ForceNew: false,
Description: "Number of replicas.",
ValidateFunc: validation.IntAtLeast(1),
},
"config": {
Type: schema.TypeMap,
Optional: true,
ForceNew: false,
Description: "A map of string k/v attributes.",
Elem: schema.TypeString,
},
},
}
}
func topicCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*LazyClient)
t := metaToTopic(d, meta)
err := c.CreateTopic(t)
if err != nil {
return diag.FromErr(err)
}
stateConf := &retry.StateChangeConf{
Pending: []string{"Pending"},
Target: []string{"Created"},
Refresh: topicCreateFunc(c, t),
Timeout: time.Duration(c.Config.Timeout) * time.Second,
Delay: 1 * time.Second,
PollInterval: 2 * time.Second,
}
if _, err := stateConf.WaitForStateContext(ctx); err != nil {
return diag.FromErr(fmt.Errorf("error waiting for topic (%s) to be created: %s", t.Name, err))
}
d.SetId(t.Name)
return nil
}
func topicCreateFunc(client *LazyClient, t Topic) retry.StateRefreshFunc {
return func() (result interface{}, s string, err error) {
topic, err := client.ReadTopic(t.Name, true)
switch e := err.(type) {
case TopicMissingError:
return topic, "Pending", nil
case nil:
return topic, "Created", nil
default:
return topic, "Error", e
}
}
}
func topicUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*LazyClient)
t := metaToTopic(d, meta)
if err := c.UpdateTopic(t); err != nil {
return diag.FromErr(err)
}
// update replica count of existing partitions before adding new ones
if d.HasChange("replication_factor") {
oi, ni := d.GetChange("replication_factor")
oldRF := oi.(int)
newRF := ni.(int)
log.Printf("[INFO] Updating replication_factor from %d to %d", oldRF, newRF)
t.ReplicationFactor = int16(newRF)
if err := c.AlterReplicationFactor(t); err != nil {
return diag.FromErr(err)
}
if err := waitForRFUpdate(ctx, c, d.Id()); 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)
log.Printf("[INFO] Updating partitions from %d to %d", oldPartitions, newPartitions)
t.Partitions = int32(newPartitions)
if err := c.AddPartitions(t); err != nil {
return diag.FromErr(err)
}
}
if err := waitForTopicRefresh(ctx, c, d.Id(), t); err != nil {
return diag.FromErr(err)
}
return nil
}
func waitForRFUpdate(ctx context.Context, client *LazyClient, topic string) error {
refresh := func() (interface{}, string, error) {
isRFUpdating, err := client.IsReplicationFactorUpdating(topic)
if err != nil {
return nil, "Error", err
} else if isRFUpdating {
return nil, "Updating", nil
} else {
return "not-nil", "Ready", nil
}
}
timeout := time.Duration(client.Config.Timeout) * time.Second
stateConf := &retry.StateChangeConf{
Pending: []string{"Updating"},
Target: []string{"Ready"},
Refresh: refresh,
Timeout: timeout,
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 waitForTopicRefresh(ctx context.Context, client *LazyClient, topic string, expected Topic) error {
timeout := time.Duration(client.Config.Timeout) * time.Second
stateConf := &retry.StateChangeConf{
Pending: []string{"Updating"},
Target: []string{"Ready"},
Refresh: topicRefreshFunc(client, topic, expected),
Timeout: timeout,
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(client *LazyClient, topic string, expected Topic) retry.StateRefreshFunc {
return func() (result interface{}, s string, err error) {
log.Printf("[DEBUG] waiting for topic to update %s", topic)
actual, err := client.ReadTopic(topic, true)
if err != nil {
log.Printf("[ERROR] could not read topic %s, %s", topic, err)
return actual, "Error", err
}
if expected.Equal(actual) {
return actual, "Ready", nil
}
return nil, fmt.Sprintf("%v != %v", strPtrMapToStrMap(actual.Config), strPtrMapToStrMap(expected.Config)), nil
}
}
func topicDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*LazyClient)
t := metaToTopic(d, meta)
err := c.DeleteTopic(t.Name)
if err != nil {
return diag.FromErr(err)
}
log.Printf("[DEBUG] waiting for topic to delete? %s", t.Name)
stateConf := &retry.StateChangeConf{
Pending: []string{"Pending"},
Target: []string{"Deleted"},
Refresh: topicDeleteFunc(c, d.Id(), t),
Timeout: 300 * time.Second,
Delay: 3 * time.Second,
PollInterval: 2 * time.Second,
MinTimeout: 20 * time.Second,
}
_, err = stateConf.WaitForStateContext(ctx)
if err != nil {
return diag.FromErr(fmt.Errorf("Error waiting for topic (%s) to delete: %s", d.Id(), err))
}
log.Printf("[DEBUG] deletetopic done! %s", t.Name)
d.SetId("")
return nil
}
func topicDeleteFunc(client *LazyClient, id string, t Topic) retry.StateRefreshFunc {
return func() (result interface{}, s string, err error) {
topic, err := client.ReadTopic(t.Name, true)
log.Printf("[DEBUG] deletetopic read %s, %v", t.Name, err)
if err != nil {
_, ok := err.(TopicMissingError)
if ok {
return topic, "Deleted", nil
}
return topic, "UNKNOWN", err
}
return topic, "Pending", nil
}
}
func topicRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
name := d.Id()
client := meta.(*LazyClient)
topic, err := client.ReadTopic(name, false)
if err != nil {
log.Printf("[ERROR] Error getting topics %s from Kafka", err)
_, ok := err.(TopicMissingError)
if ok {
d.SetId("")
return nil
}
return diag.FromErr(err)
}
log.Printf("[DEBUG] Setting the state from Kafka %v", topic)
errSet := errSetter{d: d}
errSet.Set("name", topic.Name)
errSet.Set("partitions", topic.Partitions)
errSet.Set("replication_factor", topic.ReplicationFactor)
errSet.Set("config", topic.Config)
if errSet.err != nil {
return diag.FromErr(errSet.err)
}
return nil
}
func customDiff(ctx context.Context, diff *schema.ResourceDiff, v interface{}) error {
// Skip custom logic for resource creation.
if diff.Id() == "" {
return nil
}
if diff.HasChange("partitions") {
log.Printf("[INFO] Partitions have changed!")
o, n := diff.GetChange("partitions")
oi := o.(int)
ni := n.(int)
log.Printf("[INFO] Partitions is changing from %d to %d", oi, ni)
if ni < oi {
log.Printf("Partitions decreased from %d to %d. Forcing new resource", oi, ni)
if err := diff.ForceNew("partitions"); err != nil {
return err
}
}
}
if diff.HasChange("replication_factor") {
log.Printf("[INFO] Checking the diff!")
client := v.(*LazyClient)
canAlterRF, err := client.CanAlterReplicationFactor()
if err != nil {
return err
}
if !canAlterRF {
log.Println("[INFO] Need Kafka >= 2.4.0 to update replication_factor in-place")
if err := diff.ForceNew("replication_factor"); err != nil {
return err
}
}
}
return nil
}