/
VMSpecHandler.go
177 lines (145 loc) · 5.21 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
// 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.
//
// by hyokyung.kim@innogrid.co.kr, 2020.01.
package resources
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"strings"
call "github.com/cloud-barista/poc-cicd-spider/cloud-control-manager/cloud-driver/call-log"
"github.com/cloud-barista/poc-cicd-spider/cloud-control-manager/cloud-driver/drivers/cloudit/client"
"github.com/cloud-barista/poc-cicd-spider/cloud-control-manager/cloud-driver/drivers/cloudit/client/ace/specs"
idrv "github.com/cloud-barista/poc-cicd-spider/cloud-control-manager/cloud-driver/interfaces"
irs "github.com/cloud-barista/poc-cicd-spider/cloud-control-manager/cloud-driver/interfaces/resources"
)
const (
VMSpec = "VMSPEC"
)
type ClouditVMSpecHandler struct {
CredentialInfo idrv.CredentialInfo
Client *client.RestClient
}
func setterVMSpec(region string, vmSpec specs.VMSpecInfo) *irs.VMSpecInfo {
vmSpecInfo := &irs.VMSpecInfo{
Region: region,
Name: vmSpec.Name,
VCpu: irs.VCpuInfo{Count: strconv.Itoa(vmSpec.Cpu)},
Gpu: []irs.GpuInfo{{Count: strconv.Itoa(vmSpec.GPU)}},
KeyValueList: nil,
}
vmSpecInfo.Mem = strconv.FormatFloat(float64(vmSpec.Mem)*1024, 'f', 0, 64)
return vmSpecInfo
}
func (vmSpecHandler *ClouditVMSpecHandler) ListVMSpec(Region string) ([]*irs.VMSpecInfo, error) {
// log HisCall
hiscallInfo := GetCallLogScheme(vmSpecHandler.Client.IdentityEndpoint, call.VMSPEC, VMSpec, "ListVMSpec()")
vmSpecHandler.Client.TokenID = vmSpecHandler.CredentialInfo.AuthToken
authHeader := vmSpecHandler.Client.AuthenticatedHeaders()
requestOpts := client.RequestOpts{
MoreHeaders: authHeader,
}
start := call.Start()
list, err := specs.List(vmSpecHandler.Client, &requestOpts)
if err != nil {
getError := errors.New(fmt.Sprintf("failed to get VM spec list, err : %s", err.Error()))
LoggingError(hiscallInfo, getError)
return nil, getError
}
LoggingInfo(hiscallInfo, start)
vmSpecList := make([]*irs.VMSpecInfo, len(*list))
for i, spec := range *list {
vmSpecList[i] = setterVMSpec(Region, spec)
}
return vmSpecList, nil
}
func (vmSpecHandler *ClouditVMSpecHandler) GetVMSpec(Region string, Name string) (irs.VMSpecInfo, error) {
// log HisCall
hiscallInfo := GetCallLogScheme(vmSpecHandler.Client.IdentityEndpoint, call.VMSPEC, Name, "GetVMSpec()")
start := call.Start()
specInfo, err := vmSpecHandler.GetVMSpecByName(Region, Name)
if err != nil {
notFoundErr := errors.New(fmt.Sprintf("failed to get VM spec, err : %s", err.Error()))
LoggingError(hiscallInfo, notFoundErr)
return irs.VMSpecInfo{}, notFoundErr
}
LoggingInfo(hiscallInfo, start)
return *specInfo, nil
}
func (vmSpecHandler *ClouditVMSpecHandler) ListOrgVMSpec(Region string) (string, error) {
// log HisCall
hiscallInfo := GetCallLogScheme(vmSpecHandler.Client.IdentityEndpoint, call.VMSPEC, VMSpec, "ListOrgVMSpec()")
vmSpecHandler.Client.TokenID = vmSpecHandler.CredentialInfo.AuthToken
authHeader := vmSpecHandler.Client.AuthenticatedHeaders()
requestOpts := client.RequestOpts{
MoreHeaders: authHeader,
}
start := call.Start()
list, err := specs.List(vmSpecHandler.Client, &requestOpts)
if err != nil {
getErr := errors.New(fmt.Sprintf("failed to get VM spec list, err : %s", err.Error()))
LoggingError(hiscallInfo, getErr)
return "", getErr
}
LoggingInfo(hiscallInfo, start)
var jsonResult struct {
Result []specs.VMSpecInfo `json:"list"`
}
jsonResult.Result = *list
jsonBytes, err := json.Marshal(jsonResult)
if err != nil {
LoggingError(hiscallInfo, err)
return "", err
}
jsonString := string(jsonBytes)
return jsonString, nil
}
func (vmSpecHandler *ClouditVMSpecHandler) GetOrgVMSpec(Region string, Name string) (string, error) {
// log HisCall
hiscallInfo := GetCallLogScheme(vmSpecHandler.Client.IdentityEndpoint, call.VMSPEC, Name, "GetOrgVMSpec()")
start := call.Start()
specInfo, err := vmSpecHandler.GetVMSpecByName(Region, Name)
if err != nil {
notFoundErr := errors.New(fmt.Sprintf("failed to get VM spec, err : %s", err.Error()))
LoggingError(hiscallInfo, notFoundErr)
return "", notFoundErr
}
LoggingInfo(hiscallInfo, start)
jsonBytes, err := json.Marshal(specInfo)
if err != nil {
LoggingError(hiscallInfo, err)
return "", err
}
jsonString := string(jsonBytes)
return jsonString, err
}
func (vmSpecHandler *ClouditVMSpecHandler) GetVMSpecByName(region string, specName string) (*irs.VMSpecInfo, error) {
vmSpecHandler.Client.TokenID = vmSpecHandler.CredentialInfo.AuthToken
authHeader := vmSpecHandler.Client.AuthenticatedHeaders()
requestOpts := client.RequestOpts{
MoreHeaders: authHeader,
}
specList, err := specs.List(vmSpecHandler.Client, &requestOpts)
if err != nil {
return nil, err
}
var specInfo *irs.VMSpecInfo
for _, spec := range *specList {
if strings.EqualFold(spec.Name, specName) {
specInfo = setterVMSpec(region, spec)
break
}
}
if specInfo == nil {
err := errors.New(fmt.Sprintf("failed to find vmSpec with name %s", specName))
return nil, err
}
return specInfo, nil
}