-
Notifications
You must be signed in to change notification settings - Fork 38
/
shared_config.go
183 lines (150 loc) · 4.1 KB
/
shared_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package external
import (
"fmt"
"os"
"path/filepath"
"time"
"github.com/ucloud/ucloud-sdk-go/ucloud"
"github.com/ucloud/ucloud-sdk-go/ucloud/auth"
"github.com/ucloud/ucloud-sdk-go/ucloud/log"
)
// DefaultProfile is the default named profile for ucloud sdk
const DefaultProfile = "default"
// DefaultSharedConfigFile will return the default shared config filename
func DefaultSharedConfigFile() string {
return filepath.Join(userHomeDir(), ".ucloud", "config.json")
}
// DefaultSharedCredentialsFile will return the default shared credential filename
func DefaultSharedCredentialsFile() string {
return filepath.Join(userHomeDir(), ".ucloud", "credential.json")
}
// LoadUCloudConfigFile will load ucloud client config from config file
func LoadUCloudConfigFile(cfgFile, profile string) (*ucloud.Config, error) {
if len(profile) == 0 {
return nil, fmt.Errorf("expected ucloud named profile is not empty")
}
cfgMaps, err := loadConfigFile(cfgFile)
if err != nil {
return nil, err
}
c := getSharedConfig(cfgMaps, profile)
return c.Config(), nil
}
// LoadUCloudCredentialFile will load ucloud credential config from config file
func LoadUCloudCredentialFile(credFile, profile string) (*auth.Credential, error) {
if len(profile) == 0 {
return nil, fmt.Errorf("expected ucloud named profile is not empty")
}
credMaps, err := loadCredFile(credFile)
if err != nil {
return nil, err
}
c := getSharedCredential(credMaps, profile)
return c.Credential(), nil
}
type sharedConfig struct {
ProjectID string `json:"project_id"`
Region string `json:"region"`
Zone string `json:"zone"`
BaseURL string `json:"base_url"`
Timeout int `json:"timeout_sec"`
Profile string `json:"profile"`
Active bool `json:"active"`
}
type sharedCredential struct {
PublicKey string `json:"public_key"`
PrivateKey string `json:"private_key"`
Profile string `json:"profile"`
}
func loadConfigFile(cfgFile string) ([]sharedConfig, error) {
realCfgFile := cfgFile
cfgs := make([]sharedConfig, 0)
// try to load default config
if len(realCfgFile) == 0 {
realCfgFile = DefaultSharedConfigFile()
}
// load config file
err := loadJSONFile(realCfgFile, &cfgs)
if err != nil {
// skip error for loading default config
if len(cfgFile) == 0 && os.IsNotExist(err) {
log.Debugf("config file is empty")
} else {
return nil, err
}
}
return cfgs, nil
}
func loadCredFile(credFile string) ([]sharedCredential, error) {
realCredFile := credFile
creds := make([]sharedCredential, 0)
// try to load default credential
if len(credFile) == 0 {
realCredFile = DefaultSharedCredentialsFile()
}
// load credential file
err := loadJSONFile(realCredFile, &creds)
if err != nil {
// skip error for loading default credential
if len(credFile) == 0 && os.IsNotExist(err) {
log.Debugf("credential file is empty")
} else {
return nil, err
}
}
return creds, nil
}
func loadSharedConfigFile(cfgFile, credFile, profile string) (*config, error) {
cfgs, err := loadConfigFile(cfgFile)
if err != nil {
return nil, err
}
creds, err := loadCredFile(credFile)
if err != nil {
return nil, err
}
c := &config{
Profile: profile,
SharedConfigFile: cfgFile,
SharedCredentialFile: credFile,
}
c.merge(getSharedConfig(cfgs, profile))
c.merge(getSharedCredential(creds, c.Profile))
return c, nil
}
func getSharedConfig(cfgs []sharedConfig, profile string) *config {
cfg := &sharedConfig{}
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,
BaseUrl: cfg.BaseURL,
Timeout: time.Duration(cfg.Timeout) * time.Second,
}
}
func getSharedCredential(creds []sharedCredential, profile string) *config {
cred := &sharedCredential{}
for i := 0; i < len(creds); i++ {
if creds[i].Profile == profile {
cred = &creds[i]
}
}
return &config{
PublicKey: cred.PublicKey,
PrivateKey: cred.PrivateKey,
}
}