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

382 lines
11 KiB
Go
Raw Normal View History

// Copyright IBM Corp. 2020, 2026
// SPDX-License-Identifier: MPL-2.0
2020-07-02 14:22:08 -07:00
package provider
import (
2020-09-15 19:56:43 -04:00
"context"
2020-07-02 14:22:08 -07:00
"fmt"
"net/http"
2020-09-15 19:56:43 -04:00
"strings"
2020-07-02 14:22:08 -07:00
"testing"
"repo.anhonesthost.net/CyberCoveLLC/threshold/api"
"repo.anhonesthost.net/CyberCoveLLC/threshold/api/groups"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
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"
"github.com/stretchr/testify/require"
2020-07-02 14:22:08 -07:00
)
const (
fooGroupDescription = "bar"
fooGroupDescriptionUpdate = "foo bar"
)
var (
2020-08-13 11:18:36 -07:00
orgGroup = fmt.Sprintf(`
2020-09-15 19:56:43 -04:00
resource "boundary_group" "org1" {
name = "test"
2020-08-13 11:18:36 -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-08-13 11:18:36 -07:00
}`, fooGroupDescription)
orgGroupUpdate = fmt.Sprintf(`
2020-09-15 19:56:43 -04:00
resource "boundary_group" "org1" {
name = "test"
2020-08-13 11:18:36 -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-08-13 11:18:36 -07:00
}`, fooGroupDescriptionUpdate)
orgGroupWithMembers = `
2020-09-15 19:56:43 -04:00
resource "boundary_user" "org1" {
description = "org1"
scope_id = boundary_scope.org1.id
depends_on = [boundary_role.org1_admin]
}
resource "boundary_group" "with_members" {
description = "with members"
2020-09-15 19:56:43 -04:00
member_ids = [boundary_user.org1.id]
scope_id = boundary_scope.org1.id
depends_on = [boundary_role.org1_admin]
}`
orgGroupWithMembersUpdate = `
2020-09-15 19:56:43 -04:00
resource "boundary_user" "org1" {
description = "org1"
scope_id = boundary_scope.org1.id
depends_on = [boundary_role.org1_admin]
}
resource "boundary_user" "bar" {
2020-09-15 19:56:43 -04:00
description = "bar"
scope_id = boundary_scope.org1.id
depends_on = [boundary_role.org1_admin]
}
resource "boundary_group" "with_members" {
description = "with members"
2020-09-15 19:56:43 -04:00
member_ids = [boundary_user.org1.id, boundary_user.bar.id]
scope_id = boundary_scope.org1.id
depends_on = [boundary_role.org1_admin]
}`
2020-09-15 19:56:43 -04:00
orgToProjectGroupUpdate = `
resource "boundary_group" "org1" {
name = "test-to-proj"
description = "org1-test-to-proj"
scope_id = boundary_scope.proj1.id
depends_on = [boundary_role.org1_admin]
2020-09-15 19:56:43 -04:00
}`
2020-09-15 19:56:43 -04:00
projGroup = `
resource "boundary_group" "proj1" {
name = "test-proj"
description = "desc-test-proj"
scope_id = boundary_scope.proj1.id
depends_on = [boundary_role.org1_admin]
2020-09-15 19:56:43 -04:00
}`
2020-07-02 14:22:08 -07:00
2020-09-15 19:56:43 -04:00
projGroupUpdate = `
resource "boundary_group" "proj1" {
name = "test-proj-up"
description = "desc-test-proj-up"
scope_id = boundary_scope.proj1.id
depends_on = [boundary_role.org1_admin]
2020-09-15 19:56:43 -04:00
}`
2020-09-15 19:56:43 -04:00
projNameRemoval = `
resource "boundary_group" "proj1" {
name = ""
description = "no-name"
scope_id = boundary_scope.proj1.id
depends_on = [boundary_role.org1_admin]
2020-09-15 19:56:43 -04:00
}`
projToOrgGroupUpdate = `
resource "boundary_group" "proj1" {
name = "test-back"
description = "desc-back"
scope_id = boundary_scope.org1.id
depends_on = [boundary_role.org1_admin]
2020-09-15 19:56:43 -04:00
}`
2020-07-02 14:22:08 -07:00
)
2020-09-15 19:56:43 -04:00
// NOTE: this test also tests out the recovery KMS mechanism.
2020-07-02 14:22:08 -07:00
func TestAccGroup(t *testing.T) {
cfg, err := loadTestConfig()
require.NoError(t, err)
url := cfg.BoundaryAddr
suffix := id.UniqueId()
2020-07-02 14:22:08 -07:00
2020-10-07 12:04:11 -04:00
var provider *schema.Provider
2020-07-02 14:22:08 -07:00
resource.Test(t, resource.TestCase{
2022-05-13 16:30:40 -07:00
IsUnitTest: true,
2020-10-07 12:04:11 -04:00
ProviderFactories: providerFactories(&provider),
CheckDestroy: testAccCheckGroupResourceDestroy(t, provider),
2020-07-02 14:22:08 -07:00
Steps: []resource.TestStep{
{
// test create
Config: testConfigWithRecovery(t, url, fooOrg(suffix), orgGroup),
2020-08-13 11:18:36 -07:00
Check: resource.ComposeTestCheckFunc(
2020-10-07 12:04:11 -04:00
testAccCheckGroupResourceExists(provider, "boundary_group.org1"),
2020-09-15 19:56:43 -04:00
resource.TestCheckResourceAttr("boundary_group.org1", DescriptionKey, fooGroupDescription),
resource.TestCheckResourceAttr("boundary_group.org1", NameKey, "test"),
2020-08-13 11:18:36 -07:00
),
},
2020-10-07 12:04:11 -04:00
importStep("boundary_group.org1"),
2020-08-13 11:18:36 -07:00
{
// test update
Config: testConfigWithRecovery(t, url, fooOrg(suffix), orgGroupUpdate),
Check: resource.ComposeTestCheckFunc(
2020-10-07 12:04:11 -04:00
testAccCheckGroupResourceExists(provider, "boundary_group.org1"),
2020-09-15 19:56:43 -04:00
resource.TestCheckResourceAttr("boundary_group.org1", DescriptionKey, fooGroupDescriptionUpdate),
),
},
2020-10-07 12:04:11 -04:00
importStep("boundary_group.org1"),
{
// test update to project scope
Config: testConfigWithRecovery(t, url, fooOrg(suffix), firstProjectFoo, orgToProjectGroupUpdate),
2020-08-13 11:18:36 -07:00
Check: resource.ComposeTestCheckFunc(
2020-10-07 12:04:11 -04:00
testAccCheckGroupResourceExists(provider, "boundary_group.org1"),
2020-09-15 19:56:43 -04:00
resource.TestCheckResourceAttr("boundary_group.org1", DescriptionKey, "org1-test-to-proj"),
2020-10-07 12:04:11 -04:00
testAccCheckGroupScope(provider, "boundary_group.org1", "p_"),
2020-08-13 11:18:36 -07:00
),
},
2020-10-07 12:04:11 -04:00
importStep("boundary_group.org1"),
2020-08-13 11:18:36 -07:00
{
// test create
Config: testConfigWithRecovery(t, url, fooOrg(suffix), firstProjectFoo, projGroup),
2020-07-02 14:22:08 -07:00
Check: resource.ComposeTestCheckFunc(
2020-10-07 12:04:11 -04:00
testAccCheckGroupResourceExists(provider, "boundary_group.proj1"),
2020-09-15 19:56:43 -04:00
resource.TestCheckResourceAttr("boundary_group.proj1", DescriptionKey, "desc-test-proj"),
resource.TestCheckResourceAttr("boundary_group.proj1", NameKey, "test-proj"),
2020-10-07 12:04:11 -04:00
testAccCheckGroupScope(provider, "boundary_group.proj1", "p_"),
2020-07-02 14:22:08 -07:00
),
},
2020-10-07 12:04:11 -04:00
importStep("boundary_group.proj1"),
2020-07-02 14:22:08 -07:00
{
// test update
Config: testConfigWithRecovery(t, url, fooOrg(suffix), firstProjectFoo, projGroupUpdate),
2020-07-02 14:22:08 -07:00
Check: resource.ComposeTestCheckFunc(
2020-10-07 12:04:11 -04:00
testAccCheckGroupResourceExists(provider, "boundary_group.proj1"),
2020-09-15 19:56:43 -04:00
resource.TestCheckResourceAttr("boundary_group.proj1", DescriptionKey, "desc-test-proj-up"),
2020-10-07 12:04:11 -04:00
testAccCheckGroupScope(provider, "boundary_group.proj1", "p_"),
2020-09-15 19:56:43 -04:00
),
},
2020-10-07 12:04:11 -04:00
importStep("boundary_group.proj1"),
2020-09-15 19:56:43 -04:00
{
// test name removal
Config: testConfigWithRecovery(t, url, fooOrg(suffix), firstProjectFoo, projNameRemoval),
2020-09-15 19:56:43 -04:00
Check: resource.ComposeTestCheckFunc(
2020-10-07 12:04:11 -04:00
testAccCheckGroupResourceExists(provider, "boundary_group.proj1"),
2020-09-15 19:56:43 -04:00
resource.TestCheckResourceAttr("boundary_group.proj1", NameKey, ""),
2020-10-07 12:04:11 -04:00
testAccCheckGroupScope(provider, "boundary_group.proj1", "p_"),
2020-07-02 14:22:08 -07:00
),
},
2020-10-07 12:04:11 -04:00
importStep("boundary_group.proj1"),
2020-07-02 14:22:08 -07:00
{
// test update to org scope
Config: testConfigWithRecovery(t, url, fooOrg(suffix), firstProjectFoo, projToOrgGroupUpdate),
2020-07-02 14:22:08 -07:00
Check: resource.ComposeTestCheckFunc(
2020-10-07 12:04:11 -04:00
testAccCheckGroupResourceExists(provider, "boundary_group.proj1"),
2020-09-15 19:56:43 -04:00
resource.TestCheckResourceAttr("boundary_group.proj1", DescriptionKey, "desc-back"),
2020-10-07 12:04:11 -04:00
testAccCheckGroupScope(provider, "boundary_group.proj1", "o_"),
2020-07-02 14:22:08 -07:00
),
},
2020-10-07 12:04:11 -04:00
importStep("boundary_group.proj1"),
2020-07-02 14:22:08 -07:00
},
})
}
func TestAccGroupWithMembers(t *testing.T) {
cfg, err := loadTestConfig()
require.NoError(t, err)
url := cfg.BoundaryAddr
suffix := id.UniqueId()
2020-10-07 12:04:11 -04:00
var provider *schema.Provider
resource.Test(t, resource.TestCase{
2020-10-07 12:04:11 -04:00
ProviderFactories: providerFactories(&provider),
2020-10-06 20:34:16 -04:00
2020-10-07 12:04:11 -04:00
CheckDestroy: testAccCheckGroupResourceDestroy(t, provider),
Steps: []resource.TestStep{
{
// test create
Config: testConfig(url, fooOrg(suffix), orgGroupWithMembers),
Check: resource.ComposeTestCheckFunc(
2020-10-07 12:04:11 -04:00
testAccCheckGroupResourceExists(provider, "boundary_group.with_members"),
testAccCheckUserResourceExists(provider, "boundary_user.org1"),
2020-09-15 19:56:43 -04:00
resource.TestCheckResourceAttr("boundary_group.with_members", DescriptionKey, "with members"),
2020-10-07 12:04:11 -04:00
testAccCheckGroupResourceMembersSet(provider, "boundary_group.with_members", []string{"boundary_user.org1"}),
),
},
2020-10-07 12:04:11 -04:00
importStep("boundary_group.with_members"),
{
// test update
Config: testConfig(url, fooOrg(suffix), orgGroupWithMembersUpdate),
Check: resource.ComposeTestCheckFunc(
2020-10-07 12:04:11 -04:00
testAccCheckGroupResourceExists(provider, "boundary_group.with_members"),
testAccCheckUserResourceExists(provider, "boundary_user.org1"),
2020-09-15 19:56:43 -04:00
resource.TestCheckResourceAttr("boundary_group.with_members", DescriptionKey, "with members"),
2020-10-07 12:04:11 -04:00
testAccCheckGroupResourceMembersSet(provider, "boundary_group.with_members", []string{"boundary_user.org1", "boundary_user.bar"}),
),
},
2020-10-07 12:04:11 -04:00
importStep("boundary_group.with_members"),
importStep("boundary_user.org1"),
},
})
}
2020-10-07 12:04:11 -04:00
func testAccCheckGroupResourceMembersSet(testProvider *schema.Provider, name string, members []string) resource.TestCheckFunc {
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")
}
// ensure users are declared in state
memberIDs := []string{}
for _, userResourceName := range members {
ur, ok := s.RootModule().Resources[userResourceName]
if !ok {
return fmt.Errorf("user resource not found: %s", userResourceName)
}
memberID := ur.Primary.ID
if id == "" {
return fmt.Errorf("principal resource ID not set")
}
memberIDs = append(memberIDs, memberID)
}
// check boundary to ensure it matches
md := testProvider.Meta().(*metaData)
2020-09-15 19:56:43 -04:00
grpsClient := groups.NewClient(md.client)
g, err := grpsClient.Read(context.Background(), id)
if err != nil {
2020-09-15 19:56:43 -04:00
return fmt.Errorf("Got an error when reading group %q: %v", id, err)
}
// for every member set as a member on the group in the state, ensure
// each group in boundary has the same setings
2020-09-15 19:56:43 -04:00
if len(g.Item.MemberIds) == 0 {
return fmt.Errorf("no members found on group")
}
2020-09-15 19:56:43 -04:00
for _, stateMember := range g.Item.MemberIds {
ok := false
for _, gotMember := range memberIDs {
if gotMember == stateMember {
ok = true
}
}
if !ok {
return fmt.Errorf("member in state not set in boundary: %s", stateMember)
}
}
return nil
}
}
2020-10-07 12:04:11 -04:00
func testAccCheckGroupScope(testProvider *schema.Provider, name, prefix string) resource.TestCheckFunc {
2020-07-02 14:22:08 -07:00
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
2020-07-02 14:22:08 -07:00
}
id := rs.Primary.ID
if id == "" {
2020-07-02 14:22:08 -07:00
return fmt.Errorf("No ID is set")
}
md := testProvider.Meta().(*metaData)
2020-09-15 19:56:43 -04:00
grps := groups.NewClient(md.client)
2020-08-13 11:18:36 -07:00
g, err := grps.Read(context.Background(), id)
if err != nil {
return fmt.Errorf("could not read resource state %q: %v", id, err)
2020-07-02 14:22:08 -07:00
}
2020-09-15 19:56:43 -04:00
if !strings.HasPrefix(g.Item.ScopeId, prefix) {
return fmt.Errorf("Scope ID in state does not have prefix: %s != %s", g.Item.ScopeId, prefix)
}
return nil
2020-07-02 14:22:08 -07:00
}
}
2020-10-07 12:04:11 -04:00
func testAccCheckGroupResourceExists(testProvider *schema.Provider, name string) resource.TestCheckFunc {
2020-07-02 14:22:08 -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
grps := groups.NewClient(md.client)
2020-08-13 11:18:36 -07:00
_, err := grps.Read(context.Background(), id)
2020-09-15 19:56:43 -04:00
if err != nil {
2020-07-02 14:22:08 -07:00
return fmt.Errorf("Got an error when reading group %q: %v", id, err)
}
return nil
}
}
2020-10-07 12:04:11 -04:00
func testAccCheckGroupResourceDestroy(t *testing.T, testProvider *schema.Provider) resource.TestCheckFunc {
2020-07-02 14:22:08 -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:22:08 -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_group":
2020-09-15 19:56:43 -04:00
grps := groups.NewClient(md.client)
2020-07-02 14:22:08 -07:00
id := rs.Primary.ID
_, err := grps.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 resource %q: %v", id, apiErr)
2020-07-02 14:22:08 -07:00
}
default:
2021-01-12 14:05:29 -08:00
continue
2020-07-02 14:22:08 -07:00
}
}
return nil
}
}