From b287022e797b0a5718f83c62f36f6bdf0b929817 Mon Sep 17 00:00:00 2001 From: Catalin Muresan Date: Mon, 23 Oct 2023 19:17:39 +0100 Subject: [PATCH 01/16] Initial commit libvirt_nodeinfo --- libvirt/data_source_libvirt_nodeinfo.go | 91 +++++++++++++++++++++++++ libvirt/provider.go | 1 + 2 files changed, 92 insertions(+) create mode 100644 libvirt/data_source_libvirt_nodeinfo.go diff --git a/libvirt/data_source_libvirt_nodeinfo.go b/libvirt/data_source_libvirt_nodeinfo.go new file mode 100644 index 000000000..373231200 --- /dev/null +++ b/libvirt/data_source_libvirt_nodeinfo.go @@ -0,0 +1,91 @@ +package libvirt + +import ( + "fmt" + "log" + "strconv" + + "github.com/dmacvicar/terraform-provider-libvirt/libvirt/helper/hashcode" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +// a libvirt nodeinfo datasource +// +// Datasource example: +// +// data "libvirt_nodeinfo" "info" { +// } +// +// output "cpus" { +// value = data.libvirt_nodeinfo.info.cpus +// } +func datasourceLibvirtNodeInfo() *schema.Resource { + return &schema.Resource{ + Read: resourceLibvirtNodeInfoRead, + Schema: map[string]*schema.Schema{ + "cpu_model": { + Type: schema.TypeString, + Computed: true, + }, + "memory_total": { + Type: schema.TypeInt, + Computed: true, + }, + "cpu_cores_total": { + Type: schema.TypeInt, + Computed: true, + }, + "numa_nodes": { + Type: schema.TypeInt, + Computed: true, + }, + "cpu_sockets": { + Type: schema.TypeInt, + Computed: true, + }, + "cpu_cores_per_socket": { + Type: schema.TypeInt, + Computed: true, + }, + "cpu_threads_per_core": { + Type: schema.TypeInt, + Computed: true, + }, + }, + } +} + +func resourceLibvirtNodeInfoRead(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] Read data source libvirt_nodeinfo") + + virConn := meta.(*Client).libvirt + if virConn == nil { + return fmt.Errorf(LibVirtConIsNil) + } + + model, memory, cpus, _, nodes, sockets, cores, threads, err := virConn.NodeGetInfo() + + if err != nil { + return fmt.Errorf("failed to retrieve domains: %v", err) + } + + d.Set("cpu_model", Int8ToString(model)) + d.Set("cpu_cores_total", cpus) + d.Set("cpu_cores_per_socket", cores) + d.Set("numa_nodes", nodes) + d.Set("cpu_sockets", sockets) + d.Set("cpu_threads_per_core", threads) + d.Set("numa_nodes", nodes) + d.Set("memory_total", memory) + d.SetId(strconv.Itoa(hashcode.String(fmt.Sprintf("%v%v%v%v%v%v%v", model, memory, cpus, nodes, sockets, cores, threads)))) + + return nil +} + +func Int8ToString(bs [32]int8) string { + ba := []uint8{} + for _, b := range bs { + ba = append(ba, uint8(b)) + } + return string(ba) +} \ No newline at end of file diff --git a/libvirt/provider.go b/libvirt/provider.go index 81bb05d03..c5e1216ed 100644 --- a/libvirt/provider.go +++ b/libvirt/provider.go @@ -31,6 +31,7 @@ func Provider() *schema.Provider { "libvirt_network_dns_host_template": datasourceLibvirtNetworkDNSHostTemplate(), "libvirt_network_dns_srv_template": datasourceLibvirtNetworkDNSSRVTemplate(), "libvirt_network_dnsmasq_options_template": datasourceLibvirtNetworkDnsmasqOptionsTemplate(), + "libvirt_nodeinfo": datasourceLibvirtNodeInfo(), }, ConfigureFunc: providerConfigure, From e03a29421f9a89214cf828389c34aca65d9160f5 Mon Sep 17 00:00:00 2001 From: Catalin Muresan Date: Tue, 24 Oct 2023 02:43:01 +0100 Subject: [PATCH 02/16] Added support for reading node devices and node device info --- libvirt/data_source_libvirt_nodedeviceinfo.go | 76 +++++++++++++++++++ libvirt/data_source_libvirt_nodedevices.go | 66 ++++++++++++++++ libvirt/provider.go | 2 + 3 files changed, 144 insertions(+) create mode 100644 libvirt/data_source_libvirt_nodedeviceinfo.go create mode 100644 libvirt/data_source_libvirt_nodedevices.go diff --git a/libvirt/data_source_libvirt_nodedeviceinfo.go b/libvirt/data_source_libvirt_nodedeviceinfo.go new file mode 100644 index 000000000..510d6176a --- /dev/null +++ b/libvirt/data_source_libvirt_nodedeviceinfo.go @@ -0,0 +1,76 @@ +package libvirt + +import ( + "fmt" + "log" + "strconv" + + "github.com/dmacvicar/terraform-provider-libvirt/libvirt/helper/hashcode" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +// a libvirt nodeinfo datasource +// +// Datasource example: +// +// data "libvirt_nodeinfo" "info" { +// } +// +// output "cpus" { +// value = data.libvirt_nodeinfo.info.cpus +// } +func datasourceLibvirtNodeDeviceInfo() *schema.Resource { + return &schema.Resource{ + Read: resourceLibvirtNodeDeviceInfoRead, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + "path": { + Type: schema.TypeString, + Computed: true, + }, + "parent": { + Type: schema.TypeString, + Computed: true, + }, + "xml": { + Type: schema.TypeString, + Computed: true, + }, + }, + } +} + +func resourceLibvirtNodeDeviceInfoRead(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] Read data source libvirt_nodedevices") + + virConn := meta.(*Client).libvirt + if virConn == nil { + return fmt.Errorf(LibVirtConIsNil) + } + + var device_name string + + if name, ok := d.GetOk("name"); ok { + device_name = name.(string) + log.Printf("[DEBUG] Got name: %s", device_name) + } + + device, err := virConn.NodeDeviceLookupByName(device_name) + if err != nil { + return fmt.Errorf("failed to lookup node device: %v", err) + } + + xml, err := virConn.NodeDeviceGetXMLDesc(device.Name, 0) + if err != nil { + return fmt.Errorf("failed to get XML for node device: %v", err) + } + + d.Set("xml", xml) + //d.Set("devices", ) + d.SetId(strconv.Itoa(hashcode.String(fmt.Sprintf("%v", xml)))) + + return nil +} diff --git a/libvirt/data_source_libvirt_nodedevices.go b/libvirt/data_source_libvirt_nodedevices.go new file mode 100644 index 000000000..bb6a7faf4 --- /dev/null +++ b/libvirt/data_source_libvirt_nodedevices.go @@ -0,0 +1,66 @@ +package libvirt + +import ( + "fmt" + "log" + "strconv" + + libvirt "github.com/digitalocean/go-libvirt" + "github.com/dmacvicar/terraform-provider-libvirt/libvirt/helper/hashcode" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +// a libvirt nodeinfo datasource +// +// Datasource example: +// +// data "libvirt_nodeinfo" "info" { +// } +// +// output "cpus" { +// value = data.libvirt_nodeinfo.info.cpus +// } +func datasourceLibvirtNodeDevices() *schema.Resource { + return &schema.Resource{ + Read: resourceLibvirtNodeDevicesRead, + Schema: map[string]*schema.Schema{ + // "name": { + // Type: schema.TypeString, + // Required: true, + // }, + "devices": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + } +} + +func resourceLibvirtNodeDevicesRead(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] Read data source libvirt_nodedevices") + + virConn := meta.(*Client).libvirt + if virConn == nil { + return fmt.Errorf(LibVirtConIsNil) + } + + opts := libvirt.OptString{"pci"} + + rnum, err := virConn.NodeNumOfDevices(opts, 0) + if err != nil { + return fmt.Errorf("failed to retrieve number of devices: %v", err) + } + + devices, err := virConn.NodeListDevices(opts, rnum, 0) + if err != nil { + return fmt.Errorf("failed to retrieve list of node devices: %v", err) + } + + d.Set("devices", devices) + d.SetId(strconv.Itoa(hashcode.String(fmt.Sprintf("%v", devices)))) + + return nil +} diff --git a/libvirt/provider.go b/libvirt/provider.go index c5e1216ed..799438673 100644 --- a/libvirt/provider.go +++ b/libvirt/provider.go @@ -32,6 +32,8 @@ func Provider() *schema.Provider { "libvirt_network_dns_srv_template": datasourceLibvirtNetworkDNSSRVTemplate(), "libvirt_network_dnsmasq_options_template": datasourceLibvirtNetworkDnsmasqOptionsTemplate(), "libvirt_nodeinfo": datasourceLibvirtNodeInfo(), + "libvirt_nodedeviceinfo": datasourceLibvirtNodeDeviceInfo(), + "libvirt_nodedevices": datasourceLibvirtNodeDevices(), }, ConfigureFunc: providerConfigure, From 72e842911d79168ba74067410389e37cef12a3a9 Mon Sep 17 00:00:00 2001 From: Catalin Muresan Date: Tue, 24 Oct 2023 14:53:33 +0100 Subject: [PATCH 03/16] Add support to parse device XML to extract into to attributes --- .../data_source_libvirt_node_device_info.go | 183 ++++++++++++++++++ ...go => data_source_libvirt_node_devices.go} | 25 ++- ...fo.go => data_source_libvirt_node_info.go} | 22 +-- libvirt/data_source_libvirt_nodedeviceinfo.go | 76 -------- libvirt/provider.go | 6 +- 5 files changed, 212 insertions(+), 100 deletions(-) create mode 100644 libvirt/data_source_libvirt_node_device_info.go rename libvirt/{data_source_libvirt_nodedevices.go => data_source_libvirt_node_devices.go} (70%) rename libvirt/{data_source_libvirt_nodeinfo.go => data_source_libvirt_node_info.go} (85%) delete mode 100644 libvirt/data_source_libvirt_nodedeviceinfo.go diff --git a/libvirt/data_source_libvirt_node_device_info.go b/libvirt/data_source_libvirt_node_device_info.go new file mode 100644 index 000000000..d6d01f333 --- /dev/null +++ b/libvirt/data_source_libvirt_node_device_info.go @@ -0,0 +1,183 @@ +package libvirt + +import ( + "encoding/xml" + "fmt" + "log" + "strconv" + + "github.com/dmacvicar/terraform-provider-libvirt/libvirt/helper/hashcode" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" +) + +// strutures to parse device XML +type Device struct { + Device xml.Name `xml:"device"` + Name string `xml:"name"` + Path string `xml:"path"` + Parent string `xml:"parent"` + Capability Capability `xml:"capability"` +} + +type Capability struct { + Capability xml.Name `xml:"capability"` + Type string `xml:"type,attr"` + // PCI + Class string `xml:"class"` + Domain int `xml:"domain"` + Bus int `xml:"bus"` + Slot int `xml:"slot"` + Function int `xml:"function"` + Product Product `xml:"product"` + Vendor Vendor `xml:"vendor"` + IommuGroup IommuGroup `xml:"iommuGroup"` + // STORAGE + Block string `xml:"block"` + DriveType string `xml:"drive_type"` + Model string `xml:"model"` + Serial string `xml:"serial"` + Size int `xml:"size"` + LogicalBlockSize int `xml:"logical_block_size"` + NumBlocks int `xml:"num_blocks"` + // USB + Number int `xml:"number"` + Subclass int `xml:"subclass"` + Protocol int `xml:"protocol"` +} + +type Product struct { + Name string `xml:",chardata"` + Id string `xml:"id,attr"` +} + +type Vendor struct { + Name string `xml:",chardata"` + Id string `xml:"id,attr"` +} + +type IommuGroup struct { + IommuGroup xml.Name `xml:"iommuGroup"` + Number int `xml:"number,attr"` + Addresses []Address `xml:"address"` +} + +type Address struct { + Address xml.Name `xml:"address"` + Domain string `xml:"domain,attr"` + Bus string `xml:"bus,attr"` + Slot string `xml:"slot,attr"` + Function string `xml:"function,attr"` +} + +// a libvirt nodeinfo datasource +// +// Datasource example: +// +// data "libvirt_node_device_info" "info" { +// } +// +// output "cpus" { +// value = data.libvirt_node_device_info.info.xml +// } +func datasourceLibvirtNodeDeviceInfo() *schema.Resource { + return &schema.Resource{ + Read: resourceLibvirtNodeDeviceInfoRead, + Schema: map[string]*schema.Schema{ + "name": { + Type: schema.TypeString, + Required: true, + }, + "path": { + Type: schema.TypeString, + Computed: true, + }, + "parent": { + Type: schema.TypeString, + Computed: true, + }, + "xml": { + Type: schema.TypeString, + Computed: true, + }, + "capability": { + Type: schema.TypeMap, + Computed: true, + }, + }, + } +} + +func resourceLibvirtNodeDeviceInfoRead(d *schema.ResourceData, meta interface{}) error { + log.Printf("[DEBUG] Read data source libvirt_nodedevices") + + virConn := meta.(*Client).libvirt + if virConn == nil { + return fmt.Errorf(LibVirtConIsNil) + } + + var device_name string + + if name, ok := d.GetOk("name"); ok { + device_name = name.(string) + log.Printf("[DEBUG] Got name: %s", device_name) + } + + device, err := virConn.NodeDeviceLookupByName(device_name) + if err != nil { + return fmt.Errorf("failed to lookup node device: %v", err) + } + + xml_desc, err := virConn.NodeDeviceGetXMLDesc(device.Name, 0) + if err != nil { + return fmt.Errorf("failed to get XML for node device: %v", err) + } + + var device_xml Device + + err = xml.Unmarshal([]byte(xml_desc), &device_xml) + if err != nil { + log.Fatalf("failed to unmarshal device_xml into XML: %v", err) + } + + capability := map[string]interface{}{} + if device_xml.Capability.Type == "pci" { + capability["type"] = device_xml.Capability.Type + capability["class"] = device_xml.Capability.Class + capability["domain"] = fmt.Sprintf("%d", device_xml.Capability.Domain) + capability["bus"] = fmt.Sprintf("%d", device_xml.Capability.Bus) + capability["slot"] = fmt.Sprintf("%d", device_xml.Capability.Slot) + capability["function"] = fmt.Sprintf("%d", device_xml.Capability.Function) + capability["product_id"] = device_xml.Capability.Product.Id + capability["product_name"] = device_xml.Capability.Product.Name + capability["vendor_id"] = device_xml.Capability.Vendor.Id + capability["vendor_name"] = device_xml.Capability.Vendor.Name + capability["iommu_group_number"] = fmt.Sprintf("%d", device_xml.Capability.IommuGroup.Number) + } + + if device_xml.Capability.Type == "storage" { + capability["type"] = device_xml.Capability.Type + capability["block"] = device_xml.Capability.Block + capability["drive_type"] = device_xml.Capability.DriveType + capability["mode"] = device_xml.Capability.Model + capability["serial"] = device_xml.Capability.Serial + capability["size"] = fmt.Sprintf("%d", device_xml.Capability.Size) + capability["logical_block_size"] = fmt.Sprintf("%d", device_xml.Capability.LogicalBlockSize) + capability["num_blocks"] = fmt.Sprintf("%d", device_xml.Capability.NumBlocks) + } + + if device_xml.Capability.Type == "usb" { + capability["type"] = device_xml.Capability.Type + capability["number"] = fmt.Sprintf("%d", device_xml.Capability.Number) + capability["class"] = device_xml.Capability.Class + capability["subclass"] = fmt.Sprintf("%d", device_xml.Capability.Subclass) + capability["protocol"] = fmt.Sprintf("%d", device_xml.Capability.Protocol) + } + + d.Set("xml", xml_desc) + d.Set("path", device_xml.Path) + d.Set("parent", device_xml.Parent) + d.Set("capability", capability) + d.SetId(strconv.Itoa(hashcode.String(fmt.Sprintf("%v", xml_desc)))) + + return nil +} diff --git a/libvirt/data_source_libvirt_nodedevices.go b/libvirt/data_source_libvirt_node_devices.go similarity index 70% rename from libvirt/data_source_libvirt_nodedevices.go rename to libvirt/data_source_libvirt_node_devices.go index bb6a7faf4..ce6e9bf5d 100644 --- a/libvirt/data_source_libvirt_nodedevices.go +++ b/libvirt/data_source_libvirt_node_devices.go @@ -14,24 +14,24 @@ import ( // // Datasource example: // -// data "libvirt_nodeinfo" "info" { +// data "libvirt_node_devices" "list" { // } // // output "cpus" { -// value = data.libvirt_nodeinfo.info.cpus +// value = data.libvirt_node_devices.list.devices // } func datasourceLibvirtNodeDevices() *schema.Resource { return &schema.Resource{ Read: resourceLibvirtNodeDevicesRead, Schema: map[string]*schema.Schema{ - // "name": { - // Type: schema.TypeString, - // Required: true, - // }, + "capability": { + Type: schema.TypeString, + Optional: true, + }, "devices": { Type: schema.TypeList, Computed: true, - Elem: &schema.Schema{ + Elem: &schema.Schema{ Type: schema.TypeString, }, }, @@ -47,14 +47,19 @@ func resourceLibvirtNodeDevicesRead(d *schema.ResourceData, meta interface{}) er return fmt.Errorf(LibVirtConIsNil) } - opts := libvirt.OptString{"pci"} + var cap libvirt.OptString + + if capability, ok := d.GetOk("capability"); ok { + cap = append(cap, capability.(string)) + log.Printf("[DEBUG] Got capability: %v", cap) + } - rnum, err := virConn.NodeNumOfDevices(opts, 0) + rnum, err := virConn.NodeNumOfDevices(libvirt.OptString(cap), 0) if err != nil { return fmt.Errorf("failed to retrieve number of devices: %v", err) } - devices, err := virConn.NodeListDevices(opts, rnum, 0) + devices, err := virConn.NodeListDevices(cap, rnum, 0) if err != nil { return fmt.Errorf("failed to retrieve list of node devices: %v", err) } diff --git a/libvirt/data_source_libvirt_nodeinfo.go b/libvirt/data_source_libvirt_node_info.go similarity index 85% rename from libvirt/data_source_libvirt_nodeinfo.go rename to libvirt/data_source_libvirt_node_info.go index 373231200..5d53e083e 100644 --- a/libvirt/data_source_libvirt_nodeinfo.go +++ b/libvirt/data_source_libvirt_node_info.go @@ -24,31 +24,31 @@ func datasourceLibvirtNodeInfo() *schema.Resource { Read: resourceLibvirtNodeInfoRead, Schema: map[string]*schema.Schema{ "cpu_model": { - Type: schema.TypeString, + Type: schema.TypeString, Computed: true, }, - "memory_total": { - Type: schema.TypeInt, + "memory_total_kb": { + Type: schema.TypeInt, Computed: true, }, "cpu_cores_total": { - Type: schema.TypeInt, + Type: schema.TypeInt, Computed: true, }, "numa_nodes": { - Type: schema.TypeInt, + Type: schema.TypeInt, Computed: true, }, "cpu_sockets": { - Type: schema.TypeInt, + Type: schema.TypeInt, Computed: true, }, "cpu_cores_per_socket": { - Type: schema.TypeInt, + Type: schema.TypeInt, Computed: true, }, "cpu_threads_per_core": { - Type: schema.TypeInt, + Type: schema.TypeInt, Computed: true, }, }, @@ -76,7 +76,7 @@ func resourceLibvirtNodeInfoRead(d *schema.ResourceData, meta interface{}) error d.Set("cpu_sockets", sockets) d.Set("cpu_threads_per_core", threads) d.Set("numa_nodes", nodes) - d.Set("memory_total", memory) + d.Set("memory_total_kb", memory) d.SetId(strconv.Itoa(hashcode.String(fmt.Sprintf("%v%v%v%v%v%v%v", model, memory, cpus, nodes, sockets, cores, threads)))) return nil @@ -85,7 +85,7 @@ func resourceLibvirtNodeInfoRead(d *schema.ResourceData, meta interface{}) error func Int8ToString(bs [32]int8) string { ba := []uint8{} for _, b := range bs { - ba = append(ba, uint8(b)) + ba = append(ba, uint8(b)) } return string(ba) -} \ No newline at end of file +} diff --git a/libvirt/data_source_libvirt_nodedeviceinfo.go b/libvirt/data_source_libvirt_nodedeviceinfo.go deleted file mode 100644 index 510d6176a..000000000 --- a/libvirt/data_source_libvirt_nodedeviceinfo.go +++ /dev/null @@ -1,76 +0,0 @@ -package libvirt - -import ( - "fmt" - "log" - "strconv" - - "github.com/dmacvicar/terraform-provider-libvirt/libvirt/helper/hashcode" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" -) - -// a libvirt nodeinfo datasource -// -// Datasource example: -// -// data "libvirt_nodeinfo" "info" { -// } -// -// output "cpus" { -// value = data.libvirt_nodeinfo.info.cpus -// } -func datasourceLibvirtNodeDeviceInfo() *schema.Resource { - return &schema.Resource{ - Read: resourceLibvirtNodeDeviceInfoRead, - Schema: map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - }, - "path": { - Type: schema.TypeString, - Computed: true, - }, - "parent": { - Type: schema.TypeString, - Computed: true, - }, - "xml": { - Type: schema.TypeString, - Computed: true, - }, - }, - } -} - -func resourceLibvirtNodeDeviceInfoRead(d *schema.ResourceData, meta interface{}) error { - log.Printf("[DEBUG] Read data source libvirt_nodedevices") - - virConn := meta.(*Client).libvirt - if virConn == nil { - return fmt.Errorf(LibVirtConIsNil) - } - - var device_name string - - if name, ok := d.GetOk("name"); ok { - device_name = name.(string) - log.Printf("[DEBUG] Got name: %s", device_name) - } - - device, err := virConn.NodeDeviceLookupByName(device_name) - if err != nil { - return fmt.Errorf("failed to lookup node device: %v", err) - } - - xml, err := virConn.NodeDeviceGetXMLDesc(device.Name, 0) - if err != nil { - return fmt.Errorf("failed to get XML for node device: %v", err) - } - - d.Set("xml", xml) - //d.Set("devices", ) - d.SetId(strconv.Itoa(hashcode.String(fmt.Sprintf("%v", xml)))) - - return nil -} diff --git a/libvirt/provider.go b/libvirt/provider.go index 799438673..f7720138a 100644 --- a/libvirt/provider.go +++ b/libvirt/provider.go @@ -31,9 +31,9 @@ func Provider() *schema.Provider { "libvirt_network_dns_host_template": datasourceLibvirtNetworkDNSHostTemplate(), "libvirt_network_dns_srv_template": datasourceLibvirtNetworkDNSSRVTemplate(), "libvirt_network_dnsmasq_options_template": datasourceLibvirtNetworkDnsmasqOptionsTemplate(), - "libvirt_nodeinfo": datasourceLibvirtNodeInfo(), - "libvirt_nodedeviceinfo": datasourceLibvirtNodeDeviceInfo(), - "libvirt_nodedevices": datasourceLibvirtNodeDevices(), + "libvirt_node_info": datasourceLibvirtNodeInfo(), + "libvirt_node_device_info": datasourceLibvirtNodeDeviceInfo(), + "libvirt_node_devices": datasourceLibvirtNodeDevices(), }, ConfigureFunc: providerConfigure, From c12cade66ba40191d593f701715c10cb9eb8372b Mon Sep 17 00:00:00 2001 From: Catalin Muresan Date: Tue, 24 Oct 2023 16:46:20 +0100 Subject: [PATCH 04/16] Added documentation for the data sources --- .../data_source_libvirt_node_device_info.go | 2 +- website/docs/d/node_device_info.html.markdown | 62 +++++++++++++++++++ website/docs/d/node_devices.html.markdown | 33 ++++++++++ website/docs/d/node_info.html.markdown | 35 +++++++++++ website/docs/data_sources.html.markdown | 9 +++ website/docs/toc.html.markdown | 1 + website/libvirt.erb | 9 +++ 7 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 website/docs/d/node_device_info.html.markdown create mode 100644 website/docs/d/node_devices.html.markdown create mode 100644 website/docs/d/node_info.html.markdown create mode 100644 website/docs/data_sources.html.markdown diff --git a/libvirt/data_source_libvirt_node_device_info.go b/libvirt/data_source_libvirt_node_device_info.go index d6d01f333..cace62ba9 100644 --- a/libvirt/data_source_libvirt_node_device_info.go +++ b/libvirt/data_source_libvirt_node_device_info.go @@ -76,7 +76,7 @@ type Address struct { // data "libvirt_node_device_info" "info" { // } // -// output "cpus" { +// output "xml" { // value = data.libvirt_node_device_info.info.xml // } func datasourceLibvirtNodeDeviceInfo() *schema.Resource { diff --git a/website/docs/d/node_device_info.html.markdown b/website/docs/d/node_device_info.html.markdown new file mode 100644 index 000000000..62afab531 --- /dev/null +++ b/website/docs/d/node_device_info.html.markdown @@ -0,0 +1,62 @@ +--- +layout: "libvirt" +page_title: "Libvirt: libvirt_node_device_info" +sidebar_current: "docs-libvirt-node-device-info" +description: |- + Use this data source to get information about a specific device on the current node +--- + +# Data Source: libvirt\_node\_device\_info + +Retrieve information about a specific device on the current node + +## Example Usage + +```hcl +data "libvirt_node_device_info" "device" { + name = "pci_0000_00_00_0" +} +``` + +## Argument Reference + +* `name` - (Required) The name of the device name as expected by [libvirt](https://www.libvirt.org/manpages/virsh.html#nodedev-commands). + +## Attribute Reference + +This data source exports the following attributes in addition to the arguments above: + +* `capability` - A map of the various attributes for the device, depending on the type of device. + Currently implemented are `pci`, `storage`, `usb` + * `type` - Device type: `pci`, `storage`, `usb` + + For `pci` devices the attributes are: + * `class` - Device PCI class + * `domain` - Device PCI domain + * `bus` - Device PCI bus + * `slot` - Device PCI slot + * `function` - Device PCI function + * `product_id` - Device PCI product id + * `product_name` - Device PCI product name + * `vendor_id` - Device PCI product id + * `vendor_name` - Device PCI product name + * `iommu_group_number` - IOMMU Group number for the device + + For `storage` devices the attributes are: + * `block`: Block device name + * `drive_type`: Device drive type + * `model`: Device model + * `serial`: Device serial number + * `size`: Device size in bytes + * `logical_block_size`: Device logical block size + * `num_blocks`: Number of blocks on the device + + For `usb` devices the attributes are: + * `number`: Device number + * `class`: Device class + * `subclass`: Device subclass + * `protocol`: Device protocol + +* `parent` - The parent of this device in the hierarchy +* `path` - Full path of the device +* `xml` - The XML returned by the libvirt API call diff --git a/website/docs/d/node_devices.html.markdown b/website/docs/d/node_devices.html.markdown new file mode 100644 index 000000000..566f8568a --- /dev/null +++ b/website/docs/d/node_devices.html.markdown @@ -0,0 +1,33 @@ +--- +layout: "libvirt" +page_title: "Libvirt: libvirt_node_devices" +sidebar_current: "docs-libvirt-node-devices" +description: |- + Use this data source to get information about the devices present on current node +--- + +# Data Source: libvirt\_node\_devices + +Retrieve information about the devices present on the current node + +## Example Usage + +```hcl +data "libvirt_node_devices" "node" { + capability = "pci" +} +``` + +## Argument Reference + +* `capability` - (Optional) The type of device, used to filter the output by capability type. + Can be one of `system`, `pci`, `usb_device`, `usb`, `net`, `scsi_host`, + `scsi_target`, `scsi`, `storage`, `fc_host`, `vports`, `scsi_generic`, `drm`, + `mdev`, `mdev_types`, `ccw`, `css`, `ap_card`, `ap_queue`, `ap_matrix`. + Defaults to all active devices. + +## Attribute Reference + +This data source exports the following attributes in addition to the arguments above: + +* `devices` - A list of devices that match the selected capability diff --git a/website/docs/d/node_info.html.markdown b/website/docs/d/node_info.html.markdown new file mode 100644 index 000000000..c965e7ac3 --- /dev/null +++ b/website/docs/d/node_info.html.markdown @@ -0,0 +1,35 @@ +--- +layout: "libvirt" +page_title: "Libvirt: libvirt_node_info" +sidebar_current: "docs-libvirt-node-info" +description: |- + Use this data source to get information about the current node +--- + +# Data Source: libvirt\_node\_info + +Retrieve information about the current node + +## Example Usage + +```hcl +data "libvirt_node_info" "node" { + +} +``` + +## Argument Reference + +This data source has no arguments. + +## Attribute Reference + +This data source exports the following attributes in addition to the arguments above: + +* `cpu_cores_per_socket` - Number of CPU cores per each socket +* `cpu_cores_total` - Number of CPU cores in total +* `cpu_model` - CPU Architecture, usually `x86_64` +* `cpu_sockets` - How many CPU sockets are present +* `cpu_threads_per_core` - How many CPU Threads are available per each CPU core +* `memory_total_kb` - The amount of memory installed, in KiB +* `numa_nodes` - How many NUMA nodes/cells are available. diff --git a/website/docs/data_sources.html.markdown b/website/docs/data_sources.html.markdown new file mode 100644 index 000000000..5c1fbc25f --- /dev/null +++ b/website/docs/data_sources.html.markdown @@ -0,0 +1,9 @@ + +# Data Sources + +```eval_rst +.. toctree:: + :glob: + + d/** +``` diff --git a/website/docs/toc.html.markdown b/website/docs/toc.html.markdown index 5fe1fef79..66bf41cb6 100644 --- a/website/docs/toc.html.markdown +++ b/website/docs/toc.html.markdown @@ -11,4 +11,5 @@ :maxdepth: 2 resources + data_sources ``` diff --git a/website/libvirt.erb b/website/libvirt.erb index bdfaa8c56..3d794bc9c 100644 --- a/website/libvirt.erb +++ b/website/libvirt.erb @@ -30,6 +30,15 @@ + > + Data Sesources + + + <% end %> From 9c6e66d12436ad0faf40c2aaae79cfaaf1ac7bc6 Mon Sep 17 00:00:00 2001 From: Catalin Muresan Date: Tue, 24 Oct 2023 18:39:55 +0100 Subject: [PATCH 05/16] Added testing for data structures --- ...ta_source_libvirt_node_device_info_test.go | 40 ++++++++++++++++++ .../data_source_libvirt_node_devices_test.go | 41 +++++++++++++++++++ libvirt/data_source_libvirt_node_info_test.go | 35 ++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 libvirt/data_source_libvirt_node_device_info_test.go create mode 100644 libvirt/data_source_libvirt_node_devices_test.go create mode 100644 libvirt/data_source_libvirt_node_info_test.go diff --git a/libvirt/data_source_libvirt_node_device_info_test.go b/libvirt/data_source_libvirt_node_device_info_test.go new file mode 100644 index 000000000..287b8140a --- /dev/null +++ b/libvirt/data_source_libvirt_node_device_info_test.go @@ -0,0 +1,40 @@ +package libvirt + +import ( + "regexp" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccLibvirtNodeDeviceInfoDataSource(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceNodeDeviceInfo_pci, + Check: resource.ComposeTestCheckFunc( + resource.TestMatchResourceAttr( + "data.libvirt_node_device_info.device", "name", regexp.MustCompile(`^pci_0000_00_00_0$`)), + ), + }, { + Config: testAccDataSourceNodeDeviceInfo_system, + Check: resource.ComposeTestCheckFunc( + resource.TestMatchResourceAttr( + "data.libvirt_node_device_info.device", "name", regexp.MustCompile(`^computer$`)), + ), + }, + }, + }) +} + +const testAccDataSourceNodeDeviceInfo_pci = ` +data "libvirt_node_device_info" "device" { + name = "pci_0000_00_00_0" +}` + +const testAccDataSourceNodeDeviceInfo_system = ` +data "libvirt_node_device_info" "device" { + name = "computer" +}` diff --git a/libvirt/data_source_libvirt_node_devices_test.go b/libvirt/data_source_libvirt_node_devices_test.go new file mode 100644 index 000000000..e149bca41 --- /dev/null +++ b/libvirt/data_source_libvirt_node_devices_test.go @@ -0,0 +1,41 @@ +package libvirt + +import ( + "regexp" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccLibvirtNodeDevicesDataSource(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceNodeDevices_pci, + Check: resource.ComposeTestCheckFunc( + resource.TestMatchResourceAttr( + "data.libvirt_node_devices.node", "devices[0]", regexp.MustCompile(`^pci_\d{4}_\d{2}_\d{2}_\d{1}`)), + ), + }, + { + Config: testAccDataSourceNodeDevices_system, + Check: resource.ComposeTestCheckFunc( + resource.TestMatchResourceAttr( + "data.libvirt_node_devices.node", "devices[0]", regexp.MustCompile(`^computer$`)), + ), + }, + }, + }) +} + +const testAccDataSourceNodeDevices_pci = ` +data "libvirt_node_devices" "node" { + capability = "pci" +}` + +const testAccDataSourceNodeDevices_system = ` +data "libvirt_node_devices" "node" { + capability = "system" +}` diff --git a/libvirt/data_source_libvirt_node_info_test.go b/libvirt/data_source_libvirt_node_info_test.go new file mode 100644 index 000000000..20eed1c59 --- /dev/null +++ b/libvirt/data_source_libvirt_node_info_test.go @@ -0,0 +1,35 @@ +package libvirt + +import ( + "regexp" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" +) + +func TestAccLibvirtNodeInfoDataSource(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceNodeInfo, + Check: resource.ComposeTestCheckFunc( + resource.TestMatchResourceAttr( + "data.libvirt_node_info.info", "cpus", regexp.MustCompile(`^\d+`)), + ), + }, { + Config: testAccDataSourceNodeInfo, + Check: resource.ComposeTestCheckFunc( + resource.TestMatchResourceAttr( + "data.libvirt_node_info.info", "numa_nodes", regexp.MustCompile(`^\d+`)), + ), + }, + }, + }) +} + +const testAccDataSourceNodeInfo = ` +data "libvirt_node_info" "info" { + +}` From 8657723bb034315a93164c5961fd86b9847a1a58 Mon Sep 17 00:00:00 2001 From: Catalin Muresan Date: Tue, 24 Oct 2023 18:47:49 +0100 Subject: [PATCH 06/16] Added the missing node_devices and node_device_info --- website/libvirt.erb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/website/libvirt.erb b/website/libvirt.erb index 3d794bc9c..28d1f4e01 100644 --- a/website/libvirt.erb +++ b/website/libvirt.erb @@ -33,6 +33,12 @@ > Data Sesources > - Data Sesources + Data Resources > - Data Resources + Data Sources