Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(kafka_quota): changed request_percentage field to float type #1980

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions internal/plugin/util/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,16 @@ func ToPtr[T any](v T) *T {
return &v
}

// NilIfZero returns a pointer to the value, or nil if the value equals its zero value
func NilIfZero[T comparable](v T) *T {
var zero T
if v == zero {
return nil
}

return &v
}

// First is a helper function that returns the first argument passed in out of two.
func First[T any, U any](a T, _ U) T {
return a
Expand Down
3 changes: 2 additions & 1 deletion internal/sdkprovider/service/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/aiven/terraform-provider-aiven/internal/common"
"github.com/aiven/terraform-provider-aiven/internal/plugin/util"
"github.com/aiven/terraform-provider-aiven/internal/schemautil"
)

Expand Down Expand Up @@ -134,7 +135,7 @@ func resourceAccountUpdate(ctx context.Context, d *schema.ResourceData, client a
)
resp, err := client.AccountUpdate(ctx, d.Id(), &account.AccountUpdateIn{
AccountName: &name,
PrimaryBillingGroupId: &bgID,
PrimaryBillingGroupId: util.NilIfZero(bgID),
})
if err != nil {
return err
Expand Down
39 changes: 23 additions & 16 deletions internal/sdkprovider/service/account/account_authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,17 +256,24 @@ func resourceAccountAuthenticationUpdate(ctx context.Context, d *schema.Resource
}

req := accountauthentication.AccountAuthenticationMethodUpdateIn{
AuthenticationMethodEnabled: util.ToPtr(d.Get("enabled").(bool)),
AuthenticationMethodName: util.ToPtr(d.Get("name").(string)),
AutoJoinTeamId: util.ToPtr(d.Get("auto_join_team_id").(string)),
SamlCertificate: util.ToPtr(strings.TrimSpace(d.Get("saml_certificate").(string))),
SamlDigestAlgorithm: accountauthentication.SamlDigestAlgorithmType(d.Get("saml_digest_algorithm").(string)),
SamlFieldMapping: readSAMLFieldMappingFromSchema(d),
SamlIdpLoginAllowed: util.ToPtr(d.Get("saml_idp_login_allowed").(bool)),
SamlIdpUrl: util.ToPtr(d.Get("saml_idp_url").(string)),
SamlSignatureAlgorithm: accountauthentication.SamlSignatureAlgorithmType(d.Get("saml_signature_algorithm").(string)),
SamlVariant: accountauthentication.SamlVariantType(d.Get("saml_variant").(string)),
SamlEntityId: util.ToPtr(d.Get("saml_entity_id").(string)),
AuthenticationMethodName: util.NilIfZero(d.Get("name").(string)),
AutoJoinTeamId: util.NilIfZero(d.Get("auto_join_team_id").(string)),
SamlCertificate: util.NilIfZero(strings.TrimSpace(d.Get("saml_certificate").(string))),
SamlDigestAlgorithm: accountauthentication.SamlDigestAlgorithmType(d.Get("saml_digest_algorithm").(string)),
SamlFieldMapping: readSAMLFieldMappingFromSchema(d),
SamlIdpUrl: util.NilIfZero(d.Get("saml_idp_url").(string)),
SamlSignatureAlgorithm: accountauthentication.SamlSignatureAlgorithmType(d.Get("saml_signature_algorithm").(string)),
SamlVariant: accountauthentication.SamlVariantType(d.Get("saml_variant").(string)),
SamlEntityId: util.NilIfZero(d.Get("saml_entity_id").(string)),
}

// Handle booleans separately to distinguish between not set and false
if enabled, ok := d.GetOk("enabled"); ok {
req.AuthenticationMethodEnabled = util.ToPtr(enabled.(bool))
}

if allowed, ok := d.GetOk("saml_idp_login_allowed"); ok {
req.SamlIdpLoginAllowed = util.ToPtr(allowed.(bool))
}

_, err = client.AccountAuthenticationMethodUpdate(ctx, accountID, authID, &req)
Expand Down Expand Up @@ -303,11 +310,11 @@ func readSAMLFieldMappingFromSchema(d *schema.ResourceData) *accountauthenticati
for _, v := range set {
cv := v.(map[string]interface{})

r.Email = util.ToPtr(cv["email"].(string))
r.FirstName = util.ToPtr(cv["first_name"].(string))
r.Identity = util.ToPtr(cv["identity"].(string))
r.LastName = util.ToPtr(cv["last_name"].(string))
r.RealName = util.ToPtr(cv["real_name"].(string))
r.Email = util.NilIfZero(cv["email"].(string))
r.FirstName = util.NilIfZero(cv["first_name"].(string))
r.Identity = util.NilIfZero(cv["identity"].(string))
r.LastName = util.NilIfZero(cv["last_name"].(string))
r.RealName = util.NilIfZero(cv["real_name"].(string))
}

return &r
Expand Down
4 changes: 2 additions & 2 deletions internal/sdkprovider/service/kafka/kafka_quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ Exceeding this limit results in client throttling.`,
AtLeastOneOf: []string{"consumer_byte_rate", "producer_byte_rate", "request_percentage"},
},
"request_percentage": {
Type: schema.TypeInt,
Type: schema.TypeFloat,
Optional: true,
Description: `
Sets the maximum percentage of CPU time that a client group can use on request handler I/O and network threads per broker within a quota window.
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),
ValidateFunc: validation.FloatBetween(0, 100),
AtLeastOneOf: []string{"consumer_byte_rate", "producer_byte_rate", "request_percentage"},
},
}
Expand Down
10 changes: 5 additions & 5 deletions internal/sdkprovider/service/kafka/kafka_quota_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ resource "aiven_kafka_quota" "{{ .resource_name }}" {
"request_percentage": 101, // invalid value
}).
MustRender(t),
ExpectError: regexp.MustCompile(`expected .+ to be in the range \(\d+ - \d+\), got \d+`),
ExpectError: regexp.MustCompile(`expected .+ to be in the range \([\d.]+ - [\d.]+\), got [\d.]+`),
},
{
// missing user and client_id
Expand Down Expand Up @@ -282,13 +282,13 @@ resource "aiven_kafka_quota" "{{ .resource_name }}" {
"client_id": clientID,
"consumer_byte_rate": 4000,
"producer_byte_rate": 4000,
"request_percentage": 40,
"request_percentage": 40.5,
}).
Add("kafka_quota", map[string]any{
"resource_name": "user",
"service_name": serviceName,
"user": user,
"request_percentage": 20,
"request_percentage": 20.22,
}).
Add("kafka_quota", map[string]any{
"resource_name": "client",
Expand All @@ -304,12 +304,12 @@ resource "aiven_kafka_quota" "{{ .resource_name }}" {
resource.TestCheckResourceAttr(fmt.Sprintf("%s.new_full", kafkaQuotaResource), "client_id", clientID),
resource.TestCheckResourceAttr(fmt.Sprintf("%s.new_full", kafkaQuotaResource), "consumer_byte_rate", "4000"),
resource.TestCheckResourceAttr(fmt.Sprintf("%s.new_full", kafkaQuotaResource), "producer_byte_rate", "4000"),
resource.TestCheckResourceAttr(fmt.Sprintf("%s.new_full", kafkaQuotaResource), "request_percentage", "40"),
resource.TestCheckResourceAttr(fmt.Sprintf("%s.new_full", kafkaQuotaResource), "request_percentage", "40.5"),

resource.TestCheckResourceAttr(fmt.Sprintf("%s.user", kafkaQuotaResource), "project", projectName),
resource.TestCheckResourceAttr(fmt.Sprintf("%s.user", kafkaQuotaResource), "service_name", serviceName),
resource.TestCheckResourceAttr(fmt.Sprintf("%s.user", kafkaQuotaResource), "user", user),
resource.TestCheckResourceAttr(fmt.Sprintf("%s.user", kafkaQuotaResource), "request_percentage", "20"),
resource.TestCheckResourceAttr(fmt.Sprintf("%s.user", kafkaQuotaResource), "request_percentage", "20.22"),
resource.TestCheckNoResourceAttr(fmt.Sprintf("%s.user", kafkaQuotaResource), "client_id"),
resource.TestCheckNoResourceAttr(fmt.Sprintf("%s.user", kafkaQuotaResource), "consumer_byte_rate"),
resource.TestCheckNoResourceAttr(fmt.Sprintf("%s.user", kafkaQuotaResource), "producer_byte_rate"),
Expand Down
Loading