polemos/state/config.go

61 lines
1.3 KiB
Go
Raw Normal View History

package state
import (
"io/ioutil"
2023-03-21 13:35:16 +01:00
"net/netip"
2023-03-21 13:35:16 +01:00
"github.com/google/uuid"
"gopkg.in/yaml.v3"
)
2023-03-21 13:35:16 +01:00
// Config contains all MTD services and cloud provider configs
type Config struct {
2023-03-21 13:35:16 +01:00
MTD mtdconf `yaml:"mtd"`
AWS aws `yaml:"aws"`
}
type mtdconf struct {
Services []service `yaml:"services"`
}
2023-03-22 10:39:22 +01:00
2023-03-21 13:35:16 +01:00
type service struct {
ID customUUID `yaml:"id"`
ServiceID string `yaml:"cloud_id"`
EntryIP netip.Addr `yaml:"entry_ip"`
EntryPort uint16 `yaml:"entry_port"`
ServiceIP netip.Addr `yaml:"service_ip"`
ServicePort uint16 `yaml:"service_port"`
}
2023-03-22 10:39:22 +01:00
2023-03-21 13:35:16 +01:00
type customUUID uuid.UUID
2023-03-22 10:39:22 +01:00
2023-03-21 13:35:16 +01:00
type aws struct {
Regions []string `yaml:"regions"`
CredentialsPath string `yaml:"credentials_path"`
}
func (u *customUUID) UnmarshalYAML(value *yaml.Node) error {
id, err := uuid.Parse(value.Value)
if err != nil {
return err
}
*u = customUUID(id)
return nil
}
2023-03-21 13:35:16 +01:00
// LoadConf loads config from a yaml file
func LoadConf(filename string) (Config, error) {
var config Config
data, err := ioutil.ReadFile(filename)
if err != nil {
return config, err
}
2023-03-21 13:35:16 +01:00
err = yaml.Unmarshal([]byte(data), &config)
if err != nil {
return config, err
}
return config, nil
}