@@ -11,37 +11,50 @@ import (
1111 "path/filepath"
1212)
1313
14+ type ClientOverrideOptions struct {
15+ Endpoint string
16+ SkipTLSVerify bool
17+ Certificate string
18+ Credentials Credentials
19+ }
20+
21+ type Credentials struct {
22+ Username string `yaml:"username"`
23+ Password string `yaml:"password"`
24+ Token string `yaml:"token"`
25+ }
26+
27+ type Product struct {
28+ // Name is the name of the product
29+ Name string `yaml:"name"`
30+ // Endpoint is the endpoint that will be overridden
31+ Endpoint string `yaml:"endpoint"`
32+ // SkipTLSVerify skips tls verification. Not recommended for production
33+ SkipTLSVerify bool `yaml:"skipTlsVerify"`
34+ }
35+
1436// Location is a struct that represents a location
1537type Location struct {
1638 Name string `yaml:"name"`
1739 // CertificateAuthData
1840 CertificateAuthData string `yaml:"certificateAuthData,omitempty"`
19- // Products is a list of ionos products for which we will override location
20- Products []struct {
21- // Name is the name of the product
22- Name string `yaml:"name"`
23- // Endpoint is the endpoint that will be overridden
24- Endpoint string `yaml:"endpoint"`
25- // SkipTLSVerify skips tls verification. Not recommended for production
26- SkipTLSVerify bool `yaml:"skipTlsVerify"`
27- } `yaml:"products"`
41+ // Products is a list of ionos products for which we will override endpoint, tls verification
42+ Products []Product `yaml:"products"`
2843}
2944
3045// Profiles wrapper to read only the profiles from the config file
3146type Profiles struct {
47+ // CurrentProfile active profile for configuration
48+ CurrentProfile string `yaml:"currentProfile"`
3249 // Profiles
3350 Profiles []Profile `yaml:"profiles"`
3451}
3552
36- // Profile is a struct that represents a profile and it's credentials
53+ // Profile is a struct that represents a profile and it's Credentials
3754type Profile struct {
3855 Name string `yaml:"name"`
3956 Location string `yaml:"location"`
40- Credentials struct {
41- Username string `yaml:"username"`
42- Password string `yaml:"password"`
43- Token string `yaml:"token"`
44- } `yaml:"credentials,omitempty"`
57+ Credentials Credentials
4558}
4659
4760// LoadedConfig is a struct that represents the loaded configuration
@@ -128,16 +141,16 @@ func ReadConfigFromFile() (*LoadedConfig, error) {
128141 return loadedConfig , nil
129142}
130143
131- func getCurrentProfile ( lc LoadedConfig ) * Profile {
132- currentProfile := lc .CurrentProfile
144+ func ( loadedConfig * LoadedConfig ) GetCurrentProfile ( ) * Profile {
145+ currentProfile := loadedConfig .CurrentProfile
133146 if currentProfile == "" {
134147 currentProfile = os .Getenv (IonosCurrentProfileEnvVar )
135148 }
136149 if currentProfile == "" {
137150 log .Printf ("[WARN] no current profile set" )
138151 }
139- for _ , profile := range lc .Profiles {
140- if profile .Name == lc .CurrentProfile {
152+ for _ , profile := range loadedConfig .Profiles {
153+ if profile .Name == loadedConfig .CurrentProfile {
141154 return & profile
142155 }
143156 }
@@ -149,26 +162,26 @@ func getCurrentProfile(lc LoadedConfig) *Profile {
149162func NewConfigurationFromLoaded (lc LoadedConfig , sdkProductName string ) (* Configuration , error ) {
150163 config := & Configuration {}
151164 getCredentialsFromEnv := false
152- // don't read credentials from file, if they are set in the environment
165+ // don't read Credentials from file, if they are set in the environment
153166 if os .Getenv (IonosUsernameEnvVar ) != "" || os .Getenv (IonosTokenEnvVar ) != "" {
154167 getCredentialsFromEnv = true
155168 config = NewConfigurationFromEnv ()
156169 }
157170 profileLocation := ""
158171 if ! getCredentialsFromEnv {
159- if currentProfile := getCurrentProfile ( lc ); currentProfile != nil {
172+ if currentProfile := lc . GetCurrentProfile ( ); currentProfile != nil {
160173 config .Username = currentProfile .Credentials .Username
161174 config .Password = currentProfile .Credentials .Password
162175 config .Token = currentProfile .Credentials .Token
163176 profileLocation = currentProfile .Location
164177 }
165178 }
166- processLocations (lc , config , profileLocation , sdkProductName )
179+ lc . processLocations (config , profileLocation , sdkProductName )
167180 return config , nil
168181}
169182
170183// processLocations - overrides http client and server configuration based on user location
171- func processLocations (loadedConfig LoadedConfig , config * Configuration , profileLocation , sdkProductName string ) {
184+ func (loadedConfig * LoadedConfig ) processLocations ( config * Configuration , profileLocation , sdkProductName string ) {
172185 for _ , location := range loadedConfig .Locations {
173186 for _ , product := range location .Products {
174187 if product .Name != sdkProductName {
@@ -198,6 +211,27 @@ func processLocations(loadedConfig LoadedConfig, config *Configuration, profileL
198211 }
199212}
200213
214+ func (loadedConfig * LoadedConfig ) GetLocationForCurrentProfile () string {
215+ if currentProfile := loadedConfig .GetCurrentProfile (); currentProfile != nil {
216+ return currentProfile .Location
217+ }
218+ return ""
219+ }
220+
221+ func (loadedConfig * LoadedConfig ) GetOverridesFor (productName string ) * Product {
222+ if loadedConfig == nil {
223+ return nil
224+ }
225+ for _ , location := range loadedConfig .Locations {
226+ for _ , product := range location .Products {
227+ if product .Name == productName {
228+ return & product
229+ }
230+ }
231+ }
232+ return nil
233+ }
234+
201235// AddCertsToClient adds certificates to the http client
202236func AddCertsToClient (httpClient * http.Client , authorityData string ) {
203237 rootCAs , _ := x509 .SystemCertPool ()
0 commit comments