-
Notifications
You must be signed in to change notification settings - Fork 59
Add static credentialstore datasource #489
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,112 @@ | ||||||||||||
// Copyright (c) HashiCorp, Inc. | ||||||||||||
// SPDX-License-Identifier: MPL-2.0 | ||||||||||||
|
||||||||||||
package provider | ||||||||||||
|
||||||||||||
import ( | ||||||||||||
"context" | ||||||||||||
"net/http" | ||||||||||||
|
||||||||||||
"github.com/hashicorp/boundary/api" | ||||||||||||
"github.com/hashicorp/boundary/api/credentialstores" | ||||||||||||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||||||||||||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||||||||||||
) | ||||||||||||
|
||||||||||||
func dataSourceCredentialStoreStatic() *schema.Resource { | ||||||||||||
return &schema.Resource{ | ||||||||||||
Description: "The static credential store data source allows you to discover an existing Boundary static credential store by name", | ||||||||||||
ReadContext: dataSourceCredentialStoreStaticRead, | ||||||||||||
|
||||||||||||
Schema: map[string]*schema.Schema{ | ||||||||||||
IDKey: { | ||||||||||||
Description: "The ID of the retrieved static credential store", | ||||||||||||
Type: schema.TypeString, | ||||||||||||
Computed: true, | ||||||||||||
}, | ||||||||||||
NameKey: { | ||||||||||||
Description: "The name of the static credential store to retrieve", | ||||||||||||
Type: schema.TypeString, | ||||||||||||
Required: true, | ||||||||||||
}, | ||||||||||||
DescriptionKey: { | ||||||||||||
Description: "The description of the retrieved credential store", | ||||||||||||
Type: schema.TypeString, | ||||||||||||
Computed: true, | ||||||||||||
}, | ||||||||||||
ScopeIdKey: { | ||||||||||||
Description: "The scope for this credential store", | ||||||||||||
Type: schema.TypeString, | ||||||||||||
Required: true, | ||||||||||||
}, | ||||||||||||
}, | ||||||||||||
} | ||||||||||||
} | ||||||||||||
func dataSourceCredentialStoreStaticRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||||||||||||
md := meta.(*metaData) | ||||||||||||
opts := []credentialstores.Option{} | ||||||||||||
|
||||||||||||
var name string | ||||||||||||
if v, ok := d.GetOk(NameKey); ok { | ||||||||||||
name = v.(string) | ||||||||||||
} else { | ||||||||||||
return diag.Errorf("no name provided") | ||||||||||||
} | ||||||||||||
|
||||||||||||
var scopeId string | ||||||||||||
if scopeIdVal, ok := d.GetOk(ScopeIdKey); ok { | ||||||||||||
scopeId = scopeIdVal.(string) | ||||||||||||
} else { | ||||||||||||
return diag.Errorf("no scope is set") | ||||||||||||
} | ||||||||||||
|
||||||||||||
client := credentialstores.NewClient(md.client) | ||||||||||||
|
||||||||||||
csl, err := client.List(ctx, scopeId, opts...) | ||||||||||||
if err != nil { | ||||||||||||
return diag.Errorf("error calling read static credential store: %v", err) | ||||||||||||
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. I believe we're technically listing here instead of reading. I do see that's the message we have for
Suggested change
|
||||||||||||
} | ||||||||||||
if csl == nil { | ||||||||||||
return diag.Errorf("no static credential store found") | ||||||||||||
} | ||||||||||||
|
||||||||||||
var credentialstorestaticIdRead string | ||||||||||||
for _, scopeItem := range csl.GetItems() { | ||||||||||||
if scopeItem.Name == name { | ||||||||||||
credentialstorestaticIdRead = scopeItem.Id | ||||||||||||
break | ||||||||||||
} | ||||||||||||
} | ||||||||||||
if credentialstorestaticIdRead == "" { | ||||||||||||
return diag.Errorf("static credential store %v not found", err) | ||||||||||||
} | ||||||||||||
|
||||||||||||
srr, err := client.Read(ctx, credentialstorestaticIdRead) | ||||||||||||
if err != nil { | ||||||||||||
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Response().StatusCode() == http.StatusNotFound { | ||||||||||||
d.SetId("") | ||||||||||||
return nil | ||||||||||||
} | ||||||||||||
return diag.Errorf("error calling read static credential store: %v", err) | ||||||||||||
} | ||||||||||||
if srr == nil { | ||||||||||||
return diag.Errorf("static credential store nil after read") | ||||||||||||
} | ||||||||||||
|
||||||||||||
if err := setFromStaticCredentialStoreResponseMap(d, srr.GetResponse().Map, false); err != nil { | ||||||||||||
return diag.FromErr(err) | ||||||||||||
} | ||||||||||||
|
||||||||||||
return nil | ||||||||||||
} | ||||||||||||
func setFromStaticCredentialStoreReadResponseMap(d *schema.ResourceData, raw map[string]interface{}, fromRead bool) error { | ||||||||||||
Comment on lines
+101
to
+102
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. nit: newline
Suggested change
|
||||||||||||
if err := d.Set(NameKey, raw["name"]); err != nil { | ||||||||||||
return err | ||||||||||||
} | ||||||||||||
if err := d.Set(DescriptionKey, raw["description"]); err != nil { | ||||||||||||
return err | ||||||||||||
} | ||||||||||||
|
||||||||||||
d.SetId(raw["id"].(string)) | ||||||||||||
return nil | ||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -128,7 +128,8 @@ func New() *schema.Provider { | |
"boundary_worker": resourceWorker(), | ||
}, | ||
DataSourcesMap: map[string]*schema.Resource{ | ||
"boundary_scope": dataSourceScope(), | ||
"boundary_scope": dataSourceScope(), | ||
"boundary_credential_store_static": dataSourceCredentialStoreStatic(), | ||
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. I'm not sure I follow how to break down these resources. What's the reason for creating a specific |
||
}, | ||
} | ||
|
||
|
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.
nit: newline