Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
112 changes: 112 additions & 0 deletions internal/provider/data_source_credential_store_static.go
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 {
Comment on lines +44 to +45
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: newline

Suggested change
}
func dataSourceCredentialStoreStaticRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
}
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)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 scopes. We may want to modify that one.

Suggested change
return diag.Errorf("error calling read static credential store: %v", err)
return diag.Errorf("error calling list static credential store: %v", err)

}
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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: newline

Suggested change
}
func setFromStaticCredentialStoreReadResponseMap(d *schema.ResourceData, raw map[string]interface{}, fromRead bool) error {
}
func setFromStaticCredentialStoreReadResponseMap(d *schema.ResourceData, raw map[string]interface{}, fromRead bool) error {

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
}
3 changes: 2 additions & 1 deletion internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 boundary_credential_store_static data source as opposed to a boundary_credential_store one?

},
}

Expand Down