forked from hashicorp/terraform-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_vsphere_distributed_port_group.go
216 lines (195 loc) · 6.62 KB
/
resource_vsphere_distributed_port_group.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
package vsphere
import (
"context"
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/types"
)
func resourceVSphereDistributedPortGroup() *schema.Resource {
s := map[string]*schema.Schema{
"distributed_virtual_switch_uuid": {
Type: schema.TypeString,
Description: "The UUID of the DVS to attach this port group to.",
Required: true,
ForceNew: true,
},
"key": {
Type: schema.TypeString,
Description: "The generated UUID of the portgroup.",
Computed: true,
},
// Tagging
vSphereTagAttributeKey: tagsSchema(),
}
mergeSchema(s, schemaDVPortgroupConfigSpec())
return &schema.Resource{
Create: resourceVSphereDistributedPortGroupCreate,
Read: resourceVSphereDistributedPortGroupRead,
Update: resourceVSphereDistributedPortGroupUpdate,
Delete: resourceVSphereDistributedPortGroupDelete,
Importer: &schema.ResourceImporter{
State: resourceVSphereDistributedPortGroupImport,
},
Schema: s,
}
}
func resourceVSphereDistributedPortGroupCreate(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
}
dvsID := d.Get("distributed_virtual_switch_uuid").(string)
dvs, err := dvsFromUUID(client, dvsID)
if err != nil {
return fmt.Errorf("could not find DVS %q: %s", dvsID, err)
}
spec := expandDVPortgroupConfigSpec(d)
task, err := createDVPortgroup(client, dvs, spec)
if err != nil {
return fmt.Errorf("error creating portgroup: %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 portgroup creation to complete: %s", err)
}
pg, err := dvPortgroupFromMOID(client, info.Result.(types.ManagedObjectReference).Value)
if err != nil {
return fmt.Errorf("error fetching portgroup after creation: %s", err)
}
props, err := dvPortgroupProperties(pg)
if err != nil {
return fmt.Errorf("error fetching portgroup properties after creation: %s", err)
}
d.SetId(pg.Reference().Value)
d.Set("key", props.Key)
// Apply any pending tags now
if tagsClient != nil {
if err := processTagDiff(tagsClient, d, object.NewReference(client.Client, pg.Reference())); err != nil {
return fmt.Errorf("error updating tags: %s", err)
}
}
return resourceVSphereDistributedPortGroupRead(d, meta)
}
func resourceVSphereDistributedPortGroupRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*VSphereClient).vimClient
if err := validateVirtualCenter(client); err != nil {
return err
}
pgID := d.Id()
pg, err := dvPortgroupFromMOID(client, pgID)
if err != nil {
return fmt.Errorf("could not find portgroup %q: %s", pgID, err)
}
props, err := dvPortgroupProperties(pg)
if err != nil {
return fmt.Errorf("error fetching portgroup properties: %s", err)
}
d.Set("key", props.Key)
if err := flattenDVPortgroupConfigInfo(d, props.Config); err != nil {
return err
}
if tagsClient, _ := meta.(*VSphereClient).TagsClient(); tagsClient != nil {
if err := readTagsForResource(tagsClient, pg, d); err != nil {
return fmt.Errorf("error reading tags: %s", err)
}
}
return nil
}
func resourceVSphereDistributedPortGroupUpdate(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
}
pgID := d.Id()
pg, err := dvPortgroupFromMOID(client, pgID)
if err != nil {
return fmt.Errorf("could not find portgroup %q: %s", pgID, err)
}
spec := expandDVPortgroupConfigSpec(d)
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer cancel()
task, err := pg.Reconfigure(ctx, spec)
if err != nil {
return fmt.Errorf("error reconfiguring portgroup: %s", err)
}
tctx, tcancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer tcancel()
if err := task.Wait(tctx); err != nil {
return fmt.Errorf("error waiting for portgroup update to complete: %s", err)
}
// Apply any pending tags now
if tagsClient != nil {
if err := processTagDiff(tagsClient, d, object.NewReference(client.Client, pg.Reference())); err != nil {
return fmt.Errorf("error updating tags: %s", err)
}
}
return resourceVSphereDistributedPortGroupRead(d, meta)
}
func resourceVSphereDistributedPortGroupDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*VSphereClient).vimClient
if err := validateVirtualCenter(client); err != nil {
return err
}
pgID := d.Id()
pg, err := dvPortgroupFromMOID(client, pgID)
if err != nil {
return fmt.Errorf("could not find portgroup %q: %s", pgID, err)
}
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer cancel()
task, err := pg.Destroy(ctx)
if err != nil {
return fmt.Errorf("error deleting portgroup: %s", err)
}
tctx, tcancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer tcancel()
if err := task.Wait(tctx); err != nil {
return fmt.Errorf("error waiting for portgroup deletion to complete: %s", err)
}
return nil
}
func resourceVSphereDistributedPortGroupImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
// We use the inventory path to the portgroup to import. There is not
// checking to make sure that it belongs to the configured DVS, but on
// subsequent plans, if it is not, the resource will be in an unusable state
// as all query calls for DVS CRUD calls require the correct DVS UUID in
// addition to the portgroup UUID.
client := meta.(*VSphereClient).vimClient
if err := validateVirtualCenter(client); err != nil {
return nil, err
}
p := d.Id()
pg, err := dvPortgroupFromPath(client, p, nil)
if err != nil {
return nil, fmt.Errorf("error locating portgroup: %s", err)
}
props, err := dvPortgroupProperties(pg)
if err != nil {
return nil, fmt.Errorf("error fetching portgroup properties: %s", err)
}
d.SetId(props.Key)
// We need to populate the DVS UUID here as well or else our read calls will
// fail.
dvsID := props.Config.DistributedVirtualSwitch.Value
dvs, err := dvsFromMOID(client, dvsID)
if err != nil {
return nil, fmt.Errorf("error getting DVS with ID %q: %s", dvsID, err)
}
dvProps, err := dvsProperties(dvs)
if err != nil {
return nil, fmt.Errorf("error fetching DVS properties: %s", err)
}
d.Set("distributed_virtual_switch_uuid", dvProps.Uuid)
return []*schema.ResourceData{d}, nil
}