-
Notifications
You must be signed in to change notification settings - Fork 48
/
Test_Config.go
127 lines (107 loc) · 3.55 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
// 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 TestConfig
import (
"encoding/json"
"io/ioutil"
"os"
gcpdrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/drivers/gcp"
idrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces"
"github.com/sirupsen/logrus"
cblog "github.com/cloud-barista/cb-log"
)
var cblogger *logrus.Logger
func init() {
// cblog is a global variable.
cblogger = cblog.GetLogger("GCP Resource Test")
cblog.SetLevel("debug")
}
// GCP에서 다운로드한 JSON파일 포멧
type Config struct {
Type string `json:"type"`
ProjectID string `json:"project_id"`
PrivateKeyID string `json:"private_key_id"`
PrivateKey string `json:"private_key"`
ClientEmail string `json:"client_email"`
ClientID string `json:"client_id"`
AuthURI string `json:"auth_uri"`
TokenURI string `json:"token_uri"`
AuthProvider string `json:"auth_provider_x509_cert_url"`
}
// 환경변수 : GOOGLE_APPLICATION_CREDENTIALS - 인증용 .json 파일의 위치
//handlerType : resources폴더의 xxxHandler.go에서 Handler이전까지의 문자열
//(예) ImageHandler.go -> "Image"
func GetResourceHandler(handlerType string) (interface{}, error) {
var cloudDriver idrv.CloudDriver
cloudDriver = new(gcpdrv.GCPDriver)
credentialFilePath := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
cblogger.Infof("export $GOOGLE_APPLICATION_CREDENTIALS : [%s]", credentialFilePath)
cblogger.Infof("credentialFilePath : [%s]", credentialFilePath)
config, _ := readFileConfig(credentialFilePath)
//region := "europe-west1"
region := "asia-northeast3"
zone := "asia-northeast3-a"
connectionInfo := idrv.ConnectionInfo{
CredentialInfo: idrv.CredentialInfo{
PrivateKey: config.PrivateKey,
ProjectID: config.ProjectID,
ClientEmail: config.ClientEmail,
},
RegionInfo: idrv.RegionInfo{
Region: region,
Zone: zone,
},
}
cloudConnection, errCon := cloudDriver.ConnectCloud(connectionInfo)
if errCon != nil {
cblogger.Error(errCon)
return nil, errCon
}
cblogger.Info("ConnectCloud Success!!!")
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 "VNic":
// resourceHandler, err = cloudConnection.CreateVNicHandler()
case "VM":
resourceHandler, err = cloudConnection.CreateVMHandler()
case "KeyPair":
resourceHandler, err = cloudConnection.CreateKeyPairHandler()
case "VMSpec":
resourceHandler, err = cloudConnection.CreateVMSpecHandler()
case "VPCHandler":
resourceHandler, err = cloudConnection.CreateVPCHandler()
}
if err != nil {
return nil, err
}
return resourceHandler, nil
}
func readFileConfig(filepath string) (Config, error) {
data, err := ioutil.ReadFile(filepath)
if err != nil {
cblogger.Error(err)
panic(err)
}
var config Config
json.Unmarshal(data, &config)
cblogger.Info("Loaded ConfigFile...")
//cblogger.Info("readFileConfig Json : ", config.ClientEmail)
//spew.Dump(config)
return config, err
}