forked from hashicorp/terraform-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_vsphere_distributed_virtual_switch.go
263 lines (235 loc) · 8.1 KB
/
resource_vsphere_distributed_virtual_switch.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
package vsphere
import (
"context"
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/types"
)
func resourceVSphereDistributedVirtualSwitch() *schema.Resource {
s := map[string]*schema.Schema{
"datacenter_id": {
Type: schema.TypeString,
Description: "The ID of the datacenter to create this virtual switch in.",
Required: true,
ForceNew: true,
},
"folder": {
Type: schema.TypeString,
Description: "The folder to create this virtual switch in, relative to the datacenter.",
Optional: true,
ForceNew: true,
},
"network_resource_control_enabled": {
Type: schema.TypeBool,
Description: "Whether or not to enable network resource control, enabling advanced traffic shaping and resource control features.",
Optional: true,
},
// Tagging
vSphereTagAttributeKey: tagsSchema(),
}
mergeSchema(s, schemaDVSCreateSpec())
return &schema.Resource{
Create: resourceVSphereDistributedVirtualSwitchCreate,
Read: resourceVSphereDistributedVirtualSwitchRead,
Update: resourceVSphereDistributedVirtualSwitchUpdate,
Delete: resourceVSphereDistributedVirtualSwitchDelete,
Importer: &schema.ResourceImporter{
State: resourceVSphereDistributedVirtualSwitchImport,
},
Schema: s,
}
}
func resourceVSphereDistributedVirtualSwitchCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*VSphereClient).vimClient
if err := validateVirtualCenter(client); err != nil {
return err
}
tagsClient, err := tagsClientIfDefined(d, meta)
if err != nil {
return err
}
dc, err := datacenterFromID(client, d.Get("datacenter_id").(string))
if err != nil {
return fmt.Errorf("cannot locate datacenter: %s", err)
}
folder, err := folderFromPath(client, d.Get("folder").(string), vSphereFolderTypeNetwork, dc)
if err != nil {
return fmt.Errorf("cannot locate folder: %s", err)
}
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer cancel()
spec := expandDVSCreateSpec(d)
task, err := folder.CreateDVS(ctx, spec)
if err != nil {
return fmt.Errorf("error creating DVS: %s", err)
}
tctx, tcancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer tcancel()
info, err := task.WaitForResult(tctx, nil)
if err != nil {
return fmt.Errorf("error waiting for DVS creation to complete: %s", err)
}
dvs, err := dvsFromMOID(client, info.Result.(types.ManagedObjectReference).Value)
if err != nil {
return fmt.Errorf("error fetching DVS after creation: %s", err)
}
props, err := dvsProperties(dvs)
if err != nil {
return fmt.Errorf("error fetching DVS properties after creation: %s", err)
}
d.SetId(props.Uuid)
// Enable network resource I/O control if it needs to be enabled
if d.Get("network_resource_control_enabled").(bool) {
enableDVSNetworkResourceManagement(client, dvs, true)
}
// Apply any pending tags now
if tagsClient != nil {
if err := processTagDiff(tagsClient, d, object.NewReference(client.Client, dvs.Reference())); err != nil {
return fmt.Errorf("error updating tags: %s", err)
}
}
return resourceVSphereDistributedVirtualSwitchRead(d, meta)
}
func resourceVSphereDistributedVirtualSwitchRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*VSphereClient).vimClient
if err := validateVirtualCenter(client); err != nil {
return err
}
id := d.Id()
dvs, err := dvsFromUUID(client, id)
if err != nil {
return fmt.Errorf("could not find DVS %q: %s", id, err)
}
props, err := dvsProperties(dvs)
if err != nil {
return fmt.Errorf("error fetching DVS properties: %s", err)
}
// Set the datacenter ID, for completion's sake when importing
dcp, err := rootPathParticleNetwork.SplitDatacenter(dvs.InventoryPath)
if err != nil {
return fmt.Errorf("error parsing datacenter from inventory path: %s", err)
}
dc, err := getDatacenter(client, dcp)
if err != nil {
return fmt.Errorf("error locating datacenter: %s", err)
}
d.Set("datacenter_id", dc.Reference().Value)
// Set the folder
folder, err := rootPathParticleNetwork.SplitRelativeFolder(dvs.InventoryPath)
if err != nil {
return fmt.Errorf("error parsing DVS path %q: %s", dvs.InventoryPath, err)
}
d.Set("folder", normalizeFolderPath(folder))
// Read in config info
if err := flattenVMwareDVSConfigInfo(d, props.Config.(*types.VMwareDVSConfigInfo)); err != nil {
return err
}
// Read tags if we have the ability to do so
if tagsClient, _ := meta.(*VSphereClient).TagsClient(); tagsClient != nil {
if err := readTagsForResource(tagsClient, dvs, d); err != nil {
return fmt.Errorf("error reading tags: %s", err)
}
}
return nil
}
func resourceVSphereDistributedVirtualSwitchUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*VSphereClient).vimClient
if err := validateVirtualCenter(client); err != nil {
return err
}
tagsClient, err := tagsClientIfDefined(d, meta)
if err != nil {
return err
}
id := d.Id()
dvs, err := dvsFromUUID(client, id)
if err != nil {
return fmt.Errorf("could not find DVS %q: %s", id, err)
}
// If we have a pending version upgrade, do that first.
if d.HasChange("version") {
old, new := d.GetChange("version")
var ovi, nvi int
for n, v := range dvsVersions {
if old.(string) == v {
ovi = n
}
if new.(string) == v {
nvi = n
}
}
if nvi < ovi {
return fmt.Errorf("downgrading dvSwitches are not allowed (old: %s new: %s)", old, new)
}
if err := upgradeDVS(client, dvs, new.(string)); err != nil {
return fmt.Errorf("could not upgrade DVS: %s", err)
}
props, err := dvsProperties(dvs)
if err != nil {
return fmt.Errorf("could not get DVS properties after upgrade: %s", err)
}
// ConfigVersion increments after a DVS upgrade, which means this needs to
// be updated before the post-update read to ensure that we don't run into
// ConcurrentAccess errors on the update operation below.
d.Set("config_version", props.Config.(*types.VMwareDVSConfigInfo).ConfigVersion)
}
spec := expandVMwareDVSConfigSpec(d)
if err := updateDVSConfiguration(client, dvs, spec); err != nil {
return fmt.Errorf("could not update DVS: %s", err)
}
// Modify network I/O control if necessary
if d.HasChange("network_resource_control_enabled") {
enableDVSNetworkResourceManagement(client, dvs, d.Get("network_resource_control_enabled").(bool))
}
// Apply any pending tags now
if tagsClient != nil {
if err := processTagDiff(tagsClient, d, object.NewReference(client.Client, dvs.Reference())); err != nil {
return fmt.Errorf("error updating tags: %s", err)
}
}
return resourceVSphereDistributedVirtualSwitchRead(d, meta)
}
func resourceVSphereDistributedVirtualSwitchDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*VSphereClient).vimClient
if err := validateVirtualCenter(client); err != nil {
return err
}
id := d.Id()
dvs, err := dvsFromUUID(client, id)
if err != nil {
return fmt.Errorf("could not find DVS %q: %s", id, err)
}
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer cancel()
task, err := dvs.Destroy(ctx)
if err != nil {
return fmt.Errorf("error deleting DVS: %s", err)
}
tctx, tcancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer tcancel()
if err := task.Wait(tctx); err != nil {
return fmt.Errorf("error waiting for DVS deletion to complete: %s", err)
}
return nil
}
func resourceVSphereDistributedVirtualSwitchImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
// Due to the relative difficulty in trying to fetch a DVS's UUID, we use the
// inventory path to the DVS instead, and just run it through finder. A full
// path is required unless the default datacenter can be utilized.
client := meta.(*VSphereClient).vimClient
if err := validateVirtualCenter(client); err != nil {
return nil, err
}
p := d.Id()
dvs, err := dvsFromPath(client, p, nil)
if err != nil {
return nil, fmt.Errorf("error locating DVS: %s", err)
}
props, err := dvsProperties(dvs)
if err != nil {
return nil, fmt.Errorf("error fetching DVS properties: %s", err)
}
d.SetId(props.Uuid)
return []*schema.ResourceData{d}, nil
}