|
45 | 45 | except ImportError:
|
46 | 46 | HAS_AZURE_LIBRARIES = False
|
47 | 47 |
|
| 48 | +try: |
| 49 | + from cs import AIOCloudStack, CloudStackApiException |
| 50 | + HAS_CS_LIBRARIES = True |
| 51 | +except ImportError: |
| 52 | + HAS_CS_LIBRARIES = False |
| 53 | + |
48 | 54 | routes = web.RouteTableDef()
|
49 | 55 | PROJECT_ROOT = dirname(dirname(__file__))
|
50 | 56 | pool = None
|
@@ -322,97 +328,53 @@ async def linode_regions(_):
|
322 | 328 |
|
323 | 329 | @routes.get('/cloudstack_config')
|
324 | 330 | async def get_cloudstack_config(_):
|
325 |
| - response = {'has_secret': False} |
326 |
| - if 'CLOUDSTACK_CONFIG' in os.environ: |
327 |
| - try: |
328 |
| - open(os.environ['CLOUDSTACK_CONFIG'], 'r').read() |
329 |
| - response['has_secret'] = True |
330 |
| - except IOError: |
331 |
| - pass |
332 |
| - # check default path |
333 |
| - default_path = expanduser(join('~', '.cloudstack.ini')) |
334 |
| - try: |
335 |
| - open(default_path, 'r').read() |
336 |
| - response['has_secret'] = True |
337 |
| - except IOError: |
338 |
| - pass |
| 331 | + if not HAS_REQUESTS: |
| 332 | + return web.json_response({'error': 'missing_requests'}, status=400) |
| 333 | + if not HAS_CS_LIBRARIES: |
| 334 | + return web.json_response({'error': 'missing_cloudstack'}, status=400) |
| 335 | + response = {'has_secret': _read_cloudstack_config() is not None} |
339 | 336 | return web.json_response(response)
|
340 | 337 |
|
341 | 338 |
|
342 |
| -@routes.post('/cloudstack_config') |
343 |
| -async def post_cloudstack_config(request): |
344 |
| - data = await request.json() |
345 |
| - with open(join(PROJECT_ROOT, 'cloudstack.ini'), 'w') as f: |
346 |
| - try: |
347 |
| - config = data.config_text |
348 |
| - except Exception as e: |
349 |
| - return web.json_response({'error': { |
350 |
| - 'code': type(e).__name__, |
351 |
| - 'message': e, |
352 |
| - }}, status=400) |
353 |
| - else: |
354 |
| - f.write(config) |
355 |
| - return web.json_response({'ok': True}) |
356 |
| - |
357 |
| - |
358 |
| -def _get_cloudstack_config(path=None): |
359 |
| - if path: |
360 |
| - try: |
361 |
| - return open(os.environ['CLOUDSTACK_CONFIG'], 'r').read() |
362 |
| - except IOError: |
363 |
| - pass |
364 |
| - |
| 339 | +def _read_cloudstack_config(): |
365 | 340 | if 'CLOUDSTACK_CONFIG' in os.environ:
|
366 | 341 | try:
|
367 | 342 | return open(os.environ['CLOUDSTACK_CONFIG'], 'r').read()
|
368 | 343 | except IOError:
|
369 | 344 | pass
|
370 |
| - |
| 345 | + # check default path |
371 | 346 | default_path = expanduser(join('~', '.cloudstack.ini'))
|
372 |
| - return open(default_path, 'r').read() |
373 |
| - |
374 |
| - |
375 |
| -def _sign(command, secret): |
376 |
| - """Adds the signature bit to a command expressed as a dict""" |
377 |
| - # order matters |
378 |
| - arguments = sorted(command.items()) |
379 |
| - |
380 |
| - # urllib.parse.urlencode is not good enough here. |
381 |
| - # key contains should only contain safe content already. |
382 |
| - # safe="*" is required when producing the signature. |
383 |
| - query_string = "&".join("=".join((key, quote(value, safe="*"))) |
384 |
| - for key, value in arguments) |
385 |
| - |
386 |
| - # Signing using HMAC-SHA1 |
387 |
| - digest = hmac.new( |
388 |
| - secret.encode("utf-8"), |
389 |
| - msg=query_string.lower().encode("utf-8"), |
390 |
| - digestmod=hashlib.sha1).digest() |
391 |
| - |
392 |
| - signature = base64.b64encode(digest).decode("utf-8") |
393 |
| - |
394 |
| - return dict(command, signature=signature) |
| 347 | + try: |
| 348 | + return open(default_path, 'r').read() |
| 349 | + except IOError: |
| 350 | + pass |
| 351 | + return None |
395 | 352 |
|
396 | 353 |
|
397 |
| -@routes.get('/cloudstack_regions') |
| 354 | +@routes.post('/cloudstack_regions') |
398 | 355 | async def cloudstack_regions(request):
|
399 |
| - data = {} #await request.json() |
| 356 | + data = await request.json() |
| 357 | + client_config = data.get('token') |
400 | 358 | config = configparser.ConfigParser()
|
401 |
| - config.read_string(_get_cloudstack_config(data.get('cs_config'))) |
| 359 | + config.read_string(_read_cloudstack_config() or client_config) |
402 | 360 | section = config[config.sections()[0]]
|
403 |
| - |
404 |
| - compute_endpoint = section.get('endpoint', '') |
405 |
| - api_key = section.get('key', '') |
406 |
| - api_secret = section.get('secret', '') |
407 |
| - params = _sign({ |
408 |
| - "command": "listZones", |
409 |
| - "apikey": api_key}, api_secret) |
410 |
| - query_string = urlencode(params) |
411 |
| - |
412 |
| - async with ClientSession() as session: |
413 |
| - async with session.get(f'{compute_endpoint}?{query_string}') as r: |
414 |
| - json_body = await r.json() |
415 |
| - return web.json_response(json_body) |
| 361 | + client = AIOCloudStack(**section) |
| 362 | + try: |
| 363 | + zones = await client.listZones(fetch_list=True) |
| 364 | + except CloudStackApiException as resp: |
| 365 | + return web.json_response({ |
| 366 | + 'cloud_stack_error': resp.error |
| 367 | + }, status=400) |
| 368 | + # if config was passed from client, save it after successful zone retrieval |
| 369 | + if _read_cloudstack_config() is None: |
| 370 | + path = os.environ['CLOUDSTACK_CONFIG'] or expanduser(join('~', '.cloudstack.ini')) |
| 371 | + with open(path, 'w') as f: |
| 372 | + try: |
| 373 | + f.write(client_config) |
| 374 | + except IOError as e: |
| 375 | + return web.json_response({'error': 'can not save config file'}, status=400) |
| 376 | + |
| 377 | + return web.json_response(zones) |
416 | 378 |
|
417 | 379 |
|
418 | 380 | app = web.Application()
|
|
0 commit comments