From 92d5c1da43e8763d6f1dbb4d0d467053af0d1f17 Mon Sep 17 00:00:00 2001 From: Myroslav Vivcharyk Date: Wed, 8 Jan 2025 18:24:24 +0100 Subject: [PATCH] feat(kafka_quota): changed request_percentage field to float type --- internal/plugin/util/helpers.go | 10 +++++ .../sdkprovider/service/account/account.go | 3 +- .../service/account/account_authentication.go | 39 +++++++++++-------- .../sdkprovider/service/kafka/kafka_quota.go | 4 +- .../service/kafka/kafka_quota_test.go | 10 ++--- 5 files changed, 42 insertions(+), 24 deletions(-) diff --git a/internal/plugin/util/helpers.go b/internal/plugin/util/helpers.go index 2c8935958..08ea026ee 100644 --- a/internal/plugin/util/helpers.go +++ b/internal/plugin/util/helpers.go @@ -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 diff --git a/internal/sdkprovider/service/account/account.go b/internal/sdkprovider/service/account/account.go index 73ef507bf..4525bffe7 100644 --- a/internal/sdkprovider/service/account/account.go +++ b/internal/sdkprovider/service/account/account.go @@ -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" ) @@ -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 diff --git a/internal/sdkprovider/service/account/account_authentication.go b/internal/sdkprovider/service/account/account_authentication.go index 58b24686b..d0e8b5f94 100644 --- a/internal/sdkprovider/service/account/account_authentication.go +++ b/internal/sdkprovider/service/account/account_authentication.go @@ -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) @@ -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 diff --git a/internal/sdkprovider/service/kafka/kafka_quota.go b/internal/sdkprovider/service/kafka/kafka_quota.go index a89a12c50..db89dc72b 100644 --- a/internal/sdkprovider/service/kafka/kafka_quota.go +++ b/internal/sdkprovider/service/kafka/kafka_quota.go @@ -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"}, }, } diff --git a/internal/sdkprovider/service/kafka/kafka_quota_test.go b/internal/sdkprovider/service/kafka/kafka_quota_test.go index ca7918a7e..68c9bfd6f 100644 --- a/internal/sdkprovider/service/kafka/kafka_quota_test.go +++ b/internal/sdkprovider/service/kafka/kafka_quota_test.go @@ -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 @@ -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", @@ -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"),