From fc20774a0e33a778af07ed8a519376d11a0986b7 Mon Sep 17 00:00:00 2001 From: schulze Date: Tue, 21 Mar 2023 13:35:16 +0100 Subject: [PATCH] prepare config for services state --- config.yaml | 15 ++++++++++++++- state/config.go | 41 ++++++++++++++++++++++++++++++++++------- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/config.yaml b/config.yaml index db71ee2..ea835fe 100644 --- a/config.yaml +++ b/config.yaml @@ -1,6 +1,19 @@ +mtd: + services: + - id: 3213a31c-59de-4e14-7648-e9508216e4bc + entry_ip: 192.168.1.1 + entry_port: 8080 + service_ip: 10.0.1.2 + service_port: 80 + - id: 1233a31c-59de-4e14-7648-e9508216e4bc + entry_ip: 192.168.1.3 + entry_port: 8081 + service_ip: 10.0.1.4 + service_port: 81 + aws: regions: - us-east-1 - - us-east-2 +# - us-east-2 - us-west-1 credentials_path: ./mtd_aws/.credentials \ No newline at end of file diff --git a/state/config.go b/state/config.go index a470103..174f0db 100644 --- a/state/config.go +++ b/state/config.go @@ -2,19 +2,46 @@ package state import ( "io/ioutil" + "net/netip" + "github.com/google/uuid" "gopkg.in/yaml.v3" ) - +// Config contains all MTD services and cloud provider configs type Config struct { - AWS struct { - Regions []string `yaml:"regions"` - Credentials_path string `yaml:"credentials_path"` - } `yaml:"aws"` + MTD mtdconf `yaml:"mtd"` + AWS aws `yaml:"aws"` } -func Load_conf(filename string) (Config, error) { +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 +} + +// LoadConf loads config from a yaml file +func LoadConf(filename string) (Config, error) { var config Config data, err := ioutil.ReadFile(filename) @@ -22,7 +49,7 @@ func Load_conf(filename string) (Config, error) { return config, err } - err = yaml.Unmarshal(data, &config) + err = yaml.Unmarshal([]byte(data), &config) if err != nil { return config, err }