Skip to content

Commit 89b1868

Browse files
fix: easier to get client overrides
1 parent df6d69d commit 89b1868

File tree

2 files changed

+61
-38
lines changed

2 files changed

+61
-38
lines changed

shared/loadedconfiguration.go

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -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
1537
type 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
3146
type 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
3754
type 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 {
149162
func 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
202236
func AddCertsToClient(httpClient *http.Client, authorityData string) {
203237
rootCAs, _ := x509.SystemCertPool()

shared/loadedconfiguration_test.go

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,7 @@ func TestNewConfigurationFromLoaded_ValidConfig(t *testing.T) {
9191
{
9292
Name: "testLocation",
9393
CertificateAuthData: "testCertData",
94-
Products: []struct {
95-
Name string `yaml:"name"`
96-
Endpoint string `yaml:"endpoint"`
97-
SkipTLSVerify bool `yaml:"skipTlsVerify"`
98-
}{
94+
Products: []Product{
9995
{
10096
Name: "testProduct",
10197
Endpoint: testEndpoint,
@@ -126,11 +122,7 @@ func TestNewConfigurationFromLoaded_NoProfile(t *testing.T) {
126122
{
127123
Name: "testLocation",
128124
CertificateAuthData: "testCertData",
129-
Products: []struct {
130-
Name string `yaml:"name"`
131-
Endpoint string `yaml:"endpoint"`
132-
SkipTLSVerify bool `yaml:"skipTlsVerify"`
133-
}{
125+
Products: []Product{
134126
{
135127
Name: "testProduct",
136128
Endpoint: testEndpoint,
@@ -175,11 +167,7 @@ func TestNewConfigurationFromLoaded_NoMatchingProduct(t *testing.T) {
175167
{
176168
Name: "testLocation",
177169
CertificateAuthData: "testCertData",
178-
Products: []struct {
179-
Name string `yaml:"name"`
180-
Endpoint string `yaml:"endpoint"`
181-
SkipTLSVerify bool `yaml:"skipTlsVerify"`
182-
}{
170+
Products: []Product{
183171
{
184172
Name: "otherProduct",
185173
Endpoint: "https://other.endpoint",
@@ -235,4 +223,5 @@ locations:
235223
assert.Equal(t, "testUser", profiles.Profiles[0].Credentials.Username)
236224
assert.Equal(t, "testPass", profiles.Profiles[0].Credentials.Password)
237225
assert.Equal(t, "testToken", profiles.Profiles[0].Credentials.Token)
226+
assert.Equal(t, "testProfile", profiles.CurrentProfile)
238227
}

0 commit comments

Comments
 (0)