/
resource_kafka_client_quota.go
333 lines (283 loc) · 14.4 KB
/
resource_kafka_client_quota.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
333
// Copyright 2022 Confluent Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package provider
import (
"context"
"encoding/json"
"fmt"
quotas "github.com/confluentinc/ccloud-sdk-go-v2/kafka-quotas/v1"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"net/http"
"time"
)
const (
paramThroughput = "throughput"
paramIngressByteRate = "ingress_byte_rate"
paramEgressByteRate = "egress_byte_rate"
paramPrincipals = "principals"
kafkaQuotasAPIWaitAfterCreate = 30 * time.Second
kafkaQuotasAPIWaitAfterUpdate = 15 * time.Second
)
var attributeIngressByteRate = fmt.Sprintf("%s.0.%s", paramThroughput, paramIngressByteRate)
var attributeEgressByteRate = fmt.Sprintf("%s.0.%s", paramThroughput, paramEgressByteRate)
func kafkaClientQuotaResource() *schema.Resource {
return &schema.Resource{
CreateContext: kafkaClientQuotaCreate,
ReadContext: kafkaClientQuotaRead,
UpdateContext: kafkaClientQuotaUpdate,
DeleteContext: kafkaClientQuotaDelete,
Importer: &schema.ResourceImporter{
StateContext: kafkaClientQuotaImport,
},
Schema: map[string]*schema.Schema{
paramDisplayName: {
Type: schema.TypeString,
Required: true,
Description: "The name of the Kafka Client Quota.",
ValidateFunc: validation.StringIsNotEmpty,
},
paramDescription: {
Type: schema.TypeString,
Optional: true,
Description: "A description of the Kafka Client Quota.",
ValidateFunc: validation.StringIsNotEmpty,
},
paramKafkaCluster: requiredKafkaClusterBlockSchema(),
paramEnvironment: environmentSchema(),
paramPrincipals: {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
MinItems: 1,
Required: true,
Description: "A list of service accounts and identity pools. Special name \"default\" can be used to represent the default quota for all users and service accounts.",
},
paramThroughput: throughputSchema(),
},
}
}
func kafkaClientQuotaUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
if d.HasChangesExcept(paramDisplayName, paramDescription, paramPrincipals, paramThroughput) {
return diag.Errorf("error updating Kafka Client Quota %q: only %q, %q, %q, %q attributes can be updated for Kafka Client Quota", d.Id(), paramDisplayName, paramDescription, paramPrincipals, paramThroughput)
}
updateKafkaClientQuotaRequest := quotas.NewKafkaQuotasV1ClientQuotaUpdate()
updateSpec := quotas.NewKafkaQuotasV1ClientQuotaSpecUpdate()
if d.HasChange(paramDisplayName) {
updatedDisplayName := d.Get(paramDisplayName).(string)
updateSpec.SetDisplayName(updatedDisplayName)
}
if d.HasChange(paramDescription) {
updatedDescription := d.Get(paramDescription).(string)
updateSpec.SetDescription(updatedDescription)
}
if d.HasChange(paramPrincipals) {
updatedPrincipals := convertSetToStringList(d, paramPrincipals)
updateSpec.SetPrincipals(convertToGlobalObjectReferences(updatedPrincipals))
}
if d.HasChange(paramThroughput) {
updatedIngressByteRate := d.Get(attributeIngressByteRate).(string)
updatedEgressByteRate := d.Get(attributeEgressByteRate).(string)
updatedThroughput := quotas.NewKafkaQuotasV1Throughput(updatedIngressByteRate, updatedEgressByteRate)
updateSpec.SetThroughput(*updatedThroughput)
}
updateKafkaClientQuotaRequest.SetSpec(*updateSpec)
updateKafkaClientQuotaRequestJson, err := json.Marshal(updateKafkaClientQuotaRequest)
if err != nil {
return diag.Errorf("error updating Kafka Client Quota %q: error marshaling %#v to json: %s", d.Id(), updateKafkaClientQuotaRequest, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Updating Kafka Client Quota %q: %s", d.Id(), updateKafkaClientQuotaRequestJson), map[string]interface{}{kafkaClientQuotaLoggingKey: d.Id()})
c := meta.(*Client)
updatedClientQuota, _, err := c.quotasClient.ClientQuotasKafkaQuotasV1Api.UpdateKafkaQuotasV1ClientQuota(c.quotasApiContext(ctx), d.Id()).KafkaQuotasV1ClientQuotaUpdate(*updateKafkaClientQuotaRequest).Execute()
if err != nil {
return diag.Errorf("error updating Kafka Client Quota %q: %s", d.Id(), createDescriptiveError(err))
}
time.Sleep(kafkaQuotasAPIWaitAfterUpdate)
updatedClientQuotaJson, err := json.Marshal(updatedClientQuota)
if err != nil {
return diag.Errorf("error updating Kafka Client Quota %q: error marshaling %#v to json: %s", d.Id(), updatedClientQuota, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Finished updating Kafka Client Quota %q: %s", d.Id(), updatedClientQuotaJson), map[string]interface{}{kafkaClientQuotaLoggingKey: d.Id()})
return kafkaClientQuotaRead(ctx, d, meta)
}
func kafkaClientQuotaCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*Client)
displayName := d.Get(paramDisplayName).(string)
description := d.Get(paramDescription).(string)
kafkaClusterId := extractStringValueFromBlock(d, paramKafkaCluster, paramId)
environmentId := extractStringValueFromBlock(d, paramEnvironment, paramId)
ingressByteRate := extractStringValueFromBlock(d, paramThroughput, paramIngressByteRate)
egressByteRate := extractStringValueFromBlock(d, paramThroughput, paramEgressByteRate)
globalObjectReferencePrincipals := convertToGlobalObjectReferences(convertSetToStringList(d, paramPrincipals))
spec := quotas.NewKafkaQuotasV1ClientQuotaSpec()
spec.SetDisplayName(displayName)
spec.SetDescription(description)
spec.SetCluster(quotas.EnvScopedObjectReference{Id: kafkaClusterId})
spec.SetEnvironment(quotas.GlobalObjectReference{Id: environmentId})
spec.SetPrincipals(globalObjectReferencePrincipals)
spec.SetThroughput(quotas.KafkaQuotasV1Throughput{IngressByteRate: ingressByteRate, EgressByteRate: egressByteRate})
createKafkaClientQuotaRequest := quotas.KafkaQuotasV1ClientQuota{Spec: spec}
createKafkaClientQuotaRequestJson, err := json.Marshal(createKafkaClientQuotaRequest)
if err != nil {
return diag.Errorf("error creating Kafka Client Quota: error marshaling %#v to json: %s", createKafkaClientQuotaRequest, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Creating new Kafka Client Quota: %s", createKafkaClientQuotaRequestJson))
createdKafkaClientQuota, _, err := executeKafkaClientQuotaCreate(c.quotasApiContext(ctx), c, createKafkaClientQuotaRequest)
if err != nil {
return diag.Errorf("error creating Kafka Client Quota: %s", createDescriptiveError(err))
}
d.SetId(createdKafkaClientQuota.GetId())
time.Sleep(kafkaQuotasAPIWaitAfterCreate)
createdClientQuotaJson, err := json.Marshal(createdKafkaClientQuota)
if err != nil {
return diag.Errorf("error creating Kafka Client Quota: %q: error marshaling %#v to json: %s", createdKafkaClientQuota.GetId(), createdKafkaClientQuota, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Finished creating Kafka Client Quota %q: %s", d.Id(), createdClientQuotaJson), map[string]interface{}{kafkaClientQuotaLoggingKey: d.Id()})
return kafkaClientQuotaRead(ctx, d, meta)
}
func executeKafkaClientQuotaCreate(ctx context.Context, c *Client, kafkaClientQuota quotas.KafkaQuotasV1ClientQuota) (quotas.KafkaQuotasV1ClientQuota, *http.Response, error) {
req := c.quotasClient.ClientQuotasKafkaQuotasV1Api.CreateKafkaQuotasV1ClientQuota(c.quotasApiContext(ctx)).KafkaQuotasV1ClientQuota(kafkaClientQuota)
return req.Execute()
}
func kafkaClientQuotaDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
tflog.Debug(ctx, fmt.Sprintf("Deleting Kafka Client Quota %q", d.Id()), map[string]interface{}{kafkaClientQuotaLoggingKey: d.Id()})
c := meta.(*Client)
req := c.quotasClient.ClientQuotasKafkaQuotasV1Api.DeleteKafkaQuotasV1ClientQuota(c.quotasApiContext(ctx), d.Id())
_, err := req.Execute()
if err != nil {
return diag.Errorf("error deleting Kafka Client Quota %q: %s", d.Id(), createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Finished deleting Kafka Client Quota %q", d.Id()), map[string]interface{}{kafkaClientQuotaLoggingKey: d.Id()})
return nil
}
func kafkaClientQuotaRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
tflog.Debug(ctx, fmt.Sprintf("Reading Kafka Client Quota %q", d.Id()), map[string]interface{}{kafkaClientQuotaLoggingKey: d.Id()})
kafkaClientQuotaId := d.Id()
if _, err := readKafkaClientQuotaAndSetAttributes(ctx, d, meta, kafkaClientQuotaId); err != nil {
return diag.FromErr(fmt.Errorf("error reading Kafka Client Quota %q: %s", d.Id(), createDescriptiveError(err)))
}
return nil
}
func readKafkaClientQuotaAndSetAttributes(ctx context.Context, d *schema.ResourceData, meta interface{}, kafkaClientQuotaId string) ([]*schema.ResourceData, error) {
c := meta.(*Client)
kafkaClientQuota, resp, err := executeKafkaClientQuotaRead(c.quotasApiContext(ctx), c, kafkaClientQuotaId)
if err != nil {
tflog.Warn(ctx, fmt.Sprintf("Error reading Kafka Client Quota %q: %s", d.Id(), createDescriptiveError(err)), map[string]interface{}{kafkaClientQuotaLoggingKey: d.Id()})
isResourceNotFound := isNonKafkaRestApiResourceNotFound(resp)
if isResourceNotFound && !d.IsNewResource() {
tflog.Warn(ctx, fmt.Sprintf("Removing Kafka Client Quota %q in TF state because Kafka Client Quota could not be found on the server", d.Id()), map[string]interface{}{kafkaClientQuotaLoggingKey: d.Id()})
d.SetId("")
return nil, nil
}
return nil, createDescriptiveError(err)
}
kafkaClientQuotaJson, err := json.Marshal(kafkaClientQuota)
if err != nil {
return nil, fmt.Errorf("error reading Kafka Client Quota %q: error marshaling %#v to json: %s", d.Id(), kafkaClientQuota, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Fetched Kafka Client Quota %q: %s", d.Id(), kafkaClientQuotaJson), map[string]interface{}{kafkaClientQuotaLoggingKey: d.Id()})
if _, err := setKafkaClientQuotaAttributes(d, kafkaClientQuota); err != nil {
return nil, createDescriptiveError(err)
}
tflog.Debug(ctx, fmt.Sprintf("Finished reading Kafka Client Quota %q", d.Id()), map[string]interface{}{kafkaClientQuotaLoggingKey: d.Id()})
return []*schema.ResourceData{d}, nil
}
func executeKafkaClientQuotaRead(ctx context.Context, c *Client, kafkaClientQuotaId string) (quotas.KafkaQuotasV1ClientQuota, *http.Response, error) {
req := c.quotasClient.ClientQuotasKafkaQuotasV1Api.GetKafkaQuotasV1ClientQuota(c.quotasApiContext(ctx), kafkaClientQuotaId)
return req.Execute()
}
func setKafkaClientQuotaAttributes(d *schema.ResourceData, kafkaClientQuota quotas.KafkaQuotasV1ClientQuota) (*schema.ResourceData, error) {
if err := d.Set(paramDisplayName, kafkaClientQuota.Spec.GetDisplayName()); err != nil {
return nil, createDescriptiveError(err)
}
if err := d.Set(paramDescription, kafkaClientQuota.Spec.GetDescription()); err != nil {
return nil, createDescriptiveError(err)
}
// Principals
if err := d.Set(paramPrincipals, convertToListOfIds(kafkaClientQuota.Spec.GetPrincipals())); err != nil {
return nil, createDescriptiveError(err)
}
// Throughput
if err := d.Set(paramThroughput, []interface{}{map[string]interface{}{
paramIngressByteRate: kafkaClientQuota.Spec.Throughput.GetIngressByteRate(),
paramEgressByteRate: kafkaClientQuota.Spec.Throughput.GetEgressByteRate()}}); err != nil {
return nil, err
}
if err := setStringAttributeInListBlockOfSizeOne(paramKafkaCluster, paramId, kafkaClientQuota.Spec.Cluster.GetId(), d); err != nil {
return nil, err
}
if err := setStringAttributeInListBlockOfSizeOne(paramEnvironment, paramId, kafkaClientQuota.Spec.Environment.GetId(), d); err != nil {
return nil, err
}
d.SetId(kafkaClientQuota.GetId())
return d, nil
}
func kafkaClientQuotaImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
tflog.Debug(ctx, fmt.Sprintf("Importing Kafka Client Quota %q", d.Id()), map[string]interface{}{kafkaClientQuotaLoggingKey: d.Id()})
// Mark resource as new to avoid d.Set("") when getting 404
d.MarkNewResource()
if diagnostics := kafkaClientQuotaRead(ctx, d, meta); diagnostics != nil {
return nil, fmt.Errorf("error importing Kafka Client Quota %q: %s", d.Id(), diagnostics[0].Summary)
}
tflog.Debug(ctx, fmt.Sprintf("Finished importing Kafka Client Quota %q", d.Id()), map[string]interface{}{kafkaClientQuotaLoggingKey: d.Id()})
return []*schema.ResourceData{d}, nil
}
// https://github.com/hashicorp/terraform-plugin-sdk/issues/155#issuecomment-489699737
//// alternative - https://github.com/hashicorp/terraform-plugin-sdk/issues/248#issuecomment-725013327
func throughputSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
MinItems: 1,
MaxItems: 1,
Required: true,
Description: "Block for representing a Kafka Quota.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
paramIngressByteRate: {
Type: schema.TypeString,
Required: true,
Description: "The ingress throughput limit in bytes per second.",
},
paramEgressByteRate: {
Type: schema.TypeString,
Required: true,
Description: "The egress throughput limit in bytes per second.",
},
},
},
}
}
func convertToGlobalObjectReferences(ids []string) []quotas.GlobalObjectReference {
globalObjectReferences := make([]quotas.GlobalObjectReference, len(ids))
for i, id := range ids {
globalObjectReferences[i] = quotas.GlobalObjectReference{Id: id}
}
return globalObjectReferences
}
func convertToListOfIds(globalObjectReferences []quotas.GlobalObjectReference) []string {
ids := make([]string, len(globalObjectReferences))
for i, globalObjectReference := range globalObjectReferences {
ids[i] = globalObjectReference.GetId()
}
return ids
}
func convertSetToStringList(d *schema.ResourceData, attributeName string) []string {
setValues := d.Get(attributeName).(*schema.Set).List()
stringSetValues := make([]string, len(setValues))
for i := range setValues {
stringSetValues[i] = setValues[i].(string)
}
return stringSetValues
}