Skip to content

Commit

Permalink
feat(kafka_quota): added support for kafka quota
Browse files Browse the repository at this point in the history
  • Loading branch information
vmyroslav committed Dec 27, 2024
1 parent 9f80a61 commit 200b493
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 26 deletions.
31 changes: 6 additions & 25 deletions internal/sdkprovider/service/kafka/kafka_quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ The most relevant quota is chosen for each connection.
All connections within a quota group share the same quota.
It is possible to set default quotas for each (user, client-id), user or client-id group by specifying 'default'`,
ValidateFunc: schemautil.GetServiceUserValidateFunc(),
AtLeastOneOf: []string{"user", "client_id"},
},
"client_id": {
Type: schema.TypeString,
Expand All @@ -45,6 +46,7 @@ The most relevant quota is chosen for each connection.
All connections within a quota group share the same quota.
It is possible to set default quotas for each (user, client-id), user or client-id group by specifying 'default'`,
ValidateFunc: validation.StringLenBetween(1, 255),
AtLeastOneOf: []string{"user", "client_id"},
},
"consumer_byte_rate": {
Type: schema.TypeInt,
Expand All @@ -54,6 +56,7 @@ Defines the bandwidth limit in bytes/sec for each group of clients sharing a quo
Every distinct client group is allocated a specific quota, as defined by the cluster, on a per-broker basis.
Exceeding this limit results in client throttling.`,
ValidateFunc: validation.IntBetween(0, 1073741824),
AtLeastOneOf: []string{"consumer_byte_rate", "producer_byte_rate", "request_percentage"},
},
"producer_byte_rate": {
Type: schema.TypeInt,
Expand All @@ -63,6 +66,7 @@ Defines the bandwidth limit in bytes/sec for each group of clients sharing a quo
Every distinct client group is allocated a specific quota, as defined by the cluster, on a per-broker basis.
Exceeding this limit results in client throttling.`,
ValidateFunc: validation.IntBetween(0, 1073741824),
AtLeastOneOf: []string{"consumer_byte_rate", "producer_byte_rate", "request_percentage"},
},
"request_percentage": {
Type: schema.TypeInt,
Expand All @@ -72,6 +76,7 @@ Sets the maximum percentage of CPU time that a client group can use on request h
Exceeding this limit triggers throttling.
The quota, expressed as a percentage, also indicates the total allowable CPU usage for the client groups sharing the quota.`,
ValidateFunc: validation.IntBetween(0, 100),
AtLeastOneOf: []string{"consumer_byte_rate", "producer_byte_rate", "request_percentage"},
},
}

Expand All @@ -84,8 +89,7 @@ func ResourceKafkaQuota() *schema.Resource {
DeleteContext: common.WithGenClient(resourceKafkaQuotaDelete),
Timeouts: schemautil.DefaultResourceTimeouts(),

Schema: aivenKafkaQuotaSchema,
CustomizeDiff: validateKafkaQuotaDiff,
Schema: aivenKafkaQuotaSchema,
}
}

Expand Down Expand Up @@ -205,26 +209,3 @@ func resourceKafkaQuotaDelete(ctx context.Context, d *schema.ResourceData, clien
params...,
)
}

func validateKafkaQuotaDiff(_ context.Context, d *schema.ResourceDiff, _ interface{}) error {
var (
user = d.Get("user").(string)
clientID = d.Get("client_id").(string)
)

if user == "" && clientID == "" {
return fmt.Errorf("at least one of user or client_id must be specified")
}

var (
consumerByteRate = d.Get("consumer_byte_rate").(int)
producerByteRate = d.Get("producer_byte_rate").(int)
requestPercentage = d.Get("request_percentage").(int)
)

if consumerByteRate == 0 && producerByteRate == 0 && requestPercentage == 0 {
return fmt.Errorf("at least one quota parameter must be specified")
}

return nil
}
2 changes: 1 addition & 1 deletion internal/sdkprovider/service/kafka/kafka_quota_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ resource "aiven_kafka_quota" "{{ .resource_name }}" {
"request_percentage": 10,
}).
MustRender(t),
ExpectError: regexp.MustCompile(`at least one of user or client_id must be specified`),
ExpectError: regexp.MustCompile(`"(?:user|client_id)": one of ` + "`" + `client_id,user` + "`" + ` must be specified`),
},
{
// valid configuration
Expand Down

0 comments on commit 200b493

Please sign in to comment.