forked from hashicorp/terraform-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_vsphere_host_port_group.go
147 lines (127 loc) · 4.35 KB
/
resource_vsphere_host_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
package vsphere
import (
"fmt"
"context"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceVSphereHostPortGroup() *schema.Resource {
s := map[string]*schema.Schema{
"host_system_id": &schema.Schema{
Type: schema.TypeString,
Description: "The managed object ID of the host to set the virtual switch up on.",
Required: true,
ForceNew: true,
},
"computed_policy": &schema.Schema{
Type: schema.TypeMap,
Description: "The effective network policy after inheritance. Note that this will look similar to, but is not the same, as the policy attributes defined in this resource.",
Computed: true,
},
"key": &schema.Schema{
Type: schema.TypeString,
Description: "The linkable identifier for this port group.",
Computed: true,
},
"ports": &schema.Schema{
Type: schema.TypeSet,
Description: "The ports that currently exist and are used on this port group.",
Computed: true,
MaxItems: 1,
Elem: portGroupPortSchema(),
},
}
mergeSchema(s, schemaHostPortGroupSpec())
// Transform any necessary fields in the schema that need to be updated
// specifically for this resource.
s["active_nics"].Optional = true
s["standby_nics"].Optional = true
return &schema.Resource{
Create: resourceVSphereHostPortGroupCreate,
Read: resourceVSphereHostPortGroupRead,
Update: resourceVSphereHostPortGroupUpdate,
Delete: resourceVSphereHostPortGroupDelete,
Schema: s,
}
}
func resourceVSphereHostPortGroupCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*VSphereClient).vimClient
name := d.Get("name").(string)
hsID := d.Get("host_system_id").(string)
ns, err := hostNetworkSystemFromHostSystemID(client, hsID)
if err != nil {
return fmt.Errorf("error loading network system: %s", err)
}
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer cancel()
spec := expandHostPortGroupSpec(d)
if err := ns.AddPortGroup(ctx, *spec); err != nil {
return fmt.Errorf("error adding port group: %s", err)
}
saveHostPortGroupID(d, hsID, name)
return resourceVSphereHostPortGroupRead(d, meta)
}
func resourceVSphereHostPortGroupRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*VSphereClient).vimClient
hsID, name, err := portGroupIDsFromResourceID(d)
if err != nil {
return err
}
ns, err := hostNetworkSystemFromHostSystemID(client, hsID)
if err != nil {
return fmt.Errorf("error loading host network system: %s", err)
}
pg, err := hostPortGroupFromName(meta.(*VSphereClient).vimClient, ns, name)
if err != nil {
return fmt.Errorf("error fetching port group data: %s", err)
}
if err := flattenHostPortGroupSpec(d, &pg.Spec); err != nil {
return fmt.Errorf("error setting resource data: %s", err)
}
d.Set("key", pg.Key)
cpm, err := calculateComputedPolicy(pg.ComputedPolicy)
if err != nil {
return err
}
if err := d.Set("computed_policy", cpm); err != nil {
return fmt.Errorf("error saving effective policy to state: %s", err)
}
if err := d.Set("ports", calculatePorts(pg.Port)); err != nil {
return fmt.Errorf("error setting port list: %s", err)
}
return nil
}
func resourceVSphereHostPortGroupUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*VSphereClient).vimClient
hsID, name, err := portGroupIDsFromResourceID(d)
if err != nil {
return err
}
ns, err := hostNetworkSystemFromHostSystemID(client, hsID)
if err != nil {
return fmt.Errorf("error loading host network system: %s", err)
}
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer cancel()
spec := expandHostPortGroupSpec(d)
if err := ns.UpdatePortGroup(ctx, name, *spec); err != nil {
return fmt.Errorf("error updating port group: %s", err)
}
return resourceVSphereHostPortGroupRead(d, meta)
}
func resourceVSphereHostPortGroupDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*VSphereClient).vimClient
hsID, name, err := portGroupIDsFromResourceID(d)
if err != nil {
return err
}
ns, err := hostNetworkSystemFromHostSystemID(client, hsID)
if err != nil {
return fmt.Errorf("error loading host network system: %s", err)
}
ctx, cancel := context.WithTimeout(context.Background(), defaultAPITimeout)
defer cancel()
if err := ns.RemovePortGroup(ctx, name); err != nil {
return fmt.Errorf("error deleting port group: %s", err)
}
return nil
}