Skip to content

Commit

Permalink
refact: Update to keep data sources consistent
Browse files Browse the repository at this point in the history
  • Loading branch information
moduli committed Nov 3, 2023
1 parent 5bc1895 commit 3aa568d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 68 deletions.
75 changes: 31 additions & 44 deletions internal/provider/data_source_scope.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/hashicorp/boundary/api/scopes"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func dataSourceScope() *schema.Resource {
Expand All @@ -19,71 +20,57 @@ func dataSourceScope() *schema.Resource {
ReadContext: dataSourceScopeRead,

Schema: map[string]*schema.Schema{
NameKey: {
Description: "The name of the scope to retrieve.",
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
ScopeIdKey: {
Description: "The parent scope ID that will be queried for the scope.",
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
IDKey: {
Description: "The ID of the retrieved scope.",
Type: schema.TypeString,
Computed: true,
},
NameKey: {
Description: "The name of the scope to retrieve.",
Type: schema.TypeString,
Required: true,
},
DescriptionKey: {
Description: "The description of the retrieved scope.",
Type: schema.TypeString,
Computed: true,
},
ScopeIdKey: {
Description: "The parent scope ID that will be queried for the scope.",
Type: schema.TypeString,
Required: true,
},
},
}
}

func dataSourceScopeRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
md := meta.(*metaData)
opts := []scopes.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 parent scope ID provided")
}

scp := scopes.NewClient(md.client)
name := d.Get(NameKey).(string)
scopeId := d.Get(ScopeIdKey).(string)

scpls, err := scp.List(ctx, scopeId, opts...)
scl := scopes.NewClient(md.client)
scopesList, err := scl.List(ctx, scopeId,
scopes.WithFilter(FilterWithItemNameMatches(name)),
)
if err != nil {
return diag.Errorf("error calling list scope: %v", err)
}
if scpls == nil {
scopes := scopesList.GetItems()
if scopes == nil {
return diag.Errorf("no scopes found")
}

var scopeIdRead string
for _, scopeItem := range scpls.GetItems() {
if scopeItem.Name == name {
scopeIdRead = scopeItem.Id
break
}
if len(scopes) == 0 {
return diag.Errorf("no matching scope found")
}

if scopeIdRead == "" {
return diag.Errorf("scope name %v not found in scope list", err)
if len(scopes) > 1 {
return diag.Errorf("error found more than 1 scope")
}

srr, err := scp.Read(ctx, scopeIdRead)
srr, err := scl.Read(ctx, scopes[0].Id)
if err != nil {
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Response().StatusCode() == http.StatusNotFound {
d.SetId("")
Expand All @@ -95,21 +82,21 @@ func dataSourceScopeRead(ctx context.Context, d *schema.ResourceData, meta inter
return diag.Errorf("scope nil after read")
}

if err := setFromScopeReadResponseMap(d, srr.GetResponse().Map); err != nil {
if err := setFromScopeRead(d, *srr.Item); err != nil {
return diag.FromErr(err)
}

return nil
}

func setFromScopeReadResponseMap(d *schema.ResourceData, raw map[string]interface{}) error {
if err := d.Set(NameKey, raw["name"]); err != nil {
func setFromScopeRead(d *schema.ResourceData, scope scopes.Scope) error {
if err := d.Set(NameKey, scope.Name); err != nil {
return err
}
if err := d.Set(DescriptionKey, raw["description"]); err != nil {
if err := d.Set(DescriptionKey, scope.Description); err != nil {
return err
}

d.SetId(raw["id"].(string))
d.SetId(scope.Id)
return nil
}
59 changes: 35 additions & 24 deletions internal/provider/data_source_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ package provider

import (
"context"
"net/http"

"github.com/hashicorp/boundary/api"
"github.com/hashicorp/boundary/api/users"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -18,29 +20,29 @@ func dataSourceUser() *schema.Resource {
ReadContext: dataSourceUserRead,

Schema: map[string]*schema.Schema{
IDKey: {
Description: "The ID of the user.",
Type: schema.TypeString,
Computed: true,
},
NameKey: {
Description: "The username to search for.",
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
DescriptionKey: {
Description: "The user description.",
Type: schema.TypeString,
Computed: true,
},
ScopeIdKey: {
Description: "The scope ID in which the resource is created. Defaults `global` if unset.",
Type: schema.TypeString,
Optional: true,
Default: "global",
ValidateFunc: validation.StringIsNotEmpty,
},
IDKey: {
Description: "The ID of the user.",
Type: schema.TypeString,
Computed: true,
},
DescriptionKey: {
Description: "The user description.",
Type: schema.TypeString,
Computed: true,
},
UserAccountIdsKey: {
Description: "Account ID's to associate with this user resource.",
Type: schema.TypeSet,
Expand Down Expand Up @@ -99,39 +101,48 @@ func dataSourceUser() *schema.Resource {

func dataSourceUserRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
md := meta.(*metaData)
usrs := users.NewClient(md.client)

opts := []users.Option{}

// Get user ID using name
name := d.Get(NameKey).(string)
scopeID := d.Get(ScopeIdKey).(string)

opts = append(opts, users.WithFilter(FilterWithItemNameMatches(name)))

usersList, err := usrs.List(ctx, scopeID, opts...)
ucl := users.NewClient(md.client)
usersList, err := ucl.List(ctx, scopeID,
users.WithFilter(FilterWithItemNameMatches(name)),
)
if err != nil {
return diag.Errorf("error calling list user: %v", err)
}
users := usersList.GetItems()

// check length, 0 means no user, > 1 means too many
if users == nil {
return diag.Errorf("no users found")
}
if len(users) == 0 {
return diag.Errorf("no matching user found: %v", err)
return diag.Errorf("no matching user found")
}

if len(users) > 1 {
return diag.Errorf("error found more than 1 user: %v", err)
return diag.Errorf("error found more than 1 user")
}

urr, err := ucl.Read(ctx, users[0].Id)
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 scope: %v", err)
}
if urr == nil {
return diag.Errorf("scope nil after read")
}

if err := setFromUserItem(d, *users[0]); err != nil {
if err := setFromUserRead(d, *urr.Item); err != nil {
return diag.FromErr(err)
}

return nil
}

func setFromUserItem(d *schema.ResourceData, user users.User) error {
func setFromUserRead(d *schema.ResourceData, user users.User) error {
if err := d.Set(NameKey, user.Name); err != nil {
return err
}
Expand Down

0 comments on commit 3aa568d

Please sign in to comment.