Skip to content

Commit

Permalink
improve environment variable support (#56)
Browse files Browse the repository at this point in the history
ENHANCEMENTS:

- Add environment variable `UCLOUD_ZONE`, `UCLOUD_API_BASE_URL`, `UCLOUD_TIMEOUT_SECOND` support
  • Loading branch information
lixiaojun629 authored and yufeiminds committed Apr 16, 2019
1 parent 73dbdba commit c827af7
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 32 deletions.
4 changes: 2 additions & 2 deletions external/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (c *config) loadEnv() error {
return nil
}

func (c *config) loadFile() error {
func (c *config) loadFileIfExist() error {
cfg, err := loadSharedConfigFile(
c.SharedConfigFile,
c.SharedCredentialFile,
Expand Down Expand Up @@ -118,7 +118,7 @@ func LoadDefaultUCloudConfig() (ConfigProvider, error) {
return nil, fmt.Errorf("error on loading env, %s", err)
}

if err := cfg.loadFile(); err != nil {
if err := cfg.loadFileIfExist(); err != nil {
return nil, fmt.Errorf("error on loading shared config file, %s", err)
}

Expand Down
13 changes: 12 additions & 1 deletion external/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"
"time"

Expand All @@ -16,7 +17,10 @@ var (
TestValueEnvUCloudPrivateKey = "c45f9bec5fa4c6c47fd871fadd97dd2e"
TestValueEnvUCloudProjectId = "org-3kopqz"
TestValueEnvUCloudRegion = "cn-bj2"
TestValueEnvUCloudZone = "cn-bj2-02"
TestValueEnvUCloudProfile = "default"
TestValueEnvUCloudTimeout = time.Duration(30) * time.Second
TestValueEnvUCloudBaseUrl = "https://api.ucloud.cn"
TestValueEnvUCloudSharedConfigFile = filepath.Join("test-fixtures", "config.json")
TestValueEnvUCloudSharedCredentialFile = filepath.Join("test-fixtures", "credential.json")

Expand Down Expand Up @@ -54,7 +58,7 @@ func TestLoadSharedFile(t *testing.T) {
SharedConfigFile: TestValueEnvUCloudSharedConfigFile,
SharedCredentialFile: TestValueEnvUCloudSharedCredentialFile,
}
c.loadFile()
c.loadFileIfExist()

checkTestConfig(t, c)
}
Expand All @@ -64,6 +68,9 @@ func checkTestEnvConfig(t *testing.T, c *config) {
assert.Equal(t, TestValueEnvUCloudPrivateKey, c.PrivateKey)
assert.Equal(t, TestValueEnvUCloudProjectId, c.ProjectId)
assert.Equal(t, TestValueEnvUCloudRegion, c.Region)
assert.Equal(t, TestValueEnvUCloudZone, c.Zone)
assert.Equal(t, TestValueEnvUCloudBaseUrl, c.BaseUrl)
assert.Equal(t, TestValueEnvUCloudTimeout, c.Timeout)
assert.Equal(t, TestValueEnvUCloudProfile, c.Profile)
assert.Equal(t, TestValueEnvUCloudSharedConfigFile, c.SharedConfigFile)
assert.Equal(t, TestValueEnvUCloudSharedCredentialFile, c.SharedCredentialFile)
Expand All @@ -81,9 +88,13 @@ func setTestEnv() {
os.Setenv(UCloudPrivateKeyEnvVar, TestValueEnvUCloudPrivateKey)
os.Setenv(UCloudProjectIdEnvVar, TestValueEnvUCloudProjectId)
os.Setenv(UCloudRegionEnvVar, TestValueEnvUCloudRegion)
os.Setenv(UCloudZoneEnvVar, TestValueEnvUCloudZone)
os.Setenv(UCloudAPIBaseURLEnvVar, TestValueEnvUCloudBaseUrl)
os.Setenv(UCloudSharedProfileEnvVar, TestValueEnvUCloudProfile)
os.Setenv(UCloudSharedConfigFileEnvVar, TestValueEnvUCloudSharedConfigFile)
os.Setenv(UCloudSharedCredentialFileEnvVar, TestValueEnvUCloudSharedCredentialFile)
durstr := strings.TrimSuffix(TestValueEnvUCloudTimeout.String(), "s")
os.Setenv(UCloudTimeoutSecondEnvVar, durstr)
}

func writeTestTempConfigFile(vL []sharedConfig) (string, error) {
Expand Down
31 changes: 27 additions & 4 deletions external/env_config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package external

import "os"
import (
"fmt"
"os"
"strconv"
"time"
)

const (
UCloudPublicKeyEnvVar = "UCLOUD_PUBLIC_KEY"
Expand All @@ -11,6 +16,12 @@ const (

UCloudRegionEnvVar = "UCLOUD_REGION"

UCloudZoneEnvVar = "UCLOUD_ZONE"

UCloudAPIBaseURLEnvVar = "UCLOUD_API_BASE_URL"

UCloudTimeoutSecondEnvVar = "UCLOUD_TIMEOUT_SECOND"

UCloudSharedProfileEnvVar = "UCLOUD_PROFILE"

UCloudSharedConfigFileEnvVar = "UCLOUD_SHARED_CONFIG_FILE"
Expand All @@ -19,13 +30,25 @@ const (
)

func loadEnvConfig() (*config, error) {
return &config{
cfg := &config{
PublicKey: os.Getenv(UCloudPublicKeyEnvVar),
PrivateKey: os.Getenv(UCloudPrivateKeyEnvVar),
ProjectId: os.Getenv(UCloudProjectIdEnvVar),
Region: os.Getenv(UCloudRegionEnvVar),
Profile: os.Getenv(UCloudSharedProfileEnvVar),
Zone: os.Getenv(UCloudZoneEnvVar),
BaseUrl: os.Getenv(UCloudAPIBaseURLEnvVar),
SharedConfigFile: os.Getenv(UCloudSharedConfigFileEnvVar),
SharedCredentialFile: os.Getenv(UCloudSharedCredentialFileEnvVar),
}, nil
Profile: os.Getenv(UCloudSharedProfileEnvVar),
}

durstr, ok := os.LookupEnv(UCloudTimeoutSecondEnvVar)
if ok {
durnum, err := strconv.Atoi(durstr)
if err != nil {
return nil, fmt.Errorf("parse environment variable UCLOUD_TIMEOUT_SECOND [%s] error : %v", durstr, err)
}
cfg.Timeout = time.Second * time.Duration(durnum)
}
return cfg, nil
}
57 changes: 32 additions & 25 deletions external/shared_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package external

import (
"fmt"
"os"
"path/filepath"
"time"

Expand Down Expand Up @@ -71,87 +72,93 @@ type sharedCredential struct {

func loadConfigFile(cfgFile string) ([]sharedConfig, error) {
realCfgFile := cfgFile
cfgMaps := make([]sharedConfig, 0)
cfgs := make([]sharedConfig, 0)

// try to load default config
if len(realCfgFile) == 0 {
realCfgFile = DefaultSharedConfigFile()
}

// load config file
err := loadJSONFile(realCfgFile, &cfgMaps)
err := loadJSONFile(realCfgFile, &cfgs)

if err != nil {
// skip error for loading default config
if len(cfgFile) == 0 {
if len(cfgFile) == 0 && os.IsNotExist(err) {
log.Debugf("config file is empty")
} else {
return nil, err
}
}

return cfgMaps, nil
return cfgs, nil
}

func loadCredFile(credFile string) ([]sharedCredential, error) {
realCredFile := credFile
credMaps := make([]sharedCredential, 0)
creds := make([]sharedCredential, 0)

// try to load default credential
if len(credFile) == 0 {
realCredFile = DefaultSharedCredentialsFile()
}

// load credential file
err := loadJSONFile(realCredFile, &credMaps)
err := loadJSONFile(realCredFile, &creds)

if err != nil {
// skip error for loading default credential
if len(credFile) == 0 {
if len(credFile) == 0 && os.IsNotExist(err) {
log.Debugf("credential file is empty")
} else {
return nil, err
}
}

return credMaps, nil
return creds, nil
}

func loadSharedConfigFile(cfgFile, credFile, profile string) (*config, error) {
cfgMaps, err := loadConfigFile(cfgFile)
cfgs, err := loadConfigFile(cfgFile)
if err != nil {
return nil, err
}

credMaps, err := loadCredFile(credFile)
creds, err := loadCredFile(credFile)
if err != nil {
return nil, err
}

// load configured profile
if len(profile) == 0 {
profile = DefaultProfile
}

c := &config{
Profile: profile,
SharedConfigFile: cfgFile,
SharedCredentialFile: credFile,
}
c.merge(getSharedConfig(cfgMaps, profile))
c.merge(getSharedCredential(credMaps, profile))
c.merge(getSharedConfig(cfgs, profile))
c.merge(getSharedCredential(creds, c.Profile))

return c, nil
}

func getSharedConfig(cfgMaps []sharedConfig, profile string) *config {
func getSharedConfig(cfgs []sharedConfig, profile string) *config {
cfg := &sharedConfig{}

for i := 0; i < len(cfgMaps); i++ {
if cfgMaps[i].Profile == profile {
cfg = &cfgMaps[i]
if profile != "" {
for i := 0; i < len(cfgs); i++ {
if cfgs[i].Profile == profile {
cfg = &cfgs[i]
}
}
} else {
for i := 0; i < len(cfgs); i++ {
if cfgs[i].Active {
cfg = &cfgs[i]
}
}
}

return &config{
Profile: cfg.Profile,
ProjectId: cfg.ProjectID,
Region: cfg.Region,
Zone: cfg.Zone,
Expand All @@ -160,12 +167,12 @@ func getSharedConfig(cfgMaps []sharedConfig, profile string) *config {
}
}

func getSharedCredential(credMaps []sharedCredential, profile string) *config {
func getSharedCredential(creds []sharedCredential, profile string) *config {
cred := &sharedCredential{}

for i := 0; i < len(credMaps); i++ {
if credMaps[i].Profile == profile {
cred = &credMaps[i]
for i := 0; i < len(creds); i++ {
if creds[i].Profile == profile {
cred = &creds[i]
}
}

Expand Down

0 comments on commit c827af7

Please sign in to comment.