forked from hashicorp/terraform-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_vsphere_folder_migrate.go
77 lines (67 loc) · 2.43 KB
/
resource_vsphere_folder_migrate.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
package vsphere
import (
"log"
"github.com/hashicorp/terraform/terraform"
)
// resourceVSphereFolderMigrateState is the master state migration function for
// the vsphere_folder resource.
func resourceVSphereFolderMigrateState(version int, os *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
// Guard against a nil state.
if os == nil {
return nil, nil
}
// Guard against empty state, can't do anything with it
if os.Empty() {
return os, nil
}
var migrateFunc func(*terraform.InstanceState, interface{}) error
switch version {
case 0:
log.Printf("[DEBUG] Migrating vsphere_folder state: old v%d state: %#v", version, os)
migrateFunc = resourceVSphereFolderMigrateStateV1
default:
// Migration is complete
log.Printf("[DEBUG] Migrating vsphere_folder state: completed v%d state: %#v", version, os)
return os, nil
}
if err := migrateFunc(os, meta); err != nil {
return nil, err
}
version++
log.Printf("[DEBUG] Migrating vsphere_folder state: new v%d state: %#v", version, os)
return resourceVSphereFolderMigrateState(version, os, meta)
}
// resourceVSphereFolderMigrateStateV1 migrates the state of the vsphere_folder
// from version 0 to version 1.
func resourceVSphereFolderMigrateStateV1(s *terraform.InstanceState, meta interface{}) error {
// Our path for migration here is pretty much the same as our import path, so
// we just leverage that functionality.
//
// We just need the path and the datacenter to proceed. We don't have an
// analog in for existing_path in the new resource, so we just drop that on
// the floor.
dcp := normalizeFolderPath(s.Attributes["datacenter"])
p := normalizeFolderPath(s.Attributes["path"])
// Discover our datacenter first. This field can be empty, so we have to
// search for it as we normally would.
client := meta.(*VSphereClient).vimClient
dc, err := getDatacenter(client, dcp)
if err != nil {
return err
}
// The old resource only supported VM folders, so this part is easy enough,
// we can derive our full path by combining the VM path particle and our
// relative path.
fp := rootPathParticleVM.PathFromDatacenter(dc, p)
folder, err := folderFromAbsolutePath(client, fp)
if err != nil {
return err
}
// We got our folder!
//
// Read will handle everything except for the ID, so just wipe attributes,
// update the ID, and let read take care of the rest.
s.Attributes = make(map[string]string)
s.ID = folder.Reference().Value
return nil
}