-
Notifications
You must be signed in to change notification settings - Fork 549
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
configure namespace for vault client
- Loading branch information
1 parent
9887771
commit 9f607e2
Showing
3 changed files
with
139 additions
and
33 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package base | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
"github.com/hashicorp/terraform-provider-vault/internal/consts" | ||
"github.com/hashicorp/terraform-provider-vault/internal/framework/validators" | ||
) | ||
|
||
// BaseModel describes common fields for all of the Terraform resource data models | ||
type BaseModel struct { | ||
Namespace types.String `tfsdk:"namespace"` | ||
} | ||
|
||
func baseSchema() map[string]schema.Attribute { | ||
return map[string]schema.Attribute{ | ||
consts.FieldNamespace: schema.StringAttribute{ | ||
Optional: true, | ||
PlanModifiers: []planmodifier.String{ | ||
stringplanmodifier.RequiresReplace(), | ||
}, | ||
MarkdownDescription: "Target namespace. (requires Enterprise)", | ||
Validators: []validator.String{ | ||
validators.PathValidator(), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func MustAddBaseSchema(s *schema.Schema) { | ||
for k, v := range baseSchema() { | ||
if _, ok := s.Attributes[k]; ok { | ||
panic(fmt.Sprintf("cannot add schema field %q, already exists in the Schema map", k)) | ||
} | ||
|
||
s.Attributes[k] = v | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/hashicorp/terraform-plugin-log/tflog" | ||
"github.com/hashicorp/terraform-provider-vault/internal/consts" | ||
"github.com/hashicorp/terraform-provider-vault/internal/provider" | ||
"github.com/hashicorp/vault/api" | ||
) | ||
|
||
func GetClient(ctx context.Context, meta interface{}, namespace string) (*api.Client, error) { | ||
var p *provider.ProviderMeta | ||
|
||
switch v := meta.(type) { | ||
case *provider.ProviderMeta: | ||
p = v | ||
default: | ||
return nil, fmt.Errorf("meta argument must be a %T, not %T", p, meta) | ||
} | ||
|
||
ns := namespace | ||
if namespace == "" { | ||
// in order to import namespaced resources the user must provide | ||
// the namespace from an environment variable. | ||
ns = os.Getenv(consts.EnvVarVaultNamespaceImport) | ||
if ns != "" { | ||
tflog.Debug(ctx, fmt.Sprintf("Value for %q set from environment", consts.FieldNamespace)) | ||
} | ||
} | ||
|
||
if ns != "" { | ||
return p.GetNSClient(ns) | ||
} | ||
|
||
return p.GetClient() | ||
} |
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