-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
resource_aws_quicksight_user_test.go
209 lines (177 loc) · 6.01 KB
/
resource_aws_quicksight_user_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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package aws
import (
"fmt"
"strings"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/service/quicksight"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)
func TestAccAWSQuickSightUser_basic(t *testing.T) {
var user quicksight.User
rName1 := "tfacctest" + acctest.RandString(10)
resourceName1 := "aws_quicksight_user." + rName1
rName2 := "tfacctest" + acctest.RandString(10)
resourceName2 := "aws_quicksight_user." + rName2
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, quicksight.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckQuickSightUserDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSQuickSightUserConfig(rName1),
Check: resource.ComposeTestCheckFunc(
testAccCheckQuickSightUserExists(resourceName1, &user),
resource.TestCheckResourceAttr(resourceName1, "user_name", rName1),
testAccCheckResourceAttrRegionalARN(resourceName1, "arn", "quicksight", fmt.Sprintf("user/default/%s", rName1)),
),
},
{
Config: testAccAWSQuickSightUserConfig(rName2),
Check: resource.ComposeTestCheckFunc(
testAccCheckQuickSightUserExists(resourceName2, &user),
resource.TestCheckResourceAttr(resourceName2, "user_name", rName2),
testAccCheckResourceAttrRegionalARN(resourceName2, "arn", "quicksight", fmt.Sprintf("user/default/%s", rName2)),
),
},
},
})
}
func TestAccAWSQuickSightUser_withInvalidFormattedEmailStillWorks(t *testing.T) {
var user quicksight.User
rName := "tfacctest" + acctest.RandString(10)
resourceName := "aws_quicksight_user." + rName
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, quicksight.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckQuickSightUserDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSQuickSightUserConfigWithEmail(rName, "nottarealemailbutworks"),
Check: resource.ComposeTestCheckFunc(
testAccCheckQuickSightUserExists(resourceName, &user),
resource.TestCheckResourceAttr(resourceName, "email", "nottarealemailbutworks"),
),
},
{
Config: testAccAWSQuickSightUserConfigWithEmail(rName, "nottarealemailbutworks2"),
Check: resource.ComposeTestCheckFunc(
testAccCheckQuickSightUserExists(resourceName, &user),
resource.TestCheckResourceAttr(resourceName, "email", "nottarealemailbutworks2"),
),
},
},
})
}
func TestAccAWSQuickSightUser_disappears(t *testing.T) {
var user quicksight.User
rName := "tfacctest" + acctest.RandString(10)
resourceName := "aws_quicksight_user." + rName
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ErrorCheck: testAccErrorCheck(t, quicksight.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckQuickSightUserDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSQuickSightUserConfig(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckQuickSightUserExists(resourceName, &user),
testAccCheckQuickSightUserDisappears(&user),
),
ExpectNonEmptyPlan: true,
},
},
})
}
func testAccCheckQuickSightUserExists(resourceName string, user *quicksight.User) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("Not found: %s", resourceName)
}
awsAccountID, namespace, userName, err := resourceAwsQuickSightUserParseID(rs.Primary.ID)
if err != nil {
return err
}
conn := testAccProvider.Meta().(*AWSClient).quicksightconn
input := &quicksight.DescribeUserInput{
AwsAccountId: aws.String(awsAccountID),
Namespace: aws.String(namespace),
UserName: aws.String(userName),
}
output, err := conn.DescribeUser(input)
if err != nil {
return err
}
if output == nil || output.User == nil {
return fmt.Errorf("QuickSight User (%s) not found", rs.Primary.ID)
}
*user = *output.User
return nil
}
}
func testAccCheckQuickSightUserDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).quicksightconn
for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_quicksight_user" {
continue
}
awsAccountID, namespace, userName, err := resourceAwsQuickSightUserParseID(rs.Primary.ID)
if err != nil {
return err
}
_, err = conn.DescribeUser(&quicksight.DescribeUserInput{
AwsAccountId: aws.String(awsAccountID),
Namespace: aws.String(namespace),
UserName: aws.String(userName),
})
if isAWSErr(err, quicksight.ErrCodeResourceNotFoundException, "") {
continue
}
if err != nil {
return err
}
return fmt.Errorf("QuickSight User '%s' was not deleted properly", rs.Primary.ID)
}
return nil
}
func testAccCheckQuickSightUserDisappears(v *quicksight.User) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := testAccProvider.Meta().(*AWSClient).quicksightconn
arn, err := arn.Parse(aws.StringValue(v.Arn))
if err != nil {
return err
}
parts := strings.SplitN(arn.Resource, "/", 3)
input := &quicksight.DeleteUserInput{
AwsAccountId: aws.String(arn.AccountID),
Namespace: aws.String(parts[1]),
UserName: v.UserName,
}
if _, err := conn.DeleteUser(input); err != nil {
return err
}
return nil
}
}
func testAccAWSQuickSightUserConfigWithEmail(rName, email string) string {
return fmt.Sprintf(`
data "aws_caller_identity" "current" {}
resource "aws_quicksight_user" %[1]q {
aws_account_id = data.aws_caller_identity.current.account_id
user_name = %[1]q
email = %[2]q
identity_type = "QUICKSIGHT"
user_role = "READER"
}
`, rName, email)
}
func testAccAWSQuickSightUserConfig(rName string) string {
return testAccAWSQuickSightUserConfigWithEmail(rName, "[email protected]")
}