clustvirt/cluster/builder.go
Matthew Stobbs 48bdc94351 merge barnch switch_to_templ
- Moved to templ for templating
- Started adding cluster statistics
2024-03-19 09:13:44 -06:00

70 lines
1.6 KiB
Go

package cluster
import (
"log"
"time"
"git.staur.ca/stobbsm/clustvirt/lib/host"
)
// 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.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
}