def args2body_vim(config_param, vim): """Create additional args to vim body :param vim: vim request object :return: vim body with args populated """ vim_type = ['openstack', 'kubernetes'] cert_verify_type = ['True', 'False'] if 'type' in config_param: vim['type'] = config_param.pop('type', '') if not vim['type'] in vim_type: raise exceptions.TackerClientException( message='Supported VIM types: openstack, kubernetes', status_code=400) else: vim['type'] = 'openstack' if vim['type'] == 'openstack': vim['vim_project'] = { 'name': config_param.pop('project_name', ''), 'project_domain_name': config_param.pop('project_domain_name', '') } if not vim['vim_project']['name']: raise exceptions.TackerClientException( message='Project name must be specified', status_code=404) cert_verify = config_param.pop('cert_verify', 'True') if cert_verify not in cert_verify_type: raise exceptions.TackerClientException( message='Supported cert_verify types: True, False', status_code=400) vim['auth_cred'] = { 'username': config_param.pop('username', ''), 'password': config_param.pop('password', ''), 'user_domain_name': config_param.pop('user_domain_name', ''), 'cert_verify': cert_verify } elif vim['type'] == 'kubernetes': vim['vim_project'] = {'name': config_param.pop('project_name', '')} if not vim['vim_project']['name']: raise exceptions.TackerClientException( message='Project name must be specified in Kubernetes VIM,' 'it is namespace in Kubernetes environment', status_code=404) if ('username' in config_param) and ('password' in config_param): vim['auth_cred'] = { 'username': config_param.pop('username', ''), 'password': config_param.pop('password', '') } elif 'bearer_token' in config_param: vim['auth_cred'] = { 'bearer_token': config_param.pop('bearer_token', '') } else: raise exceptions.TackerClientException( message='username and password or bearer_token must be' 'provided', status_code=404) ssl_ca_cert = config_param.pop('ssl_ca_cert', '') if ssl_ca_cert: vim['auth_cred']['ssl_ca_cert'] = ssl_ca_cert
def exception_handler_v10(status_code, error_content): """Exception handler for API v1.0 client. This routine generates the appropriate Tacker exception according to the contents of the response body. :param status_code: HTTP error status code :param error_content: deserialized body of error response """ error_dict = None if isinstance(error_content, dict): error_dict = error_content.get('TackerError') if not error_dict: error_content = error_content.get(STATUS_CODE_MAP.get(status_code), 'tackerFault') # Find real error type bad_tacker_error_flag = False if error_dict: # If Tacker key is found, it will definitely contain # a 'message' and 'type' keys? try: error_type = error_dict['type'] error_message = error_dict['message'] if error_dict['detail']: error_message += "\n" + error_dict['detail'] except Exception: bad_tacker_error_flag = True if not bad_tacker_error_flag: # If corresponding exception is defined, use it. client_exc = getattr(exceptions, '%sClient' % error_type, None) # Otherwise look up per status-code client exception if not client_exc: client_exc = exceptions.HTTP_EXCEPTION_MAP.get(status_code) if client_exc: raise client_exc(message=error_message, status_code=status_code) else: raise exceptions.TackerClientException( status_code=status_code, message=error_message) else: raise exceptions.TackerClientException(status_code=status_code, message=error_dict) else: message = None if isinstance(error_content, dict): message = error_content.get('message') if message: raise exceptions.TackerClientException(status_code=status_code, message=message) # If we end up here the exception was not a tacker error msg = "%s-%s" % (status_code, error_content) raise exceptions.TackerClientException(status_code=status_code, message=msg)
def find_resourceid_by_id(client, resource, resource_id): resource_plural = _get_resource_plural(resource, client) obj_lister = getattr(client, "list_%s" % resource_plural) # perform search by id only if we are passing a valid UUID match = re.match(UUID_PATTERN, resource_id) collection = resource_plural if match: data = obj_lister(id=resource_id, fields='id') if data and data[collection]: return data[collection][0]['id'] not_found_message = (_("Unable to find %(resource)s with id " "'%(id)s'") % {'resource': resource, 'id': resource_id}) # 404 is used to simulate server side behavior raise exceptions.TackerClientException( message=not_found_message, status_code=404)
def args2body(self, parsed_args): body = {self.resource: {}} # config arg passed as data overrides config yaml when both args passed if parsed_args.config_file: with open(parsed_args.config_file) as f: config_yaml = f.read() config_param = yaml.load(config_yaml) if 'auth_url' in config_param: raise exceptions.TackerClientException(message='Auth URL cannot ' 'be updated', status_code=404) vim_obj = body[self.resource] vim_utils.args2body_vim(config_param, vim_obj) tackerV10.update_dict(parsed_args, body[self.resource], ['tenant_id', 'is_default']) return body
def _find_resourceid_by_name(client, resource, name): resource_plural = _get_resource_plural(resource, client) obj_lister = getattr(client, "list_%s" % resource_plural) data = obj_lister(name=name, fields='id') collection = resource_plural info = data[collection] if len(info) > 1: raise exceptions.TackerClientNoUniqueMatch(resource=resource, name=name) elif len(info) == 0: not_found_message = (_("Unable to find %(resource)s with name " "'%(name)s'") % {'resource': resource, 'name': name}) # 404 is used to simulate server side behavior raise exceptions.TackerClientException( message=not_found_message, status_code=404) else: return info[0]['id']
def args2body_vim(config_param, vim): """Create additional args to vim body :param vim: vim request object :return: vim body with args populated """ vim['vim_project'] = { 'name': config_param.pop('project_name', ''), 'project_domain_name': config_param.pop('project_domain_name', '') } if not vim['vim_project']['name']: raise exceptions.TackerClientException(message='Project name ' 'must be specified', status_code=404) vim['auth_cred'] = { 'username': config_param.pop('username', ''), 'password': config_param.pop('password', ''), 'user_domain_name': config_param.pop('user_domain_name', '') }
def args2body(self, parsed_args): body = {self.resource: {}} if parsed_args.config_file: with open(parsed_args.config_file) as f: vim_config = f.read() config_param = yaml.load(vim_config, Loader=yaml.SafeLoader) vim_obj = body[self.resource] try: auth_url = config_param.pop('auth_url') except KeyError: raise exceptions.TackerClientException(message='Auth URL must be ' 'specified', status_code=404) vim_obj['auth_url'] = vim_utils.validate_auth_url(auth_url).geturl() vim_obj['type'] = config_param.pop('type', 'openstack') vim_utils.args2body_vim(config_param, vim_obj) tackerV10.update_dict( parsed_args, body[self.resource], ['tenant_id', 'name', 'description', 'is_default']) return body
def validate_auth_url(url): url_parts = urlparse.urlparse(url) if not url_parts.scheme or not url_parts.netloc: raise exceptions.TackerClientException(message='Invalid auth URL') return url_parts