clustvirt/cluster/builder.go
Matthew Stobbs ffca8c3970 done migrating host module to libvirtxml
- need to add accessor methods
- eventually will move the VMList to the local guest module
2024-03-21 11:15:23 -06:00

80 lines
1.8 KiB
Go

package cluster
import (
"time"
"git.staur.ca/stobbsm/clustvirt/lib/host"
"git.staur.ca/stobbsm/clustvirt/lib/log"
)
// ClusterBuilder is used to build a Cluster object, which can then be used
type ClusterBuilder struct {
cluster *Cluster
}
// New starts the builder pattern for the Cluster.
// Sets the default interval time to 30 seconds.
func New() *ClusterBuilder {
return &ClusterBuilder{
cluster: &Cluster{
interval: time.Duration(time.Second * 30),
hosts: map[string]*host.Host{},
defaultURI: host.URI_QEMU_SSH_SYSTEM,
},
}
}
// SetInterval sets the check interval of the Cluster being built.
func (c *ClusterBuilder) SetInterval(i time.Duration) *ClusterBuilder {
c.cluster.interval = i
return c
}
// Build retuns the built cluster
func (c *ClusterBuilder) Build() *Cluster {
return c.cluster
}
func (c *ClusterBuilder) DefaultHostURI(uri *host.URI) *ClusterBuilder {
c.cluster.defaultURI = uri
return c
}
func (c *ClusterBuilder) AddHost(h string) *ClusterBuilder {
if _, ok := c.cluster.hosts[h]; ok {
log.Warn("cluster.AddHost").
Str("hostname", h).
Msg("won't overwrite existing host")
return c
}
hc, err := host.ConnectHost(c.cluster.defaultURI, h)
if err != nil {
log.Error("cluster.AddHost").
Str("hostname", h).
Err(err).
Msg("failed to connect to host")
return c
}
c.cluster.hosts[h] = hc
return c
}
func (c *ClusterBuilder) AddHostWithURI(h string, uri *host.URI) *ClusterBuilder {
if _, ok := c.cluster.hosts[h]; ok {
log.Warn("cluster.AddHostWithURI").
Str("hostname", h).
Msg("won't overwrite existing host")
return c
}
hc, err := host.ConnectHost(uri, h)
if err != nil {
log.Error("cluster.AddHostWithURI").
Str("hostname", h).
Err(err).
Msg("failed to connect to host")
return c
}
c.cluster.hosts[h] = hc
return c
}