forked from hashicorp/terraform-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host_virtual_switch_structure.go
198 lines (178 loc) · 7.45 KB
/
host_virtual_switch_structure.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
package vsphere
import (
"fmt"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/vmware/govmomi/vim25/types"
)
const hostVirtualSwitchIDPrefix = "tf-HostVirtualSwitch"
var linkDiscoveryProtocolConfigOperationAllowedValues = []string{
string(types.LinkDiscoveryProtocolConfigOperationTypeNone),
string(types.LinkDiscoveryProtocolConfigOperationTypeListen),
string(types.LinkDiscoveryProtocolConfigOperationTypeAdvertise),
string(types.LinkDiscoveryProtocolConfigOperationTypeBoth),
}
var linkDiscoveryProtocolConfigProtocolAllowedValues = []string{
string(types.LinkDiscoveryProtocolConfigProtocolTypeCdp),
string(types.LinkDiscoveryProtocolConfigProtocolTypeLldp),
}
// schemaHostVirtualSwitchBondBridge returns schema items for resources that
// need to work with a HostVirtualSwitchBondBridge, such as virtual switches.
func schemaHostVirtualSwitchBondBridge() map[string]*schema.Schema {
return map[string]*schema.Schema{
// HostVirtualSwitchBeaconConfig
"beacon_interval": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Description: "Determines how often, in seconds, a beacon should be sent to probe for the validity of a link.",
Default: 1,
ValidateFunc: validation.IntAtLeast(0),
},
// LinkDiscoveryProtocolConfig
"link_discovery_operation": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "Whether to advertise or listen for link discovery. Valid values are advertise, both, listen, and none.",
Default: string(types.LinkDiscoveryProtocolConfigOperationTypeListen),
ValidateFunc: validation.StringInSlice(linkDiscoveryProtocolConfigOperationAllowedValues, false),
},
"link_discovery_protocol": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "The discovery protocol type. Valid values are cdp and lldp.",
Default: string(types.LinkDiscoveryProtocolConfigProtocolTypeCdp),
ValidateFunc: validation.StringInSlice(linkDiscoveryProtocolConfigProtocolAllowedValues, false),
},
// HostVirtualSwitchBondBridge
"network_adapters": &schema.Schema{
Type: schema.TypeList,
Required: true,
Description: "The list of network adapters to bind to this virtual switch.",
Elem: &schema.Schema{Type: schema.TypeString},
},
}
}
// expandHostVirtualSwitchBeaconConfig reads certain ResourceData keys and
// returns a HostVirtualSwitchBeaconConfig.
func expandHostVirtualSwitchBeaconConfig(d *schema.ResourceData) *types.HostVirtualSwitchBeaconConfig {
obj := &types.HostVirtualSwitchBeaconConfig{
Interval: int32(d.Get("beacon_interval").(int)),
}
return obj
}
// flattenHostVirtualSwitchBeaconConfig reads various fields from a
// HostVirtualSwitchBeaconConfig into the passed in ResourceData.
func flattenHostVirtualSwitchBeaconConfig(d *schema.ResourceData, obj *types.HostVirtualSwitchBeaconConfig) error {
d.Set("beacon_interval", obj.Interval)
return nil
}
// expandLinkDiscoveryProtocolConfig reads certain ResourceData keys and
// returns a LinkDiscoveryProtocolConfig.
func expandLinkDiscoveryProtocolConfig(d *schema.ResourceData) *types.LinkDiscoveryProtocolConfig {
obj := &types.LinkDiscoveryProtocolConfig{
Operation: d.Get("link_discovery_operation").(string),
Protocol: d.Get("link_discovery_protocol").(string),
}
return obj
}
// flattenLinkDiscoveryProtocolConfig reads various fields from a
// LinkDiscoveryProtocolConfig into the passed in ResourceData.
func flattenLinkDiscoveryProtocolConfig(d *schema.ResourceData, obj *types.LinkDiscoveryProtocolConfig) error {
d.Set("link_discovery_operation", obj.Operation)
d.Set("link_discovery_protocol", obj.Protocol)
return nil
}
// expandHostVirtualSwitchBondBridge reads certain ResourceData keys and
// returns a HostVirtualSwitchBondBridge.
func expandHostVirtualSwitchBondBridge(d *schema.ResourceData) *types.HostVirtualSwitchBondBridge {
obj := &types.HostVirtualSwitchBondBridge{
NicDevice: sliceInterfacesToStrings(d.Get("network_adapters").([]interface{})),
}
obj.Beacon = expandHostVirtualSwitchBeaconConfig(d)
obj.LinkDiscoveryProtocolConfig = expandLinkDiscoveryProtocolConfig(d)
return obj
}
// flattenHostVirtualSwitchBondBridge reads various fields from a
// HostVirtualSwitchBondBridge into the passed in ResourceData.
func flattenHostVirtualSwitchBondBridge(d *schema.ResourceData, obj *types.HostVirtualSwitchBondBridge) error {
if err := d.Set("network_adapters", sliceStringsToInterfaces(obj.NicDevice)); err != nil {
return err
}
if err := flattenHostVirtualSwitchBeaconConfig(d, obj.Beacon); err != nil {
return err
}
if err := flattenLinkDiscoveryProtocolConfig(d, obj.LinkDiscoveryProtocolConfig); err != nil {
return err
}
return nil
}
// schemaHostVirtualSwitchSpec returns schema items for resources that need to
// work with a HostVirtualSwitchSpec, such as virtual switches.
func schemaHostVirtualSwitchSpec() map[string]*schema.Schema {
s := map[string]*schema.Schema{
// HostVirtualSwitchSpec
"mtu": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Description: "The maximum transmission unit (MTU) of the virtual switch in bytes.",
Default: 1500,
ValidateFunc: validation.IntBetween(1, 9000),
},
"number_of_ports": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Description: "The number of ports that this virtual switch is configured to use.",
Default: 128,
ValidateFunc: validation.IntBetween(0, 1024),
},
}
mergeSchema(s, schemaHostVirtualSwitchBondBridge())
mergeSchema(s, schemaHostNetworkPolicy())
return s
}
// expandHostVirtualSwitchSpec reads certain ResourceData keys and returns a
// HostVirtualSwitchSpec.
func expandHostVirtualSwitchSpec(d *schema.ResourceData) *types.HostVirtualSwitchSpec {
obj := &types.HostVirtualSwitchSpec{
Mtu: int32(d.Get("mtu").(int)),
NumPorts: int32(d.Get("number_of_ports").(int)),
Bridge: expandHostVirtualSwitchBondBridge(d),
Policy: expandHostNetworkPolicy(d),
}
return obj
}
// flattenHostVirtualSwitchSpec reads various fields from a
// HostVirtualSwitchSpec into the passed in ResourceData.
func flattenHostVirtualSwitchSpec(d *schema.ResourceData, obj *types.HostVirtualSwitchSpec) error {
d.Set("mtu", obj.Mtu)
d.Set("number_of_ports", obj.NumPorts)
if err := flattenHostVirtualSwitchBondBridge(d, obj.Bridge.(*types.HostVirtualSwitchBondBridge)); err != nil {
return err
}
if err := flattenHostNetworkPolicy(d, obj.Policy); err != nil {
return err
}
return nil
}
// saveHostVirtualSwitchID sets a special ID for a host virtual switch,
// composed of the MOID for the concerned HostSystem and the virtual switch's
// key.
func saveHostVirtualSwitchID(d *schema.ResourceData, hsID, name string) {
d.SetId(fmt.Sprintf("%s:%s:%s", hostVirtualSwitchIDPrefix, hsID, name))
}
// splitHostVirtualSwitchID splits a vsphere_host_virtual_switch resource ID
// into its counterparts: the prefix, the HostSystem ID, and the virtual switch
// name.
func splitHostVirtualSwitchID(raw string) (string, string, error) {
s := strings.SplitN(raw, ":", 3)
if len(s) != 3 || s[0] != hostVirtualSwitchIDPrefix || s[1] == "" || s[2] == "" {
return "", "", fmt.Errorf("corrupt ID: %s", raw)
}
return s[1], s[2], nil
}
// virtualSwitchIDsFromResourceID passes a resource's ID through
// splitHostVirtualSwitchID.
func virtualSwitchIDsFromResourceID(d *schema.ResourceData) (string, string, error) {
return splitHostVirtualSwitchID(d.Id())
}