-
Notifications
You must be signed in to change notification settings - Fork 45
/
IBMCloudDriver.go
136 lines (126 loc) · 4.08 KB
/
IBMCloudDriver.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
package ibmcloudvpc
import (
"context"
"errors"
"github.com/IBM/go-sdk-core/v5/core"
"github.com/IBM/platform-services-go-sdk/globaltaggingv1"
vpc0230 "github.com/IBM/vpc-go-sdk/0.23.0/vpcv1"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/drivers/ibmcloud-vpc/connect"
ibms "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/drivers/ibmcloud-vpc/resources"
"github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/drivers/ibmcloud-vpc/utils/kubernetesserviceapiv1"
idrv "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces"
icon "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/interfaces/connect"
"time"
)
type IbmCloudDriver struct{}
const (
cspTimeout time.Duration = 6000
)
func (IbmCloudDriver) GetDriverVersion() string {
return "IBM DRIVER Version 1.0"
}
func (IbmCloudDriver) GetDriverCapability() idrv.DriverCapabilityInfo {
var drvCapabilityInfo idrv.DriverCapabilityInfo
drvCapabilityInfo.ImageHandler = true
drvCapabilityInfo.VPCHandler = true
drvCapabilityInfo.SecurityHandler = true
drvCapabilityInfo.KeyPairHandler = true
drvCapabilityInfo.VNicHandler = false
drvCapabilityInfo.PublicIPHandler = false
drvCapabilityInfo.VMHandler = true
drvCapabilityInfo.VMSpecHandler = true
drvCapabilityInfo.NLBHandler = true
drvCapabilityInfo.RegionZoneHandler = true
drvCapabilityInfo.PriceInfoHandler = true
return drvCapabilityInfo
}
func (driver *IbmCloudDriver) ConnectCloud(connectionInfo idrv.ConnectionInfo) (icon.CloudConnection, error) {
ibms.InitLog()
err := checkConnectionInfo(connectionInfo)
if err != nil {
return nil, err
}
ctx, _ := context.WithTimeout(context.Background(), cspTimeout*time.Second)
// Region & Zone Check
initVpcService, err := vpcv1.NewVpcV1(&vpcv1.VpcV1Options{
Authenticator: &core.IamAuthenticator{
ApiKey: connectionInfo.CredentialInfo.ApiKey,
},
})
if err != nil {
return nil, err
}
var endPoint string
getRegionOptions := &vpcv1.GetRegionOptions{}
getRegionOptions.SetName(connectionInfo.RegionInfo.Region)
region, _, err := initVpcService.GetRegionWithContext(ctx, getRegionOptions)
if err != nil {
return nil, err
} else {
getZoneOptions := &vpcv1.GetRegionZoneOptions{}
getZoneOptions.SetRegionName(*region.Name)
getZoneOptions.SetName(connectionInfo.RegionInfo.Zone)
_, _, err := initVpcService.GetRegionZoneWithContext(ctx, getZoneOptions)
if err != nil {
return nil, err
}
endPoint = *region.Endpoint + "/v1"
}
vpcService, err := vpcv1.NewVpcV1(&vpcv1.VpcV1Options{
Authenticator: &core.IamAuthenticator{
ApiKey: connectionInfo.CredentialInfo.ApiKey,
},
URL: endPoint,
})
if err != nil {
return nil, err
}
vpcService0230, err := vpc0230.NewVpcV1(&vpc0230.VpcV1Options{
Authenticator: &core.IamAuthenticator{
ApiKey: connectionInfo.CredentialInfo.ApiKey,
},
URL: endPoint,
})
if err != nil {
return nil, err
}
clusterService, err := kubernetesserviceapiv1.NewKubernetesServiceApiV1(&kubernetesserviceapiv1.KubernetesServiceApiV1Options{
Authenticator: &core.IamAuthenticator{
ApiKey: connectionInfo.CredentialInfo.ApiKey,
},
})
if err != nil {
return nil, err
}
taggingService, err := globaltaggingv1.NewGlobalTaggingV1(&globaltaggingv1.GlobalTaggingV1Options{
Authenticator: &core.IamAuthenticator{
ApiKey: connectionInfo.CredentialInfo.ApiKey,
},
})
if err != nil {
return nil, err
}
iConn := connect.IbmCloudConnection{
CredentialInfo: connectionInfo.CredentialInfo,
Region: connectionInfo.RegionInfo,
VpcService: vpcService,
VpcService0230: vpcService0230,
ClusterService: clusterService,
TaggingService: taggingService,
Ctx: ctx,
}
return &iConn, nil
}
func checkConnectionInfo(connectionInfo idrv.ConnectionInfo) error {
if connectionInfo.CredentialInfo.ApiKey == "" {
return errors.New("not exist ApiKey")
}
if connectionInfo.RegionInfo.Region == "" {
return errors.New("not exist Region")
}
if connectionInfo.RegionInfo.Zone == "" {
return errors.New("not exist Zone")
}
return nil
}