-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
70 lines (60 loc) · 1.99 KB
/
common.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
package service
import (
"errors"
"fmt"
"github.com/jmleefree/actiontest2/src/core/model"
"github.com/jmleefree/actiontest2/src/core/model/spider"
"github.com/jmleefree/actiontest2/src/utils/config"
)
type NodeConfigInfo struct {
model.NodeConfig
Csp config.CSP `json:"csp"`
Role string `json:"role"`
Region string `json:"region"`
Account string `json:"account"`
}
func SetNodeConfigInfos(nodeConfigs []model.NodeConfig, role string) ([]NodeConfigInfo, error) {
var nodeConfigInfos []NodeConfigInfo
for _, nodeConfig := range nodeConfigs {
conn := spider.NewConnection(nodeConfig.Connection)
exists, err := conn.GET()
if err != nil {
return nil, errors.New(fmt.Sprintf("%s Connection connect error (connectionName=%s)", role, nodeConfig.Connection))
}
if !exists {
return nil, errors.New(fmt.Sprintf("%s Connection does not exist (connectionName=%s)", role, nodeConfig.Connection))
}
csp, err := GetCSPName(conn.ProviderName)
if err != nil {
return nil, err
}
region := spider.NewRegion(conn.RegionName)
exists, err = region.GET()
if err != nil {
return nil, errors.New(fmt.Sprintf("%s get region error (connectionName=%s)", role, nodeConfig.Connection))
}
if !exists {
return nil, errors.New(fmt.Sprintf("%s region does not exist (connectionName=%s)", role, nodeConfig.Connection))
}
regionName := GetRegionName(region.KeyValueInfoList)
var nodeConfigInfo NodeConfigInfo
nodeConfigInfo.Connection = nodeConfig.Connection
nodeConfigInfo.Count = nodeConfig.Count
nodeConfigInfo.Spec = nodeConfig.Spec
nodeConfigInfo.Csp = csp
nodeConfigInfo.Role = role
nodeConfigInfo.Region = regionName
nodeConfigInfo.Account = GetUserAccount(nodeConfigInfo.Csp)
nodeConfigInfos = append(nodeConfigInfos, nodeConfigInfo)
}
return nodeConfigInfos, nil
}
func GetControlPlaneIPs(VMs []model.VM) []string {
var IPs []string
for _, vm := range VMs {
if vm.Role == config.CONTROL_PLANE {
IPs = append(IPs, vm.PublicIP)
}
}
return IPs
}