-
Notifications
You must be signed in to change notification settings - Fork 112
/
resource_vcd_vapp.go
377 lines (328 loc) · 10.9 KB
/
resource_vcd_vapp.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
package vcd
import (
"fmt"
"log"
"regexp"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/vmware/go-vcloud-director/v2/govcd"
)
const vAppUnknownStatus = "-unknown-status-"
func resourceVcdVApp() *schema.Resource {
return &schema.Resource{
Create: resourceVcdVAppCreate,
Update: resourceVcdVAppUpdate,
Read: resourceVcdVAppRead,
Delete: resourceVcdVAppDelete,
Importer: &schema.ResourceImporter{
State: resourceVcdVappImport,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "A name for the vApp, unique withing the VDC",
},
"org": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The name of organization to use, optional if defined at provider " +
"level. Useful when connected as sysadmin working across different organizations",
},
"vdc": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The name of VDC to use, optional if defined at provider level",
},
"description": {
Type: schema.TypeString,
Optional: true,
Description: "Optional description of the vApp",
},
"metadata": {
Type: schema.TypeMap,
Optional: true,
// For now underlying go-vcloud-director repo only supports
// a value of type String in this map.
Description: "Key value map of metadata to assign to this vApp. Key and value can be any string.",
},
"href": {
Type: schema.TypeString,
Computed: true,
Description: "vApp Hyper Reference",
},
"power_on": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "A boolean value stating if this vApp should be powered on",
},
"guest_properties": {
Type: schema.TypeMap,
Optional: true,
Description: "Key/value settings for guest properties. Will be picked up by new VMs when created.",
},
"status": {
Type: schema.TypeInt,
Computed: true,
Description: "Shows the status code of the vApp",
},
"status_text": {
Type: schema.TypeString,
Computed: true,
Description: "Shows the status of the vApp",
},
},
}
}
func resourceVcdVAppCreate(d *schema.ResourceData, meta interface{}) error {
vcdClient := meta.(*VCDClient)
_, vdc, err := vcdClient.GetOrgAndVdcFromResource(d)
if err != nil {
return fmt.Errorf("error retrieving Org and VDC: %s", err)
}
vappName := d.Get("name").(string)
vcdClient.lockVapp(d)
defer vcdClient.unLockVapp(d)
e := vdc.ComposeRawVApp(d.Get("name").(string))
if e != nil {
return fmt.Errorf("error: %#v", e)
}
e = vdc.Refresh()
if e != nil {
return fmt.Errorf("error: %#v", e)
}
vapp, err := vdc.GetVAppByName(vappName, true)
if err != nil {
return fmt.Errorf("unable to find vApp by name %s: %s", vappName, err)
}
if _, ok := d.GetOk("guest_properties"); ok {
// Even though vApp has a task and waits for its completion it happens that it is not ready
// for operation just after provisioning therefore we wait for it to exit UNRESOLVED state
err = vapp.BlockWhileStatus("UNRESOLVED", vcdClient.MaxRetryTimeout)
if err != nil {
return fmt.Errorf("timed out waiting for vApp to exit UNRESOLVED state: %s", err)
}
guestProperties, err := getGuestProperties(d)
if err != nil {
return fmt.Errorf("unable to convert guest properties to data structure")
}
log.Printf("[TRACE] Setting vApp guest properties")
_, err = vapp.SetProductSectionList(guestProperties)
if err != nil {
return fmt.Errorf("error setting guest properties: %s", err)
}
}
d.SetId(vapp.VApp.ID)
return resourceVcdVAppUpdate(d, meta)
}
func resourceVcdVAppUpdate(d *schema.ResourceData, meta interface{}) error {
vcdClient := meta.(*VCDClient)
_, vdc, err := vcdClient.GetOrgAndVdcFromResource(d)
if err != nil {
return fmt.Errorf(errorRetrievingOrgAndVdc, err)
}
vapp, err := vdc.GetVAppByNameOrId(d.Id(), false)
if err != nil {
return fmt.Errorf("error finding VApp: %#v", err)
}
if d.HasChange("guest_properties") {
vappProperties, err := getGuestProperties(d)
if err != nil {
return fmt.Errorf("unable to convert guest properties to data structure")
}
log.Printf("[TRACE] Updating vApp guest properties")
_, err = vapp.SetProductSectionList(vappProperties)
if err != nil {
return fmt.Errorf("error setting guest properties: %s", err)
}
}
if d.HasChange("metadata") {
oldRaw, newRaw := d.GetChange("metadata")
oldMetadata := oldRaw.(map[string]interface{})
newMetadata := newRaw.(map[string]interface{})
var toBeRemovedMetadata []string
// Check if any key in old metadata was removed in new metadata.
// Creates a list of keys to be removed.
for k := range oldMetadata {
if _, ok := newMetadata[k]; !ok {
toBeRemovedMetadata = append(toBeRemovedMetadata, k)
}
}
for _, k := range toBeRemovedMetadata {
task, err := vapp.DeleteMetadata(k)
if err != nil {
return fmt.Errorf("error deleting metadata: %#v", err)
}
err = task.WaitTaskCompletion()
if err != nil {
return fmt.Errorf(errorCompletingTask, err)
}
}
for k, v := range newMetadata {
task, err := vapp.AddMetadata(k, v.(string))
if err != nil {
return fmt.Errorf("error adding metadata: %#v", err)
}
err = task.WaitTaskCompletion()
if err != nil {
return fmt.Errorf(errorCompletingTask, err)
}
}
}
if d.HasChange("power_on") && d.Get("power_on").(bool) {
task, err := vapp.PowerOn()
if err != nil {
return fmt.Errorf("error Powering Up: %#v", err)
}
err = task.WaitTaskCompletion()
if err != nil {
return fmt.Errorf("error completing tasks: %#v", err)
}
}
return resourceVcdVAppRead(d, meta)
}
func resourceVcdVAppRead(d *schema.ResourceData, meta interface{}) error {
return genericVcdVAppRead(d, meta, "resource")
}
func genericVcdVAppRead(d *schema.ResourceData, meta interface{}, origin string) error {
vcdClient := meta.(*VCDClient)
_, vdc, err := vcdClient.GetOrgAndVdcFromResource(d)
if err != nil {
return fmt.Errorf(errorRetrievingOrgAndVdc, err)
}
identifier := d.Id()
if identifier == "" {
identifier = d.Get("name").(string)
}
if identifier == "" {
return fmt.Errorf("[vapp read] no identifier provided")
}
vapp, err := vdc.GetVAppByNameOrId(identifier, false)
if err != nil {
if origin == "resource" {
log.Printf("[DEBUG] Unable to find vApp. Removing from tfstate")
d.SetId("")
return nil
}
return fmt.Errorf("[vapp read] error retrieving vApp %s: %s", identifier, err)
}
// update guest properties
guestProperties, err := vapp.GetProductSectionList()
if err != nil {
return fmt.Errorf("unable to read guest properties: %s", err)
}
err = setGuestProperties(d, guestProperties)
if err != nil {
return fmt.Errorf("unable to set guest properties in state: %s", err)
}
statusText, err := vapp.GetStatus()
if err != nil {
statusText = vAppUnknownStatus
}
_ = d.Set("status", vapp.VApp.Status)
_ = d.Set("status_text", statusText)
_ = d.Set("href", vapp.VApp.HREF)
_ = d.Set("description", vapp.VApp.Description)
metadata, err := vapp.GetMetadata()
if err != nil {
return fmt.Errorf("[vapp read] error retrieving metadata: %s", err)
}
metadataStruct := getMetadataStruct(metadata.MetadataEntry)
err = d.Set("metadata", metadataStruct)
if err != nil {
return fmt.Errorf("[vapp read] error setting metadata: %s", err)
}
d.SetId(vapp.VApp.ID)
return nil
}
func resourceVcdVAppDelete(d *schema.ResourceData, meta interface{}) error {
vcdClient := meta.(*VCDClient)
vcdClient.lockVapp(d)
defer vcdClient.unLockVapp(d)
_, vdc, err := vcdClient.GetOrgAndVdcFromResource(d)
if err != nil {
return fmt.Errorf(errorRetrievingOrgAndVdc, err)
}
vapp, err := vdc.GetVAppByNameOrId(d.Id(), false)
if err != nil {
return fmt.Errorf("error finding vapp: %s", err)
}
// to avoid network destroy issues - detach networks from vApp
task, err := vapp.RemoveAllNetworks()
if err != nil {
return fmt.Errorf("error with networking change: %#v", err)
}
err = task.WaitTaskCompletion()
if err != nil {
return fmt.Errorf("error changing network: %#v", err)
}
err = tryUndeploy(*vapp)
if err != nil {
return err
}
task, err = vapp.Delete()
if err != nil {
return fmt.Errorf("error deleting: %#v", err)
}
err = task.WaitTaskCompletion()
if err != nil {
return fmt.Errorf("error with deleting vApp task: %#v", err)
}
return nil
}
// Try to undeploy a vApp, but do not throw an error if the vApp is powered off.
// Very often the vApp is powered off at this point and Undeploy() would fail with error:
// "The requested operation could not be executed since vApp vApp_name is not running"
// So, if the error matches we just ignore it and the caller may fast forward to vapp.Delete()
func tryUndeploy(vapp govcd.VApp) error {
task, err := vapp.Undeploy()
var reErr = regexp.MustCompile(`.*The requested operation could not be executed since vApp.*is not running.*`)
if err != nil && reErr.MatchString(err.Error()) {
// ignore - can't be undeployed
return nil
} else if err != nil {
return fmt.Errorf("error undeploying vApp: %#v", err)
}
err = task.WaitTaskCompletion()
if err != nil {
return fmt.Errorf("error undeploying vApp: %#v", err)
}
return nil
}
// resourceVcdVappImport is responsible for importing the resource.
// The following steps happen as part of import
// 1. The user supplies `terraform import _resource_name_ _the_id_string_` command
// 2. `_the_id_string_` contains a dot formatted path to resource as in the example below
// 3. The functions splits the dot-formatted path and tries to lookup the object
// 4. If the lookup succeeds it sets the ID field for `_resource_name_` resource in statefile
// (the resource must be already defined in .tf config otherwise `terraform import` will complain)
// 5. `terraform refresh` is being implicitly launched. The Read method looks up all other fields
// based on the known ID of object.
//
// Example resource name (_resource_name_): vcd_vapp.vapp_name
// Example import path (_the_id_string_): org-name.vdc-name.vapp-name
func resourceVcdVappImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
resourceURI := strings.Split(d.Id(), ImportSeparator)
if len(resourceURI) != 3 {
return nil, fmt.Errorf("[vapp import] resource name must be specified as org-name.vdc-name.vapp-name")
}
orgName, vdcName, vappName := resourceURI[0], resourceURI[1], resourceURI[2]
vcdClient := meta.(*VCDClient)
_, vdc, err := vcdClient.GetOrgAndVdc(orgName, vdcName)
if err != nil {
return nil, fmt.Errorf("[vapp import] unable to find VDC %s: %s ", vdcName, err)
}
vapp, err := vdc.GetVAppByName(vappName, false)
if err != nil {
return nil, fmt.Errorf("[vapp import] error retrieving vapp %s: %s", vappName, err)
}
_ = d.Set("name", vappName)
_ = d.Set("org", orgName)
_ = d.Set("vdc", vdcName)
d.SetId(vapp.VApp.ID)
return []*schema.ResourceData{d}, nil
}