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

559 lines
17 KiB
Go
Raw Normal View History

2020-07-02 14:23:56 -07:00
package provider
import (
2020-09-15 19:56:43 -04:00
"context"
2020-07-02 14:23:56 -07:00
"errors"
"fmt"
"net/http"
"strings"
"testing"
"github.com/hashicorp/boundary/api"
2020-08-11 16:12:26 -07:00
"github.com/hashicorp/boundary/api/roles"
"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:23:56 -07:00
)
const (
fooRoleDescription = "bar"
fooRoleDescriptionUpdate = "foo bar"
)
var (
orgRole = fmt.Sprintf(`
2020-08-11 16:12:26 -07:00
resource "boundary_role" "foo" {
2020-09-15 19:56:43 -04:00
name = "test"
2020-07-02 14:23:56 -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-07-02 14:23:56 -07:00
}`, fooRoleDescription)
orgRoleUpdate = fmt.Sprintf(`
2020-08-11 16:12:26 -07:00
resource "boundary_role" "foo" {
2020-09-15 19:56:43 -04:00
name = "test"
2020-07-02 14:23:56 -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-07-02 14:23:56 -07:00
}`, fooRoleDescriptionUpdate)
2020-07-10 15:32:44 -07:00
projRole = fmt.Sprintf(`
resource "boundary_role" "foo" {
2020-09-15 19:56:43 -04:00
name = "test"
description = "%s"
2020-09-15 19:56:43 -04:00
scope_id = boundary_scope.proj1.id
depends_on = [boundary_role.proj1_admin]
}`, fooRoleDescription)
projRoleUpdate = fmt.Sprintf(`
resource "boundary_role" "foo" {
2020-09-15 19:56:43 -04:00
name = "test"
description = "%s"
2020-09-15 19:56:43 -04:00
scope_id = boundary_scope.proj1.id
depends_on = [boundary_role.proj1_admin]
}`, fooRoleDescriptionUpdate)
projRoleWithPrincipal = `
2020-08-11 16:12:26 -07:00
resource "boundary_user" "foo" {
name = "foo"
scope_id = boundary_scope.org1.id
depends_on = [boundary_role.org1_admin]
2020-07-10 15:32:44 -07:00
}
resource "boundary_role" "with_principal" {
2020-09-15 19:56:43 -04:00
name = "with_principal"
description = "with principal"
principal_ids = [boundary_user.foo.id]
scope_id = boundary_scope.proj1.id
depends_on = [boundary_role.proj1_admin]
2020-07-10 15:32:44 -07:00
}`
projRoleWithPrincipalUpdate = `
2020-08-11 16:12:26 -07:00
resource "boundary_user" "foo" {
name = "foo"
scope_id = boundary_scope.org1.id
depends_on = [boundary_role.org1_admin]
2020-07-10 15:32:44 -07:00
}
2020-08-11 16:12:26 -07:00
resource "boundary_user" "bar" {
name = "bar"
scope_id = boundary_scope.org1.id
depends_on = [boundary_role.org1_admin]
2020-07-10 15:32:44 -07:00
}
resource "boundary_role" "with_principal" {
2020-09-15 19:56:43 -04:00
name = "with_principal"
description = "with principal"
principal_ids = [boundary_user.foo.id, boundary_user.bar.id]
scope_id = boundary_scope.proj1.id
depends_on = [boundary_role.proj1_admin]
2020-07-10 15:32:44 -07:00
}`
projRoleWithGroups = `
2020-08-11 16:12:26 -07:00
resource "boundary_group" "foo" {
name = "foo"
scope_id = boundary_scope.proj1.id
depends_on = [boundary_role.proj1_admin]
2020-07-10 15:32:44 -07:00
}
resource "boundary_role" "with_groups" {
2020-09-15 19:56:43 -04:00
name = "with_groups"
description = "with groups"
principal_ids = [boundary_group.foo.id]
scope_id = boundary_scope.proj1.id
depends_on = [boundary_role.proj1_admin]
2020-07-10 15:32:44 -07:00
}`
projRoleWithGroupsUpdate = `
2020-08-11 16:12:26 -07:00
resource "boundary_group" "foo" {
name = "foo"
scope_id = boundary_scope.proj1.id
depends_on = [boundary_role.proj1_admin]
2020-07-10 15:32:44 -07:00
}
2020-08-11 16:12:26 -07:00
resource "boundary_group" "bar" {
name = "bar"
scope_id = boundary_scope.proj1.id
depends_on = [boundary_role.proj1_admin]
2020-07-10 15:32:44 -07:00
}
resource "boundary_role" "with_groups" {
2020-09-15 19:56:43 -04:00
name = "with_groups"
description = "with groups"
principal_ids = [boundary_group.foo.id, boundary_group.bar.id]
scope_id = boundary_scope.proj1.id
depends_on = [boundary_role.proj1_admin]
2020-07-10 15:32:44 -07:00
}`
2020-07-12 15:00:16 -07:00
2020-10-01 11:00:30 -04:00
readonlyGrant = "id=*;type=*;actions=read"
readonlyGrantUpdate = "id=*;type=*;actions=read,create"
2020-08-13 11:18:36 -07:00
projRoleWithGrants = fmt.Sprintf(`
resource "boundary_role" "with_grants" {
2020-09-15 19:56:43 -04:00
name = "with_grants"
description = "with grants"
grant_strings = ["%s"]
scope_id = boundary_scope.proj1.id
depends_on = [boundary_role.proj1_admin]
2020-07-12 15:00:16 -07:00
}`, readonlyGrant)
2020-08-13 11:18:36 -07:00
projRoleWithGrantsUpdate = fmt.Sprintf(`
resource "boundary_role" "with_grants" {
2020-09-15 19:56:43 -04:00
name = "with_grants"
description = "with grants"
grant_strings = ["%s", "%s"]
scope_id = boundary_scope.proj1.id
depends_on = [boundary_role.proj1_admin]
2020-07-12 15:00:16 -07:00
}`, readonlyGrant, readonlyGrantUpdate)
2020-07-02 14:23:56 -07:00
)
func TestAccRoleToOrgToProject(t *testing.T) {
tc := controller.NewTestController(t, tcConfig...)
2020-07-12 15:00:16 -07:00
defer tc.Shutdown()
url := tc.ApiAddrs()[0]
2020-10-07 12:04:11 -04:00
var provider *schema.Provider
2020-07-12 15:00:16 -07:00
resource.Test(t, resource.TestCase{
2020-10-07 12:04:11 -04:00
ProviderFactories: providerFactories(&provider),
CheckDestroy: testAccCheckRoleResourceDestroy(t, provider),
2020-07-12 15:00:16 -07:00
Steps: []resource.TestStep{
{
// test org role create
Config: testConfig(url, fooOrg, orgRole),
2020-07-12 15:00:16 -07:00
Check: resource.ComposeTestCheckFunc(
2020-10-07 12:04:11 -04:00
testAccCheckRoleResourceExists(provider, "boundary_role.foo"),
resource.TestCheckResourceAttr("boundary_role.foo", "name", "test"),
resource.TestCheckResourceAttr("boundary_role.foo", "description", fooRoleDescription),
2020-07-12 15:00:16 -07:00
),
},
2020-10-07 12:04:11 -04:00
importStep("boundary_role.foo"),
2020-07-12 15:00:16 -07:00
{
// test org role update
Config: testConfig(url, fooOrg, orgRoleUpdate),
Check: resource.ComposeTestCheckFunc(
2020-10-07 12:04:11 -04:00
testAccCheckRoleResourceExists(provider, "boundary_role.foo"),
resource.TestCheckResourceAttr("boundary_role.foo", "name", "test"),
resource.TestCheckResourceAttr("boundary_role.foo", "description", fooRoleDescriptionUpdate),
),
},
2020-10-07 12:04:11 -04:00
importStep("boundary_role.foo"),
{
// test org to project role create
2020-09-15 19:56:43 -04:00
Config: testConfig(url, fooOrg, firstProjectFoo, projRole),
Check: resource.ComposeTestCheckFunc(
2020-10-07 12:04:11 -04:00
testAccCheckRoleResourceExists(provider, "boundary_role.foo"),
resource.TestCheckResourceAttr("boundary_role.foo", "name", "test"),
resource.TestCheckResourceAttr("boundary_role.foo", "description", fooRoleDescription),
),
},
2020-10-07 12:04:11 -04:00
importStep("boundary_role.foo"),
{
// test project role update
2020-09-15 19:56:43 -04:00
Config: testConfig(url, fooOrg, firstProjectFoo, projRoleUpdate),
2020-07-12 15:00:16 -07:00
Check: resource.ComposeTestCheckFunc(
2020-10-07 12:04:11 -04:00
testAccCheckRoleResourceExists(provider, "boundary_role.foo"),
resource.TestCheckResourceAttr("boundary_role.foo", "name", "test"),
resource.TestCheckResourceAttr("boundary_role.foo", "description", fooRoleDescriptionUpdate),
2020-07-12 15:00:16 -07:00
),
},
2020-10-07 12:04:11 -04:00
importStep("boundary_role.foo"),
2020-07-12 15:00:16 -07:00
},
})
}
func TestAccRoleWithGrants(t *testing.T) {
tc := controller.NewTestController(t, tcConfig...)
2020-07-10 15:32:44 -07:00
defer tc.Shutdown()
url := tc.ApiAddrs()[0]
2020-10-07 12:04:11 -04:00
var provider *schema.Provider
2020-07-10 15:32:44 -07:00
resource.Test(t, resource.TestCase{
2020-10-07 12:04:11 -04:00
ProviderFactories: providerFactories(&provider),
CheckDestroy: testAccCheckRoleResourceDestroy(t, provider),
2020-07-10 15:32:44 -07:00
Steps: []resource.TestStep{
{
// test project role create with grants
2020-09-15 19:56:43 -04:00
Config: testConfig(url, fooOrg, firstProjectFoo, projRoleWithGrants),
2020-07-10 15:32:44 -07:00
Check: resource.ComposeTestCheckFunc(
2020-10-07 12:04:11 -04:00
testAccCheckRoleResourceExists(provider, "boundary_role.with_grants"),
testAccCheckRoleResourceGrantsSet(provider, "boundary_role.with_grants", []string{readonlyGrant}),
resource.TestCheckResourceAttr("boundary_role.with_grants", "name", "with_grants"),
resource.TestCheckResourceAttr("boundary_role.with_grants", "description", "with grants"),
2020-07-10 15:32:44 -07:00
),
},
2020-10-07 12:04:11 -04:00
importStep("boundary_role.with_grants"),
2020-07-10 15:32:44 -07:00
{
// test project role update with grants
2020-09-15 19:56:43 -04:00
Config: testConfig(url, fooOrg, firstProjectFoo, projRoleWithGrantsUpdate),
2020-07-10 15:32:44 -07:00
Check: resource.ComposeTestCheckFunc(
2020-10-07 12:04:11 -04:00
testAccCheckRoleResourceExists(provider, "boundary_role.with_grants"),
testAccCheckRoleResourceGrantsSet(provider, "boundary_role.with_grants", []string{readonlyGrant, readonlyGrantUpdate}),
resource.TestCheckResourceAttr("boundary_role.with_grants", "name", "with_grants"),
resource.TestCheckResourceAttr("boundary_role.with_grants", "description", "with grants"),
2020-07-10 15:32:44 -07:00
),
},
2020-10-07 12:04:11 -04:00
importStep("boundary_role.with_grants"),
2020-07-10 15:32:44 -07:00
},
})
}
func TestAccRoleWithPrincipals(t *testing.T) {
tc := controller.NewTestController(t, tcConfig...)
2020-07-10 15:32:44 -07:00
defer tc.Shutdown()
url := tc.ApiAddrs()[0]
2020-10-07 12:04:11 -04:00
var provider *schema.Provider
2020-07-10 15:32:44 -07:00
resource.Test(t, resource.TestCase{
2020-10-07 12:04:11 -04:00
ProviderFactories: providerFactories(&provider),
CheckDestroy: testAccCheckRoleResourceDestroy(t, provider),
2020-07-10 15:32:44 -07:00
Steps: []resource.TestStep{
{
// test create
2020-09-15 19:56:43 -04:00
Config: testConfig(url, fooOrg, firstProjectFoo, projRoleWithPrincipal),
2020-07-10 15:32:44 -07:00
Check: resource.ComposeTestCheckFunc(
2020-10-07 12:04:11 -04:00
testAccCheckRoleResourceExists(provider, "boundary_role.with_principal"),
testAccCheckRoleResourcePrincipalsSet(provider, "boundary_role.with_principal", []string{"boundary_user.foo"}),
testAccCheckUserResourceExists(provider, "boundary_user.foo"),
2020-09-15 19:56:43 -04:00
resource.TestCheckResourceAttr("boundary_role.with_principal", DescriptionKey, "with principal"),
resource.TestCheckResourceAttr("boundary_role.with_principal", NameKey, "with_principal"),
resource.TestCheckResourceAttr("boundary_user.foo", "name", "foo"),
2020-07-10 15:32:44 -07:00
),
},
2020-10-07 12:04:11 -04:00
importStep("boundary_role.with_principal"),
2020-07-10 15:32:44 -07:00
{
// test update
2020-09-15 19:56:43 -04:00
Config: testConfig(url, fooOrg, firstProjectFoo, projRoleWithPrincipalUpdate),
2020-07-10 15:32:44 -07:00
Check: resource.ComposeTestCheckFunc(
2020-10-07 12:04:11 -04:00
testAccCheckRoleResourceExists(provider, "boundary_role.with_principal"),
testAccCheckUserResourceExists(provider, "boundary_user.foo"),
testAccCheckUserResourceExists(provider, "boundary_user.bar"),
testAccCheckRoleResourcePrincipalsSet(provider, "boundary_role.with_principal", []string{"boundary_user.foo", "boundary_user.bar"}),
2020-07-10 15:32:44 -07:00
),
},
2020-10-07 12:04:11 -04:00
importStep("boundary_role.with_principal"),
2020-07-10 15:32:44 -07:00
},
})
}
func TestAccRoleWithGroups(t *testing.T) {
tc := controller.NewTestController(t, tcConfig...)
2020-07-02 14:23:56 -07:00
defer tc.Shutdown()
url := tc.ApiAddrs()[0]
2020-10-07 12:04:11 -04:00
var provider *schema.Provider
2020-07-02 14:23:56 -07:00
resource.Test(t, resource.TestCase{
2020-10-07 12:04:11 -04:00
ProviderFactories: providerFactories(&provider),
CheckDestroy: testAccCheckRoleResourceDestroy(t, provider),
2020-07-02 14:23:56 -07:00
Steps: []resource.TestStep{
{
// test create
2020-09-15 19:56:43 -04:00
Config: testConfig(url, fooOrg, firstProjectFoo, projRoleWithGroups),
2020-07-02 14:23:56 -07:00
Check: resource.ComposeTestCheckFunc(
2020-10-07 12:04:11 -04:00
testAccCheckRoleResourceExists(provider, "boundary_role.with_groups"),
testAccCheckRoleResourceGroupsSet(provider, "boundary_role.with_groups", []string{"boundary_group.foo"}),
testAccCheckGroupResourceExists(provider, "boundary_group.foo"),
2020-09-15 19:56:43 -04:00
resource.TestCheckResourceAttr("boundary_role.with_groups", DescriptionKey, "with groups"),
resource.TestCheckResourceAttr("boundary_role.with_groups", NameKey, "with_groups"),
resource.TestCheckResourceAttr("boundary_group.foo", "name", "foo"),
2020-07-02 14:23:56 -07:00
),
},
2020-10-07 12:04:11 -04:00
importStep("boundary_role.with_groups"),
2020-07-02 14:23:56 -07:00
{
// test update
2020-09-15 19:56:43 -04:00
Config: testConfig(url, fooOrg, firstProjectFoo, projRoleWithGroupsUpdate),
2020-07-02 14:23:56 -07:00
Check: resource.ComposeTestCheckFunc(
2020-10-07 12:04:11 -04:00
testAccCheckRoleResourceExists(provider, "boundary_role.with_groups"),
testAccCheckGroupResourceExists(provider, "boundary_group.foo"),
testAccCheckGroupResourceExists(provider, "boundary_group.bar"),
testAccCheckRoleResourceGroupsSet(provider, "boundary_role.with_groups", []string{"boundary_group.foo", "boundary_group.bar"}),
2020-07-02 14:23:56 -07:00
),
},
2020-10-07 12:04:11 -04:00
importStep("boundary_role.with_groups"),
2020-07-02 14:23:56 -07:00
},
})
}
// testAccCheckRoleDestroyed checks the terraform state for the host
// catalog and returns an error if found.
//
2020-08-11 16:12:26 -07:00
// TODO(malnick) This method falls short of checking the Boundary API for
2020-07-02 14:23:56 -07:00
// the resource if the resource is not found in state. This is due to us not
// having the host catalog ID, but it doesn't guarantee that the resource was
// successfully removed.
//
2020-08-11 16:12:26 -07:00
// It does check Boundary if the resource is found in state to point out any
2020-07-02 14:23:56 -07:00
// misalignment between what is in state and the actual configuration.
2020-10-07 12:04:11 -04:00
func testAccCheckRoleDestroyed(testProvider *schema.Provider, name string) resource.TestCheckFunc {
2020-07-02 14:23:56 -07:00
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
// If it's not in state, it's destroyed in TF but not guaranteed to be destroyed
2020-08-11 16:12:26 -07:00
// in Boundary. Need to find a way to get the host catalog ID here so we can
2020-07-02 14:23:56 -07:00
// form a lookup to the WT API to check this.
return nil
}
errs := []string{}
errs = append(errs, fmt.Sprintf("Found role resource in state: %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
rolesClient := roles.NewClient(md.client)
2020-08-13 11:18:36 -07:00
_, err := rolesClient.Read(context.Background(), id)
2021-03-10 11:58:06 -08:00
if apiErr := api.AsServerError(err); apiErr == nil || apiErr.Response().StatusCode() != http.StatusNotFound {
2020-07-02 14:23:56 -07:00
errs = append(errs, fmt.Sprintf("Role not destroyed %q: %v", id, apiErr))
}
return errors.New(strings.Join(errs, ","))
}
}
2020-10-07 12:04:11 -04:00
func testAccCheckRoleResourceExists(testProvider *schema.Provider, name string) resource.TestCheckFunc {
2020-07-02 14:23:56 -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
rolesClient := roles.NewClient(md.client)
2020-08-13 11:18:36 -07:00
if _, err := rolesClient.Read(context.Background(), id); err != nil {
2020-07-02 14:23:56 -07:00
return fmt.Errorf("Got an error when reading role %q: %v", id, err)
}
return nil
}
}
2020-10-07 12:04:11 -04:00
func testAccCheckRoleResourcePrincipalsSet(testProvider *schema.Provider, name string, principals []string) resource.TestCheckFunc {
2020-07-10 15:32:44 -07:00
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("role resource not found: %s", name)
}
id := rs.Primary.ID
if id == "" {
return fmt.Errorf("role resource ID is not set")
}
userIDs := []string{}
for _, userResourceName := range principals {
2020-07-10 15:32:44 -07:00
ur, ok := s.RootModule().Resources[userResourceName]
if !ok {
return fmt.Errorf("user resource not found: %s", userResourceName)
}
userID := ur.Primary.ID
if id == "" {
return fmt.Errorf("principal resource ID not set")
2020-07-10 15:32:44 -07:00
}
userIDs = append(userIDs, userID)
}
md := testProvider.Meta().(*metaData)
2020-09-15 19:56:43 -04:00
rolesClient := roles.NewClient(md.client)
2020-07-10 15:32:44 -07:00
rr, err := rolesClient.Read(context.Background(), id)
2020-07-10 15:32:44 -07:00
if err != nil {
return fmt.Errorf("Got an error when reading role %q: %v", id, err)
}
// for every principal set as a principal on the role in the state, ensure
2020-08-11 16:12:26 -07:00
// each role in boundary has the same setings
2020-09-15 19:56:43 -04:00
if len(rr.Item.PrincipalIds) == 0 {
return fmt.Errorf("no principals found in boundary")
2020-07-10 15:32:44 -07:00
}
2020-09-15 19:56:43 -04:00
for _, statePrincipal := range rr.Item.PrincipalIds {
2020-07-10 15:32:44 -07:00
ok := false
for _, gotPrincipal := range userIDs {
if gotPrincipal == statePrincipal {
2020-07-10 15:32:44 -07:00
ok = true
}
}
if !ok {
return fmt.Errorf("principal in state not set in boundary: %s", statePrincipal)
2020-07-10 15:32:44 -07:00
}
}
return nil
}
}
2020-10-07 12:04:11 -04:00
func testAccCheckRoleResourceGroupsSet(testProvider *schema.Provider, name string, groups []string) resource.TestCheckFunc {
2020-07-10 15:32:44 -07:00
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("role resource not found: %s", name)
}
id := rs.Primary.ID
if id == "" {
return fmt.Errorf("role resource ID is not set")
}
groupIDs := []string{}
for _, groupResourceName := range groups {
gr, ok := s.RootModule().Resources[groupResourceName]
if !ok {
return fmt.Errorf("group resource not found: %s", groupResourceName)
}
groupID := gr.Primary.ID
if id == "" {
return fmt.Errorf("group resource ID not set")
}
groupIDs = append(groupIDs, groupID)
}
md := testProvider.Meta().(*metaData)
2020-09-15 19:56:43 -04:00
rolesClient := roles.NewClient(md.client)
2020-07-10 15:32:44 -07:00
rr, err := rolesClient.Read(context.Background(), id)
2020-07-10 15:32:44 -07:00
if err != nil {
return fmt.Errorf("Got an error when reading role %q: %v", id, err)
}
// for every principal set as a principal on the role in the state, ensure
2020-08-11 16:12:26 -07:00
// each role in boundary has the same setings
2020-09-15 19:56:43 -04:00
if len(rr.Item.PrincipalIds) == 0 {
2020-08-11 16:12:26 -07:00
return fmt.Errorf("no groups found in boundary")
2020-07-10 15:32:44 -07:00
}
2020-09-15 19:56:43 -04:00
for _, stateGroup := range rr.Item.PrincipalIds {
2020-07-10 15:32:44 -07:00
ok := false
for _, gotGroup := range groupIDs {
if gotGroup == stateGroup {
ok = true
}
}
if !ok {
2020-08-11 16:12:26 -07:00
return fmt.Errorf("group in state not set in boundary: %s", stateGroup)
2020-07-10 15:32:44 -07:00
}
}
return nil
}
}
2020-07-12 15:00:16 -07:00
2020-10-07 12:04:11 -04:00
func testAccCheckRoleResourceGrantsSet(testProvider *schema.Provider, name string, expectedGrants []string) resource.TestCheckFunc {
2020-07-12 15:00:16 -07:00
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("role resource not found: %s", name)
}
id := rs.Primary.ID
if id == "" {
return fmt.Errorf("role resource ID is not set")
}
md := testProvider.Meta().(*metaData)
2020-09-15 19:56:43 -04:00
rolesClient := roles.NewClient(md.client)
2020-07-12 15:00:16 -07:00
rr, err := rolesClient.Read(context.Background(), id)
2020-07-12 15:00:16 -07:00
if err != nil {
return fmt.Errorf("Got an error when reading role %q: %v", id, err)
}
// for each expected grant, ensure they're set on the role
2020-09-15 19:56:43 -04:00
if len(rr.Item.Grants) == 0 {
return fmt.Errorf("no grants found on role, %+v\n", rr)
2020-07-12 15:00:16 -07:00
}
for _, grant := range expectedGrants {
ok = false
2020-09-15 19:56:43 -04:00
for _, gotGrant := range rr.Item.Grants {
2020-08-13 11:18:36 -07:00
if gotGrant.Raw == grant {
2020-07-12 15:00:16 -07:00
ok = true
}
}
if !ok {
2020-09-15 19:56:43 -04:00
return fmt.Errorf("expected grant not found on role, %s: %s\n Have: %#v\n", rr.Item.Name, grant, rr.Item)
2020-07-12 15:00:16 -07:00
}
}
return nil
}
}
2020-10-07 12:04:11 -04:00
func testAccCheckRoleResourceDestroy(t *testing.T, testProvider *schema.Provider) resource.TestCheckFunc {
2020-07-02 14:23:56 -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:23:56 -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_role":
2020-07-02 14:23:56 -07:00
id := rs.Primary.ID
2020-09-15 19:56:43 -04:00
rolesClient := roles.NewClient(md.client)
2020-07-02 14:23:56 -07:00
_, err := rolesClient.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 role %q: %v", id, err)
2020-07-02 14:23:56 -07:00
}
default:
continue
2020-07-02 14:23:56 -07:00
}
}
return nil
}
}