-
Notifications
You must be signed in to change notification settings - Fork 31
/
resource_active_directory_computer_test.go
143 lines (130 loc) · 3.56 KB
/
resource_active_directory_computer_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
package ad
import (
"fmt"
"os"
"strings"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"gopkg.in/ldap.v3"
)
func TestAccAdComputer_Basic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccResourceAdComputerPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testAccCheckAdComputerDestroy("ad_computer.test"),
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccResourceAdComputerConfig(),
Check: resource.ComposeTestCheckFunc(
testAccCheckAdComputerExists("ad_computer.test"),
resource.TestCheckResourceAttr(
"ad_computer.test", "computer_name", "terraform"),
),
},
},
})
}
func testAccResourceAdComputerPreCheck(t *testing.T) {
if v := os.Getenv("AD_COMPUTER_DOMAIN"); v == "" {
t.Fatal("AD_COMPUTER_DOMAIN must be set for acceptance tests")
}
}
func testAccCheckAdComputerDestroy(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 Computer ID is set")
}
client := testAccProvider.Meta().(*ldap.Conn)
domain := rs.Primary.Attributes["domain"]
var dnOfComputer string
domainArr := strings.Split(domain, ".")
dnOfComputer = "dc=" + domainArr[0]
for index, item := range domainArr {
if index == 0 {
continue
}
dnOfComputer += ",dc=" + item
}
searchRequest := ldap.NewSearchRequest(
dnOfComputer, //"cn=code1,cn=Computers,dc=terraform,dc=local", // The base dn to search
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
"(&(objectClass=Computer)(cn="+rs.Primary.Attributes["computer_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("Computer AD still exists")
}
}
func testAccCheckAdComputerExists(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 Computer ID is set")
}
client := testAccProvider.Meta().(*ldap.Conn)
domain := rs.Primary.Attributes["domain"]
var dnOfComputer string
domainArr := strings.Split(domain, ".")
dnOfComputer = "dc=" + domainArr[0]
for index, item := range domainArr {
if index == 0 {
continue
}
dnOfComputer += ",dc=" + item
}
searchRequest := ldap.NewSearchRequest(
dnOfComputer, //"cn=code1,cn=Computers,dc=terraform,dc=local", // The base dn to search
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
"(&(objectClass=Computer)(cn="+rs.Primary.Attributes["computer_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 testAccResourceAdComputerConfig() string {
return fmt.Sprintf(`
provider "ad" {
domain = "%s"
ip = "%s"
url = "%s"
user = "%s"
password = "%s"
}
resource "ad_computer" "test" {
domain = "%s"
computer_name = "terraform"
description = "terraform test"
}`,
os.Getenv("AD_DOMAIN"),
os.Getenv("AD_IP"),
os.Getenv("AD_URL"),
os.Getenv("AD_USER"),
os.Getenv("AD_PASSWORD"),
os.Getenv("AD_COMPUTER_DOMAIN"))
}