virt: Avoid parsing URI all the time

Signed-off-by: Vinzenz Feenstra <vfeenstr@redhat.com>
This commit is contained in:
Vinzenz Feenstra
2016-11-03 22:54:07 +01:00
parent 12f7d0a516
commit de5ada30b7

View File

@@ -40,6 +40,13 @@ var (
libvirtInitialized = false libvirtInitialized = false
) )
type virtURISchemeType int
const (
defaultURI virtURISchemeType = iota
lxcURI
)
// VirtRes is a libvirt resource. A transient virt resource, which has its state // VirtRes is a libvirt resource. A transient virt resource, which has its state
// set to `shutoff` is one which does not exist. The parallel equivalent is a // set to `shutoff` is one which does not exist. The parallel equivalent is a
// file resource which removes a particular path. // file resource which removes a particular path.
@@ -50,15 +57,16 @@ type VirtRes struct {
Transient bool `yaml:"transient"` // defined (false) or undefined (true) Transient bool `yaml:"transient"` // defined (false) or undefined (true)
CPUs uint16 `yaml:"cpus"` CPUs uint16 `yaml:"cpus"`
Memory uint64 `yaml:"memory"` // in KBytes Memory uint64 `yaml:"memory"` // in KBytes
OSInit string `yaml:"osinit"` // init used by lxc OSInit string `yaml:"osinit"` // init used by lxc
Boot []string `yaml:"boot"` // boot order. values: fd, hd, cdrom, network Boot []string `yaml:"boot"` // boot order. values: fd, hd, cdrom, network
Disk []diskDevice `yaml:"disk"` Disk []diskDevice `yaml:"disk"`
CDRom []cdRomDevice `yaml:"cdrom"` CDRom []cdRomDevice `yaml:"cdrom"`
Network []networkDevice `yaml:"network"` Network []networkDevice `yaml:"network"`
Filesystem []filesystemDevice `yaml:"filesystem"` Filesystem []filesystemDevice `yaml:"filesystem"`
conn libvirt.VirConnection conn libvirt.VirConnection
absent bool // cached state absent bool // cached state
uriScheme virtURISchemeType
} }
// NewVirtRes is a constructor for this resource. It also calls Init() for you. // NewVirtRes is a constructor for this resource. It also calls Init() for you.
@@ -85,6 +93,14 @@ func (obj *VirtRes) Init() error {
} }
libvirtInitialized = true libvirtInitialized = true
} }
if u, err := url.Parse(obj.URI); err != nil {
return fmt.Errorf("%s[%s]: Parsing URI failed: %s Error: %s", obj.Kind(), obj.GetName(), obj.URI, err.Error())
} else {
switch u.Scheme {
case "lxc":
obj.uriScheme = lxcURI
}
}
obj.absent = (obj.Transient && obj.State == "shutoff") // machine shouldn't exist obj.absent = (obj.Transient && obj.State == "shutoff") // machine shouldn't exist
@@ -511,14 +527,9 @@ func (obj *VirtRes) CheckApply(apply bool) (bool, error) {
} }
// Return the correct domain type based on the uri // Return the correct domain type based on the uri
func (obj *VirtRes) getDomainType() string { func (obj VirtRes) getDomainType() string {
u, err := url.Parse(obj.URI) switch obj.uriScheme {
if err != nil { case lxcURI:
log.Printf("%s[%s]: Parsing URI failed: %s", obj.Kind(), obj.GetName(),
obj.URI)
}
switch u.Scheme {
case "lxc":
return "<domain type='lxc'>" return "<domain type='lxc'>"
default: default:
return "<domain type='kvm'>" return "<domain type='kvm'>"
@@ -526,28 +537,18 @@ func (obj *VirtRes) getDomainType() string {
} }
// Return the correct os type based on the uri // Return the correct os type based on the uri
func (obj *VirtRes) getOSType() string { func (obj VirtRes) getOSType() string {
u, err := url.Parse(obj.URI) switch obj.uriScheme {
if err != nil { case lxcURI:
log.Printf("%s[%s]: Parsing URI failed: %s", obj.Kind(), obj.GetName(),
obj.URI)
}
switch u.Scheme {
case "lxc":
return "<type>exe</type>" return "<type>exe</type>"
default: default:
return "<type>hvm</type>" return "<type>hvm</type>"
} }
} }
func (obj *VirtRes) getOSInit() string { func (obj VirtRes) getOSInit() string {
u, err := url.Parse(obj.URI) switch obj.uriScheme {
if err != nil { case lxcURI:
log.Printf("%s[%s]: Parsing URI failed: %s", obj.Kind(), obj.GetName(),
obj.URI)
}
switch u.Scheme {
case "lxc":
return fmt.Sprintf("<init>%s</init>", obj.OSInit) return fmt.Sprintf("<init>%s</init>", obj.OSInit)
default: default:
return "" return ""