Skip to content

Commit

Permalink
fix: wait for quota state after creation
Browse files Browse the repository at this point in the history
  • Loading branch information
sbuliarca authored and Mongey committed Feb 20, 2024
1 parent 83347c2 commit 58d278d
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions kafka/resource_kafka_quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package kafka

import (
"context"
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"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"
)
Expand Down Expand Up @@ -51,12 +54,38 @@ func quotaCreate(ctx context.Context, d *schema.ResourceData, meta interface{})
return diag.FromErr(err)
}

stateConf := &retry.StateChangeConf{
Pending: []string{"Pending"},
Target: []string{"Created"},
Refresh: quotaCreatedFunc(c, quota),
Timeout: time.Duration(c.Config.Timeout) * time.Second,
Delay: 1 * time.Second,
PollInterval: 2 * time.Second,
}

if _, err := stateConf.WaitForStateContext(ctx); err != nil {
return diag.FromErr(fmt.Errorf("error waiting for quota (%s) to be created: %s", quota.ID(), err))
}

d.SetId(quota.ID())
quotaRead(ctx, d, meta)

return nil
}

func quotaCreatedFunc(client *LazyClient, q Quota) retry.StateRefreshFunc {
return func() (result interface{}, s string, err error) {
fq, err := client.DescribeQuota(q.EntityType, q.EntityName)
switch e := err.(type) {
case QuotaMissingError:
return fq, "Pending", nil
case nil:
return fq, "Created", nil
default:
return fq, "Error", e
}
}
}

func quotaDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*LazyClient)
quota := newQuota(d, true)
Expand All @@ -74,7 +103,7 @@ func quotaDelete(ctx context.Context, d *schema.ResourceData, meta interface{})
func quotaRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Println("[INFO] Reading Quota")
c := meta.(*LazyClient)

entityType := d.Get("entity_type").(string)
entityName := d.Get("entity_name").(string)
log.Printf("[INFO] Reading Quota %s", entityName)
Expand Down

0 comments on commit 58d278d

Please sign in to comment.