Skip to content

Commit

Permalink
feat(kafka_quota): added retries to kafka_quota resource
Browse files Browse the repository at this point in the history
  • Loading branch information
vmyroslav committed Jan 9, 2025
1 parent 76d3b06 commit df23af7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
24 changes: 21 additions & 3 deletions internal/sdkprovider/service/kafka/kafka_quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package kafka
import (
"context"
"fmt"

"github.com/aiven/aiven-go-client/v2"
avngen "github.com/aiven/go-client-codegen"
"github.com/aiven/go-client-codegen/handler/kafka"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"time"

"github.com/aiven/terraform-provider-aiven/internal/common"
"github.com/aiven/terraform-provider-aiven/internal/schemautil"
Expand Down Expand Up @@ -120,7 +122,7 @@ func resourceKafkaQuotaCreate(ctx context.Context, d *schema.ResourceData, clien

d.SetId(schemautil.BuildResourceID(project, service, clientID, user))

return resourceKafkaQuotaRead(ctx, d, client)
return readKafkaQuotaWithRetry(ctx, d, client)
}

func resourceKafkaQuotaUpdate(ctx context.Context, d *schema.ResourceData, client avngen.Client) error {
Expand All @@ -145,7 +147,7 @@ func resourceKafkaQuotaUpdate(ctx context.Context, d *schema.ResourceData, clien
return err
}

return resourceKafkaQuotaRead(ctx, d, client)
return readKafkaQuotaWithRetry(ctx, d, client)
}

func resourceKafkaQuotaRead(ctx context.Context, d *schema.ResourceData, client avngen.Client) error {
Expand Down Expand Up @@ -209,3 +211,19 @@ func resourceKafkaQuotaDelete(ctx context.Context, d *schema.ResourceData, clien
params...,
)
}

// readKafkaQuotaWithRetry is a helper function that retries reading a Kafka quota resource in case of a 404 error.
// We need to retry reading the resource because the Kafka quota may not be immediately available after creation.
func readKafkaQuotaWithRetry(ctx context.Context, d *schema.ResourceData, client avngen.Client) error {
return retry.RetryContext(ctx, 3*time.Second, func() *retry.RetryError {
err := resourceKafkaQuotaRead(ctx, d, client)
if err == nil {
return nil
}

return &retry.RetryError{
Err: err,
Retryable: aiven.IsNotFound(err), // retries not found errors only
}
})
}
18 changes: 9 additions & 9 deletions internal/sdkprovider/service/kafka/kafka_quota_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,50 +273,50 @@ resource "aiven_kafka_quota" "{{ .resource_name }}" {
),
},
{
// craete multiple resources with different configurations
// create multiple resources with different configurations
Config: newComposition().
Add("kafka_quota", map[string]any{
"resource_name": "new_full",
"service_name": serviceName,
"user": user,
"client_id": clientID,
"user": fmt.Sprintf("%s_1", user),
"client_id": fmt.Sprintf("%s_1", clientID),
"consumer_byte_rate": 4000,
"producer_byte_rate": 4000,
"request_percentage": 40.5,
}).
Add("kafka_quota", map[string]any{
"resource_name": "user",
"service_name": serviceName,
"user": user,
"user": fmt.Sprintf("%s_2", user),
"request_percentage": 20.22,
}).
Add("kafka_quota", map[string]any{
"resource_name": "client",
"service_name": serviceName,
"client_id": clientID,
"client_id": fmt.Sprintf("%s_3", clientID),
"producer_byte_rate": 2000,
}).
MustRender(t),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(fmt.Sprintf("%s.new_full", kafkaQuotaResource), "project", projectName),
resource.TestCheckResourceAttr(fmt.Sprintf("%s.new_full", kafkaQuotaResource), "service_name", serviceName),
resource.TestCheckResourceAttr(fmt.Sprintf("%s.new_full", kafkaQuotaResource), "user", user),
resource.TestCheckResourceAttr(fmt.Sprintf("%s.new_full", kafkaQuotaResource), "client_id", clientID),
resource.TestCheckResourceAttr(fmt.Sprintf("%s.new_full", kafkaQuotaResource), "user", fmt.Sprintf("%s_1", user)),
resource.TestCheckResourceAttr(fmt.Sprintf("%s.new_full", kafkaQuotaResource), "client_id", fmt.Sprintf("%s_1", 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.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), "user", fmt.Sprintf("%s_2", user)),
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"),

resource.TestCheckResourceAttr(fmt.Sprintf("%s.client", kafkaQuotaResource), "project", projectName),
resource.TestCheckResourceAttr(fmt.Sprintf("%s.client", kafkaQuotaResource), "service_name", serviceName),
resource.TestCheckResourceAttr(fmt.Sprintf("%s.client", kafkaQuotaResource), "client_id", clientID),
resource.TestCheckResourceAttr(fmt.Sprintf("%s.client", kafkaQuotaResource), "client_id", fmt.Sprintf("%s_3", clientID)),
resource.TestCheckResourceAttr(fmt.Sprintf("%s.client", kafkaQuotaResource), "producer_byte_rate", "2000"),
resource.TestCheckNoResourceAttr(fmt.Sprintf("%s.client", kafkaQuotaResource), "user"),
resource.TestCheckNoResourceAttr(fmt.Sprintf("%s.client", kafkaQuotaResource), "consumer_byte_rate"),
Expand Down

0 comments on commit df23af7

Please sign in to comment.