-
Notifications
You must be signed in to change notification settings - Fork 47
/
VMSpecHandler.go
180 lines (170 loc) · 6.11 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
package resources
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/IBM/vpc-go-sdk/vpcv1"
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"
"strconv"
"strings"
)
type IbmVmSpecHandler struct {
CredentialInfo idrv.CredentialInfo
Region idrv.RegionInfo
VpcService *vpcv1.VpcV1
Ctx context.Context
}
func (vmSpecHandler *IbmVmSpecHandler) ListVMSpec() ([]*irs.VMSpecInfo, error) {
hiscallInfo := GetCallLogScheme(vmSpecHandler.Region, call.VMSPEC, "VMSpec", "ListVMSpec()")
start := call.Start()
var specList []*irs.VMSpecInfo
options := &vpcv1.ListInstanceProfilesOptions{}
profiles, _, err := vmSpecHandler.VpcService.ListInstanceProfilesWithContext(vmSpecHandler.Ctx, options)
if err != nil {
getErr := errors.New(fmt.Sprintf("Failed to Get VMSpecList. err = %s", err.Error()))
cblogger.Error(getErr.Error())
LoggingError(hiscallInfo, getErr)
return nil, getErr
}
for _, profile := range profiles.Profiles {
vmSpecInfo, err := setVmSpecInfo(profile, vmSpecHandler.Region.Region)
if err != nil {
getErr := errors.New(fmt.Sprintf("Failed to Get VMSpecList. err = %s", err.Error()))
cblogger.Error(getErr.Error())
LoggingError(hiscallInfo, getErr)
return nil, getErr
}
specList = append(specList, &vmSpecInfo)
}
LoggingInfo(hiscallInfo, start)
return specList, nil
}
func (vmSpecHandler *IbmVmSpecHandler) GetVMSpec(Name string) (irs.VMSpecInfo, error) {
hiscallInfo := GetCallLogScheme(vmSpecHandler.Region, call.VMSPEC, Name, "GetVMSpec()")
start := call.Start()
if Name == "" {
getErr := errors.New(fmt.Sprintf("Failed to Get VMSpec. err = invalid Name"))
cblogger.Error(getErr.Error())
LoggingError(hiscallInfo, getErr)
return irs.VMSpecInfo{}, getErr
}
profile, err := getRawSpec(Name, vmSpecHandler.VpcService, vmSpecHandler.Ctx)
if err != nil {
getErr := errors.New(fmt.Sprintf("Failed to Get VMSpec. err = %s", err.Error()))
cblogger.Error(getErr.Error())
LoggingError(hiscallInfo, getErr)
return irs.VMSpecInfo{}, getErr
}
vmSpecInfo, err := setVmSpecInfo(profile, vmSpecHandler.Region.Region)
if err != nil {
getErr := errors.New(fmt.Sprintf("Failed to Get VMSpec. err = %s", err.Error()))
cblogger.Error(getErr.Error())
LoggingError(hiscallInfo, getErr)
return irs.VMSpecInfo{}, getErr
}
LoggingInfo(hiscallInfo, start)
return vmSpecInfo, nil
}
func (vmSpecHandler *IbmVmSpecHandler) ListOrgVMSpec() (string, error) {
hiscallInfo := GetCallLogScheme(vmSpecHandler.Region, call.VMSPEC, "OrgVMSpec", "ListOrgVMSpec()")
start := call.Start()
var specList []*irs.VMSpecInfo
options := &vpcv1.ListInstanceProfilesOptions{}
profiles, _, err := vmSpecHandler.VpcService.ListInstanceProfilesWithContext(vmSpecHandler.Ctx, options)
if err != nil {
getErr := errors.New(fmt.Sprintf("Failed to Get OrgVMSpecList. err = %s", err.Error()))
cblogger.Error(getErr.Error())
LoggingError(hiscallInfo, getErr)
return "", getErr
}
for _, profile := range profiles.Profiles {
vmSpecInfo, err := setVmSpecInfo(profile, vmSpecHandler.Region.Region)
if err != nil {
getErr := errors.New(fmt.Sprintf("Failed to Get OrgVMSpecList. err = %s", err.Error()))
cblogger.Error(getErr.Error())
LoggingError(hiscallInfo, getErr)
return "", getErr
}
specList = append(specList, &vmSpecInfo)
}
jsonBytes, err := json.Marshal(specList)
if err != nil {
getErr := errors.New(fmt.Sprintf("Failed to Get OrgVMSpecList. err = %s", err.Error()))
cblogger.Error(getErr.Error())
LoggingError(hiscallInfo, getErr)
return "", getErr
}
jsonString := string(jsonBytes)
LoggingInfo(hiscallInfo, start)
return jsonString, nil
}
func (vmSpecHandler *IbmVmSpecHandler) GetOrgVMSpec(Name string) (string, error) {
hiscallInfo := GetCallLogScheme(vmSpecHandler.Region, call.VMSPEC, "OrgVMSpec", "GetOrgVMSpec()")
start := call.Start()
if Name == "" {
getErr := errors.New(fmt.Sprintf("Failed to Get OrgVMSpec. err = invalid Name"))
cblogger.Error(getErr.Error())
LoggingError(hiscallInfo, getErr)
return "", getErr
}
profile, err := getRawSpec(Name, vmSpecHandler.VpcService, vmSpecHandler.Ctx)
if err != nil {
getErr := errors.New(fmt.Sprintf("Failed to Get OrgVMSpec. err = %s", err.Error()))
cblogger.Error(getErr.Error())
LoggingError(hiscallInfo, getErr)
return "", getErr
}
vmSpecInfo, err := setVmSpecInfo(profile, vmSpecHandler.Region.Region)
if err != nil {
getErr := errors.New(fmt.Sprintf("Failed to Get OrgVMSpec. err = %s", err.Error()))
cblogger.Error(getErr.Error())
LoggingError(hiscallInfo, getErr)
return "", getErr
}
jsonBytes, err := json.Marshal(vmSpecInfo)
if err != nil {
getErr := errors.New(fmt.Sprintf("Failed to Get OrgVMSpec. err = %s", err.Error()))
cblogger.Error(getErr.Error())
LoggingError(hiscallInfo, getErr)
return "", getErr
}
jsonString := string(jsonBytes)
LoggingInfo(hiscallInfo, start)
return jsonString, nil
}
func setVmSpecInfo(profile vpcv1.InstanceProfile, region string)(irs.VMSpecInfo, error){
if profile.Name == nil {
return irs.VMSpecInfo{}, errors.New(fmt.Sprintf("Invalid vmspec"))
}
vmSpecInfo := irs.VMSpecInfo{
Region: region,
Name: *profile.Name,
}
specslice := strings.Split(*profile.Name, "-")
if len(specslice) > 1 {
specslice2 := strings.Split(specslice[1], "x")
if len(specslice2) > 1 {
vmSpecInfo.VCpu = irs.VCpuInfo{Count: specslice2[0]}
memValue, err := strconv.Atoi(specslice2[1])
if err != nil {
memValue = 0
}
memValue = memValue * 1024
memValueString := strconv.Itoa(memValue)
vmSpecInfo.Mem = memValueString
}
}
return vmSpecInfo, nil
}
func getRawSpec(specName string, vpcService *vpcv1.VpcV1, ctx context.Context) (vpcv1.InstanceProfile, error) {
getInstanceProfileOptions := &vpcv1.GetInstanceProfileOptions{}
getInstanceProfileOptions.SetName(specName)
profile, _, err := vpcService.GetInstanceProfileWithContext(ctx, getInstanceProfileOptions)
if err != nil {
return vpcv1.InstanceProfile{}, err
}
return *profile, nil
}