forked from hashicorp/terraform-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
distributed_virtual_switch_helper.go
146 lines (129 loc) · 4.66 KB
/
distributed_virtual_switch_helper.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
package vsphere
import (
"context"
"fmt"
"github.com/terraform-providers/terraform-provider-vsphere/vsphere/internal/helper/network"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/find"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/methods"
"github.com/vmware/govmomi/vim25/mo"
"github.com/vmware/govmomi/vim25/types"
)
var dvsVersions = []string{
"5.0.0",
"5.1.0",
"5.5.0",
"6.0.0",
"6.5.0",
}
// dvsFromUUID gets a DVS object from its UUID.
func dvsFromUUID(client *govmomi.Client, uuid string) (*object.VmwareDistributedVirtualSwitch, error) {
dvsm := types.ManagedObjectReference{Type: "DistributedVirtualSwitchManager", Value: "DVSManager"}
req := &types.QueryDvsByUuid{
This: dvsm,
Uuid: uuid,
}
resp, err := methods.QueryDvsByUuid(context.TODO(), client, req)
if err != nil {
return nil, err
}
return dvsFromMOID(client, resp.Returnval.Reference().Value)
}
// dvsFromMOID locates a DVS by its managed object reference ID.
func dvsFromMOID(client *govmomi.Client, id string) (*object.VmwareDistributedVirtualSwitch, error) {
finder := find.NewFinder(client.Client, false)
ref := types.ManagedObjectReference{
Type: "VmwareDistributedVirtualSwitch",
Value: id,
}
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer cancel()
ds, err := finder.ObjectReference(ctx, ref)
if err != nil {
return nil, err
}
// Should be safe to return here. If our reference returned here and is not a
// VmwareDistributedVirtualSwitch, then we have bigger problems and to be
// honest we should be panicking anyway.
return ds.(*object.VmwareDistributedVirtualSwitch), nil
}
// dvsFromPath gets a DVS object from its path.
func dvsFromPath(client *govmomi.Client, name string, dc *object.Datacenter) (*object.VmwareDistributedVirtualSwitch, error) {
net, err := network.FromPath(client, name, dc)
if err != nil {
return nil, err
}
if net.Reference().Type != "VmwareDistributedVirtualSwitch" {
return nil, fmt.Errorf("network at path %q is not a VMware distributed virtual switch (type %s)", name, net.Reference().Type)
}
return dvsFromMOID(client, net.Reference().Value)
}
// dvsProperties is a convenience method that wraps fetching the DVS MO from
// its higher-level object.
func dvsProperties(dvs *object.VmwareDistributedVirtualSwitch) (*mo.VmwareDistributedVirtualSwitch, error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer cancel()
var props mo.VmwareDistributedVirtualSwitch
if err := dvs.Properties(ctx, dvs.Reference(), nil, &props); err != nil {
return nil, err
}
return &props, nil
}
// upgradeDVS upgrades a DVS to a specific version. Downgrades are not
// supported and will result in an error. This should be checked before running
// this function.
func upgradeDVS(client *govmomi.Client, dvs *object.VmwareDistributedVirtualSwitch, version string) error {
req := &types.PerformDvsProductSpecOperation_Task{
This: dvs.Reference(),
Operation: "upgrade",
ProductSpec: &types.DistributedVirtualSwitchProductSpec{
Version: version,
},
}
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer cancel()
resp, err := methods.PerformDvsProductSpecOperation_Task(ctx, client, req)
if err != nil {
return err
}
task := object.NewTask(client.Client, resp.Returnval)
tctx, tcancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer tcancel()
if err := task.Wait(tctx); err != nil {
return err
}
return nil
}
// updateDVSConfiguration contains the atomic update/wait operation for a DVS.
func updateDVSConfiguration(client *govmomi.Client, dvs *object.VmwareDistributedVirtualSwitch, spec *types.VMwareDVSConfigSpec) error {
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer cancel()
task, err := dvs.Reconfigure(ctx, spec)
if err != nil {
return err
}
tctx, tcancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer tcancel()
if err := task.Wait(tctx); err != nil {
return err
}
return nil
}
// enableDVSNetworkResourceManagement exposes the
// EnableNetworkResourceManagement method of the DistributedVirtualSwitch MO.
// This local implementation may go away if this is exposed in the higher-level
// object upstream.
func enableDVSNetworkResourceManagement(client *govmomi.Client, dvs *object.VmwareDistributedVirtualSwitch, enabled bool) error {
req := &types.EnableNetworkResourceManagement{
This: dvs.Reference(),
Enable: enabled,
}
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer cancel()
_, err := methods.EnableNetworkResourceManagement(ctx, client, req)
if err != nil {
return err
}
return nil
}