def exception_handler_v20(status_code, error_content): """ Exception handler for API v2.0 client This routine generates the appropriate Quantum exception according to the contents of the response body :param status_code: HTTP error status code :param error_content: deserialized body of error response """ quantum_errors = { 'NetworkNotFound': exceptions.NetworkNotFoundClient, 'NetworkInUse': exceptions.NetworkInUseClient, 'PortNotFound': exceptions.PortNotFoundClient, 'RequestedStateInvalid': exceptions.StateInvalidClient, 'PortInUse': exceptions.PortInUseClient, 'AlreadyAttached': exceptions.AlreadyAttachedClient, } error_dict = None if isinstance(error_content, dict): error_dict = error_content.get('QuantumError') # Find real error type bad_quantum_error_flag = False if error_dict: # If QuantumError key is found, it will definitely contain # a 'message' and 'type' keys? try: error_type = error_dict['type'] error_message = (error_dict['message'] + "\n" + error_dict['detail']) except Exception: bad_quantum_error_flag = True if not bad_quantum_error_flag: ex = None try: # raise the appropriate error! ex = quantum_errors[error_type](message=error_message) ex.args = ([dict(status_code=status_code, message=error_message)], ) except Exception: pass if ex: raise ex else: raise exceptions.QuantumClientException(message=error_dict) else: message = None if isinstance(error_content, dict): message = error_content.get('message', None) if message: raise exceptions.QuantumClientException(status_code=status_code, message=message) # If we end up here the exception was not a quantum error msg = "%s-%s" % (status_code, error_content) raise exceptions.QuantumClientException(status_code=status_code, message=msg)
def show_subnet(self, subnet, **_params): try: return {'subnet': self._fake_subnets[subnet]} except KeyError: msg = 'Port %s not found' % subnet raise q_exc.QuantumClientException(message=msg, status_code=404)
def show_port(self, port, **_params): try: return {'port': self._fake_ports[port]} except KeyError: msg = 'Port %s not found' % port raise q_exc.QuantumClientException(message=msg, status_code=404)
def show_network(self, network, **_params): try: return {'network': self._fake_networks[network]} except KeyError: msg = 'Network %s not found' % network raise q_exc.QuantumClientException(message=msg, status_code=404)
def show_security_group_rule(self, security_group_rule, **_params): try: return {'security_group_rule': self._fake_security_group_rules[security_group_rule]} except KeyError: msg = 'Security Group rule %s not found' % security_group_rule raise q_exc.QuantumClientException(message=msg, status_code=404)
def _check_ports_on_network(self, network): ports = self.list_ports() for port in ports: if port['network_id'] == network: msg = ('Unable to complete operation on network %s. There is ' 'one or more ports still in use on the network' % network) raise q_exc.QuantumClientException(message=msg, status_code=409)
def _validate_int(self, name, value): try: return_value = int(value) except Exception: message = (_('quota limit for %(name)s must be an integer') % { 'name': name }) raise exceptions.QuantumClientException(message=message) return return_value
def delete_security_group(self, security_group): self.show_security_group(security_group) ports = self.list_ports() for port in ports.get('ports'): for sg_port in port['security_groups']: if sg_port == security_group: msg = ('Unable to delete Security group %s in use' % security_group) raise q_exc.QuantumClientException(message=msg, status_code=409) del self._fake_security_groups[security_group]
def show_security_group(self, security_group, **_params): try: sg = self._fake_security_groups[security_group] except KeyError: msg = 'Security Group %s not found' % security_group raise q_exc.QuantumClientException(message=msg, status_code=404) for security_group_rule in self._fake_security_group_rules.values(): if security_group_rule['security_group_id'] == sg['id']: sg['security_group_rules'].append(security_group_rule) return {'security_group': sg}
def create_security_group(self, body=None): s = body.get('security_group') if len(s.get('name')) > 255 or len(s.get('description')) > 255: msg = 'Security Group name great than 255' raise q_exc.QuantumClientException(message=msg, status_code=401) ret = {'name': s.get('name'), 'description': s.get('description'), 'tenant_id': 'fake_tenant', 'security_group_rules': [], 'id': str(uuid.uuid4())} self._fake_security_groups[ret['id']] = ret return {'security_group': ret}
def _find_resourceid_by_name(client, resource, name): obj_lister = getattr(client, "list_%ss" % resource) data = obj_lister(name=name, fields='id') collection = resource + "s" info = data[collection] if len(info) > 1: msg = (_("Multiple %(resource)s matches found for name '%(name)s'," " use an ID to be more specific.") % { 'resource': resource, 'name': name }) raise exceptions.QuantumClientException(message=msg) 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.QuantumClientException(message=not_found_message, status_code=404) else: return info[0]['id']
def create_subnet(self, body): s = body.get('subnet') try: net = self._fake_networks[s.get('network_id')] except KeyError: msg = 'Network %s not found' % s.get('network_id') raise q_exc.QuantumClientException(message=msg, status_code=404) ret = {'name': s.get('name'), 'network_id': s.get('network_id'), 'tenant_id': 'fake_tenant', 'cidr': s.get('cidr'), 'id': str(uuid.uuid4()), 'gateway_ip': '10.0.0.1'} net['subnets'].append(ret['id']) self._fake_networks[net['id']] = net self._fake_subnets[ret['id']] = ret return {'subnet': ret}