@@ -2,13 +2,39 @@ package fileconfiguration
22
33import (
44 "fmt"
5- "gopkg.in/yaml.v3"
65 "os"
76 "path/filepath"
87
8+ "gopkg.in/yaml.v3"
9+
910 "github.com/ionos-cloud/sdk-go-bundle/shared"
1011)
1112
13+ // Usage:
14+ // // Load from default file
15+ // cfg, err := fileconfiguration.New("")
16+ // // From explicit path:
17+ // cfg, err := fileconfiguration.New("/path/to/config")
18+ // // From env:
19+ // cfg, err := fileconfiguration.NewFromEnv()
20+ //
21+ // // Read profiles list:
22+ // profiles := fileconfiguration.ReadProfilesFromFile()
23+ // // Switch/get current profile:
24+ // prof := cfg.GetCurrentProfile()
25+ // // Get environment names:
26+ // envs := cfg.GetEnvironmentNames()
27+ // // Get profile names:
28+ // profs := cfg.GetProfileNames()
29+ //
30+ // // Get endpoint for regionless API:
31+ // ep := cfg.GetProductOverrides("psql")
32+ // // Get endpoint for region-specific API:
33+ // ep := cfg.GetProductLocationOverrides("dns", "de/fra")
34+ // // Get endpoint for region-specific API with fallback to global endpoint: (combine the two funcs above)
35+ // ep := cfg.GetOverride("dns", "de/fra")
36+ // ep := cfg.GetOverride("psql", "")
37+
1238// products that do not have a location and will override the endpoint that is used globally
1339const (
1440 Autoscaling = "autoscaling"
@@ -169,19 +195,51 @@ func NewFromEnv() (*FileConfig, error) {
169195 return New (os .Getenv (shared .IonosFilePathEnvVar ))
170196}
171197
198+ // GetProfileNames returns a list of profile names from the loaded configuration
199+ func (f * FileConfig ) GetProfileNames () []string {
200+ names := make ([]string , len (f .Profiles ))
201+ for i , p := range f .Profiles {
202+ names [i ] = p .Name
203+ }
204+ return names
205+ }
206+
207+ // GetEnvironmentNames returns a list of environment names from the loaded configuration
208+ func (f * FileConfig ) GetEnvironmentNames () []string {
209+ names := make ([]string , len (f .Environments ))
210+ for i , e := range f .Environments {
211+ names [i ] = e .Name
212+ }
213+ return names
214+ }
215+
216+ // GetOverride returns the endpoint for a specific product and location
217+ // with fallback to the global endpoint if no location is found.
218+ //
219+ // It is a helper function combining GetProductLocationOverrides and GetProductOverrides
220+ func (f * FileConfig ) GetOverride (productName , location string ) * Endpoint {
221+ if locEp := f .GetProductLocationOverrides (productName , location ); locEp != nil {
222+ return locEp
223+ }
224+ if prod := f .GetProductOverrides (productName ); prod != nil && len (prod .Endpoints ) > 0 {
225+ return & prod .Endpoints [0 ]
226+ }
227+ return nil
228+ }
229+
172230// GetCurrentProfile returns the current profile from the loaded configuration
173231// if the current profile is not set, it returns nil
174232// if the current profile is set and found in the loaded configuration, it returns the profile
175- func (fileConfig * FileConfig ) GetCurrentProfile () * Profile {
233+ func (f * FileConfig ) GetCurrentProfile () * Profile {
176234 currentProfile := os .Getenv (shared .IonosCurrentProfileEnvVar )
177235 if currentProfile == "" {
178- currentProfile = fileConfig .CurrentProfile
236+ currentProfile = f .CurrentProfile
179237 }
180238 if currentProfile == "" {
181239 shared .SdkLogger .Printf ("[WARN] no current profile set" )
182240 return nil
183241 }
184- for _ , profile := range fileConfig .Profiles {
242+ for _ , profile := range f .Profiles {
185243 if profile .Name == currentProfile {
186244 if shared .SdkLogLevel .Satisfies (shared .Debug ) {
187245 shared .SdkLogger .Printf ("[DEBUG] using profile %s" , profile .Name )
@@ -195,20 +253,20 @@ func (fileConfig *FileConfig) GetCurrentProfile() *Profile {
195253 return nil
196254}
197255
198- func (fileConfig * FileConfig ) GetEnvForCurrentProfile () string {
199- if fileConfig == nil {
256+ func (f * FileConfig ) GetEnvForCurrentProfile () string {
257+ if f == nil {
200258 return ""
201259 }
202- if currentProfile := fileConfig .GetCurrentProfile (); currentProfile != nil {
260+ if currentProfile := f .GetCurrentProfile (); currentProfile != nil {
203261 return currentProfile .Environment
204262 }
205263 return ""
206264}
207265
208266// GetProductOverrides returns the overrides for a specific product for the current environment
209267// if no current environment is found, the first environment is used for the product that matches productName is returned
210- func (fileConfig * FileConfig ) GetProductOverrides (productName string ) * Product {
211- if fileConfig == nil {
268+ func (f * FileConfig ) GetProductOverrides (productName string ) * Product {
269+ if f == nil {
212270 return nil
213271 }
214272 if productName == "" {
@@ -217,8 +275,8 @@ func (fileConfig *FileConfig) GetProductOverrides(productName string) *Product {
217275 }
218276 return nil
219277 }
220- currentEnv := fileConfig .GetEnvForCurrentProfile ()
221- for _ , environment := range fileConfig .Environments {
278+ currentEnv := f .GetEnvForCurrentProfile ()
279+ for _ , environment := range f .Environments {
222280 if currentEnv != "" && environment .Name != currentEnv {
223281 continue
224282 }
@@ -235,11 +293,11 @@ func (fileConfig *FileConfig) GetProductOverrides(productName string) *Product {
235293}
236294
237295// GetProductLocationOverrides returns the overrides for a specific product and location for the current environment
238- func (fileConfig * FileConfig ) GetProductLocationOverrides (productName , location string ) * Endpoint {
239- if fileConfig == nil {
296+ func (f * FileConfig ) GetProductLocationOverrides (productName , location string ) * Endpoint {
297+ if f == nil {
240298 return nil
241299 }
242- product := fileConfig .GetProductOverrides (productName )
300+ product := f .GetProductOverrides (productName )
243301 if product == nil || len (product .Endpoints ) == 0 {
244302 return nil
245303 }
0 commit comments