def request(self, method, url, token=None, args=None, headers=None, accepted_codes=[200, 201, 202, 204]): params = {} if args: # extract filter and fields if 'filters' in args: params = args.pop('filters') if 'fields' in args: params['fields'] = args.pop('fields') args = jsonutils.dumps(args) if not headers: headers = { 'Content-type': 'application/json', 'X-Auth-Token': token } headers['User-Agent'] = OCTAVIA_PROXY_CLIENT url = '{}/{}'.format(self.base_url, str(url)) LOG.debug("url = %s", url) LOG.debug("args = %s", args) LOG.debug("params = %s", str(params)) r = requests.request(method, url, data=args, params=params, headers=headers) LOG.debug("Octavia Response Code: {0}".format(r.status_code)) LOG.debug("Octavia Response Body: {0}".format(r.content)) LOG.debug("Octavia Response Headers: {0}".format(r.headers)) if r.status_code in accepted_codes: if method != 'DELETE': return r.json() elif r.status_code == 413: e = lib_exc.OverQuota() e.msg = str(r.content) raise e elif r.status_code == 409: e = lib_exc.Conflict() e.msg = str(r.content) raise e elif r.status_code == 401: e = lib_exc.NotAuthorized() e.msg = str(r.content) raise e elif r.status_code == 404: e = lib_exc.NotFound() e.msg = str(r.content) raise e elif r.status_code == 400: e = lib_exc.BadRequest(resource="", msg="") e.msg = str(r.content) raise e else: raise loadbalancerv2.DriverError(msg=str(r.content))
def _subnet_create(self, context, subnet, clean_on_err=True): if subnet['enable_dhcp']: network_id = subnet['network_id'] # Create port for DHCP service dhcp_port = { "name": "", "admin_state_up": True, "device_id": "", "device_owner": const.DEVICE_OWNER_DHCP, "network_id": network_id, "tenant_id": subnet["tenant_id"], "mac_address": const.ATTR_NOT_SPECIFIED, "fixed_ips": [{"subnet_id": subnet['id']}] } try: # This will end up calling handle_port_dhcp_access # down below as well as handle_port_metadata_access self.plugin.create_port(context, {'port': dhcp_port}) except p_exc.PortConfigurationError as e: LOG.error(_LE("Error while creating subnet %(cidr)s for " "network %(network)s. Please, contact " "administrator"), {"cidr": subnet["cidr"], "network": network_id}) db_base_plugin_v2.NeutronDbPluginV2.delete_port( self.plugin, context, e.port_id) if clean_on_err: self.plugin.delete_subnet(context, subnet['id']) raise n_exc.Conflict()
def _validate_multiple_subnets_routers(self, context, router_id, interface_info): _nsxv_plugin = self.plugin net_id, subnet_id = _nsxv_plugin._get_interface_info(context, interface_info) port_filters = {'device_owner': [l3_db.DEVICE_OWNER_ROUTER_INTF], 'network_id': [net_id]} intf_ports = _nsxv_plugin.get_ports(context.elevated(), filters=port_filters) router_ids = [port['device_id'] for port in intf_ports] all_routers = _nsxv_plugin.get_routers(context, filters={'id': router_ids}) dist_routers = [router['id'] for router in all_routers if router.get('distributed') is True] if len(dist_routers) > 0: err_msg = _("network can only be attached to just one distributed " "router, the network is already attached to router " "%(router_id)s") % {'router_id': dist_routers[0]} if router_id in dist_routers: # attach to the same router again raise n_exc.InvalidInput(error_message=err_msg) else: # attach to multiple routers raise n_exc.Conflict(error_message=err_msg)
def _validate_subnets_routers(self, context, router_id, interface_info): # Validate that multiple subnets are not connected to the router _nsxv_plugin = self.plugin net_id, subnet_id = _nsxv_plugin._get_interface_info(context, interface_info) port_filters = {'device_owner': [l3_db.DEVICE_OWNER_ROUTER_INTF], 'network_id': [net_id]} intf_ports = _nsxv_plugin.get_ports(context.elevated(), filters=port_filters) router_ids = [port['device_id'] for port in intf_ports] all_routers = _nsxv_plugin.get_routers(context, filters={'id': router_ids}) dist_routers = [router['id'] for router in all_routers if router.get('distributed') is True] if len(dist_routers) > 0: err_msg = _("network can only be attached to just one distributed " "router, the network is already attached to router " "%(router_id)s") % {'router_id': dist_routers[0]} if router_id in dist_routers: # attach to the same router again raise n_exc.InvalidInput(error_message=err_msg) else: # attach to multiple routers raise n_exc.Conflict(error_message=err_msg) # Validate that the subnet is not a v6 one subnet = self.plugin.get_subnet(context.elevated(), subnet_id) if (subnet.get('ip_version') == 6 or (subnet['cidr'] not in (constants.ATTR_NOT_SPECIFIED, None) and netaddr.IPNetwork(subnet['cidr']).version == 6)): err_msg = _("No support for IPv6 interfaces") raise n_exc.InvalidInput(error_message=err_msg)
def _find_or_create_policygroup(self, context, security_group_id, vsd_subnet): external_id = cms_id_helper.get_vsd_external_id(security_group_id) policygroups = self.get_policygroups(external_id, vsd_subnet) if len(policygroups) > 1: msg = _("Found multiple policygroups with externalID %s") raise n_exc.Conflict(msg=msg % external_id) elif len(policygroups) == 1: return policygroups[0] else: return self._create_policygroup(context, security_group_id, vsd_subnet)
def _find_or_create_policygroup_using_rt(self, context, security_group_id, redirect_target, vsd_managed): parent_id = redirect_target['parentID'] parent_type = redirect_target['parentType'] external_id = cms_id_helper.get_vsd_external_id(security_group_id) policygroups = self._get_policygroups(external_id, parent_id, parent_type) if len(policygroups) > 1: msg = _("Found multiple policygroups with externalID %s") raise n_exc.Conflict(msg=msg % external_id) elif len(policygroups) == 1: return policygroups[0] else: return self._create_pg_for_rt(context, security_group_id, redirect_target, vsd_managed)
def _find_or_create_policygroup(self, context, security_group_id, vsd_subnet): external_id = cms_id_helper.get_vsd_external_id(security_group_id) if vsd_subnet['type'] == constants.L2DOMAIN: policygroups = self.vsdclient.get_nuage_l2domain_policy_groups( vsd_subnet['ID'], externalID=external_id) else: domain_id = self.vsdclient.get_router_by_domain_subnet_id( vsd_subnet['ID']) policygroups = self.vsdclient.get_nuage_domain_policy_groups( domain_id, externalID=external_id) if len(policygroups) > 1: msg = _("Found multiple policygroups with externalID %s") raise n_exc.Conflict(msg=msg % external_id) elif len(policygroups) == 1: return policygroups[0] else: return self._create_policygroup(context, security_group_id, vsd_subnet)
def _find_or_create_policygroup_using_rt(self, context, security_group_id, redirect_target, vsd_managed): parent_id = redirect_target['parentID'] parent_type = redirect_target['parentType'] external_id = cms_id_helper.get_vsd_external_id(security_group_id) if parent_type == constants.L2DOMAIN: policygroups = self.vsdclient.get_nuage_l2domain_policy_groups( parent_id, externalID=external_id) else: policygroups = self.vsdclient.get_nuage_domain_policy_groups( parent_id, externalID=external_id) if len(policygroups) > 1: msg = _("Found multiple policygroups with externalID %s") raise n_exc.Conflict(msg=msg % external_id) elif len(policygroups) == 1: return policygroups[0] else: return self._create_pg_for_rt(context, security_group_id, redirect_target, vsd_managed)
def convert_exception_to_http_exc(e, faults, language): serializer = wsgi.JSONDictSerializer() if isinstance(e, exceptions.MultipleExceptions): converted_exceptions = [ convert_exception_to_http_exc(inner, faults, language) for inner in e.inner_exceptions ] # if no internal exceptions, will be handled as single exception if converted_exceptions: codes = {c.code for c in converted_exceptions} if len(codes) == 1: # all error codes are the same so we can maintain the code # and just concatenate the bodies joined_msg = "\n".join( (jsonutils.loads(c.body)['NeutronError']['message'] for c in converted_exceptions)) new_body = jsonutils.loads(converted_exceptions[0].body) new_body['NeutronError']['message'] = joined_msg converted_exceptions[0].body = serializer.serialize(new_body) return converted_exceptions[0] else: # multiple error types so we turn it into a Conflict with the # inner codes and bodies packed in new_exception = exceptions.Conflict() inner_error_strings = [] for c in converted_exceptions: c_body = jsonutils.loads(c.body) err = ('HTTP %s %s: %s' % (c.code, c_body['NeutronError']['type'], c_body['NeutronError']['message'])) inner_error_strings.append(err) new_exception.msg = "\n".join(inner_error_strings) return convert_exception_to_http_exc(new_exception, faults, language) e = translate(e, language) body = serializer.serialize({'NeutronError': get_exception_data(e)}) kwargs = {'body': body, 'content_type': 'application/json'} if isinstance(e, exc.HTTPException): # already an HTTP error, just update with content type and body e.body = body e.content_type = kwargs['content_type'] return e faults_tuple = tuple(faults.keys()) + (exceptions.NeutronException, ) if isinstance(e, faults_tuple): for fault in faults: if isinstance(e, fault): mapped_exc = faults[fault] break else: mapped_exc = exc.HTTPInternalServerError return mapped_exc(**kwargs) if isinstance(e, NotImplementedError): # NOTE(armando-migliaccio): from a client standpoint # it makes sense to receive these errors, because # extensions may or may not be implemented by # the underlying plugin. So if something goes south, # because a plugin does not implement a feature, # returning 500 is definitely confusing. kwargs['body'] = serializer.serialize( {'NotImplementedError': get_exception_data(e)}) return exc.HTTPNotImplemented(**kwargs) # NOTE(jkoelker) Everything else is 500 # Do not expose details of 500 error to clients. msg = _('Request Failed: internal server error while ' 'processing your request.') msg = translate(msg, language) kwargs['body'] = serializer.serialize( {'NeutronError': get_exception_data(exc.HTTPInternalServerError(msg))}) return exc.HTTPInternalServerError(**kwargs)
def raise_conflict(self): raise n_exc.Conflict()