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

248 lines
7.1 KiB
Go
Raw Normal View History

// Copyright IBM Corp. 2020, 2026
// 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"
2020-07-02 14:19:05 -07:00
"fmt"
"net/http"
"testing"
"github.com/hashicorp/boundary/api"
2020-08-11 16:12:26 -07:00
"github.com/hashicorp/boundary/api/users"
"github.com/hashicorp/boundary/testing/controller"
2020-09-15 19:56:43 -04:00
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
2020-10-07 12:04:11 -04:00
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
2020-09-15 19:56:43 -04:00
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
2020-07-02 14:19:05 -07:00
)
const (
2020-09-15 19:56:43 -04:00
fooDescription = "bar"
fooDescriptionUpdate = "foo bar"
fooDescriptionUnset = ""
2020-07-02 14:19:05 -07:00
)
var (
orgUser = fmt.Sprintf(`
2020-09-15 19:56:43 -04:00
resource "boundary_user" "org1" {
name = "test"
2020-07-02 14:19:05 -07:00
description = "%s"
2020-09-15 19:56:43 -04:00
scope_id = boundary_scope.org1.id
depends_on = [boundary_role.org1_admin]
2020-09-15 19:56:43 -04:00
}`, fooDescription)
2020-07-02 14:19:05 -07:00
orgUserUpdate = fmt.Sprintf(`
2020-09-15 19:56:43 -04:00
resource "boundary_user" "org1" {
name = "test"
2020-07-02 14:19:05 -07:00
description = "%s"
2020-09-15 19:56:43 -04:00
scope_id = boundary_scope.org1.id
depends_on = [boundary_role.org1_admin]
2020-09-15 19:56:43 -04:00
}`, fooDescriptionUpdate)
2020-09-27 08:07:23 -07:00
orgUserWithAccts = `
resource "boundary_user" "org1" {
name = "test"
description = "with accts"
scope_id = boundary_scope.org1.id
account_ids = [
boundary_account.foo.id
]
depends_on = [boundary_role.org1_admin]
}`
orgUserWithAcctsUpdate = `
resource "boundary_user" "org1" {
name = "test"
description = "with accts"
scope_id = boundary_scope.org1.id
depends_on = [boundary_role.org1_admin]
}`
2020-07-02 14:19:05 -07:00
)
2020-09-15 19:56:43 -04:00
// NOTE: this test also tests out the direct token auth mechanism.
2020-07-02 14:19:05 -07:00
func TestAccUser(t *testing.T) {
tc := controller.NewTestController(t, tcConfig...)
2020-07-02 14:19:05 -07:00
defer tc.Shutdown()
url := tc.ApiAddrs()[0]
2020-09-15 19:56:43 -04:00
token := tc.Token().Token
2020-07-02 14:19:05 -07:00
2020-10-07 12:04:11 -04:00
var provider *schema.Provider
2020-07-02 14:19:05 -07:00
resource.Test(t, resource.TestCase{
2020-10-07 12:04:11 -04:00
ProviderFactories: providerFactories(&provider),
CheckDestroy: testAccCheckUserResourceDestroy(t, provider),
2020-07-02 14:19:05 -07:00
Steps: []resource.TestStep{
{
// test create
2020-09-15 19:56:43 -04:00
Config: testConfigWithToken(url, token, fooOrg, orgUser),
2020-07-02 14:19:05 -07:00
Check: resource.ComposeTestCheckFunc(
2020-10-07 12:04:11 -04:00
testAccCheckUserResourceExists(provider, "boundary_user.org1"),
2020-09-15 19:56:43 -04:00
resource.TestCheckResourceAttr("boundary_user.org1", DescriptionKey, fooDescription),
resource.TestCheckResourceAttr("boundary_user.org1", NameKey, "test"),
2020-07-02 14:19:05 -07:00
),
},
2020-10-07 12:04:11 -04:00
importStep("boundary_user.org1"),
2020-07-02 14:19:05 -07:00
{
// test update description
2020-09-15 19:56:43 -04:00
Config: testConfigWithToken(url, token, fooOrg, orgUserUpdate),
2020-07-02 14:19:05 -07:00
Check: resource.ComposeTestCheckFunc(
2020-10-07 12:04:11 -04:00
testAccCheckUserResourceExists(provider, "boundary_user.org1"),
2020-09-15 19:56:43 -04:00
resource.TestCheckResourceAttr("boundary_user.org1", DescriptionKey, fooDescriptionUpdate),
resource.TestCheckResourceAttr("boundary_user.org1", NameKey, "test"),
2020-07-02 14:19:05 -07:00
),
},
2020-10-07 12:04:11 -04:00
importStep("boundary_user.org1"),
2020-07-02 14:19:05 -07:00
},
})
}
2020-09-27 08:07:23 -07:00
func TestAccUserWithAccounts(t *testing.T) {
tc := controller.NewTestController(t, tcConfig...)
defer tc.Shutdown()
url := tc.ApiAddrs()[0]
token := tc.Token().Token
2020-10-07 12:04:11 -04:00
var provider *schema.Provider
2020-09-27 08:07:23 -07:00
resource.Test(t, resource.TestCase{
2020-10-07 12:04:11 -04:00
ProviderFactories: providerFactories(&provider),
CheckDestroy: testAccCheckUserResourceDestroy(t, provider),
2020-09-27 08:07:23 -07:00
Steps: []resource.TestStep{
{
// test create
Config: testConfigWithToken(url, token, fooOrg, fooAccount, orgUserWithAccts),
Check: resource.ComposeTestCheckFunc(
2020-10-07 12:04:11 -04:00
testAccCheckUserResourceExists(provider, "boundary_user.org1"),
testAccCheckAccountResourceExists(provider, "boundary_account.foo"),
2020-09-27 08:07:23 -07:00
resource.TestCheckResourceAttr("boundary_user.org1", DescriptionKey, "with accts"),
resource.TestCheckResourceAttr("boundary_user.org1", NameKey, "test"),
2020-10-07 12:04:11 -04:00
testAccCheckUserResourceAccountsSet(provider, "boundary_user.org1", []string{"boundary_account.foo"}),
2020-09-27 08:07:23 -07:00
),
},
2020-10-07 12:04:11 -04:00
importStep("boundary_user.org1"),
importStep("boundary_account.foo", "password"),
2020-09-27 08:07:23 -07:00
{
// test update description
Config: testConfigWithToken(url, token, fooOrg, fooAccount, orgUserWithAcctsUpdate),
Check: resource.ComposeTestCheckFunc(
2020-10-07 12:04:11 -04:00
testAccCheckUserResourceExists(provider, "boundary_user.org1"),
testAccCheckAccountResourceExists(provider, "boundary_account.foo"),
2020-09-27 08:07:23 -07:00
resource.TestCheckResourceAttr("boundary_user.org1", DescriptionKey, "with accts"),
resource.TestCheckResourceAttr("boundary_user.org1", NameKey, "test"),
),
},
2020-10-07 12:04:11 -04:00
importStep("boundary_user.org1"),
importStep("boundary_account.foo", "password"),
2020-09-27 08:07:23 -07:00
},
})
}
2020-10-07 12:04:11 -04:00
func testAccCheckUserResourceAccountsSet(testProvider *schema.Provider, name string, accounts []string) resource.TestCheckFunc {
2020-09-27 08:07:23 -07:00
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("user resource not found: %s", name)
}
id := rs.Primary.ID
if id == "" {
return fmt.Errorf("user resource ID is not set")
}
// ensure accts are declared in state
acctIDs := []string{}
for _, acctResourceName := range acctIDs {
ur, ok := s.RootModule().Resources[acctResourceName]
if !ok {
return fmt.Errorf("account resource not found: %s", acctResourceName)
}
acctID := ur.Primary.ID
if id == "" {
return fmt.Errorf("account resource ID not set")
}
acctIDs = append(acctIDs, acctID)
}
// check boundary to ensure it matches
md := testProvider.Meta().(*metaData)
usrClient := users.NewClient(md.client)
u, err := usrClient.Read(context.Background(), id)
if err != nil {
return fmt.Errorf("Got an error when reading user %q: %v", id, err)
}
// for every account set on the user in the state, ensure
// each group in boundary has the same setings
if len(u.Item.AccountIds) == 0 {
return fmt.Errorf("no account found on user")
}
for _, stateAccount := range acctIDs {
ok := false
for _, gotAccount := range u.Item.AccountIds {
if gotAccount == stateAccount {
ok = true
}
}
if !ok {
return fmt.Errorf("account in state not set in boundary:\n in state: %+v\n in boundary: %+v", acctIDs, u.Item.AccountIds)
}
}
return nil
}
}
2020-10-07 12:04:11 -04:00
func testAccCheckUserResourceExists(testProvider *schema.Provider, name string) resource.TestCheckFunc {
2020-07-02 14:19:05 -07:00
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
id := rs.Primary.ID
if id == "" {
return fmt.Errorf("No ID is set")
}
md := testProvider.Meta().(*metaData)
2020-09-15 19:56:43 -04:00
usrs := users.NewClient(md.client)
2020-08-13 11:18:36 -07:00
_, err := usrs.Read(context.Background(), id)
2020-09-15 19:56:43 -04:00
if err != nil {
2020-07-02 14:19:05 -07:00
return fmt.Errorf("Got an error when reading user %q: %v", id, err)
}
return nil
}
}
2020-10-07 12:04:11 -04:00
func testAccCheckUserResourceDestroy(t *testing.T, testProvider *schema.Provider) resource.TestCheckFunc {
2020-07-02 14:19:05 -07:00
return func(s *terraform.State) error {
2020-08-13 11:18:36 -07:00
if testProvider.Meta() == nil {
t.Fatal("got nil provider metadata")
}
2020-07-02 14:19:05 -07:00
md := testProvider.Meta().(*metaData)
for _, rs := range s.RootModule().Resources {
switch rs.Type {
2020-08-11 16:12:26 -07:00
case "boundary_user":
2020-07-02 14:19:05 -07:00
id := rs.Primary.ID
2020-09-15 19:56:43 -04:00
usrs := users.NewClient(md.client)
2020-07-02 14:19:05 -07:00
_, err := usrs.Read(context.Background(), id)
2021-03-10 11:58:06 -08:00
if apiErr := api.AsServerError(err); apiErr == nil || apiErr.Response().StatusCode() != http.StatusNotFound {
2021-01-12 14:05:29 -08:00
return fmt.Errorf("didn't get a 404 when reading destroyed user %q: %v", id, err)
2020-07-02 14:19:05 -07:00
}
default:
2021-01-12 14:05:29 -08:00
continue
2020-07-02 14:19:05 -07:00
}
}
return nil
}
}