-
Notifications
You must be signed in to change notification settings - Fork 45
/
Test_Config.go
144 lines (123 loc) · 4.44 KB
/
Test_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
// Proof of Concepts of CB-Spider.
// The CB-Spider is 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
//
// This is a Cloud Driver Example for PoC Test.
//
// by devunet@mz.co.kr, 2019.08.
package AliTestConfig
import (
"io/ioutil"
"os"
alidrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/drivers/alibaba"
idrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces"
"github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
cblog "github.com/cloud-barista/cb-log"
)
var cblogger *logrus.Logger
func init() {
// cblog is a global variable.
cblogger = cblog.GetLogger("ALI Resource Test")
cblog.SetLevel("debug")
}
// Region : 사용할 리전명 (ex) ap-northeast-2
// ImageID : VM 생성에 사용할 AMI ID (ex) ami-047f7b46bd6dd5d84
// BaseName : 다중 VM 생성 시 사용할 Prefix이름 ("BaseName" + "_" + "숫자" 형식으로 VM을 생성 함.) (ex) mcloud-barista
// VmID : 라이프 사이트클을 테스트할 EC2 인스턴스ID
// InstanceType : VM 생성시 사용할 인스턴스 타입 (ex) t2.micro
// KeyName : VM 생성시 사용할 키페어 이름 (ex) mcloud-barista-keypair
// MinCount :
// MaxCount :
// SubnetId : VM이 생성될 VPC의 SubnetId (ex) subnet-cf9ccf83
// SecurityGroupID : 생성할 VM에 적용할 보안그룹 ID (ex) sg-0df1c209ea1915e4b
type Config struct {
Ali struct {
AliAccessKeyID string `yaml:"ali_access_key_id"`
AliSecretAccessKey string `yaml:"ali_secret_access_key"`
Region string `yaml:"region"`
Zone string `yaml:"zone"`
ImageID string `yaml:"image_id"`
VmID string `yaml:"ec2_instance_id"`
BaseName string `yaml:"base_name"`
InstanceType string `yaml:"instance_type"`
KeyName string `yaml:"key_name"`
MinCount int64 `yaml:"min_count"`
MaxCount int64 `yaml:"max_count"`
SubnetID string `yaml:"subnet_id"`
SecurityGroupID string `yaml:"security_group_id"`
PublicIP string `yaml:"public_ip"`
} `yaml:"ali"`
}
//환경 설정 파일 읽기
//환경변수 CBSPIDER_PATH 설정 후 해당 폴더 하위에 /config/config.yaml 파일 생성해야 함.
func ReadConfigFile() Config {
// Set Environment Value of Project Root Path
rootPath := os.Getenv("CBSPIDER_PATH")
//rootpath := "D:/Workspace/mcloud-barista-config"
// /mnt/d/Workspace/mcloud-barista-config/config/config.yaml
cblogger.Debugf("Test Data 설정파일 : [%]", rootPath+"/config/configAli.yaml")
data, err := ioutil.ReadFile(rootPath + "/config/configAli.yaml")
//data, err := ioutil.ReadFile("D:/Workspace/mcloud-bar-config/config/config.yaml")
if err != nil {
panic(err)
}
var config Config
err = yaml.Unmarshal(data, &config)
if err != nil {
panic(err)
}
cblogger.Info("Loaded ConfigFile...")
//spew.Dump(config)
//cblogger.Info(config)
return config
}
//handlerType : resources폴더의 xxxHandler.go에서 Handler이전까지의 문자열
//(예) ImageHandler.go -> "Image"
func GetResourceHandler(handlerType string) (interface{}, error) {
var cloudDriver idrv.CloudDriver
cloudDriver = new(alidrv.AlibabaDriver)
config := ReadConfigFile()
connectionInfo := idrv.ConnectionInfo{
CredentialInfo: idrv.CredentialInfo{
ClientId: config.Ali.AliAccessKeyID,
ClientSecret: config.Ali.AliSecretAccessKey,
},
RegionInfo: idrv.RegionInfo{
Region: config.Ali.Region,
Zone: config.Ali.Zone,
},
}
cloudConnection, errCon := cloudDriver.ConnectCloud(connectionInfo)
if errCon != nil {
return nil, errCon
}
var resourceHandler interface{}
var err error
switch handlerType {
case "Image":
resourceHandler, err = cloudConnection.CreateImageHandler()
//case "Publicip":
//resourceHandler, err = cloudConnection.CreatePublicIPHandler()
case "Security":
resourceHandler, err = cloudConnection.CreateSecurityHandler()
// case "VNetwork":
//resourceHandler, err = cloudConnection.CreateVNetworkHandler()
case "KeyPair":
resourceHandler, err = cloudConnection.CreateKeyPairHandler()
case "VPC":
resourceHandler, err = cloudConnection.CreateVPCHandler()
//case "VNic":
//resourceHandler, err = cloudConnection.CreateVNicHandler()
case "VMSpec":
resourceHandler, err = cloudConnection.CreateVMSpecHandler()
case "VM":
resourceHandler, err = cloudConnection.CreateVMHandler()
}
if err != nil {
return nil, err
}
return resourceHandler, nil
}