-
Notifications
You must be signed in to change notification settings - Fork 540
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 custom metadata support to namespace resource #2033
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8e5f297
add custom metadata support to namespace resource
vinay-gopalan 9b9e73f
add changelog entry
vinay-gopalan 1d9c42b
Update vault/resource_namespace.go
vinay-gopalan 0564c3e
set custom_metadata to nil for vault versions <=1.11
vinay-gopalan 0c82279
resolve changelog conflict
vinay-gopalan d374c84
add documentation for new param
vinay-gopalan 9de6841
Update vault/resource_namespace.go
vinay-gopalan ca960ec
respond to feedback
vinay-gopalan 26e31c3
resolve merge conflicts
vinay-gopalan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,13 +4,15 @@ | |
package vault | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"strings" | ||
"time" | ||
|
||
"github.com/cenkalti/backoff/v4" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/vault/api" | ||
|
||
|
@@ -21,10 +23,10 @@ import ( | |
|
||
func namespaceResource() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: namespaceCreate, | ||
Update: namespaceCreate, | ||
Delete: namespaceDelete, | ||
Read: provider.ReadWrapper(namespaceRead), | ||
CreateContext: namespaceCreate, | ||
UpdateContext: namespaceUpdate, | ||
DeleteContext: namespaceDelete, | ||
ReadContext: provider.ReadContextWrapper(namespaceRead), | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
@@ -48,31 +50,80 @@ func namespaceResource() *schema.Resource { | |
Optional: true, | ||
Description: "The fully qualified namespace path.", | ||
}, | ||
consts.FieldCustomMetadata: { | ||
Type: schema.TypeMap, | ||
Computed: true, | ||
Optional: true, | ||
Description: "A map of arbitrary string to string valued user-provided " + | ||
vinay-gopalan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"metadata meant to describe the namespace.", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func namespaceCreate(d *schema.ResourceData, meta interface{}) error { | ||
func namespaceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client, e := provider.GetClient(d, meta) | ||
if e != nil { | ||
return diag.FromErr(e) | ||
} | ||
|
||
path := d.Get(consts.FieldPath).(string) | ||
|
||
var data map[string]interface{} | ||
|
||
// data is non-nil only if Vault version >= 1.12 | ||
// and custom_metadata is provided | ||
if provider.IsAPISupported(meta, provider.VaultVersion112) { | ||
if v, ok := d.GetOk(consts.FieldCustomMetadata); ok { | ||
data = map[string]interface{}{ | ||
consts.FieldCustomMetadata: v, | ||
} | ||
} | ||
} | ||
|
||
log.Printf("[DEBUG] Creating namespace %s in Vault", path) | ||
_, err := client.Logical().Write(consts.SysNamespaceRoot+path, data) | ||
if err != nil { | ||
return diag.Errorf("error writing to Vault: %s", err) | ||
} | ||
|
||
return namespaceRead(ctx, d, meta) | ||
} | ||
|
||
func namespaceUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
// Updating a namespace is only supported in | ||
// Vault versions >= 1.12 | ||
if !provider.IsAPISupported(meta, provider.VaultVersion112) { | ||
return nil | ||
} | ||
|
||
client, e := provider.GetClient(d, meta) | ||
if e != nil { | ||
return e | ||
return diag.FromErr(e) | ||
} | ||
|
||
path := d.Get(consts.FieldPath).(string) | ||
|
||
var data map[string]interface{} | ||
if v, ok := d.GetOk(consts.FieldCustomMetadata); ok { | ||
data = map[string]interface{}{ | ||
consts.FieldCustomMetadata: v, | ||
} | ||
} | ||
|
||
log.Printf("[DEBUG] Creating namespace %s in Vault", path) | ||
_, err := client.Logical().Write(consts.SysNamespaceRoot+path, nil) | ||
_, err := client.Logical().JSONMergePatch(ctx, consts.SysNamespaceRoot+path, data) | ||
vinay-gopalan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if err != nil { | ||
return fmt.Errorf("error writing to Vault: %s", err) | ||
return diag.Errorf("error writing to Vault: %s", err) | ||
} | ||
|
||
return namespaceRead(d, meta) | ||
return namespaceRead(ctx, d, meta) | ||
} | ||
|
||
func namespaceDelete(d *schema.ResourceData, meta interface{}) error { | ||
func namespaceDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client, e := provider.GetClient(d, meta) | ||
if e != nil { | ||
return e | ||
return diag.FromErr(e) | ||
} | ||
|
||
path := d.Get(consts.FieldPath).(string) | ||
|
@@ -99,11 +150,11 @@ func namespaceDelete(d *schema.ResourceData, meta interface{}) error { | |
if err := backoff.RetryNotify(deleteNS, bo, func(err error, duration time.Duration) { | ||
log.Printf("[WARN] Deleting namespace %q failed, retrying in %s", path, duration) | ||
}); err != nil { | ||
return fmt.Errorf("error deleting from Vault: %s", err) | ||
return diag.Errorf("error deleting from Vault: %s", err) | ||
} | ||
|
||
// wait for the namespace to be gone... | ||
return backoff.RetryNotify(func() error { | ||
return diag.FromErr(backoff.RetryNotify(func() error { | ||
if resp, _ := client.Logical().Read(consts.SysNamespaceRoot + path); resp != nil { | ||
return fmt.Errorf("namespace %q still exists", path) | ||
} | ||
|
@@ -115,13 +166,13 @@ func namespaceDelete(d *schema.ResourceData, meta interface{}) error { | |
"[WARN] Waiting for Vault to garbage collect the %q namespace, retrying in %s", | ||
path, duration) | ||
}, | ||
) | ||
)) | ||
} | ||
|
||
func namespaceRead(d *schema.ResourceData, meta interface{}) error { | ||
func namespaceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client, e := provider.GetClient(d, meta) | ||
if e != nil { | ||
return e | ||
return diag.FromErr(e) | ||
} | ||
|
||
upgradeNonPathdNamespaceID(d) | ||
|
@@ -130,7 +181,7 @@ func namespaceRead(d *schema.ResourceData, meta interface{}) error { | |
|
||
resp, err := client.Logical().Read(consts.SysNamespaceRoot + path) | ||
if err != nil { | ||
return fmt.Errorf("error reading from Vault: %s", err) | ||
return diag.Errorf("error reading from Vault: %s", err) | ||
} | ||
|
||
if resp == nil { | ||
|
@@ -142,18 +193,27 @@ func namespaceRead(d *schema.ResourceData, meta interface{}) error { | |
d.SetId(resp.Data[consts.FieldPath].(string)) | ||
|
||
toSet := map[string]interface{}{ | ||
consts.FieldNamespaceID: resp.Data["id"], | ||
consts.FieldNamespaceID: resp.Data[consts.FieldID], | ||
consts.FieldPath: util.TrimSlashes(path), | ||
} | ||
|
||
if provider.IsAPISupported(meta, provider.VaultVersion112) { | ||
toSet[consts.FieldCustomMetadata] = resp.Data[consts.FieldCustomMetadata] | ||
} else { | ||
// set computed parameter to nil for vault versions <= 1.11 | ||
// prevents 'known after apply' drift in TF state since field | ||
// would never be set otherwise | ||
toSet[consts.FieldCustomMetadata] = nil | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍
vinay-gopalan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
pathFQ := path | ||
if parent, ok := d.GetOk(consts.FieldNamespace); ok { | ||
pathFQ = strings.Join([]string{parent.(string), path}, "/") | ||
} | ||
toSet[consts.FieldPathFQ] = pathFQ | ||
|
||
if err := util.SetResourceData(d, toSet); err != nil { | ||
return err | ||
return diag.FromErr(err) | ||
} | ||
|
||
return nil | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍