forked from DrFaust92/terraform-provider-airflow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
resource_user_test.go
101 lines (89 loc) · 3.09 KB
/
resource_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
package main
import (
"fmt"
"testing"
"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 TestAccAirflowUser_basic(t *testing.T) {
rName := acctest.RandomWithPrefix("tf-acc-test")
rNameUpdated := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "airflow_user.test"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAirflowUserCheckDestroy,
Steps: []resource.TestStep{
{
Config: testAccAirflowUserConfigBasic(rName, rName),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "email", rName),
resource.TestCheckResourceAttr(resourceName, "first_name", rName),
resource.TestCheckResourceAttr(resourceName, "last_name", rName),
resource.TestCheckResourceAttr(resourceName, "username", rName),
resource.TestCheckResourceAttr(resourceName, "password", rName),
resource.TestCheckResourceAttr(resourceName, "active", "true"),
resource.TestCheckResourceAttr(resourceName, "roles.#", "1"),
resource.TestCheckTypeSetElemAttrPair(resourceName, "roles.*", "airflow_role.test", "name"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"password"},
},
{
Config: testAccAirflowUserConfigBasic(rName, rNameUpdated),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "email", rName),
resource.TestCheckResourceAttr(resourceName, "first_name", rNameUpdated),
resource.TestCheckResourceAttr(resourceName, "last_name", rName),
resource.TestCheckResourceAttr(resourceName, "username", rName),
resource.TestCheckResourceAttr(resourceName, "password", rName),
resource.TestCheckResourceAttr(resourceName, "active", "true"),
resource.TestCheckResourceAttr(resourceName, "roles.#", "1"),
resource.TestCheckTypeSetElemAttrPair(resourceName, "roles.*", "airflow_role.test", "name"),
),
},
},
})
}
func testAccCheckAirflowUserCheckDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(ProviderConfig)
for _, rs := range s.RootModule().Resources {
if rs.Type != "airflow_user" {
continue
}
user, res, err := client.ApiClient.UserApi.GetUser(client.AuthContext, rs.Primary.ID).Execute()
if err == nil {
if *user.Username == rs.Primary.ID {
return fmt.Errorf("Airflow User (%s) still exists.", rs.Primary.ID)
}
}
if res != nil && res.StatusCode == 404 {
continue
}
}
return nil
}
func testAccAirflowUserConfigBasic(rName, fName string) string {
return fmt.Sprintf(`
resource "airflow_role" "test" {
name = %[1]q
action {
action = "can_read"
resource = "Audit Logs"
}
}
resource "airflow_user" "test" {
email = %[1]q
first_name = %[2]q
last_name = %[1]q
username = %[1]q
password = %[1]q
roles = [airflow_role.test.name]
}
`, rName, fName)
}