forked from GSLabDev/terraform-provider-ad
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_active_directory_user_test.go
executable file
·146 lines (133 loc) · 3.33 KB
/
resource_active_directory_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
package ad
import (
"fmt"
"os"
"strings"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"gopkg.in/ldap.v3"
)
//test function:
func TestAccAdUser_basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccResourceAdUserPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckAdUserDestroy("ad_user.test"),
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccResourceAdUserConfig(),
Check: resource.ComposeTestCheckFunc(
testAccCheckAdUserExists("ad_user.test"),
resource.TestCheckResourceAttr(
"ad_user.test", "logon_name", "test"),
),
},
},
})
}
func testAccResourceAdUserPreCheck(t *testing.T) {
if v := os.Getenv("AD_USER_DOMAIN"); v == "" {
t.Fatal("User Domain must be set for acceptance tests")
}
}
func testAccCheckAdUserDestroy(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No AD User ID is set")
}
client := testAccProvider.Meta().(*ldap.Conn)
domain := rs.Primary.Attributes["domain"]
var dnOfUser string
domainArr := strings.Split(domain, ".")
dnOfUser = "dc=" + domainArr[0]
for index, item := range domainArr {
if index == 0 {
continue
}
dnOfUser += ",dc=" + item
}
searchRequest := ldap.NewSearchRequest(
dnOfUser,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
"(&(objectClass=User)(cn="+rs.Primary.Attributes["name"]+"))", // The filter to apply
[]string{"dn", "cn"}, // A list attributes to retrieve
nil,
)
sr, err := client.Search(searchRequest)
if err != nil {
return err
}
if len(sr.Entries) == 0 {
return nil
}
return fmt.Errorf("User AD still exists")
}
}
func testAccCheckAdUserExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No AD User ID is set")
}
client := testAccProvider.Meta().(*ldap.Conn)
domain := rs.Primary.Attributes["domain"]
var dnOfUser string
domainArr := strings.Split(domain, ".")
dnOfUser = "dc=" + domainArr[0]
for index, item := range domainArr {
if index == 0 {
continue
}
dnOfUser += ",dc=" + item
}
searchRequest := ldap.NewSearchRequest(
dnOfUser,
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
"(&(objectClass=User)(cn="+rs.Primary.Attributes["name"]+"))", // The filter to apply
[]string{"dn", "cn"}, // A list attributes to retrieve
nil,
)
sr, err := client.Search(searchRequest)
if err != nil {
return err
}
if len(sr.Entries) > 0 {
return nil
}
return nil
}
}
func testAccResourceAdUserConfig() string {
return fmt.Sprintf(`
provider "ad" {
domain = "%s"
ip = "%s"
url = "%s"
user = "%s"
password = "%s"
}
resource "ad_user" "test" {
domain = "%s"
first_name = "first"
last_name = "last"
logon_name = "test"
password = "testpassword"
}`,
os.Getenv("AD_DOMAIN"),
os.Getenv("AD_IP"),
os.Getenv("AD_URL"),
os.Getenv("AD_USER"),
os.Getenv("AD_PASSWORD"),
os.Getenv("AD_USER_DOMAIN"))
}