kairat_kushaev | 6685262 | 2018-06-07 17:27:35 +0400 | [diff] [blame] | 1 | import logging |
| 2 | import six |
| 3 | import uuid |
| 4 | |
| 5 | import os_client_config |
| 6 | from salt import exceptions |
| 7 | |
| 8 | |
| 9 | log = logging.getLogger(__name__) |
| 10 | |
| 11 | SERVICE_KEY = 'orchestration' |
| 12 | |
| 13 | |
| 14 | def get_raw_client(cloud_name): |
| 15 | config = os_client_config.OpenStackConfig() |
| 16 | cloud = config.get_one_cloud(cloud_name) |
| 17 | adapter = cloud.get_session_client(SERVICE_KEY) |
| 18 | adapter.version = '1' |
| 19 | try: |
| 20 | access_info = adapter.session.auth.get_access(adapter.session) |
| 21 | endpoints = access_info.service_catalog.get_endpoints() |
| 22 | except (AttributeError, ValueError) as exc: |
| 23 | six.raise_from(exc, exceptions.SaltInvocationError( |
| 24 | "Cannot load keystoneauth plugin. Please check your environment " |
| 25 | "configuration.")) |
| 26 | if SERVICE_KEY not in endpoints: |
| 27 | raise exceptions.SaltInvocationError("Cannot find heat endpoint in " |
| 28 | "environment endpoint list.") |
| 29 | return adapter |
| 30 | |
| 31 | |
| 32 | def send(method): |
| 33 | def wrap(func): |
| 34 | @six.wraps(func) |
| 35 | def wrapped_f(*args, **kwargs): |
| 36 | cloud_name = kwargs.get('cloud_name', None) |
| 37 | if not cloud_name: |
| 38 | raise exceptions.SaltInvocationError( |
| 39 | "No cloud_name specified. Please provide cloud_name " |
| 40 | "parameter") |
| 41 | adapter = get_raw_client(cloud_name) |
| 42 | # Remove salt internal kwargs |
| 43 | kwarg_keys = list(kwargs.keys()) |
| 44 | for k in kwarg_keys: |
| 45 | if k.startswith('__'): |
| 46 | kwargs.pop(k) |
| 47 | url, request_kwargs = func(*args, **kwargs) |
| 48 | try: |
| 49 | response = getattr(adapter, method.lower())(url, |
| 50 | **request_kwargs) |
| 51 | except Exception as e: |
| 52 | log.exception("Error occured when executing request") |
| 53 | return {"result": False, |
| 54 | "comment": str(e), |
| 55 | "status_code": getattr(e, "http_status", 500)} |
| 56 | try: |
| 57 | resp_body = response.json() if response.content else {} |
| 58 | except: |
| 59 | resp_body = str(response.content) |
| 60 | return {"result": True, |
| 61 | "body": resp_body, |
| 62 | "status_code": response.status_code} |
| 63 | return wrapped_f |
| 64 | return wrap |
| 65 | |
| 66 | |
| 67 | def _check_uuid(val): |
| 68 | try: |
| 69 | return str(uuid.UUID(val)) == val |
| 70 | except (TypeError, ValueError, AttributeError): |
| 71 | return False |
| 72 | |
| 73 | |
| 74 | def get_by_name_or_uuid(resource_list, resp_key): |
| 75 | def wrap(func): |
| 76 | @six.wraps(func) |
| 77 | def wrapped_f(*args, **kwargs): |
| 78 | if 'name' in kwargs: |
| 79 | ref = kwargs.get('name', None) |
| 80 | start_arg = 0 |
| 81 | else: |
| 82 | start_arg = 1 |
| 83 | ref = args[0] |
| 84 | kwargs["name"] = ref |
| 85 | if _check_uuid(ref): |
| 86 | uuid = ref |
| 87 | else: |
| 88 | # Then we have name not uuid |
| 89 | cloud_name = kwargs['cloud_name'] |
| 90 | resp = resource_list( |
| 91 | name=ref, cloud_name=cloud_name)["body"][resp_key] |
| 92 | if len(resp) == 0: |
| 93 | msg = ("Uniq {resource} resource " |
| 94 | "with name={name} not found.").format( |
| 95 | resource=resp_key, name=ref) |
| 96 | return {"result": False, |
| 97 | "body": msg, |
| 98 | "status_code": 404} |
| 99 | elif len(resp) > 1: |
| 100 | msg = ("Multiple resource: {resource} " |
| 101 | "with name: {name} found ").format( |
| 102 | resource=resp_key, name=ref) |
| 103 | return {"result": False, |
| 104 | "body": msg, |
| 105 | "status_code": 400} |
| 106 | uuid = resp[0]['id'] |
| 107 | return func(uuid, *args[start_arg:], **kwargs) |
| 108 | return wrapped_f |
| 109 | return wrap |