-
Notifications
You must be signed in to change notification settings - Fork 1
/
tenanter.go
119 lines (96 loc) · 3.02 KB
/
tenanter.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
package tenanter
import (
"encoding/json"
"flag"
"github.com/JCCE-nudt/PCM/lan_trans/idl/pbtenant"
"github.com/golang/glog"
"io/ioutil"
"os"
"sync"
"github.com/go-yaml/yaml"
"github.com/pkg/errors"
)
const osEnvKey = "CLOUD_FITTER_CONFIGS"
var (
ErrLoadTenanterFromFile = errors.New("load tenanter from file failed")
ErrLoadTenanterFromOsEnv = errors.New("load tenanter from os env failed")
ErrLoadTenanterFileEmpty = errors.New("load tenanter from file failed")
ErrNoTenanters = errors.New("no tenanters for the cloud")
)
type Tenanter interface {
AccountName() string
Clone() Tenanter
}
var gStore = globalStore{stores: make(map[pbtenant.CloudProvider][]Tenanter)}
type globalStore struct {
sync.Mutex
stores map[pbtenant.CloudProvider][]Tenanter
}
func LoadCloudConfigs(configFile string) error {
if err := LoadCloudConfigsFromFile(configFile); errors.Is(err, ErrLoadTenanterFileEmpty) {
return LoadCloudConfigsFromOsEnv()
}
return nil
}
func LoadCloudConfigsFromFile(configFile string) error {
b, err := ioutil.ReadFile(configFile)
if err != nil {
return ErrLoadTenanterFileEmpty
}
var configs = new(pbtenant.CloudConfigs)
if err = yaml.Unmarshal(b, configs); err != nil {
return errors.WithMessage(ErrLoadTenanterFromFile, err.Error())
}
return load(configs)
}
func LoadCloudConfigsFromOsEnv() error {
data := os.Getenv(osEnvKey)
var configs = new(pbtenant.CloudConfigs)
if err := json.Unmarshal([]byte(data), configs); err != nil {
return errors.WithMessage(ErrLoadTenanterFromOsEnv, err.Error())
}
return load(configs)
}
func ShowConfigJson() ([]byte, error) {
data := os.Getenv(osEnvKey)
var configs = new(pbtenant.CloudConfigs)
if err := yaml.Unmarshal([]byte(data), configs); err != nil {
return nil, errors.WithMessage(ErrLoadTenanterFromFile, err.Error())
}
return json.Marshal(configs)
}
func load(configs *pbtenant.CloudConfigs) error {
gStore.Lock()
defer gStore.Unlock()
for _, c := range configs.Configs {
//if c.AccessId != "" && c.AccessSecret != "" {
gStore.stores[c.Provider] = append(gStore.stores[c.Provider], NewTenantWithAccessKey(c.Name, c.AccessId, c.AccessSecret, c.Url, c.Token))
//}
}
return nil
}
func GetTenanters(provider pbtenant.CloudProvider) ([]Tenanter, error) {
gStore.Lock()
defer gStore.Unlock()
if len(gStore.stores[provider]) == 0 {
return nil, errors.WithMessagef(ErrNoTenanters, "cloud is %v", provider)
}
var tenanters = make([]Tenanter, len(gStore.stores[provider]))
for k := range gStore.stores[provider] {
tenanters[k] = gStore.stores[provider][k].Clone()
}
return tenanters, nil
}
func GenTenanter(configPath string) {
var configFile string
flag.StringVar(&configFile, "conf", configPath, "config.yaml")
flag.Parse()
defer glog.Flush()
if err := LoadCloudConfigsFromFile(configFile); err != nil {
if !errors.Is(err, ErrLoadTenanterFileEmpty) {
glog.Fatalf("LoadCloudConfigsFromFile error %+v", err)
}
glog.Warningf("LoadCloudConfigsFromFile empty file path %s", configFile)
}
glog.Infof("load tenant from file finished")
}