forked from hashicorp/terraform-provider-vsphere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_source_vsphere_resource_pool.go
56 lines (48 loc) · 1.59 KB
/
data_source_vsphere_resource_pool.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
package vsphere
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-vsphere/vsphere/internal/helper/resourcepool"
"github.com/terraform-providers/terraform-provider-vsphere/vsphere/internal/helper/viapi"
"github.com/vmware/govmomi/object"
)
func dataSourceVSphereResourcePool() *schema.Resource {
return &schema.Resource{
Read: dataSourceVSphereResourcePoolRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "The name or path of the resource pool.",
Optional: true,
},
"datacenter_id": {
Type: schema.TypeString,
Description: "The managed object ID of the datacenter the resource pool is in. This is not required when using ESXi directly, or if there is only one datacenter in your infrastructure.",
Optional: true,
},
},
}
}
func dataSourceVSphereResourcePoolRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*VSphereClient).vimClient
name := d.Get("name").(string)
if err := viapi.ValidateVirtualCenter(client); err == nil {
if name == "" {
return fmt.Errorf("name cannot be empty when using vCenter")
}
}
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)
}
}
rp, err := resourcepool.FromPathOrDefault(client, name, dc)
if err != nil {
return fmt.Errorf("error fetching resource pool: %s", err)
}
d.SetId(rp.Reference().Value)
return nil
}