-
Notifications
You must be signed in to change notification settings - Fork 48
/
AzureDriver.go
331 lines (276 loc) · 11.3 KB
/
AzureDriver.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
// 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, 2019.07.
package azure
import (
"context"
"time"
"github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2021-03-01/compute"
"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-02-01/network"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-10-01/resources"
"github.com/Azure/go-autorest/autorest/azure/auth"
"github.com/Azure/go-autorest/autorest/to"
azcon "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/drivers/azure/connect"
azrs "github.com/cloud-barista/cb-spider/cloud-control-manager/cloud-driver/drivers/azure/resources"
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"
)
type AzureDriver struct{}
func (AzureDriver) GetDriverVersion() string {
return "AZURE DRIVER Version 1.0"
}
const (
cspTimeout time.Duration = 6000
)
func (AzureDriver) 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
return drvCapabilityInfo
}
func (driver *AzureDriver) ConnectCloud(connectionInfo idrv.ConnectionInfo) (icon.CloudConnection, error) {
// 1. get info of credential and region for Test A Cloud from connectionInfo.
// 2. create a client object(or service object) of Test A Cloud with credential info.
// 3. create CloudConnection Instance of "connect/TDA_CloudConnection".
// 4. return CloudConnection Interface of TDA_CloudConnection.
// Initialize Logger
azrs.InitLog()
// Credentail에 등록된 ResourceGroup 존재 여부 체크 및 생성
err := checkResourceGroup(connectionInfo.CredentialInfo, connectionInfo.RegionInfo)
if err != nil {
return nil, err
}
Ctx, VMClient, err := getVMClient(connectionInfo.CredentialInfo)
if err != nil {
return nil, err
}
Ctx, imageClient, err := getImageClient(connectionInfo.CredentialInfo)
if err != nil {
return nil, err
}
Ctx, publicIPClient, err := getPublicIPClient(connectionInfo.CredentialInfo)
if err != nil {
return nil, err
}
Ctx, sgClient, err := getSecurityGroupClient(connectionInfo.CredentialInfo)
if err != nil {
return nil, err
}
Ctx, vNicClient, err := getVNicClient(connectionInfo.CredentialInfo)
if err != nil {
return nil, err
}
Ctx, SubnetClient, err := getSubnetClient(connectionInfo.CredentialInfo)
if err != nil {
return nil, err
}
Ctx, VNetClient, err := getVNetworkClient(connectionInfo.CredentialInfo)
if err != nil {
return nil, err
}
Ctx, IPConfigClient, err := getIPConfigClient(connectionInfo.CredentialInfo)
if err != nil {
return nil, err
}
Ctx, VMImageClient, err := getVMImageClient(connectionInfo.CredentialInfo)
if err != nil {
return nil, err
}
Ctx, DiskClient, err := getDiskClient(connectionInfo.CredentialInfo)
if err != nil {
return nil, err
}
Ctx, VmSpecClient, err := getVmSpecClient(connectionInfo.CredentialInfo)
if err != nil {
return nil, err
}
Ctx, sshKeyClient, err := getSshKeyClient(connectionInfo.CredentialInfo)
if err != nil {
return nil, err
}
iConn := azcon.AzureCloudConnection{
CredentialInfo: connectionInfo.CredentialInfo,
Region: connectionInfo.RegionInfo,
Ctx: Ctx,
VMClient: VMClient,
ImageClient: imageClient,
PublicIPClient: publicIPClient,
SecurityGroupClient: sgClient,
VNetClient: VNetClient,
VNicClient: vNicClient,
IPConfigClient: IPConfigClient,
SubnetClient: SubnetClient,
VMImageClient: VMImageClient,
DiskClient: DiskClient,
VmSpecClient: VmSpecClient,
SshKeyClient: sshKeyClient,
}
return &iConn, nil
}
func checkResourceGroup(credential idrv.CredentialInfo, region idrv.RegionInfo) error {
config := auth.NewClientCredentialsConfig(credential.ClientId, credential.ClientSecret, credential.TenantId)
authorizer, err := config.Authorizer()
if err != nil {
return nil
}
resourceClient := resources.NewGroupsClient(credential.SubscriptionId)
resourceClient.Authorizer = authorizer
ctx, _ := context.WithTimeout(context.Background(), cspTimeout*time.Second)
rg, err := resourceClient.Get(ctx, region.ResourceGroup)
// 해당 리소스 그룹이 없을 경우 생성
if rg.ID == nil {
rg, err = resourceClient.CreateOrUpdate(ctx, region.ResourceGroup,
resources.Group{
Name: to.StringPtr(region.ResourceGroup),
Location: to.StringPtr(region.Region),
})
if err != nil {
return err
}
}
return nil
}
func getVMClient(credential idrv.CredentialInfo) (context.Context, *compute.VirtualMachinesClient, error) {
config := auth.NewClientCredentialsConfig(credential.ClientId, credential.ClientSecret, credential.TenantId)
authorizer, err := config.Authorizer()
if err != nil {
return nil, nil, err
}
vmClient := compute.NewVirtualMachinesClient(credential.SubscriptionId)
vmClient.Authorizer = authorizer
ctx, _ := context.WithTimeout(context.Background(), cspTimeout*time.Second)
return ctx, &vmClient, nil
}
func getImageClient(credential idrv.CredentialInfo) (context.Context, *compute.ImagesClient, error) {
config := auth.NewClientCredentialsConfig(credential.ClientId, credential.ClientSecret, credential.TenantId)
authorizer, err := config.Authorizer()
if err != nil {
return nil, nil, err
}
imageClient := compute.NewImagesClient(credential.SubscriptionId)
imageClient.Authorizer = authorizer
ctx, _ := context.WithTimeout(context.Background(), cspTimeout*time.Second)
return ctx, &imageClient, nil
}
func getPublicIPClient(credential idrv.CredentialInfo) (context.Context, *network.PublicIPAddressesClient, error) {
config := auth.NewClientCredentialsConfig(credential.ClientId, credential.ClientSecret, credential.TenantId)
authorizer, err := config.Authorizer()
if err != nil {
return nil, nil, err
}
publicIPClient := network.NewPublicIPAddressesClient(credential.SubscriptionId)
publicIPClient.Authorizer = authorizer
ctx, _ := context.WithTimeout(context.Background(), cspTimeout*time.Second)
return ctx, &publicIPClient, nil
}
func getSecurityGroupClient(credential idrv.CredentialInfo) (context.Context, *network.SecurityGroupsClient, error) {
config := auth.NewClientCredentialsConfig(credential.ClientId, credential.ClientSecret, credential.TenantId)
authorizer, err := config.Authorizer()
if err != nil {
return nil, nil, err
}
sgClient := network.NewSecurityGroupsClient(credential.SubscriptionId)
sgClient.Authorizer = authorizer
ctx, _ := context.WithTimeout(context.Background(), cspTimeout*time.Second)
return ctx, &sgClient, nil
}
func getVNetworkClient(credential idrv.CredentialInfo) (context.Context, *network.VirtualNetworksClient, error) {
config := auth.NewClientCredentialsConfig(credential.ClientId, credential.ClientSecret, credential.TenantId)
authorizer, err := config.Authorizer()
if err != nil {
return nil, nil, err
}
vNetClient := network.NewVirtualNetworksClient(credential.SubscriptionId)
vNetClient.Authorizer = authorizer
ctx, _ := context.WithTimeout(context.Background(), cspTimeout*time.Second)
return ctx, &vNetClient, nil
}
func getVNicClient(credential idrv.CredentialInfo) (context.Context, *network.InterfacesClient, error) {
config := auth.NewClientCredentialsConfig(credential.ClientId, credential.ClientSecret, credential.TenantId)
authorizer, err := config.Authorizer()
if err != nil {
return nil, nil, err
}
vNicClient := network.NewInterfacesClient(credential.SubscriptionId)
vNicClient.Authorizer = authorizer
ctx, _ := context.WithTimeout(context.Background(), cspTimeout*time.Second)
return ctx, &vNicClient, nil
}
func getIPConfigClient(credential idrv.CredentialInfo) (context.Context, *network.InterfaceIPConfigurationsClient, error) {
config := auth.NewClientCredentialsConfig(credential.ClientId, credential.ClientSecret, credential.TenantId)
authorizer, err := config.Authorizer()
if err != nil {
return nil, nil, err
}
ipConfigClient := network.NewInterfaceIPConfigurationsClient(credential.SubscriptionId)
ipConfigClient.Authorizer = authorizer
ctx, _ := context.WithTimeout(context.Background(), cspTimeout*time.Second)
return ctx, &ipConfigClient, nil
}
func getSubnetClient(credential idrv.CredentialInfo) (context.Context, *network.SubnetsClient, error) {
config := auth.NewClientCredentialsConfig(credential.ClientId, credential.ClientSecret, credential.TenantId)
authorizer, err := config.Authorizer()
if err != nil {
return nil, nil, err
}
subnetClient := network.NewSubnetsClient(credential.SubscriptionId)
subnetClient.Authorizer = authorizer
ctx, _ := context.WithTimeout(context.Background(), cspTimeout*time.Second)
return ctx, &subnetClient, nil
}
func getSshKeyClient(credential idrv.CredentialInfo) (context.Context, *compute.SSHPublicKeysClient, error) {
config := auth.NewClientCredentialsConfig(credential.ClientId, credential.ClientSecret, credential.TenantId)
authorizer, err := config.Authorizer()
if err != nil {
return nil, nil, err
}
sshClientClient := compute.NewSSHPublicKeysClient(credential.SubscriptionId)
sshClientClient.Authorizer = authorizer
ctx, _ := context.WithTimeout(context.Background(), cspTimeout*time.Second)
return ctx, &sshClientClient, nil
}
func getVMImageClient(credential idrv.CredentialInfo) (context.Context, *compute.VirtualMachineImagesClient, error) {
config := auth.NewClientCredentialsConfig(credential.ClientId, credential.ClientSecret, credential.TenantId)
authorizer, err := config.Authorizer()
if err != nil {
return nil, nil, err
}
vmImageClient := compute.NewVirtualMachineImagesClient(credential.SubscriptionId)
vmImageClient.Authorizer = authorizer
ctx, _ := context.WithTimeout(context.Background(), cspTimeout*time.Second)
return ctx, &vmImageClient, nil
}
func getDiskClient(credential idrv.CredentialInfo) (context.Context, *compute.DisksClient, error) {
config := auth.NewClientCredentialsConfig(credential.ClientId, credential.ClientSecret, credential.TenantId)
authorizer, err := config.Authorizer()
if err != nil {
return nil, nil, err
}
diskClient := compute.NewDisksClient(credential.SubscriptionId)
diskClient.Authorizer = authorizer
ctx, _ := context.WithTimeout(context.Background(), cspTimeout*time.Second)
return ctx, &diskClient, nil
}
func getVmSpecClient(credential idrv.CredentialInfo) (context.Context, *compute.VirtualMachineSizesClient, error) {
config := auth.NewClientCredentialsConfig(credential.ClientId, credential.ClientSecret, credential.TenantId)
authorizer, err := config.Authorizer()
if err != nil {
return nil, nil, err
}
vmSpecClient := compute.NewVirtualMachineSizesClient(credential.SubscriptionId)
vmSpecClient.Authorizer = authorizer
ctx, _ := context.WithTimeout(context.Background(), cspTimeout*time.Second)
return ctx, &vmSpecClient, nil
}