This repository has been archived by the owner on Apr 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
instances.py
executable file
·344 lines (286 loc) · 11.1 KB
/
instances.py
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
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/env python
# encoding: utf-8
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
import json
import urllib
from flask import request, url_for
from gstack import app, authentication
from gstack.services import requester
from gstack.controllers import zones, helper, operations, images, errors, machine_type, networks
def _get_virtual_machines(authorization, args=None):
command = 'listVirtualMachines'
if not args:
args = {}
cloudstack_response = requester.make_request(
command,
args,
authorization.client_id,
authorization.client_secret
)
return cloudstack_response
def _deploy_virtual_machine(authorization, args, projectid):
command = 'deployVirtualMachine'
converted_args = {}
template = images.get_template_by_name(
authorization=authorization,
image=args['template']
)
converted_args['templateid'] = template['id']
zone = zones.get_zone_by_name(
authorization=authorization,
zone=args['zone']
)
converted_args['zoneid'] = zone['id']
serviceoffering = machine_type.get_machinetype_by_name(
authorization=authorization,
machinetype=args['serviceoffering']
)
converted_args['serviceofferingid'] = serviceoffering['id']
if 'network' in args:
network = networks.get_network_by_name(
authorization=authorization,
securitygroup=args['network']
)
converted_args['securitygroupids'] = network['id']
converted_args['displayname'] = args['name']
converted_args['name'] = args['name']
converted_args['keypair'] = projectid
cloudstack_response = requester.make_request(
command,
converted_args,
authorization.client_id,
authorization.client_secret
)
return cloudstack_response
def _destroy_virtual_machine(authorization, instance):
virtual_machine_id = _get_virtual_machine_by_name(
authorization,
instance)['id']
if virtual_machine_id is None:
func_route = url_for('_destroy_virtual_machine', instance=instance)
return errors.resource_not_found(func_route)
args = {
'id': virtual_machine_id
}
return requester.make_request(
'destroyVirtualMachine',
args,
authorization.client_id,
authorization.client_secret
)
def _cloudstack_virtual_machine_to_gce(cloudstack_response, zone, projectid):
response = {}
response['kind'] = 'compute#instance'
response['id'] = cloudstack_response['id']
response['creationTimestamp'] = cloudstack_response['created']
response['status'] = cloudstack_response['state'].upper()
response['name'] = cloudstack_response['name']
response['description'] = cloudstack_response['name']
response['machineType'] = cloudstack_response['serviceofferingname']
response['image'] = cloudstack_response['templatename']
response['canIpForward'] = 'true'
response['networkInterfaces'] = []
response['disks'] = []
networking = {}
if cloudstack_response['securitygroup']:
networking['network'] = cloudstack_response['securitygroup'][0]['name']
networking['networkIP'] = cloudstack_response['nic'][0]['ipaddress']
networking['name'] = cloudstack_response['nic'][0]['id']
networking['accessConfigs'] = []
accessconfig = {}
accessconfig['kind'] = 'compute#accessConfig'
accessconfig['type'] = 'ONE_TO_ONE_NAT'
accessconfig['name'] = 'External NAT'
accessconfig['natIP'] = cloudstack_response['nic'][0]['ipaddress']
networking['accessConfigs'] = accessconfig
response['networkInterfaces'].append(networking)
response['selfLink'] = urllib.unquote_plus(helper.get_root_url() + url_for(
'getinstance',
projectid=projectid,
instance=cloudstack_response['name'],
zone=zone
))
response['zone'] = zone
return response
def _get_virtual_machine_by_name(authorization, instance):
virtual_machine_list = _get_virtual_machines(
authorization=authorization,
args={
'keyword': instance
}
)
if virtual_machine_list['listvirtualmachinesresponse']:
response = helper.filter_by_name(
data=virtual_machine_list[
'listvirtualmachinesresponse']['virtualmachine'],
name=instance
)
return response
else:
return None
@app.route('/' + app.config['PATH'] + '<projectid>/aggregated/instances', methods=['GET'])
@authentication.required
def aggregatedlistinstances(authorization, projectid):
zone_list = zones.get_zone_names(authorization=authorization)
virtual_machine_list = _get_virtual_machines(authorization=authorization)
instance = None
filter = helper.get_filter(request.args)
if 'name' in filter:
instance = filter['name']
items = {}
zone_instances = []
for zone in zone_list:
if instance:
virtual_machine = _get_virtual_machine_by_name(
authorization=authorization,
instance=instance
)
if virtual_machine:
zone_instances.append(
_cloudstack_virtual_machine_to_gce(
cloudstack_response=virtual_machine,
projectid=projectid,
zone=zone
)
)
elif virtual_machine_list['listvirtualmachinesresponse']:
for instance in virtual_machine_list['listvirtualmachinesresponse']['virtualmachine']:
zone_instances.append(
_cloudstack_virtual_machine_to_gce(
cloudstack_response=instance,
projectid=projectid,
zone=zone
)
)
items['zone/' + zone] = {}
items['zone/' + zone]['instances'] = zone_instances
populated_response = {
'kind': 'compute#instanceAggregatedList',
'id': 'projects/' + projectid + '/instances',
'selfLink': request.base_url,
'items': items
}
return helper.create_response(data=populated_response)
@app.route('/' + app.config['PATH'] + '<projectid>/zones/<zone>/instances', methods=['GET'])
@authentication.required
def listinstances(authorization, projectid, zone):
instance = None
filter = helper.get_filter(request.args)
if 'name' in filter:
instance = filter['name']
items = []
if instance:
virtual_machine = _get_virtual_machine_by_name(
authorization=authorization,
instance=instance
)
if virtual_machine:
items.append(
_cloudstack_virtual_machine_to_gce(
cloudstack_response=virtual_machine,
projectid=projectid,
zone=zone
)
)
else:
virtual_machine_list = _get_virtual_machines(
authorization=authorization)
if virtual_machine_list['listvirtualmachinesresponse']:
for instance in virtual_machine_list['listvirtualmachinesresponse']['virtualmachine']:
items.append(
_cloudstack_virtual_machine_to_gce(
cloudstack_response=instance,
projectid=projectid,
zone=zone,
)
)
populated_response = {
'kind': 'compute#instance_list',
'id': 'projects/' + projectid + '/instances',
'selfLink': request.base_url,
'items': items
}
return helper.create_response(data=populated_response)
@app.route('/' + app.config['PATH'] + '<projectid>/zones/<zone>/instances/<instance>', methods=['GET'])
@authentication.required
def getinstance(projectid, authorization, zone, instance):
response = _get_virtual_machine_by_name(
authorization=authorization,
instance=instance
)
if response:
return helper.create_response(
data=_cloudstack_virtual_machine_to_gce(
cloudstack_response=response,
projectid=projectid,
zone=zone
)
)
else:
function_route = url_for(
'getinstance',
projectid=projectid,
instance=instance)
return errors.resource_not_found(function_route)
@app.route('/' + app.config['PATH'] + '<projectid>/zones/<zone>/instances', methods=['POST'])
@authentication.required
def addinstance(authorization, projectid, zone):
data = json.loads(request.data)
print data
args = {}
args['name'] = data['name']
args['serviceoffering'] = data['machineType'].rsplit('/', 1)[1]
args['template'] = data['disks'][0]['initializeParams']['sourceImage'].rsplit('/', 1)[1]
args['zone'] = zone
network = data['networkInterfaces'][0]['network'].rsplit('/', 1)[1]
if network is not 'default':
args['network'] = network
deployment_result = _deploy_virtual_machine(authorization, args, projectid)
if 'errortext' in deployment_result['deployvirtualmachineresponse']:
populated_response = {
'kind': 'compute#operation',
'operationType': 'insert',
'targetLink': '',
'status': 'DONE',
'progress': 100,
'error': {
'errors': [{
'code': 'RESOURCE_ALREADY_EXISTS',
'message': 'the resource \'projects/\'' + projectid +
'/zones/' + zone + '/instances/' + args['name']
}]
}
}
else:
populated_response = operations.create_response(
projectid=projectid,
operationid=deployment_result[
'deployvirtualmachineresponse']['jobid'],
authorization=authorization
)
return helper.create_response(data=populated_response)
@app.route('/' + app.config['PATH'] + '<projectid>/zones/<zone>/instances/<instance>', methods=['DELETE'])
@authentication.required
def deleteinstance(projectid, authorization, zone, instance):
deletion_result = _destroy_virtual_machine(authorization, instance)
populated_response = operations.create_response(
projectid=projectid,
operationid=deletion_result['destroyvirtualmachineresponse']['jobid'],
authorization=authorization
)
return helper.create_response(data=populated_response)