def get_port_tag_dict(self): """Get a dict of port names and associated vlan tags. e.g. the returned dict is of the following form:: {u'int-br-eth2': [], u'patch-tun': [], u'qr-76d9e6b6-21': 1, u'tapce5318ff-78': 1, u'tape1400310-e6': 1} The TAG ID is only available in the "Port" table and is not available in the "Interface" table queried by the get_vif_port_set() method. """ port_names = self.get_port_name_list() args = ['--format=json', '--', '--columns=name,tag', 'list', 'Port'] result = self.run_vsctl(args, check_error=True) port_tag_dict = {} if not result: return port_tag_dict for name, tag in jsonutils.loads(result)['data']: if name not in port_names: continue # 'tag' can be [u'set', []] or an integer if isinstance(tag, list): tag = tag[1] port_tag_dict[name] = tag return port_tag_dict
def __call__(self, req): # Determine the user ID user_id = req.headers.get('X_USER_ID', req.headers.get('X_USER')) if not user_id: LOG.debug(_("Neither X_USER_ID nor X_USER found in request")) return webob.exc.HTTPUnauthorized() # Determine the tenant tenant_id = req.headers.get('X_TENANT_ID', req.headers.get('X_TENANT')) # Suck Authentication token auth_token=req.headers.get('X_AUTH_TOKEN') # Suck User Name user_name=req.headers.get('X_USER_NAME') # Suck Service Catalog service_catalog = jsonutils.loads(req.headers.get('X_SERVICE_CATALOG')) # Suck out the roles roles = [r.strip() for r in req.headers.get('X_ROLE', '').split(',')] # Create a context with the authentication data ctx = context.Context(user_id, tenant_id, roles=roles, auth_token=auth_token, user_name=user_name, service_catalog=service_catalog) # Inject the context... req.environ['crdservice.context'] = ctx return self.application
def get_configuration_dict(self, agent_db): try: conf = jsonutils.loads(agent_db.configurations) except Exception: msg = _("Configuration for agent %(agent_type)s on host %(host)s" " is invalid.") LOG.warn(msg, {"agent_type": agent_db.agent_type, "host": agent_db.host}) conf = {} return conf
def load_json(cls, data, default_rule=None): """ Allow loading of JSON rule data. """ # Suck in the JSON data and parse the rules rules = dict((k, parse_rule(v)) for k, v in jsonutils.loads(data).items()) return cls(rules, default_rule)
def get_vif_port_by_id(self, port_id): args = ['--format=json', '--', '--columns=external_ids,name,ofport', 'find', 'Interface', 'external_ids:iface-id="%s"' % port_id] result = self.run_vsctl(args) if not result: return json_result = jsonutils.loads(result) try: # Retrieve the indexes of the columns we're looking for headings = json_result['headings'] ext_ids_idx = headings.index('external_ids') name_idx = headings.index('name') ofport_idx = headings.index('ofport') # If data attribute is missing or empty the line below will raise # an exeception which will be captured in this block. # We won't deal with the possibility of ovs-vsctl return multiple # rows since the interface identifier is unique data = json_result['data'][0] port_name = data[name_idx] switch = get_bridge_for_iface(self.root_helper, port_name) if switch != self.br_name: LOG.info(_("Port: %(port_name)s is on %(switch)s," " not on %(br_name)s"), {'port_name': port_name, 'switch': switch, 'br_name': self.br_name}) return ofport = data[ofport_idx] # ofport must be integer otherwise return None if not isinstance(ofport, int) or ofport == -1: LOG.warn(_("ofport: %(ofport)s for VIF: %(vif)s is not a " "positive integer"), {'ofport': ofport, 'vif': port_id}) return # Find VIF's mac address in external ids ext_id_dict = dict((item[0], item[1]) for item in data[ext_ids_idx][1]) vif_mac = ext_id_dict['attached-mac'] return VifPort(port_name, ofport, port_id, vif_mac, self) except Exception as e: LOG.warn(_("Unable to parse interface details. Exception: %s"), e) return
def deserialize_msg(msg): # NOTE(russellb): Hang on to your hats, this road is about to # get a little bumpy. # # Robustness Principle: # "Be strict in what you send, liberal in what you accept." # # At this point we have to do a bit of guessing about what it # is we just received. Here is the set of possibilities: # # 1) We received a dict. This could be 2 things: # # a) Inspect it to see if it looks like a standard message envelope. # If so, great! # # b) If it doesn't look like a standard message envelope, it could either # be a notification, or a message from before we added a message # envelope (referred to as version 1.0). # Just return the message as-is. # # 2) It's any other non-dict type. Just return it and hope for the best. # This case covers return values from rpc.call() from before message # envelopes were used. (messages to call a method were always a dict) if not isinstance(msg, dict): # See #2 above. return msg base_envelope_keys = (_VERSION_KEY, _MESSAGE_KEY) if not all(map(lambda key: key in msg, base_envelope_keys)): # See #1.b above. return msg # At this point we think we have the message envelope # format we were expecting. (#1.a above) if not version_is_compatible(_RPC_ENVELOPE_VERSION, msg[_VERSION_KEY]): raise UnsupportedRpcEnvelopeVersion(version=msg[_VERSION_KEY]) raw_msg = jsonutils.loads(msg[_MESSAGE_KEY]) return raw_msg
def deserialize_remote_exception(conf, data): failure = jsonutils.loads(str(data)) trace = failure.get('tb', []) message = failure.get('message', "") + "\n" + "\n".join(trace) name = failure.get('class') module = failure.get('module') # NOTE(ameade): We DO NOT want to allow just any module to be imported, in # order to prevent arbitrary code execution. if module not in conf.allowed_rpc_exception_modules: return RemoteError(name, failure.get('message'), trace) try: mod = importutils.import_module(module) klass = getattr(mod, name) if not issubclass(klass, Exception): raise TypeError("Can only deserialize Exceptions") failure = klass(**failure.get('kwargs', {})) except (AttributeError, TypeError, ImportError): return RemoteError(name, failure.get('message'), trace) ex_type = type(failure) str_override = lambda self: message new_ex_type = type(ex_type.__name__ + "_Remote", (ex_type,), {'__str__': str_override, '__unicode__': str_override}) try: # NOTE(ameade): Dynamically create a new exception type and swap it in # as the new type for the exception. This only works on user defined # Exceptions and not core python exceptions. This is important because # we cannot necessarily change an exception message so we must override # the __str__ method. failure.__class__ = new_ex_type except TypeError: # NOTE(ameade): If a core exception then just add the traceback to the # first exception argument. failure.args = (message,) + failure.args[1:] return failure
def get_vif_port_set(self): port_names = self.get_port_name_list() edge_ports = set() args = ['--format=json', '--', '--columns=name,external_ids,ofport', 'list', 'Interface'] result = self.run_vsctl(args, check_error=True) if not result: return edge_ports for row in jsonutils.loads(result)['data']: name = row[0] if name not in port_names: continue external_ids = dict(row[1][1]) # Do not consider VIFs which aren't yet ready # This can happen when ofport values are either [] or ["set", []] # We will therefore consider only integer values for ofport ofport = row[2] try: int_ofport = int(ofport) except (ValueError, TypeError): LOG.warn(_("Found not yet ready openvswitch port: %s"), row) else: if int_ofport > 0: if ("iface-id" in external_ids and "attached-mac" in external_ids): edge_ports.add(external_ids['iface-id']) elif ("xs-vif-uuid" in external_ids and "attached-mac" in external_ids): # if this is a xenserver and iface-id is not # automatically synced to OVS from XAPI, we grab it # from XAPI directly iface_id = self.get_xapi_iface_id( external_ids["xs-vif-uuid"]) edge_ports.add(iface_id) else: LOG.warn(_("Found failed openvswitch port: %s"), row) return edge_ports
def _from_json(self, datastring): try: return jsonutils.loads(datastring) except ValueError: msg = _("Cannot understand JSON") raise exception.MalformedRequestBody(reason=msg)