clustvirt/cluster/builder.go

70 lines
1.6 KiB
Go
Raw Normal View History

2024-03-19 04:38:28 +00:00
package cluster
import (
"log"
"time"
"git.staur.ca/stobbsm/clustvirt/lib/host"
)
2024-03-19 04:38:28 +00:00
// 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,
2024-03-19 04:38:28 +00:00
},
}
}
// SetInterval sets the check interval of the Cluster being built.
func (c *ClusterBuilder) SetInterval(i time.Duration) *ClusterBuilder {
c.cluster.interval = i
return c
}
2024-03-19 04:38:28 +00:00
// 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.Println("warning: trying to add duplicate host")
return c
}
hc, err := host.ConnectHost(c.cluster.defaultURI, h)
if err != nil {
log.Printf("failed to connect to host: %s, %s", h, err)
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.Println("warning: trying to add duplicate host")
return c
}
hc, err := host.ConnectHost(uri, h)
if err != nil {
log.Printf("failed to connect to host: %s, %s", h, err)
return c
}
c.cluster.hosts[h] = hc
return c
}