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

add kv-v2 write retry #1579

Merged
merged 5 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
20 changes: 16 additions & 4 deletions vault/resource_generic_secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"encoding/json"
"fmt"
"log"
"time"

"github.com/cenkalti/backoff/v4"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/hashicorp/terraform-provider-vault/internal/consts"
Expand Down Expand Up @@ -145,10 +147,20 @@ func genericSecretResourceWrite(d *schema.ResourceData, meta interface{}) error

}

log.Printf("[DEBUG] Writing generic Vault secret to %s", path)
_, err = client.Logical().Write(path, data)
if err != nil {
return fmt.Errorf("error writing to Vault: %s", err)
writeData := func() error {
if _, err := client.Logical().Write(path, data); err != nil {
return err
benashz marked this conversation as resolved.
Show resolved Hide resolved
}
return nil
}

bo := backoff.WithMaxRetries(backoff.NewConstantBackOff(time.Millisecond*500), 10)

log.Printf("[DEBUG] Writing generic Vault secret to %s", path)
if err := backoff.RetryNotify(writeData, bo, func(err error, duration time.Duration) {
log.Printf("[WARN] create generic secret %q failed, retrying in %s", path, duration)
}); err != nil {
return fmt.Errorf("error creating generic secret: %s", err)
}

d.SetId(originalPath)
Expand Down
18 changes: 16 additions & 2 deletions vault/resource_kv_secret_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"encoding/json"
"fmt"
"log"
"time"

"github.com/cenkalti/backoff/v4"
benashz marked this conversation as resolved.
Show resolved Hide resolved
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

Expand Down Expand Up @@ -134,8 +136,20 @@ func kvSecretV2Write(ctx context.Context, d *schema.ResourceData, meta interface
data[k] = d.Get(k)
}

if _, err := client.Logical().Write(path, data); err != nil {
return diag.Errorf("error writing secret data to %s, err=%s", path, err)
writeData := func() error {
benashz marked this conversation as resolved.
Show resolved Hide resolved
if _, err := client.Logical().Write(path, data); err != nil {
return err
}
return nil
}

bo := backoff.WithMaxRetries(backoff.NewConstantBackOff(time.Millisecond*500), 10)

log.Printf("[DEBUG] creating KVV2 secret %s", path)
if err := backoff.RetryNotify(writeData, bo, func(err error, duration time.Duration) {
log.Printf("[WARN] create KVV2 %q failed, retrying in %s", path, duration)
}); err != nil {
return diag.Errorf("error creating KVV2 secret: %s", err)
}

d.SetId(path)
Expand Down