package dsdssandboxes import ( "io/fs" "os" "path/filepath" "slices" "gopkg.in/yaml.v3" ) type BillingConf struct { Spec Spec `yaml:"spec"` Metadata Metadata `yaml:"metadata"` } type Metadata struct { Labels Labels `yaml:"labels"` Name string `yaml:"name"` } type Labels struct { BillingRef string `yaml:"billing-reference"` } type Spec struct { Name string `yaml:"name"` BillingAccountRef BillingAccountRef `yaml:"billingAccountRef"` } type BillingAccountRef struct { External string `yaml:"external"` } func (b *BillingConf) Load(configPath string) error { yamlFile, err := os.ReadFile(configPath) if err != nil { return err } return yaml.Unmarshal(yamlFile, b) } // Read in the YAML used to create the sandbox projects and pull out // the limited subset of information required for billing func GetBillingConfs(projectsFilePath string) (map[string]*BillingConf, error) { configDirs, err := os.ReadDir(projectsFilePath) if err != nil { return nil, err } configDirs = filterDirsOnly(configDirs) configDirs = filterNotExcluded(configDirs) return iterateConfigDirs(projectsFilePath, configDirs) } func filterDirsOnly(fileList []fs.DirEntry) (ret []fs.DirEntry) { for _, file := range fileList { if file.IsDir() { ret = append(ret, file) } } return ret } func filterNotExcluded(fileList []fs.DirEntry) (ret []fs.DirEntry) { for _, file := range fileList { if !slices.Contains(IgnoreDirectories, file.Name()) { ret = append(ret, file) } } return ret } func iterateConfigDirs(projectsDir string, fileList []fs.DirEntry) (map[string]*BillingConf, error) { ret := make(map[string]*BillingConf) for _, dir := range fileList { path := filepath.Join(projectsDir, dir.Name(), ProjectYamlFilename) conf := &BillingConf{} err := conf.Load(path) if err != nil { return nil, err } ret[conf.Metadata.Name] = conf } return ret, nil }