From 3ae34208de3e7d150f9547f5e314cf064e1f42d1 Mon Sep 17 00:00:00 2001 From: imjoey Date: Tue, 10 Sep 2019 13:03:54 +0800 Subject: [PATCH] Provide IP configs for ovirt_vms data source Signed-off-by: imjoey --- ovirt/data_source_ovirt_vms.go | 72 +++++++++++++++++++++++++++++ ovirt/data_source_ovirt_vms_test.go | 5 ++ website/docs/d/vms.html.markdown | 64 +++++++++++++++++++++++++ 3 files changed, 141 insertions(+) create mode 100644 website/docs/d/vms.html.markdown diff --git a/ovirt/data_source_ovirt_vms.go b/ovirt/data_source_ovirt_vms.go index 29a0024a..57789f8f 100644 --- a/ovirt/data_source_ovirt_vms.go +++ b/ovirt/data_source_ovirt_vms.go @@ -80,6 +80,62 @@ func dataSourceOvirtVMs() *schema.Resource { Type: schema.TypeInt, Computed: true, }, + "reported_devices": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Computed: true, + }, + "mac_address": { + Type: schema.TypeString, + Computed: true, + }, + "description": { + Type: schema.TypeString, + Computed: true, + }, + "comment": { + Type: schema.TypeString, + Computed: true, + }, + "type": { + Type: schema.TypeString, + Computed: true, + }, + "ips": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "address": { + Type: schema.TypeString, + Computed: true, + }, + "gateway": { + Type: schema.TypeString, + Computed: true, + }, + "netmask": { + Type: schema.TypeString, + Computed: true, + }, + "version": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + }, + }, }, }, }, @@ -151,6 +207,8 @@ func dataSourceOvirtVMsRead(d *schema.ResourceData, meta interface{}) error { } func vmsDescriptionAttributes(d *schema.ResourceData, vms []*ovirtsdk4.Vm, meta interface{}) error { + conn := meta.(*ovirtsdk4.Connection) + var s []map[string]interface{} for _, v := range vms { @@ -166,6 +224,20 @@ func vmsDescriptionAttributes(d *schema.ResourceData, vms []*ovirtsdk4.Vm, meta "sockets": v.MustCpu().MustTopology().MustSockets(), "threads": v.MustCpu().MustTopology().MustThreads(), } + + devicesResp, err := conn.SystemService(). + VmsService(). + VmService(v.MustId()). + ReportedDevicesService(). + List(). + Send() + if err != nil { + return err + } + if devices, ok := devicesResp.ReportedDevice(); ok && len(devices.Slice()) > 0 { + mapping["reported_devices"] = flattenOvirtNicReportedDevices(devices.Slice()) + } + s = append(s, mapping) } d.SetId(resource.UniqueId()) diff --git a/ovirt/data_source_ovirt_vms_test.go b/ovirt/data_source_ovirt_vms_test.go index 93995eb7..21befba5 100644 --- a/ovirt/data_source_ovirt_vms_test.go +++ b/ovirt/data_source_ovirt_vms_test.go @@ -40,6 +40,11 @@ func TestAccOvirtVMsDataSource_searchFilter(t *testing.T) { testAccCheckOvirtDataSourceID("data.ovirt_vms.search_filtered_vm"), resource.TestCheckResourceAttr("data.ovirt_vms.search_filtered_vm", "vms.#", "1"), resource.TestCheckResourceAttr("data.ovirt_vms.search_filtered_vm", "vms.0.name", "HostedEngine"), + resource.TestCheckResourceAttr("data.ovirt_vms.search_filtered_vm", "vms.0.reported_devices.#", "1"), + resource.TestCheckResourceAttr("data.ovirt_vms.search_filtered_vm", "vms.0.reported_devices.0.name", "eth0"), + resource.TestCheckResourceAttr("data.ovirt_vms.search_filtered_vm", "vms.0.reported_devices.0.ips.#", "3"), + resource.TestCheckResourceAttr("data.ovirt_vms.search_filtered_vm", "vms.0.reported_devices.0.ips.0.address", "10.1.111.64"), + resource.TestCheckResourceAttr("data.ovirt_vms.search_filtered_vm", "vms.0.reported_devices.0.ips.0.version", "v4"), ), }, }, diff --git a/website/docs/d/vms.html.markdown b/website/docs/d/vms.html.markdown new file mode 100644 index 00000000..d9d66051 --- /dev/null +++ b/website/docs/d/vms.html.markdown @@ -0,0 +1,64 @@ +--- +layout: "ovirt" +page_title: "oVirt: ovirt_vms" +sidebar_current: "docs-ovirt-datasource-vms" +description: |- + Provides details about oVirt VMs +--- + +# Data Source: ovirt\_vms + +The oVirt VMs data source allows access to details of list of VMs within oVirt. + +## Example Usage + +```hcl +data "ovirt_vms" "filtered_vms" { + name_regex = "^HostedEngine*" + + search = { + criteria = "name = HostedEngine and status = up" + max = 2 + case_sensitive = false + } +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name_regex` - (Optional) The fully functional regular expression for name +* `search` - (Optional) The general search criteria representation, fitting the rules of [Searching](http://ovirt.github.io/ovirt-engine-api-model/master/#_searching) + * criteria - (Optional) The criteria for searching, using the same syntax as the oVirt query language + * max - (Optional) The maximum amount of objects returned. If not specified, the search will return all the objects. + * case_sensitive - (Optional) If the search are case sensitive, default value is `false` + +> The `search.criteria` also supports asterisk for searching by name, to indicate that any string matches, including the empty string. For example, the criteria `search=name=myobj*` will return all the objects with names beginning with `myobj`, such as `myobj2`, `myobj-test`. So, you could use `name_regex` for searching by complicated regular expression, and `search.criteria` for simple case accordingly. + +## Attributes Reference + +`vms` is set to the wrapper of the found VMs. Each item of `vms` contains the following attributes exported: + +* `id` - The ID of oVirt VM +* `name` - The name of oVirt VM +* `cluster_id` - The ID of oVirt Cluster the VM belongs to +* `status` - The current status of the VM +* `template_id` - The ID of oVirt Template the VM creates from +* `high_availability` - Defines if the HA is enabled +* `memory` - The VM's memory, in Megabytes(MB) +* `cores` - The CPU cores of the VM +* `sockets` - The CPU sockets of the VM +* `threads` - The CPU threads of the VM +* `reported_devices` - A collection of reported devices that are associated with the network interface of the VM + * `id` - The ID of the reported device + * `name` - The name of the reported device + * `mac_address` - The MAC address of the reported device + * `description` - A human-readable description in plain text about the reported device + * `comment` - Free text containing comments about the reported device + * `type` - The type of the reported device + * `ips` - A list of IP configurations of the reported device + * `address` - The text representation of the IP address + * `gateway` - The address of the default gateway + * `netmask` - The network mask + * `version` - The version of the IP protocol