clustvirt/domains.go
Matthew Stobbs 4ecf05bf82 Adding basic wrappers
- Wrapper for Host connection called Host
  - Allows for easier usage of the API
- Wrapper for Domains called GuestVM, which pulls VM information and allows
  other functionality in a better way
2024-03-10 20:32:11 -06:00

54 lines
961 B
Go

package main
import (
"fmt"
"log"
"libvirt.org/go/libvirt"
)
// GuestVM holds a handle is used to communicate to Domains
type GuestVM struct {
Name string
ID uint
BackupXML string
close chan struct{}
closeErr chan error
dom *libvirt.Domain
}
// GetGuest loads guest information by name and returns it
func GetGuest(name string, conn *libvirt.Connect) (*GuestVM, error) {
g := &GuestVM{
Name: name,
close: make(chan struct{}),
closeErr: make(chan error),
}
var err error
if g.dom, err = conn.LookupDomainByName(name); err != nil {
return nil, err
}
if g.ID, err = g.dom.GetID(); err != nil {
return nil, err
}
if g.BackupXML, err = g.dom.BackupGetXMLDesc(0); err != nil {
return nil, err
}
go func() {
defer close(g.closeErr)
<-g.close
g.closeErr <- g.dom.Free()
}()
return g, nil
}
// Close closes an open connection
func (g *GuestVM) Close() error {
close(g.close)
return <-g.closeErr
}