-
Notifications
You must be signed in to change notification settings - Fork 48
/
RegionInfoManager.go
123 lines (96 loc) · 2.82 KB
/
RegionInfoManager.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
// Cloud regioninfomanager Info. Manager of CB-Spider.
// The CB-Spider is a sub-Framework of the Cloud-Barista Multi-Cloud Project.
// The CB-Spider Mission is to connect all the clouds with a single interface.
//
// * Cloud-Barista: https://github.com/cloud-barista
//
// by CB-Spider Team, 2019.09.
package regioninfomanager
import (
"fmt"
"github.com/cloud-barista/cb-store/config"
icbs "github.com/cloud-barista/cb-store/interfaces"
"github.com/sirupsen/logrus"
)
var cblog *logrus.Logger
func init() {
cblog = config.Cblogger
}
//====================================================================
type RegionInfo struct {
RegionName string // ex) "region01"
ProviderName string // ex) "GCP"
KeyValueInfoList []icbs.KeyValue // ex) { {region, us-east1},
// {zone, us-east1-c},
}
//====================================================================
func RegisterRegionInfo(rgnInfo RegionInfo) (*RegionInfo, error) {
return RegisterRegion(rgnInfo.RegionName, rgnInfo.ProviderName, rgnInfo.KeyValueInfoList)
}
// 1. check params
// 2. insert them into cb-store
func RegisterRegion(regionName string, providerName string, keyValueInfoList []icbs.KeyValue) (*RegionInfo, error) {
cblog.Info("call RegisterRegion()")
cblog.Debug("check params")
err := checkParams(regionName, providerName, keyValueInfoList)
if err != nil {
return nil, err
}
cblog.Debug("insert metainfo into store")
err = insertInfo(regionName, providerName, keyValueInfoList)
if err != nil {
cblog.Error(err)
return nil, err
}
rgnInfo := &RegionInfo{regionName, providerName, keyValueInfoList}
return rgnInfo, nil
}
func ListRegion() ([]*RegionInfo, error) {
cblog.Info("call ListRegion()")
regionInfoList, err := listInfo()
if err != nil {
return nil, err
}
return regionInfoList, nil
}
// 1. check params
// 2. get CredentialInfo from cb-store
func GetRegion(regionName string) (*RegionInfo, error) {
cblog.Info("call GetRegion()")
if regionName == "" {
return nil, fmt.Errorf("RegionName is empty!")
}
rgnInfo, err := getInfo(regionName)
if err != nil {
cblog.Error(err)
return nil, err
}
return rgnInfo, err
}
func UnRegisterRegion(regionName string) (bool, error) {
cblog.Info("call UnRegisterRegion()")
if regionName == "" {
return false, fmt.Errorf("RegionName is empty!")
}
result, err := deleteInfo(regionName)
if err != nil {
cblog.Error(err)
return false, err
}
return result, nil
}
//----------------
func checkParams(regionName string, providerName string, keyValueInfoList []icbs.KeyValue) error {
if regionName == "" {
return fmt.Errorf("RegionName is empty!")
}
if providerName == "" {
return fmt.Errorf("ProviderName is empty!")
}
for _, kv := range keyValueInfoList {
if kv.Key == "" { // Value can be empty.
return fmt.Errorf("Key is empty!")
}
}
return nil
}