Commit 6891d510 authored by Daniel P. Berrange's avatar Daniel P. Berrange

Stop wrapping pointers for basic vir*Info structs

Instead of wrapping the underlying C info struct, and
providing getters, add explicit struct fields in the
Go struct for each C field.
Signed-off-by: 's avatarDaniel P. Berrange <berrange@redhat.com>
parent bd42aabd
......@@ -222,6 +222,17 @@ type VirConnection struct {
ptr C.virConnectPtr
}
type VirNodeInfo struct {
Model string
Memory uint64
Cpus uint
MHz uint
Nodes uint32
Sockets uint32
Cores uint32
Threads uint32
}
// Additional data associated to the connection.
type virConnectionData struct {
errCallbackId *int
......@@ -404,12 +415,25 @@ func (c *VirConnection) GetCapabilities() (string, error) {
}
func (c *VirConnection) GetNodeInfo() (*VirNodeInfo, error) {
var ptr C.virNodeInfo
result := C.virNodeGetInfo(c.ptr, (*C.virNodeInfo)(unsafe.Pointer(&ptr)))
var cinfo C.virNodeInfo
result := C.virNodeGetInfo(c.ptr, &cinfo)
if result == -1 {
return nil, GetLastError()
}
return &VirNodeInfo{ptr: ptr}, nil
return &VirNodeInfo{
Model: C.GoString((*C.char)(unsafe.Pointer(&cinfo.model[0]))),
Memory: uint64(cinfo.memory),
Cpus: uint(cinfo.cpus),
MHz: uint(cinfo.mhz),
Nodes: uint32(cinfo.nodes),
Sockets: uint32(cinfo.sockets),
Cores: uint32(cinfo.cores),
Threads: uint32(cinfo.threads),
}, nil
}
func (ni *VirNodeInfo) GetMaxCPUs() uint32 {
return ni.Nodes * ni.Sockets * ni.Cores * ni.Threads
}
func (c *VirConnection) GetHostname() (string, error) {
......
......@@ -166,7 +166,7 @@ func TestGetNodeInfo(t *testing.T) {
t.Error(err)
return
}
if ni.GetModel() != "i686" {
if ni.Model != "i686" {
t.Error("Expected i686 model in test transport")
return
}
......
......@@ -867,11 +867,17 @@ const (
)
type VirDomainBlockInfo struct {
ptr C.virDomainBlockInfo
Capacity uint64
Allocation uint64
Physical uint64
}
type VirDomainInfo struct {
ptr C.virDomainInfo
State VirDomainState
MaxMem uint64
Memory uint64
NrVirtCpu uint
CpuTime uint64
}
type VirDomainMemoryStat struct {
......@@ -1009,27 +1015,19 @@ func (d *VirDomain) GetAutostart() (bool, error) {
}
func (d *VirDomain) GetBlockInfo(disk string, flag uint) (*VirDomainBlockInfo, error) {
var ptr C.virDomainBlockInfo
var cinfo C.virDomainBlockInfo
cDisk := C.CString(disk)
defer C.free(unsafe.Pointer(cDisk))
result := C.virDomainGetBlockInfo(d.ptr, cDisk, (*C.virDomainBlockInfo)(unsafe.Pointer(&ptr)), C.uint(flag))
result := C.virDomainGetBlockInfo(d.ptr, cDisk, &cinfo, C.uint(flag))
if result == -1 {
return nil, GetLastError()
}
return &VirDomainBlockInfo{ptr: ptr}, nil
}
func (b *VirDomainBlockInfo) Allocation() uint64 {
return uint64(b.ptr.allocation)
}
func (b *VirDomainBlockInfo) Capacity() uint64 {
return uint64(b.ptr.capacity)
}
func (b *VirDomainBlockInfo) Physical() uint64 {
return uint64(b.ptr.physical)
return &VirDomainBlockInfo{
Capacity: uint64(cinfo.capacity),
Allocation: uint64(cinfo.allocation),
Physical: uint64(cinfo.physical),
}, nil
}
func (d *VirDomain) GetName() (string, error) {
......@@ -1082,12 +1080,18 @@ func (d *VirDomain) GetUUIDString() (string, error) {
}
func (d *VirDomain) GetInfo() (*VirDomainInfo, error) {
var ptr C.virDomainInfo
result := C.virDomainGetInfo(d.ptr, (*C.virDomainInfo)(unsafe.Pointer(&ptr)))
var cinfo C.virDomainInfo
result := C.virDomainGetInfo(d.ptr, &cinfo)
if result == -1 {
return nil, GetLastError()
}
return &VirDomainInfo{ptr: ptr}, nil
return &VirDomainInfo{
State: VirDomainState(cinfo.state),
MaxMem: uint64(cinfo.maxMem),
Memory: uint64(cinfo.memory),
NrVirtCpu: uint(cinfo.nrVirtCpu),
CpuTime: uint64(cinfo.cpuTime),
}, nil
}
func (d *VirDomain) GetXMLDesc(flags uint32) (string, error) {
......@@ -1100,26 +1104,6 @@ func (d *VirDomain) GetXMLDesc(flags uint32) (string, error) {
return xml, nil
}
func (i *VirDomainInfo) GetState() uint8 {
return uint8(i.ptr.state)
}
func (i *VirDomainInfo) GetMaxMem() uint64 {
return uint64(i.ptr.maxMem)
}
func (i *VirDomainInfo) GetMemory() uint64 {
return uint64(i.ptr.memory)
}
func (i *VirDomainInfo) GetNrVirtCpu() uint16 {
return uint16(i.ptr.nrVirtCpu)
}
func (i *VirDomainInfo) GetCpuTime() uint64 {
return uint64(i.ptr.cpuTime)
}
type VirDomainCPUStats struct {
CpuTimeSet bool
CpuTime uint64
......
......@@ -82,6 +82,18 @@ type VirNetwork struct {
ptr C.virNetworkPtr
}
type VirNetworkDHCPLease struct {
Iface string
ExpiryTime time.Time
Type VirIPAddrType
Mac string
Iaid string
IPaddr string
Prefix uint
Hostname string
Clientid string
}
func (n *VirNetwork) Free() error {
if result := C.virNetworkFree(n.ptr); result != 0 {
return GetLastError()
......@@ -235,45 +247,20 @@ func (n *VirNetwork) GetDHCPLeases() ([]VirNetworkDHCPLease, error) {
}
var leases []VirNetworkDHCPLease
slice := *(*[]C.virNetworkDHCPLeasePtr)(unsafe.Pointer(&hdr))
for _, ptr := range slice {
leases = append(leases, VirNetworkDHCPLease{ptr})
for _, clease := range slice {
leases = append(leases, VirNetworkDHCPLease{
Iface: C.GoString(clease.iface),
ExpiryTime: time.Unix(int64(clease.expirytime), 0),
Type: VirIPAddrType(clease._type),
Mac: C.GoString(clease.mac),
Iaid: C.GoString(clease.iaid),
IPaddr: C.GoString(clease.ipaddr),
Prefix: uint(clease.prefix),
Hostname: C.GoString(clease.hostname),
Clientid: C.GoString(clease.clientid),
})
C.virNetworkDHCPLeaseFree(clease)
}
C.free(unsafe.Pointer(cLeases))
return leases, nil
}
type VirNetworkDHCPLease struct {
ptr C.virNetworkDHCPLeasePtr
}
func (l *VirNetworkDHCPLease) Free() {
C.virNetworkDHCPLeaseFree(l.ptr)
}
func (l *VirNetworkDHCPLease) GetIface() string {
return C.GoString(l.ptr.iface)
}
func (l *VirNetworkDHCPLease) GetExpiryTime() time.Time {
return time.Unix(int64(l.ptr.expirytime), 0)
}
func (l *VirNetworkDHCPLease) GetMACAddress() string {
return C.GoString(l.ptr.mac)
}
func (l *VirNetworkDHCPLease) GetIPAddress() string {
return C.GoString(l.ptr.ipaddr)
}
func (l *VirNetworkDHCPLease) GetIPPrefix() uint {
return uint(l.ptr.prefix)
}
func (l *VirNetworkDHCPLease) GetHostname() string {
return C.GoString(l.ptr.hostname)
}
func (l *VirNetworkDHCPLease) GetClientID() string {
return C.GoString(l.ptr.clientid)
}
package libvirt
/*
#cgo LDFLAGS: -lvirt
#include <libvirt/libvirt.h>
#include <libvirt/virterror.h>
#include <stdlib.h>
*/
import "C"
import (
"unsafe"
)
type VirNodeInfo struct {
ptr C.virNodeInfo
}
func (ni *VirNodeInfo) GetModel() string {
model := C.GoString((*C.char)(unsafe.Pointer(&ni.ptr.model)))
return model
}
func (ni *VirNodeInfo) GetMemoryKB() uint64 {
return uint64(ni.ptr.memory)
}
func (ni *VirNodeInfo) GetCPUs() uint32 {
return uint32(ni.ptr.cpus)
}
func (ni *VirNodeInfo) GetMhz() uint32 {
return uint32(ni.ptr.mhz)
}
func (ni *VirNodeInfo) GetNodes() uint32 {
return uint32(ni.ptr.nodes)
}
func (ni *VirNodeInfo) GetSockets() uint32 {
return uint32(ni.ptr.sockets)
}
func (ni *VirNodeInfo) GetCores() uint32 {
return uint32(ni.ptr.cores)
}
func (ni *VirNodeInfo) GetThreads() uint32 {
return uint32(ni.ptr.threads)
}
// libvirt.h: VIR_NODEINFO_MAXCPUS
func (ni *VirNodeInfo) GetMaxCPUs() uint32 {
return ni.GetNodes() * ni.GetSockets() * ni.GetCores() * ni.GetThreads()
}
......@@ -70,7 +70,10 @@ type VirStoragePool struct {
}
type VirStoragePoolInfo struct {
ptr C.virStoragePoolInfo
State VirStoragePoolState
Capacity uint64
Allocation uint64
Available uint64
}
func (p *VirStoragePool) Build(flags VirStoragePoolBuildFlags) error {
......@@ -127,12 +130,17 @@ func (p *VirStoragePool) GetAutostart() (bool, error) {
}
func (p *VirStoragePool) GetInfo() (*VirStoragePoolInfo, error) {
var ptr C.virStoragePoolInfo
result := C.virStoragePoolGetInfo(p.ptr, (*C.virStoragePoolInfo)(unsafe.Pointer(&ptr)))
var cinfo C.virStoragePoolInfo
result := C.virStoragePoolGetInfo(p.ptr, &cinfo)
if result == -1 {
return nil, GetLastError()
}
return &VirStoragePoolInfo{ptr: ptr}, nil
return &VirStoragePoolInfo{
State: VirStoragePoolState(cinfo.state),
Capacity: uint64(cinfo.capacity),
Allocation: uint64(cinfo.allocation),
Available: uint64(cinfo.available),
}, nil
}
func (p *VirStoragePool) GetName() (string, error) {
......@@ -226,22 +234,6 @@ func (p *VirStoragePool) Undefine() error {
return nil
}
func (i *VirStoragePoolInfo) GetState() VirStoragePoolState {
return VirStoragePoolState(i.ptr.state)
}
func (i *VirStoragePoolInfo) GetCapacityInBytes() uint64 {
return uint64(i.ptr.capacity)
}
func (i *VirStoragePoolInfo) GetAllocationInBytes() uint64 {
return uint64(i.ptr.allocation)
}
func (i *VirStoragePoolInfo) GetAvailableInBytes() uint64 {
return uint64(i.ptr.available)
}
func (p *VirStoragePool) StorageVolCreateXML(xmlConfig string, flags uint32) (*VirStorageVol, error) {
cXml := C.CString(string(xmlConfig))
defer C.free(unsafe.Pointer(cXml))
......
......@@ -164,12 +164,12 @@ func TestCreateDestroyStoragePool(t *testing.T) {
t.Error(err)
return
}
state, err := pool.GetInfo()
info, err := pool.GetInfo()
if err != nil {
t.Error(err)
return
}
if state.GetState() != VIR_STORAGE_POOL_RUNNING {
if info.State != VIR_STORAGE_POOL_RUNNING {
t.Fatal("Storage pool should be running")
}
if err = pool.Destroy(); err != nil {
......@@ -177,12 +177,12 @@ func TestCreateDestroyStoragePool(t *testing.T) {
return
}
state, err = pool.GetInfo()
info, err = pool.GetInfo()
if err != nil {
t.Error(err)
return
}
if state.GetState() != VIR_STORAGE_POOL_INACTIVE {
if info.State != VIR_STORAGE_POOL_INACTIVE {
t.Fatal("Storage pool should be inactive")
}
}
......
......@@ -72,7 +72,9 @@ type VirStorageVol struct {
}
type VirStorageVolInfo struct {
ptr C.virStorageVolInfo
Type VirStorageVolType
Capacity uint64
Allocation uint64
}
func (v *VirStorageVol) Delete(flags VirStorageVolDeleteFlags) error {
......@@ -91,24 +93,16 @@ func (v *VirStorageVol) Free() error {
}
func (v *VirStorageVol) GetInfo() (*VirStorageVolInfo, error) {
var ptr C.virStorageVolInfo
result := C.virStorageVolGetInfo(v.ptr, (*C.virStorageVolInfo)(unsafe.Pointer(&ptr)))
var cinfo C.virStorageVolInfo
result := C.virStorageVolGetInfo(v.ptr, &cinfo)
if result == -1 {
return nil, GetLastError()
}
return &VirStorageVolInfo{ptr: ptr}, nil
}
func (i *VirStorageVolInfo) GetType() VirStorageVolType {
return VirStorageVolType(i.ptr._type)
}
func (i *VirStorageVolInfo) GetCapacityInBytes() uint64 {
return uint64(i.ptr.capacity)
}
func (i *VirStorageVolInfo) GetAllocationInBytes() uint64 {
return uint64(i.ptr.allocation)
return &VirStorageVolInfo{
Type: VirStorageVolType(cinfo._type),
Capacity: uint64(cinfo.capacity),
Allocation: uint64(cinfo.allocation),
}, nil
}
func (v *VirStorageVol) GetKey() (string, error) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment