forked from hashicorp/terraform-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_vsphere_distributed_virtual_switch.go
67 lines (59 loc) · 1.99 KB
/
data_source_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
package vsphere
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/vmware/govmomi/object"
"github.com/vmware/govmomi/vim25/types"
)
func dataSourceVSphereDistributedVirtualSwitch() *schema.Resource {
return &schema.Resource{
Read: dataSourceVSphereDistributedVirtualSwitchRead,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Description: "The name of the distributed virtual switch. This can be a name or path.",
Required: true,
},
"datacenter_id": &schema.Schema{
Type: schema.TypeString,
Description: "The managed object ID of the datacenter the DVS is in. This is required if the supplied path is not an absolute path containing a datacenter and there are multiple datacenters in your infrastructure.",
Optional: true,
},
"uplinks": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Description: "The uplink ports on this DVS.",
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func dataSourceVSphereDistributedVirtualSwitchRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*VSphereClient).vimClient
if err := validateVirtualCenter(client); err != nil {
return err
}
name := d.Get("name").(string)
var dc *object.Datacenter
if dcID, ok := d.GetOk("datacenter_id"); ok {
var err error
dc, err = datacenterFromID(client, dcID.(string))
if err != nil {
return fmt.Errorf("cannot locate datacenter: %s", err)
}
}
dvs, err := dvsFromPath(client, name, dc)
if err != nil {
return fmt.Errorf("error fetching distributed virtual switch: %s", err)
}
props, err := dvsProperties(dvs)
if err != nil {
return fmt.Errorf("error fetching DVS properties: %s", err)
}
d.SetId(props.Uuid)
uplinkPolicy := props.Config.(*types.VMwareDVSConfigInfo).UplinkPortPolicy.(*types.DVSNameArrayUplinkPortPolicy)
if err := flattenDVSNameArrayUplinkPortPolicy(d, uplinkPolicy); err != nil {
return err
}
return nil
}