forked from hashicorp/terraform-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_source_vsphere_network.go
61 lines (54 loc) · 1.81 KB
/
data_source_vsphere_network.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
package vsphere
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/terraform-providers/terraform-provider-vsphere/vsphere/internal/helper/network"
"github.com/vmware/govmomi/object"
)
func dataSourceVSphereNetwork() *schema.Resource {
return &schema.Resource{
Read: dataSourceVSphereNetworkRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "The name or path of the network.",
Required: true,
},
"datacenter_id": {
Type: schema.TypeString,
Description: "The managed object ID of the datacenter the network 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,
},
"type": {
Type: schema.TypeString,
Description: "The managed object type of the network.",
Computed: true,
},
"distributed_virtual_switch_uuid": {
Type: schema.TypeString,
Description: "Id of the distributed virtual switch of which the port group is a part of",
Optional: true,
},
},
}
}
func dataSourceVSphereNetworkRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*VSphereClient).vimClient
name := d.Get("name").(string)
dvSwitchUuid := d.Get("distributed_virtual_switch_uuid").(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)
}
}
net, err := network.FromNameAndDVSUuid(client, name, dc, dvSwitchUuid)
if err != nil {
return fmt.Errorf("error fetching network: %s", err)
}
d.SetId(net.Reference().Value)
d.Set("type", net.Reference().Type)
return nil
}