2023-03-15 15:30:29 +01:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
2023-03-21 13:35:16 +01:00
|
|
|
"net/netip"
|
2023-03-15 15:30:29 +01:00
|
|
|
|
2023-03-21 13:35:16 +01:00
|
|
|
"github.com/google/uuid"
|
2023-03-15 15:30:29 +01:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
2023-03-21 13:35:16 +01:00
|
|
|
// Config contains all MTD services and cloud provider configs
|
2023-03-15 15:30:29 +01:00
|
|
|
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"`
|
|
|
|
}
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
type customUUID uuid.UUID
|
|
|
|
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-15 15:30:29 +01:00
|
|
|
}
|
|
|
|
|
2023-03-21 13:35:16 +01:00
|
|
|
// LoadConf loads config from a yaml file
|
|
|
|
func LoadConf(filename string) (Config, error) {
|
2023-03-15 15:30:29 +01:00
|
|
|
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)
|
2023-03-15 15:30:29 +01:00
|
|
|
if err != nil {
|
|
|
|
return config, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|