Skip to content

Commit 63ac0f1

Browse files
authored
Add possibility to create non-lazy Env (#77)
Add possibility to create non-lazy `Env` Add optional lazy argument to NewEnv Reviewed-by: None <None> Reviewed-by: Rodion Gyrbu <fpsoff@outlook.com> Reviewed-by: Anton Sidelnikov <None>
1 parent bb6c292 commit 63ac0f1

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

openstack/client_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,38 @@ func restoreBackup(t *testing.T, files ...string) {
6464
}
6565
}
6666

67+
func checkLazyness(t *testing.T, env Env, expected bool) {
68+
authUrl0 := "http://url:0"
69+
_ = os.Setenv("OS_AUTH_URL", authUrl0)
70+
cloud0, err := env.Cloud()
71+
th.AssertNoErr(t, err)
72+
th.AssertEquals(t, authUrl0, cloud0.AuthInfo.AuthURL)
73+
74+
authUrl1 := "http://url:1"
75+
_ = os.Setenv("OS_AUTH_URL", authUrl1)
76+
cloud1, err := env.Cloud()
77+
th.AssertNoErr(t, err)
78+
79+
th.AssertEquals(t, expected, authUrl0 == cloud1.AuthInfo.AuthURL)
80+
th.AssertEquals(t, !expected, authUrl1 == cloud1.AuthInfo.AuthURL)
81+
}
82+
83+
func TestLazyEnv(t *testing.T) {
84+
t.Run("lazy", func(sub *testing.T) {
85+
env := NewEnv("OS_", true)
86+
checkLazyness(sub, env, true)
87+
})
88+
t.Run("not lazy", func(sub *testing.T) {
89+
env := NewEnv("OS_", false)
90+
checkLazyness(sub, env, false)
91+
})
92+
t.Run("default", func(sub *testing.T) {
93+
env := NewEnv("OS_")
94+
checkLazyness(sub, env, true)
95+
sub.Log("Lazy by default")
96+
})
97+
}
98+
6799
func TestCloudYamlPaths(t *testing.T) {
68100
_ = os.Setenv("OS_CLOUD", "useless_cloud")
69101
home, _ := os.UserHomeDir()

openstack/loader.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,21 @@ type env struct {
7878
prefix string
7979
cloud *Cloud
8080

81-
// Unstable make env ignore lazy cloud loading and
81+
// unstable make env ignore lazy cloud loading and
8282
// refresh it every time it's requested
83-
Unstable bool
83+
unstable bool
8484
}
8585

86-
// NewEnv create new <prefixed> env loader
87-
func NewEnv(prefix string) Env {
86+
// NewEnv create new <prefixed> env loader, lazy by default
87+
func NewEnv(prefix string, lazy ...bool) Env {
8888
if prefix != "" && !strings.HasSuffix(prefix, "_") {
8989
prefix += "_"
9090
}
91-
return &env{prefix: prefix}
91+
unstable := false
92+
if len(lazy) > 0 {
93+
unstable = !lazy[0]
94+
}
95+
return &env{prefix: prefix, unstable: unstable}
9296
}
9397

9498
func (e *env) Prefix() string {
@@ -453,7 +457,7 @@ func mergeWithVendors(cloudConfig *Config, vendorPath string) *Config {
453457

454458
// Cloud get cloud merged from configuration and env variables
455459
func (e *env) Cloud() (*Cloud, error) {
456-
if e.cloud == nil || e.Unstable {
460+
if e.cloud == nil || e.unstable {
457461
config, err := e.loadOpenstackConfig()
458462
if err != nil {
459463
return nil, fmt.Errorf("failed to authenticate client: %s", err)

0 commit comments

Comments
 (0)