-
Notifications
You must be signed in to change notification settings - Fork 47
/
VMSpecHandler.go
202 lines (173 loc) · 5.19 KB
/
VMSpecHandler.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Proof of Concepts 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
//
// This is a Cloud Driver Example for PoC Test.
//
// program by ysjeon@mz.co.kr, 2020.01.
package resources
import (
"context"
_ "errors"
"strconv"
"strings"
compute "google.golang.org/api/compute/v1"
call "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/call-log"
idrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces"
irs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/resources"
)
type GCPVMSpecHandler struct {
Region idrv.RegionInfo
Ctx context.Context
Client *compute.Service
Credential idrv.CredentialInfo
}
func (vmSpecHandler *GCPVMSpecHandler) ListVMSpec() ([]*irs.VMSpecInfo, error) {
projectID := vmSpecHandler.Credential.ProjectID
zone := vmSpecHandler.Region.Zone
// logger for HisCall
callogger := call.GetLogger("HISCALL")
callLogInfo := call.CLOUDLOGSCHEMA{
CloudOS: call.GCP,
RegionZone: vmSpecHandler.Region.Zone,
ResourceType: call.VMSPEC,
ResourceName: "",
CloudOSAPI: "List()",
ElapsedTime: "",
ErrorMSG: "",
}
callLogStart := call.Start()
resp, err := vmSpecHandler.Client.MachineTypes.List(projectID, zone).Do()
callLogInfo.ElapsedTime = call.Elapsed(callLogStart)
if err != nil {
callLogInfo.ErrorMSG = err.Error()
callogger.Info(call.String(callLogInfo))
cblogger.Error(err)
return []*irs.VMSpecInfo{}, err
}
callogger.Info(call.String(callLogInfo))
var vmSpecInfo []*irs.VMSpecInfo
for _, i := range resp.Items {
info := irs.VMSpecInfo{
Region: zone,
Name: i.Name,
VCpu: irs.VCpuInfo{
Count: strconv.FormatInt(i.GuestCpus, 10),
},
Mem: strconv.FormatInt(i.MemoryMb, 10),
}
vmSpecInfo = append(vmSpecInfo, &info)
}
return vmSpecInfo, nil
}
func (vmSpecHandler *GCPVMSpecHandler) GetVMSpec(Name string) (irs.VMSpecInfo, error) {
// default info
projectID := vmSpecHandler.Credential.ProjectID
zone := vmSpecHandler.Region.Zone
// logger for HisCall
callogger := call.GetLogger("HISCALL")
callLogInfo := call.CLOUDLOGSCHEMA{
CloudOS: call.GCP,
RegionZone: vmSpecHandler.Region.Zone,
ResourceType: call.VMSPEC,
ResourceName: Name,
CloudOSAPI: "Get()",
ElapsedTime: "",
ErrorMSG: "",
}
callLogStart := call.Start()
info, err := vmSpecHandler.Client.MachineTypes.Get(projectID, zone, Name).Do()
callLogInfo.ElapsedTime = call.Elapsed(callLogStart)
if err != nil {
callLogInfo.ErrorMSG = err.Error()
callogger.Info(call.String(callLogInfo))
cblogger.Error(err)
return irs.VMSpecInfo{}, err
}
callogger.Info(call.String(callLogInfo))
vmSpecInfo := irs.VMSpecInfo{
Region: vmSpecHandler.Region.Region,
Name: Name,
VCpu: irs.VCpuInfo{
Count: strconv.FormatInt(info.GuestCpus, 10),
Clock: "",
},
Mem: strconv.FormatInt(info.MemoryMb, 10),
Gpu: []irs.GpuInfo{
{
Count: "",
Mfr: "",
Model: "",
Mem: "",
},
},
}
return vmSpecInfo, nil
}
func (vmSpecHandler *GCPVMSpecHandler) ListOrgVMSpec() (string, error) {
projectID := vmSpecHandler.Credential.ProjectID
zone := vmSpecHandler.Region.Zone
// logger for HisCall
callogger := call.GetLogger("HISCALL")
callLogInfo := call.CLOUDLOGSCHEMA{
CloudOS: call.GCP,
RegionZone: vmSpecHandler.Region.Zone,
ResourceType: call.VMSPEC,
ResourceName: "",
CloudOSAPI: "List()",
ElapsedTime: "",
ErrorMSG: "",
}
callLogStart := call.Start()
resp, err := vmSpecHandler.Client.MachineTypes.List(projectID, zone).Do()
callLogInfo.ElapsedTime = call.Elapsed(callLogStart)
if err != nil {
callLogInfo.ErrorMSG = err.Error()
callogger.Info(call.String(callLogInfo))
cblogger.Error(err)
return "", err
}
callogger.Info(call.String(callLogInfo))
j, _ := resp.MarshalJSON()
return string(j), err
}
func (vmSpecHandler *GCPVMSpecHandler) GetOrgVMSpec(Name string) (string, error) {
projectID := vmSpecHandler.Credential.ProjectID
zone := vmSpecHandler.Region.Zone
// logger for HisCall
callogger := call.GetLogger("HISCALL")
callLogInfo := call.CLOUDLOGSCHEMA{
CloudOS: call.GCP,
RegionZone: vmSpecHandler.Region.Zone,
ResourceType: call.VMSPEC,
ResourceName: Name,
CloudOSAPI: "Get()",
ElapsedTime: "",
ErrorMSG: "",
}
callLogStart := call.Start()
info, err := vmSpecHandler.Client.MachineTypes.Get(projectID, zone, Name).Do()
callLogInfo.ElapsedTime = call.Elapsed(callLogStart)
if err != nil {
callLogInfo.ErrorMSG = err.Error()
callogger.Info(call.String(callLogInfo))
cblogger.Error(err)
return "", err
}
callogger.Info(call.String(callLogInfo))
j, _ := info.MarshalJSON()
return string(j), err
}
// gcp 같은경우 n1 타입만 그래픽 카드가 추가 되며
// 1. n1타입인지 확인하는 로직 필요
// 2. 해당 카드에 관련된 정보를 조회하는 로직필요.
// 3. 해당 리스트를 조회하고 해당 GPU를 선택하는 로직
func CheckMachineType(Name string) bool {
prefix := "n1"
if ok := strings.HasPrefix(prefix, Name); ok {
return ok
}
return false
}