generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 25
/
types_streaming_namespace_policies.go
379 lines (342 loc) · 18.4 KB
/
types_streaming_namespace_policies.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
package provider
import (
"context"
"encoding/json"
"fmt"
"github.com/datastax/pulsar-admin-client-go/src/pulsaradmin"
"github.com/hashicorp/terraform-plugin-framework-validators/mapvalidator"
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/int64planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/mapplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/objectplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)
const (
// String constants to help avoid typos. These are used to define the Terraform
// schema for Pulsar namespace policies, therefore they must use snake_case.
// The capitalization rules may not always match the capitalization of the JSON
// policy coming from Pulsar which sometimes uses camelCase.
policyAllowAutoTopicCreation = "allow_auto_topic_creation"
policyAutoTopicCreationOverride = "auto_topic_creation_override"
policyAutoTopicCreationType = "topic_type"
policyAutoTopicCreationDefaultNumPartitions = "default_num_partitions"
policyBacklogQuotaMap = "backlog_quota_map"
policyBacklogQuotaLimit = "limit"
policyBacklogQuotaLimitSize = "limit_size"
policyBacklogQuotaLimitTime = "limit_time"
policyBacklogQuotaLimitPolicy = "policy"
policyIsAllowAutoUpdateSchema = "is_allow_auto_update_schema"
policyMessageTTLInSeconds = "message_ttl_in_seconds"
policyRetentionPolicies = "retention_policies"
policyRetentionTimeInMinutes = "retention_time_in_minutes"
policyRetentionSizeInMB = "retention_size_in_mb"
policySchemaAutoUpdateCompatibilityStrategy = "schema_auto_update_compatibility_strategy"
policySchemaCompatibilityStrategy = "schema_compatibility_strategy"
policySchemaValidationEnforced = "schema_validation_enforced"
policyInactiveTopicPolicies = "inactive_topic_policies"
policyInactiveTopicDeleteWhileInactive = "delete_while_inactive"
policyInactiveTopicMaxInactiveDurationSeconds = "max_inactive_duration_seconds"
policyInactiveTopicDeleteMode = "delete_mode"
policySubscriptionExpirationTimeMinutes = "subscription_expiration_time_minutes"
policyOffloadThreshold = "offload_threshold"
)
type PulsarNamespacePolicies struct {
IsAllowAutoUpdateSchema *bool `tfsdk:"is_allow_auto_update_schema" json:"is_allow_auto_update_schema,omitempty"`
MessageTTLInSeconds *int32 `tfsdk:"message_ttl_in_seconds" json:"message_ttl_in_seconds,omitempty"`
SchemaAutoUpdateCompatibilityStrategy *string `tfsdk:"schema_auto_update_compatibility_strategy" json:"schema_auto_update_compatibility_strategy,omitempty"`
SchemaCompatibilityStrategy *string `tfsdk:"schema_compatibility_strategy" json:"schema_compatibility_strategy,omitempty"`
SchemaValidationEnforced *bool `tfsdk:"schema_validation_enforced" json:"schema_validation_enforced,omitempty"`
AutoTopicCreationOverride *PulsarNamespaceAutoTopicCreationOverride `tfsdk:"auto_topic_creation_override" json:"autoTopicCreationOverride,omitempty"`
BacklogQuota map[string]*PulsarNamespaceBacklogQuota `tfsdk:"backlog_quota_map" json:"backlog_quota_map,omitempty"`
RetentionPolicies *PulsarNamespaceRetentionPolicies `tfsdk:"retention_policies" json:"retention_policies,omitempty"`
InactiveTopicPolicies *PulsarNamespaceInactiveTopicPolicies `tfsdk:"inactive_topic_policies" json:"inactive_topic_policies,omitempty"`
SubscriptionExpirationTimeMinutes *int64 `tfsdk:"subscription_expiration_time_minutes" json:"subscription_expiration_time_minutes,omitempty"`
OffloadThreshold *int64 `tfsdk:"offload_threshold" json:"offload_threshold,omitempty"`
}
type PulsarNamespaceRetentionPolicies struct {
RetentionTimeInMinutes *int32 `tfsdk:"retention_time_in_minutes" json:"retentionTimeInMinutes,omitempty"`
RetentionSizeInMB *int64 `tfsdk:"retention_size_in_mb" json:"retentionSizeInMB,omitempty"`
}
type PulsarNamespaceAutoTopicCreationOverride struct {
AllowAutoTopicCreation *bool `tfsdk:"allow_auto_topic_creation" json:"allowAutoTopicCreation,omitempty"`
TopicType *string `tfsdk:"topic_type" json:"topicType,omitempty"`
DefaultNumPartitions *int64 `tfsdk:"default_num_partitions" json:"defaultNumPartitions,omitempty"`
}
type PulsarNamespaceBacklogQuota struct {
Limit *int64 `tfsdk:"limit" json:"limit,omitempty"`
LimitSize *int64 `tfsdk:"limit_size" json:"limitSize,omitempty"`
LimitTime *int64 `tfsdk:"limit_time" json:"limitTime,omitempty"`
Policy *string `tfsdk:"policy" json:"policy,omitempty"`
}
type PulsarNamespaceInactiveTopicPolicies struct {
DeleteWhileInactive *bool `tfsdk:"delete_while_inactive" json:"deleteWhileInactive,omitempty"`
DeleteMode *string `tfsdk:"delete_mode" json:"inactiveTopicDeleteMode,omitempty"`
MaxInactiveDurationSeconds *int64 `tfsdk:"max_inactive_duration_seconds" json:"maxInactiveDurationSeconds,omitempty"`
}
var (
boolPulsarNamespacePolicyAttribute = schema.BoolAttribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Bool{
boolplanmodifier.UseStateForUnknown(),
},
}
int64PulsarNamespacePolicyAttribute = schema.Int64Attribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Int64{
int64planmodifier.UseStateForUnknown(),
},
}
stringPulsarNamespacePolicyAttribute = schema.StringAttribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
}
// Schema definition for Pulsar namespace policies
pulsarNamespacePoliciesSchema = schema.SingleNestedAttribute{
Description: "Policies to be applied to the Pulsar namespace. For more details related to valid policy configuration, " +
"refer to the Pulsar namespace policies documentation (https://pulsar.apache.org/docs/3.0.x/admin-api-namespaces/).",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Object{
objectplanmodifier.UseStateForUnknown(),
},
Attributes: map[string]schema.Attribute{
policyIsAllowAutoUpdateSchema: boolPulsarNamespacePolicyAttribute,
policySchemaAutoUpdateCompatibilityStrategy: stringPulsarNamespacePolicyAttribute,
policySchemaCompatibilityStrategy: stringPulsarNamespacePolicyAttribute,
policySchemaValidationEnforced: boolPulsarNamespacePolicyAttribute,
policyMessageTTLInSeconds: int64PulsarNamespacePolicyAttribute,
policyAutoTopicCreationOverride: schema.SingleNestedAttribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Object{
objectplanmodifier.UseStateForUnknown(),
AutoTopicCreationOverridePlanModifier{},
},
Attributes: map[string]schema.Attribute{
policyAllowAutoTopicCreation: boolPulsarNamespacePolicyAttribute,
policyAutoTopicCreationType: stringPulsarNamespacePolicyAttribute,
policyAutoTopicCreationDefaultNumPartitions: int64PulsarNamespacePolicyAttribute,
},
},
policyBacklogQuotaMap: schema.MapNestedAttribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Map{
mapplanmodifier.UseStateForUnknown(),
},
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
policyBacklogQuotaLimit: int64PulsarNamespacePolicyAttribute,
policyBacklogQuotaLimitSize: int64PulsarNamespacePolicyAttribute,
policyBacklogQuotaLimitTime: int64PulsarNamespacePolicyAttribute,
policyBacklogQuotaLimitPolicy: schema.StringAttribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{
stringvalidator.OneOf([]string{
"producer_request_hold", "producer_exception", "consumer_backlog_eviction"}...,
),
},
},
},
},
Validators: []validator.Map{
mapvalidator.KeysAre(
stringvalidator.OneOf([]string{
"destination_storage", "message_age"}...,
),
),
},
},
policyRetentionPolicies: schema.SingleNestedAttribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Object{
objectplanmodifier.UseStateForUnknown(),
},
Attributes: map[string]schema.Attribute{
policyRetentionTimeInMinutes: int64PulsarNamespacePolicyAttribute,
policyRetentionSizeInMB: int64PulsarNamespacePolicyAttribute,
},
},
policyInactiveTopicPolicies: schema.SingleNestedAttribute{
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Object{
objectplanmodifier.UseStateForUnknown(),
},
Attributes: map[string]schema.Attribute{
policyInactiveTopicDeleteWhileInactive: boolPulsarNamespacePolicyAttribute,
policyInactiveTopicDeleteMode: stringPulsarNamespacePolicyAttribute,
policyInactiveTopicMaxInactiveDurationSeconds: int64PulsarNamespacePolicyAttribute,
},
},
policySubscriptionExpirationTimeMinutes: int64PulsarNamespacePolicyAttribute,
policyOffloadThreshold: int64PulsarNamespacePolicyAttribute,
},
}
)
func pulsarAdminPoliciesStructToPulsarNamespacePoliciesStruct(p *pulsaradmin.Policies) (*PulsarNamespacePolicies, error) {
if p == nil {
return nil, nil
}
data, err := json.Marshal(p)
if err != nil {
return nil, fmt.Errorf("failed to marshal Pulsar admin client namespace policies: %w", err)
}
policies := &PulsarNamespacePolicies{}
err = json.Unmarshal(data, policies)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal Terraform Pulsar namespace policies: %w", err)
}
return policies, nil
}
func pulsarNamespacePoliciesStructToPulsarAdminPoliciesStruct(p *PulsarNamespacePolicies) (*pulsaradmin.Policies, error) {
if p == nil {
return nil, nil
}
data, err := json.Marshal(p)
if err != nil {
return nil, fmt.Errorf("failed to marshal Terraform Pulsar namespace policies: %w", err)
}
policies := &pulsaradmin.Policies{}
err = json.Unmarshal(data, policies)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal Pulsar admin client namespace policies: %w", err)
}
return policies, nil
}
// pulsarNamespacePoliciesObjectToStruct converts from a Terraform object into a pulsar admin namespace policies struct
func pulsarNamespacePoliciesObjectToStruct(ctx context.Context, policyObj types.Object) (*pulsaradmin.Policies, diag.Diagnostics) {
diags := diag.Diagnostics{}
objOptions := basetypes.ObjectAsOptions{
UnhandledUnknownAsEmpty: true,
}
policies := &PulsarNamespacePolicies{}
diags.Append(policyObj.As(ctx, policies, objOptions)...)
adminClientPolicies, err := pulsarNamespacePoliciesStructToPulsarAdminPoliciesStruct(policies)
if err != nil {
diags.AddError("Unable to convert from Terraform Namespace Policies Object to Pulsar admin client struct", err.Error())
return nil, diags
}
return adminClientPolicies, diags
}
func pulsarNamespacePolicyError(policy string) string {
return fmt.Sprintf("Error setting namespace policy '%v', default values will be used. Perform a `terraform apply --refresh-only` view the diff", policy)
}
func getPulsarNamespacePolicies(ctx context.Context, pulsarAdminClient *pulsaradmin.ClientWithResponses, plan StreamingNamespaceResourceModel, requestEditors ...pulsaradmin.RequestEditorFn) (types.Object, diag.Diagnostics) {
diags := diag.Diagnostics{}
policiesAttrTypes := plan.Policies.AttributeTypes(ctx)
resp, err := pulsarAdminClient.NamespacesGetPoliciesWithResponse(ctx, plan.Tenant.ValueString(), plan.Namespace.ValueString(), requestEditors...)
diags.Append(HTTPResponseDiagErrWithBody(resp.StatusCode(), resp.Body, err, "failed to get namespace policies")...)
if diags.HasError() {
return types.ObjectNull(policiesAttrTypes), diags
}
policiesStruct, err := pulsarAdminPoliciesStructToPulsarNamespacePoliciesStruct(resp.JSON200)
if err != nil {
diags.AddError("Failed to convert from Terraform Namespace Policies Object to Pulsar admin client struct", err.Error())
return types.ObjectNull(policiesAttrTypes), diags
}
policiesTerraformObj, objDiags := types.ObjectValueFrom(ctx, policiesAttrTypes, policiesStruct)
diags.Append(objDiags...)
return policiesTerraformObj, diags
}
// setNamespacePolicies calls the endpoints for the various policy values to update
// TODO: can this be made more generic?
func setNamespacePolicies(ctx context.Context, client *pulsaradmin.ClientWithResponses, plan StreamingNamespaceResourceModel, requestEditors ...pulsaradmin.RequestEditorFn) diag.Diagnostics {
tenant := plan.Tenant.ValueString()
namespace := plan.Namespace.ValueString()
diags := diag.Diagnostics{}
if plan.Policies.IsNull() || plan.Policies.IsUnknown() {
return diags
}
policies, policyDiags := pulsarNamespacePoliciesObjectToStruct(ctx, plan.Policies)
diags.Append(policyDiags...)
// Individual attributes
if policies.IsAllowAutoUpdateSchema != nil {
resp, err := client.NamespacesSetIsAllowAutoUpdateSchema(ctx, tenant, namespace, *policies.IsAllowAutoUpdateSchema, requestEditors...)
diags.Append(HTTPResponseDiagWarn(resp, err, pulsarNamespacePolicyError(policySchemaValidationEnforced))...)
}
if policies.MessageTtlInSeconds != nil {
resp, err := client.NamespacesSetNamespaceMessageTTL(ctx, tenant, namespace, *policies.MessageTtlInSeconds, requestEditors...)
diags.Append(HTTPResponseDiagWarn(resp, err, pulsarNamespacePolicyError(policyMessageTTLInSeconds))...)
}
if policies.SchemaAutoUpdateCompatibilityStrategy != nil {
resp, err := client.NamespacesSetSchemaAutoUpdateCompatibilityStrategy(ctx, tenant, namespace, string(*policies.SchemaAutoUpdateCompatibilityStrategy), requestEditors...)
diags.Append(HTTPResponseDiagWarn(resp, err, pulsarNamespacePolicyError(policySchemaAutoUpdateCompatibilityStrategy))...)
}
if policies.SchemaCompatibilityStrategy != nil {
resp, err := client.NamespacesSetSchemaCompatibilityStrategy(ctx, tenant, namespace, string(*policies.SchemaCompatibilityStrategy), requestEditors...)
diags.Append(HTTPResponseDiagWarn(resp, err, pulsarNamespacePolicyError(policySchemaCompatibilityStrategy))...)
}
if policies.SchemaValidationEnforced != nil {
resp, err := client.NamespacesSetSchemaValidationEnforced(ctx, tenant, namespace, *policies.SchemaValidationEnforced, requestEditors...)
diags.Append(HTTPResponseDiagWarn(resp, err, pulsarNamespacePolicyError(policySchemaValidationEnforced))...)
}
if policies.OffloadThreshold != nil {
resp, err := client.NamespacesSetOffloadThreshold(ctx, tenant, namespace, *policies.OffloadThreshold, requestEditors...)
diags.Append(HTTPResponseDiagWarn(resp, err, pulsarNamespacePolicyError(policyOffloadThreshold))...)
}
// Nested objects
if policies.AutoTopicCreationOverride != nil {
resp, err := client.NamespacesSetAutoTopicCreation(ctx, tenant, namespace, *policies.AutoTopicCreationOverride, requestEditors...)
diags.Append(HTTPResponseDiagWarn(resp, err, pulsarNamespacePolicyError(policyAutoTopicCreationOverride))...)
}
if policies.BacklogQuotaMap != nil {
for quotaTypeName, quota := range *policies.BacklogQuotaMap {
quotaType := (pulsaradmin.NamespacesSetBacklogQuotaParamsBacklogQuotaType)(quotaTypeName)
params := pulsaradmin.NamespacesSetBacklogQuotaParams{BacklogQuotaType: "aType}
resp, err := client.NamespacesSetBacklogQuota(ctx, tenant, namespace, ¶ms, quota, requestEditors...)
diags.Append(HTTPResponseDiagWarn(resp, err, pulsarNamespacePolicyError(policyBacklogQuotaMap))...)
}
}
if policies.RetentionPolicies != nil {
resp, err := client.NamespacesSetRetention(ctx, tenant, namespace, *policies.RetentionPolicies, requestEditors...)
diags.Append(HTTPResponseDiagWarn(resp, err, pulsarNamespacePolicyError(policyRetentionPolicies))...)
}
if policies.InactiveTopicPolicies != nil {
resp, err := client.NamespacesSetInactiveTopicPolicies(ctx, tenant, namespace, *policies.InactiveTopicPolicies, requestEditors...)
diags.Append(HTTPResponseDiagWarn(resp, err, pulsarNamespacePolicyError(policyInactiveTopicPolicies))...)
}
if policies.SubscriptionExpirationTimeMinutes != nil {
resp, err := client.NamespacesSetSubscriptionExpirationTime(ctx, tenant, namespace, *policies.SubscriptionExpirationTimeMinutes, requestEditors...)
diags.Append(HTTPResponseDiagWarn(resp, err, pulsarNamespacePolicyError(policySubscriptionExpirationTimeMinutes))...)
}
return diags
}
// AutoTopicCreationOverridePlanModifier handles special cases related to auto topic creation, such as ensuring that
// the number of partitions is set to null if the topic_type is set to 'non-partitioned'
type AutoTopicCreationOverridePlanModifier struct{}
func (m AutoTopicCreationOverridePlanModifier) Description(_ context.Context) string {
return "Adjust plan for certain cases in auto topic creation override settings."
}
func (m AutoTopicCreationOverridePlanModifier) MarkdownDescription(_ context.Context) string {
return "Adjust plan for certain cases in auto topic creation override settings."
}
func (m AutoTopicCreationOverridePlanModifier) PlanModifyObject(ctx context.Context, req planmodifier.ObjectRequest, resp *planmodifier.ObjectResponse) {
// Do nothing if there is no state value.
if req.StateValue.IsNull() || req.StateValue.IsUnknown() {
return
}
// If topic type is non-partitioned, the number of partitions must be nil
if topicType, ok := req.PlanValue.Attributes()[policyAutoTopicCreationType]; ok && CompareTerraformAttrToString(topicType, "non-partitioned") {
newPlan, diags := UpdateTerraformObjectWithAttr(ctx, req.PlanValue, policyAutoTopicCreationDefaultNumPartitions, types.Int64Null())
resp.Diagnostics.Append(diags...)
resp.PlanValue = newPlan
}
}