-
Notifications
You must be signed in to change notification settings - Fork 147
/
resource_tfe_organization_membership_test.go
145 lines (122 loc) · 3.94 KB
/
resource_tfe_organization_membership_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package tfe
import (
"fmt"
"math/rand"
"testing"
"time"
tfe "github.com/hashicorp/go-tfe"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
func TestAccTFEOrganizationMembership_basic(t *testing.T) {
mem := &tfe.OrganizationMembership{}
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
orgName := fmt.Sprintf("tst-terraform-%d", rInt)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFEOrganizationMembershipDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFEOrganizationMembership_basic(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckTFEOrganizationMembershipExists(
"tfe_organization_membership.foobar", mem),
testAccCheckTFEOrganizationMembershipAttributes(mem, orgName),
resource.TestCheckResourceAttr(
"tfe_organization_membership.foobar", "email", "example@hashicorp.com"),
resource.TestCheckResourceAttr(
"tfe_organization_membership.foobar", "organization", orgName),
resource.TestCheckResourceAttrSet("tfe_organization_membership.foobar", "user_id"),
resource.TestCheckResourceAttr(
"tfe_organization_membership.foobar", "username", ""),
),
},
},
})
}
func TestAccTFEOrganizationMembershipImport(t *testing.T) {
rInt := rand.New(rand.NewSource(time.Now().UnixNano())).Int()
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckTFEOrganizationMembershipDestroy,
Steps: []resource.TestStep{
{
Config: testAccTFEOrganizationMembership_basic(rInt),
},
{
ResourceName: "tfe_organization_membership.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func testAccCheckTFEOrganizationMembershipExists(
n string, membership *tfe.OrganizationMembership) resource.TestCheckFunc {
return func(s *terraform.State) error {
tfeClient := testAccProvider.Meta().(*tfe.Client)
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No instance ID is set")
}
options := tfe.OrganizationMembershipReadOptions{
Include: []tfe.OrgMembershipIncludeOpt{tfe.OrgMembershipUser},
}
m, err := tfeClient.OrganizationMemberships.ReadWithOptions(ctx, rs.Primary.ID, options)
if err != nil {
return err
}
if m == nil {
return fmt.Errorf("Membership not found")
}
*membership = *m
return nil
}
}
func testAccCheckTFEOrganizationMembershipAttributes(
membership *tfe.OrganizationMembership, expectedOrgName string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if membership.Organization.Name != expectedOrgName {
return fmt.Errorf("Bad organization: %s", membership.Organization.Name)
}
if membership.User.Email != "example@hashicorp.com" {
return fmt.Errorf("Bad email: %s", membership.User.Email)
}
if membership.User.ID == "" {
return fmt.Errorf("Bad user ID: %s", membership.User.ID)
}
return nil
}
}
func testAccCheckTFEOrganizationMembershipDestroy(s *terraform.State) error {
tfeClient := testAccProvider.Meta().(*tfe.Client)
for _, rs := range s.RootModule().Resources {
if rs.Type != "tfe_organization_membership" {
continue
}
if rs.Primary.ID == "" {
return fmt.Errorf("No instance ID is set")
}
_, err := tfeClient.OrganizationMemberships.Read(ctx, rs.Primary.ID)
if err == nil {
return fmt.Errorf("Membership %s still exists", rs.Primary.ID)
}
}
return nil
}
func testAccTFEOrganizationMembership_basic(rInt int) string {
return fmt.Sprintf(`
resource "tfe_organization" "foobar" {
name = "tst-terraform-%d"
email = "admin@company.com"
}
resource "tfe_organization_membership" "foobar" {
email = "example@hashicorp.com"
organization = tfe_organization.foobar.id
}`, rInt)
}