forked from hashicorp/terraform-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost_port_group_structure.go
143 lines (131 loc) · 5.05 KB
/
host_port_group_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
package vsphere
import (
"fmt"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/hashicorp/terraform/terraform"
"github.com/vmware/govmomi/vim25/types"
)
const hostPortGroupIDPrefix = "tf-HostPortGroup"
// schemaHostPortGroupSpec returns schema items for resources that
// need to work with HostPortGroupSpec, such as port groups.
func schemaHostPortGroupSpec() map[string]*schema.Schema {
s := map[string]*schema.Schema{
// HostPortGroupSpec
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The name of the port group.",
ForceNew: true,
},
"vlan_id": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Description: "The VLAN ID/trunk mode for this port group. An ID of 0 denotes no tagging, an ID of 1-4094 tags with the specific ID, and an ID of 4095 enables trunk mode, allowing the guest to manage its own tagging.",
Default: 0,
ValidateFunc: validation.IntBetween(0, 4095),
},
"virtual_switch_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "The name of the virtual switch to bind this port group to.",
ForceNew: true,
},
}
mergeSchema(s, schemaHostNetworkPolicy())
return s
}
// expandHostPortGroupSpec reads certain ResourceData keys and returns a
// HostPortGroupSpec.
func expandHostPortGroupSpec(d *schema.ResourceData) *types.HostPortGroupSpec {
obj := &types.HostPortGroupSpec{
Name: d.Get("name").(string),
VlanId: int32(d.Get("vlan_id").(int)),
VswitchName: d.Get("virtual_switch_name").(string),
Policy: *expandHostNetworkPolicy(d),
}
return obj
}
// flattenHostPortGroupSpec reads various fields from a HostPortGroupSpec into
// the passed in ResourceData.
func flattenHostPortGroupSpec(d *schema.ResourceData, obj *types.HostPortGroupSpec) error {
d.Set("vlan_id", obj.VlanId)
if err := flattenHostNetworkPolicy(d, &obj.Policy); err != nil {
return err
}
return nil
}
// calculateComputedPolicy is a utility function to compute a map of state
// attributes for the port group's effective policy. It uses a bit of a
// roundabout way to set the attributes, but allows us to utilize our
// functional deep reading helpers to perform this task, versus having to
// re-write code.
//
// This function relies a bit on some of the lower-level utility functionality
// of helper/schema, so it may need to change in the future.
func calculateComputedPolicy(policy types.HostNetworkPolicy) (map[string]string, error) {
cpr := &schema.Resource{Schema: schemaHostNetworkPolicy()}
cpd := cpr.Data(&terraform.InstanceState{})
cpd.SetId("effectivepolicy")
if err := flattenHostNetworkPolicy(cpd, &policy); err != nil {
return nil, fmt.Errorf("error setting effective policy data: %s", err)
}
cpm := cpd.State().Attributes
delete(cpm, "id")
return cpm, nil
}
// calculatePorts is a utility function that returns a set of port data.
func calculatePorts(ports []types.HostPortGroupPort) *schema.Set {
s := make([]interface{}, 0)
for _, port := range ports {
m := make(map[string]interface{})
m["key"] = port.Key
m["mac_addresses"] = sliceStringsToInterfaces(port.Mac)
m["type"] = port.Type
s = append(s, m)
}
return schema.NewSet(schema.HashResource(portGroupPortSchema()), s)
}
// portGroupPortSchema returns a sub-schema for a port group's connected ports.
func portGroupPortSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"key": &schema.Schema{
Type: schema.TypeString,
Description: "The linkable identifier for this port entry.",
Computed: true,
},
"mac_addresses": &schema.Schema{
Type: schema.TypeList,
Description: "The MAC addresses of the network service of the virtual machine connected on this port.",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"type": &schema.Schema{
Type: schema.TypeString,
Description: "Type type of the entity connected on this port. Possible values are host (VMKkernel), systemManagement (service console), virtualMachine, or unknown.",
Computed: true,
},
},
}
}
// saveHostPortGroupID sets a special ID for a host virtual switch, composed of
// the MOID for the concerned HostSystem and the port group's key.
func saveHostPortGroupID(d *schema.ResourceData, hsID, name string) {
d.SetId(fmt.Sprintf("%s:%s:%s", hostPortGroupIDPrefix, hsID, name))
}
// splitHostPortGroupID splits a vsphere_host_port_group resource ID into its
// counterparts: the prefix, the HostSystem ID, and the port group name.
func splitHostPortGroupID(raw string) (string, string, error) {
s := strings.SplitN(raw, ":", 3)
if len(s) != 3 || s[0] != hostPortGroupIDPrefix || s[1] == "" || s[2] == "" {
return "", "", fmt.Errorf("corrupt ID: %s", raw)
}
return s[1], s[2], nil
}
// portGroupIDsFromResourceID passes a resource's ID through
// splitHostPortGroupID.
func portGroupIDsFromResourceID(d *schema.ResourceData) (string, string, error) {
return splitHostPortGroupID(d.Id())
}