forked from hashicorp/terraform-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_vsphere_datacenter_test.go
125 lines (108 loc) · 3.13 KB
/
resource_vsphere_datacenter_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
package vsphere
import (
"fmt"
"os"
"testing"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/vmware/govmomi/find"
"golang.org/x/net/context"
)
const resourceName = "vsphere_datacenter.testDC"
const testAccCheckVSphereDatacenterConfig = `
resource "vsphere_datacenter" "testDC" {
name = "testDC"
}
`
const testAccCheckVSphereDatacenterConfigSubfolder = `
resource "vsphere_datacenter" "testDC" {
name = "testDC"
folder = "%s"
}
`
// Create a datacenter on the root folder
func TestAccVSphereDatacenter_createOnRootFolder(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVSphereDatacenterDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckVSphereDatacenterConfig,
Check: resource.ComposeTestCheckFunc(testAccCheckVSphereDatacenterExists(resourceName, true)),
},
},
})
}
// Create a datacenter on a subfolder
func TestAccVSphereDatacenter_createOnSubfolder(t *testing.T) {
dcFolder := os.Getenv("VSPHERE_DC_FOLDER")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckVSphereDatacenterDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testAccCheckVSphereDatacenterConfigSubfolder, dcFolder),
Check: resource.ComposeTestCheckFunc(testAccCheckVSphereDatacenterExists(resourceName, true)),
},
},
})
}
func testAccCheckVSphereDatacenterDestroy(s *terraform.State) error {
client := testAccProvider.Meta().(*VSphereClient).vimClient
finder := find.NewFinder(client.Client, true)
for _, rs := range s.RootModule().Resources {
if rs.Type != "vsphere_datacenter" {
continue
}
path := rs.Primary.Attributes["name"]
if _, ok := rs.Primary.Attributes["folder"]; ok {
path = rs.Primary.Attributes["folder"] + "/" + path
}
_, err := finder.Datacenter(context.TODO(), path)
if err != nil {
switch err.(type) {
case *find.NotFoundError:
fmt.Printf("Expected error received: %s\n", err)
return nil
default:
return err
}
} else {
return fmt.Errorf("datacenter '%s' still exists", path)
}
}
return nil
}
func testAccCheckVSphereDatacenterExists(n string, exists bool) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("resource not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("no ID is set")
}
client := testAccProvider.Meta().(*VSphereClient).vimClient
finder := find.NewFinder(client.Client, true)
path := rs.Primary.Attributes["name"]
if _, ok := rs.Primary.Attributes["folder"]; ok {
path = rs.Primary.Attributes["folder"] + "/" + path
}
_, err := finder.Datacenter(context.TODO(), path)
if err != nil {
switch e := err.(type) {
case *find.NotFoundError:
if exists {
return fmt.Errorf("datacenter does not exist: %s", e.Error())
}
fmt.Printf("Expected error received: %s\n", e.Error())
return nil
default:
return err
}
}
return nil
}
}