pkg/localconfig/localconfig.go

83 lines
1.2 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 (
"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 06:22:01 +00:00
Clusters []*Cluster
Hosts []*Host
}
type Cluster struct {
Name string
Addons []byte
}
type Host struct {
Name string
MACs []string
IPs []string
Kernel string
Initrd string
Layers map[string]string
Config []byte
}
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)
}
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 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
}