/
datasource_vcd_vm_group.go
68 lines (61 loc) · 2.13 KB
/
datasource_vcd_vm_group.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
package vcd
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"log"
)
// datasourceVcdVmGroup defines the data source for a VM Group, used to create VM Placement Policies.
func datasourceVcdVmGroup() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceVcdVmGroupRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Name of the VM Group to fetch",
},
"provider_vdc_id": {
Type: schema.TypeString,
Required: true,
Description: "ID of the Provider VDC to which the VM Group to fetch belongs",
},
"cluster_name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the vSphere cluster associated to this VM Group",
},
"named_vm_group_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the named VM Group. Used to create Logical VM Groups",
},
"vcenter_id": {
Type: schema.TypeString,
Computed: true,
Description: "ID of the vCenter server",
},
"cluster_moref": {
Type: schema.TypeString,
Computed: true,
Description: "Managed object reference of the vSphere cluster associated to this VM Group",
},
},
}
}
func datasourceVcdVmGroupRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
vcdClient := meta.(*VCDClient)
name := d.Get("name").(string)
providerVdcId := d.Get("provider_vdc_id").(string)
vmGroup, err := vcdClient.GetVmGroupByNameAndProviderVdcUrn(name, providerVdcId)
if err != nil {
log.Printf("[DEBUG] Could not find any VM Group with name %s and pVDC %s: %s", name, providerVdcId, err)
return diag.Errorf("could not find any VM Group with name %s and pVDC %s: %s", name, providerVdcId, err)
}
dSet(d, "cluster_name", vmGroup.VmGroup.ClusterName)
dSet(d, "named_vm_group_id", vmGroup.VmGroup.NamedVmGroupId)
dSet(d, "vcenter_id", vmGroup.VmGroup.VcenterId)
dSet(d, "cluster_moref", vmGroup.VmGroup.ClusterMoref)
d.SetId(vmGroup.VmGroup.ID)
return nil
}