forked from hashicorp/terraform-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_vsphere_datacenter.go
218 lines (183 loc) · 5.59 KB
/
resource_vsphere_datacenter.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
210
211
212
213
214
215
216
217
218
package vsphere
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/types"
"golang.org/x/net/context"
)
func resourceVSphereDatacenter() *schema.Resource {
return &schema.Resource{
Create: resourceVSphereDatacenterCreate,
Read: resourceVSphereDatacenterRead,
Update: resourceVSphereDatacenterUpdate,
Delete: resourceVSphereDatacenterDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"folder": &schema.Schema{
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
// Add tags schema
vSphereTagAttributeKey: tagsSchema(),
},
}
}
func resourceVSphereDatacenterCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*VSphereClient).vimClient
// Load up the tags client, which will validate a proper vCenter before
// attempting to proceed if we have tags defined.
tagsClient, err := tagsClientIfDefined(d, meta)
if err != nil {
return err
}
name := d.Get("name").(string)
var f *object.Folder
if v, ok := d.GetOk("folder"); ok {
finder := find.NewFinder(client.Client, true)
var err error
f, err = finder.Folder(context.TODO(), v.(string))
if err != nil {
return fmt.Errorf("failed to find folder that will contain the datacenter: %s", err)
}
} else {
f = object.NewRootFolder(client.Client)
}
dc, err := f.CreateDatacenter(context.TODO(), name)
if err != nil || dc == nil {
return fmt.Errorf("failed to create datacenter: %s", err)
}
// From govmomi code: "Response will be nil if this is an ESX host that does not belong to a vCenter"
if dc == nil {
return fmt.Errorf("ESX host does not belong to a vCenter")
}
// Wait for the datacenter resource to be ready
stateConf := &resource.StateChangeConf{
Pending: []string{"InProgress"},
Target: []string{"Created"},
Refresh: resourceVSphereDatacenterStateRefreshFunc(d, meta),
Timeout: 10 * time.Minute,
MinTimeout: 3 * time.Second,
Delay: 5 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("error waiting for datacenter (%s) to become ready: %s", name, err)
}
// Apply any pending tags now
if tagsClient != nil {
if err := processTagDiff(tagsClient, d, dc); err != nil {
return err
}
}
d.SetId(name)
return resourceVSphereDatacenterRead(d, meta)
}
func resourceVSphereDatacenterStateRefreshFunc(d *schema.ResourceData, meta interface{}) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
log.Print("[TRACE] Refreshing datacenter state")
dc, err := datacenterExists(d, meta)
if err != nil {
switch err.(type) {
case *find.NotFoundError:
log.Printf("[TRACE] Refreshing state. Datacenter not found: %s", err)
return nil, "InProgress", nil
default:
return nil, "Failed", err
}
}
log.Print("[TRACE] Refreshing state. Datacenter found")
return dc, "Created", nil
}
}
func datacenterExists(d *schema.ResourceData, meta interface{}) (*object.Datacenter, error) {
client := meta.(*VSphereClient).vimClient
name := d.Get("name").(string)
path := name
if v, ok := d.GetOk("folder"); ok {
path = v.(string) + "/" + name
}
finder := find.NewFinder(client.Client, true)
dc, err := finder.Datacenter(context.TODO(), path)
return dc, err
}
func resourceVSphereDatacenterRead(d *schema.ResourceData, meta interface{}) error {
dc, err := datacenterExists(d, meta)
if err != nil {
log.Printf("couldn't find the specified datacenter: %s", err)
d.SetId("")
return nil
}
// Read tags if we have the ability to do so
if tagsClient, _ := meta.(*VSphereClient).TagsClient(); tagsClient != nil {
if err := readTagsForResource(tagsClient, dc, d); err != nil {
return err
}
}
return nil
}
func resourceVSphereDatacenterUpdate(d *schema.ResourceData, meta interface{}) error {
// Load up the tags client, which will validate a proper vCenter before
// attempting to proceed if we have tags defined.
tagsClient, err := tagsClientIfDefined(d, meta)
if err != nil {
return err
}
dc, err := datacenterExists(d, meta)
if err != nil {
return fmt.Errorf("couldn't find the specified datacenter: %s", err)
}
// Apply any pending tags now
if tagsClient != nil {
if err := processTagDiff(tagsClient, d, dc); err != nil {
return err
}
}
return nil
}
func resourceVSphereDatacenterDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*VSphereClient).vimClient
name := d.Get("name").(string)
path := name
if v, ok := d.GetOk("folder"); ok {
path = v.(string) + "/" + name
}
finder := find.NewFinder(client.Client, true)
dc, err := finder.Datacenter(context.TODO(), path)
if err != nil {
log.Printf("couldn't find the specified datacenter: %s", err)
d.SetId("")
return nil
}
req := &types.Destroy_Task{
This: dc.Common.Reference(),
}
_, err = methods.Destroy_Task(context.TODO(), client, req)
if err != nil {
return fmt.Errorf("%s", err)
}
// Wait for the datacenter resource to be destroyed
stateConf := &resource.StateChangeConf{
Pending: []string{"Created"},
Target: []string{},
Refresh: resourceVSphereDatacenterStateRefreshFunc(d, meta),
Timeout: 10 * time.Minute,
MinTimeout: 3 * time.Second,
Delay: 5 * time.Second,
}
_, err = stateConf.WaitForState()
if err != nil {
return fmt.Errorf("error waiting for datacenter (%s) to become ready: %s", name, err)
}
return nil
}