Files
terraform-provider-threshold/internal/provider/resource_user.go
T

239 lines
6.0 KiB
Go
Raw Normal View History

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
2020-07-02 14:19:05 -07:00
package provider
import (
2020-09-15 19:56:43 -04:00
"context"
"net/http"
2020-07-02 14:19:05 -07:00
"github.com/hashicorp/boundary/api"
2020-08-11 16:12:26 -07:00
"github.com/hashicorp/boundary/api/users"
2020-09-15 19:56:43 -04:00
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2020-07-02 14:19:05 -07:00
)
2020-09-27 08:07:23 -07:00
const userAccountIDsKey = "account_ids"
2020-07-02 14:19:05 -07:00
func resourceUser() *schema.Resource {
return &schema.Resource{
2020-10-06 20:34:16 -04:00
Description: "The user resource allows you to configure a Boundary user.",
2020-09-15 19:56:43 -04:00
CreateContext: resourceUserCreate,
ReadContext: resourceUserRead,
UpdateContext: resourceUserUpdate,
DeleteContext: resourceUserDelete,
2020-10-07 12:04:11 -04:00
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
2020-07-02 14:19:05 -07:00
Schema: map[string]*schema.Schema{
IDKey: {
Description: "The ID of the user.",
Type: schema.TypeString,
Computed: true,
},
2020-09-15 19:56:43 -04:00
NameKey: {
2020-10-06 20:34:16 -04:00
Description: "The username. Defaults to the resource name.",
Type: schema.TypeString,
Optional: true,
2020-07-02 14:19:05 -07:00
},
2020-09-15 19:56:43 -04:00
DescriptionKey: {
2020-10-06 20:34:16 -04:00
Description: "The user description.",
Type: schema.TypeString,
Optional: true,
2020-07-02 14:19:05 -07:00
},
2020-09-15 19:56:43 -04:00
ScopeIdKey: {
2020-10-06 20:34:16 -04:00
Description: "The scope ID in which the resource is created. Defaults to the provider's `default_scope` if unset.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
2020-08-13 11:18:36 -07:00
},
2020-09-27 08:07:23 -07:00
userAccountIDsKey: {
2020-10-06 20:34:16 -04:00
Description: "Account ID's to associate with this user resource.",
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
2020-09-27 08:07:23 -07:00
},
2020-07-02 14:19:05 -07:00
},
}
}
func setFromUserResponseMap(d *schema.ResourceData, raw map[string]interface{}) error {
if err := d.Set(NameKey, raw["name"]); err != nil {
return err
}
if err := d.Set(DescriptionKey, raw["description"]); err != nil {
return err
}
if err := d.Set(ScopeIdKey, raw["scope_id"]); err != nil {
return err
}
if err := d.Set(userAccountIDsKey, raw["account_ids"]); err != nil {
return err
}
2020-09-15 19:56:43 -04:00
d.SetId(raw["id"].(string))
return nil
2020-07-02 14:19:05 -07:00
}
func resourceUserCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) (errs diag.Diagnostics) {
2020-09-15 19:56:43 -04:00
md := meta.(*metaData)
2020-09-15 19:56:43 -04:00
var scopeId string
if scopeIdVal, ok := d.GetOk(ScopeIdKey); ok {
scopeId = scopeIdVal.(string)
} else {
return diag.Errorf("no scope ID provided")
2020-07-02 14:19:05 -07:00
}
2020-09-15 19:56:43 -04:00
opts := []users.Option{}
2020-09-15 19:56:43 -04:00
nameVal, ok := d.GetOk(NameKey)
if ok {
nameStr := nameVal.(string)
opts = append(opts, users.WithName(nameStr))
2020-07-02 14:19:05 -07:00
}
2020-09-15 19:56:43 -04:00
descVal, ok := d.GetOk(DescriptionKey)
if ok {
descStr := descVal.(string)
opts = append(opts, users.WithDescription(descStr))
2020-07-02 14:19:05 -07:00
}
2020-09-15 19:56:43 -04:00
usrs := users.NewClient(md.client)
2020-07-02 14:19:05 -07:00
ucr, err := usrs.Create(ctx, scopeId, opts...)
2020-07-02 14:19:05 -07:00
if err != nil {
return diag.Errorf("error creating user: %v", err)
2020-09-15 19:56:43 -04:00
}
if ucr == nil {
return diag.Errorf("user nil after create")
2020-07-02 14:19:05 -07:00
}
apiResponse := ucr.GetResponse().Map
defer func() {
if err := setFromUserResponseMap(d, apiResponse); err != nil {
errs = append(errs, diag.FromErr(err)...)
}
}()
2020-07-02 14:19:05 -07:00
2020-09-27 08:07:23 -07:00
if val, ok := d.GetOk(userAccountIDsKey); ok {
list := val.(*schema.Set).List()
acctIds := make([]string, 0, len(list))
for _, i := range list {
acctIds = append(acctIds, i.(string))
}
usrac, err := usrs.SetAccounts(ctx, ucr.Item.Id, ucr.Item.Version, acctIds)
if err != nil {
return diag.Errorf("error setting accounts on user: %v", err)
}
if usrac == nil {
return diag.Errorf("user nil after setting accounts")
}
apiResponse = usrac.GetResponse().Map
}
2020-07-02 14:19:05 -07:00
2020-09-15 19:56:43 -04:00
return nil
2020-07-02 14:19:05 -07:00
}
2020-09-15 19:56:43 -04:00
func resourceUserRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
2020-07-02 14:19:05 -07:00
md := meta.(*metaData)
2020-09-15 19:56:43 -04:00
usrs := users.NewClient(md.client)
2020-07-02 14:19:05 -07:00
urr, err := usrs.Read(ctx, d.Id())
2020-07-02 14:19:05 -07:00
if err != nil {
2021-03-10 11:58:06 -08:00
if apiErr := api.AsServerError(err); apiErr != nil && apiErr.Response().StatusCode() == http.StatusNotFound {
d.SetId("")
return nil
}
return diag.Errorf("error calling read user: %v", err)
2020-09-15 19:56:43 -04:00
}
if urr == nil {
return diag.Errorf("user nil after read")
2020-07-02 14:19:05 -07:00
}
if err := setFromUserResponseMap(d, urr.GetResponse().Map); err != nil {
return diag.FromErr(err)
}
2020-09-15 19:56:43 -04:00
return nil
2020-07-02 14:19:05 -07:00
}
2020-09-15 19:56:43 -04:00
func resourceUserUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
2020-07-02 14:19:05 -07:00
md := meta.(*metaData)
2020-09-15 19:56:43 -04:00
usrs := users.NewClient(md.client)
opts := []users.Option{}
var name *string
if d.HasChange(NameKey) {
opts = append(opts, users.DefaultName())
nameVal, ok := d.GetOk(NameKey)
if ok {
nameStr := nameVal.(string)
name = &nameStr
opts = append(opts, users.WithName(nameStr))
}
2020-07-02 14:19:05 -07:00
}
2020-09-15 19:56:43 -04:00
var desc *string
if d.HasChange(DescriptionKey) {
opts = append(opts, users.DefaultDescription())
descVal, ok := d.GetOk(DescriptionKey)
if ok {
descStr := descVal.(string)
desc = &descStr
opts = append(opts, users.WithDescription(descStr))
}
2020-07-02 14:19:05 -07:00
}
2020-09-15 19:56:43 -04:00
if len(opts) > 0 {
opts = append(opts, users.WithAutomaticVersioning(true))
_, err := usrs.Update(ctx, d.Id(), 0, opts...)
2020-09-15 19:56:43 -04:00
if err != nil {
return diag.Errorf("error updating user: %v", err)
2020-09-15 19:56:43 -04:00
}
}
2020-09-15 19:56:43 -04:00
if d.HasChange(NameKey) {
if err := d.Set(NameKey, name); err != nil {
return diag.FromErr(err)
}
2020-07-02 14:19:05 -07:00
}
2020-09-15 19:56:43 -04:00
if d.HasChange(DescriptionKey) {
if err := d.Set(DescriptionKey, desc); err != nil {
return diag.FromErr(err)
}
2020-07-02 14:19:05 -07:00
}
2020-09-27 08:07:23 -07:00
if d.HasChange(userAccountIDsKey) {
var accountIds []string
if accountsVal, ok := d.GetOk(userAccountIDsKey); ok {
accounts := accountsVal.(*schema.Set).List()
for _, account := range accounts {
accountIds = append(accountIds, account.(string))
}
}
_, err := usrs.SetAccounts(ctx, d.Id(), 0, accountIds, users.WithAutomaticVersioning(true))
if err != nil {
return diag.Errorf("error updating accounts on user: %v", err)
}
if err := d.Set(userAccountIDsKey, accountIds); err != nil {
return diag.FromErr(err)
}
2020-09-27 08:07:23 -07:00
}
2020-09-15 19:56:43 -04:00
return nil
2020-07-02 14:19:05 -07:00
}
2020-09-15 19:56:43 -04:00
func resourceUserDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
2020-07-02 14:19:05 -07:00
md := meta.(*metaData)
2020-09-15 19:56:43 -04:00
usrs := users.NewClient(md.client)
2020-07-02 14:19:05 -07:00
_, err := usrs.Delete(ctx, d.Id())
2020-07-02 14:19:05 -07:00
if err != nil {
return diag.Errorf("error deleting user: %v", err)
2020-07-02 14:19:05 -07:00
}
return nil
}