pkg/localconfig/localconfig.go

96 lines
1.5 KiB
Go
Raw Normal View History

2018-12-10 06:22:01 +00:00
package localconfig
2018-12-10 06:34:40 +00:00
import (
2018-12-10 10:52:14 +00:00
"io"
2018-12-10 06:34:40 +00:00
"io/ioutil"
"strings"
2018-12-10 06:22:01 +00:00
2018-12-10 06:34:40 +00:00
yaml "gopkg.in/yaml.v2"
)
type Config struct {
2018-12-10 10:52:14 +00:00
Clusters []*Cluster
Hosts []*Host
SSLConfig string
2018-12-10 06:22:01 +00:00
}
type Cluster struct {
2019-10-09 05:40:56 +00:00
Name string
Addons string
BootstrapPods string
2018-12-10 06:22:01 +00:00
}
2018-12-10 06:34:40 +00:00
func FromBytes(data []byte) (*Config, error) {
config := &Config{}
if err := yaml.Unmarshal(data, config); err != nil {
return nil, err
}
return config, nil
}
func FromFile(path string) (*Config, error) {
ba, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
return FromBytes(ba)
}
2018-12-10 10:52:14 +00:00
func (c *Config) WriteTo(w io.Writer) error {
return yaml.NewEncoder(w).Encode(c)
}
func (c *Config) Cluster(name string) *Cluster {
for _, cluster := range c.Clusters {
if cluster.Name == name {
return cluster
}
}
return nil
}
2018-12-10 06:34:40 +00:00
func (c *Config) ClusterByName(name string) *Cluster {
2018-12-10 06:22:01 +00:00
for _, cluster := range c.Clusters {
if cluster.Name == name {
return cluster
}
}
return nil
}
2018-12-10 10:52:14 +00:00
func (c *Config) Host(name string) *Host {
for _, host := range c.Hosts {
if host.Name == name {
return host
}
}
return nil
}
2018-12-10 06:34:40 +00:00
func (c *Config) HostByIP(ip string) *Host {
2018-12-10 06:22:01 +00:00
for _, host := range c.Hosts {
for _, hostIP := range host.IPs {
if hostIP == ip {
return host
}
}
}
return nil
}
2018-12-10 06:34:40 +00:00
func (c *Config) HostByMAC(mac string) *Host {
2018-12-10 06:22:01 +00:00
// a bit of normalization
mac = strings.Replace(strings.ToLower(mac), "-", ":", -1)
for _, host := range c.Hosts {
for _, hostMAC := range host.MACs {
if strings.ToLower(hostMAC) == mac {
return host
}
}
}
return nil
}