Python oslo_serialization.jsonutils.loads() Examples

The following are 30 code examples of oslo_serialization.jsonutils.loads(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module oslo_serialization.jsonutils , or try the search function .
Example #1
Source File: driver.py    From networking-sfc with Apache License 2.0 6 votes vote down vote up
def _update_next_hop_details(self, flow_rule, port_detail, detail):
        # Update detail with next-hop node_type and the correlation of
        # port-pair of next-hop
        for node in port_detail['path_nodes']:
            _node = self.get_path_node(node['pathnode_id'])
            if flow_rule['fwd_path'] == _node['fwd_path']:
                detail['nsi'], detail['nsp'] = _node['nsi'], _node['nsp']
                if _node['next_hop']:
                    next_hops = jsonutils.loads(_node['next_hop'])
                    for next_hop in next_hops:
                        pp = self.get_port_detail(next_hop['portpair_id'])
                        detail['pp_corr_tap_nh'] = pp['correlation']
                        for pp_node in pp['path_nodes']:
                            pp_node_detail = self.get_path_node(
                                pp_node['pathnode_id'])
                            detail['tap_nh_node_type'] = pp_node_detail[
                                'node_type']
                            return 
Example #2
Source File: pci_device_pool.py    From zun with Apache License 2.0 6 votes vote down vote up
def from_pci_stats(pci_stats):
    """Create and return a PciDevicePoolList from the data stored in the db,

    which can be either the serialized object, or, prior to the creation of the
    device pool objects, a simple dict or a list of such dicts.
    """
    pools = []
    if isinstance(pci_stats, str):
        try:
            pci_stats = jsonutils.loads(pci_stats)
        except (ValueError, TypeError):
            pci_stats = None
    if pci_stats:
        # Check for object-ness, or old-style storage format.
        if 'zun_object.namespace' in pci_stats:
            return PciDevicePoolList.obj_from_primitive(pci_stats)
        else:
            # This can be either a dict or a list of dicts
            if isinstance(pci_stats, list):
                pools = [PciDevicePool.from_dict(stat)
                         for stat in pci_stats]
            else:
                pools = [PciDevicePool.from_dict(pci_stats)]
    return PciDevicePoolList(objects=pools) 
Example #3
Source File: cinder_workflow.py    From zun with Apache License 2.0 6 votes vote down vote up
def detach_volume(self, context, volmap):
        volume_id = volmap.cinder_volume_id
        try:
            self.cinder_api.begin_detaching(volume_id)
        except cinder_exception.BadRequest as e:
            raise exception.Invalid(_("Invalid volume: %s") %
                                    str(e))

        conn_info = jsonutils.loads(volmap.connection_info)
        if not self._volume_connection_keep(context, volume_id):
            try:
                self._disconnect_volume(conn_info)
            except Exception:
                with excutils.save_and_reraise_exception():
                    LOG.exception('Failed to disconnect volume %(volume_id)s',
                                  {'volume_id': volume_id})
                    self.cinder_api.roll_detaching(volume_id)

            self.cinder_api.terminate_connection(
                volume_id, get_volume_connector_properties())
        self.cinder_api.detach(volmap) 
Example #4
Source File: http_error.py    From zun with Apache License 2.0 6 votes vote down vote up
def __call__(self, environ, start_response):
        for err_str in self.app_iter:
            err = {}
            try:
                err = json.loads(err_str.decode('utf-8'))
            except ValueError:
                pass

            links = {'rel': 'help', 'href': 'https://docs.openstack.org'
                     '/api-guide/compute/microversions.html'}

            err['max_version'] = self.max_version
            err['min_version'] = self.min_version
            err['code'] = "zun.microversion-unsupported"
            err['links'] = [links]
            err['title'] = "Requested microversion is unsupported"

        self.app_iter = [json.dump_as_bytes(err).encode("latin-1")]
        self.headers['Content-Length'] = str(len(self.app_iter[0]))

        return super(HTTPNotAcceptableAPIVersion, self).__call__(
            environ, start_response) 
Example #5
Source File: manager.py    From zun with Apache License 2.0 6 votes vote down vote up
def update_devices_from_compute_resources(self, devices_json):
        """Sync the pci device tracker with compute node information.

        To support pci device hot plug, we sync with the compute node
        periodically, fetching all devices information from compute node,
        update the tracker and sync the DB information.

        Devices should not be hot-plugged when assigned to a container,
        but possibly the compute node has no such guarantee. The best
        we can do is to give a warning if a device is changed
        or removed while assigned.

        :param devices_json: The JSON-ified string of device information
                             that is returned from the compute node.
        """

        devices = []
        for dev in jsonutils.loads(devices_json):
            if self.dev_filter.device_assignable(dev):
                devices.append(dev)
        self._set_hvdevs(devices) 
Example #6
Source File: sfc_db.py    From networking-sfc with Apache License 2.0 6 votes vote down vote up
def _make_port_chain_dict(self, port_chain, fields=None):
        res = {
            'id': port_chain['id'],
            'name': port_chain['name'],
            'project_id': port_chain['project_id'],
            'description': port_chain['description'],
            'port_pair_groups': [
                assoc['portpairgroup_id']
                for assoc in port_chain['chain_group_associations']
            ],
            'flow_classifiers': [
                assoc['flowclassifier_id']
                for assoc in port_chain['chain_classifier_associations']
            ],
            'chain_parameters': {
                param['keyword']: jsonutils.loads(param['value'])
                for k, param in port_chain['chain_parameters'].items()
            },
            'chain_id': port_chain['chain_id'],
        }
        return db_utils.resource_fields(res, fields) 
Example #7
Source File: sfc_db.py    From networking-sfc with Apache License 2.0 6 votes vote down vote up
def _make_port_pair_dict(self, port_pair, fields=None):
        res = {
            'id': port_pair['id'],
            'name': port_pair['name'],
            'description': port_pair['description'],
            'project_id': port_pair['project_id'],
            'ingress': port_pair['ingress'],
            'egress': port_pair['egress'],
            'service_function_parameters': {
                param['keyword']: jsonutils.loads(param['value'])
                for k, param in
                port_pair['service_function_parameters'].items()
            }
        }

        return db_utils.resource_fields(res, fields) 
Example #8
Source File: test_utils.py    From ara-archive with GNU General Public License v3.0 6 votes vote down vote up
def test_playbook_treeview(self):
        ctx = ansible_run()
        treeview = jsonutils.loads(u.playbook_treeview(ctx['playbook'].id))

        # ansible_run provides two fake files:
        # /some/path/main.yml and /playbook.yml
        for f in treeview:
            if f['text'] == 'some':
                self.assertEqual(f['text'], 'some')
                child = f['nodes'][0]
                self.assertEqual(child['text'], 'path')
                child = child['nodes'][0]
                self.assertEqual(child['text'], 'main.yml')
                self.assertEqual(child['dataAttr']['load'],
                                 ctx['task_file'].id)
            else:
                self.assertEqual(f['text'], 'playbook.yml')
                self.assertEqual(f['dataAttr']['load'], ctx['pb_file'].id) 
Example #9
Source File: test_middleware.py    From ec2-api with Apache License 2.0 6 votes vote down vote up
def test_params_for_keystone_call(self, mock_request):
        req = wsgi.Request.blank('/test')
        req.GET['Signature'] = 'test-signature'
        req.GET['AWSAccessKeyId'] = 'test-key-id'
        self.kauth(req)
        mock_request.assert_called_with(
            'POST', CONF.keystone_ec2_tokens_url,
            data=mock.ANY, headers=mock.ANY)

        data = jsonutils.loads(mock_request.call_args[1]['data'])
        expected_data = {
            'ec2Credentials': {
                'access': 'test-key-id',
                'headers': {'Host': 'localhost:80'},
                'host': 'localhost:80',
                'verb': 'GET',
                'params': {'AWSAccessKeyId': 'test-key-id'},
                'signature': 'test-signature',
                'path': '/test',
                'body_hash': 'e3b0c44298fc1c149afbf4c8996fb924'
                             '27ae41e4649b934ca495991b7852b855'}}
        self.assertEqual(expected_data, data) 
Example #10
Source File: images_client.py    From tempest-lib with Apache License 2.0 6 votes vote down vote up
def list_images(self, detail=False, **params):
        """Return a list of all images filtered by any parameter.

        Available params: see http://developer.openstack.org/
                              api-ref-compute-v2.1.html#listImages
        """
        url = 'images'
        _schema = schema.list_images
        if detail:
            url += '/detail'
            _schema = schema.list_images_details

        if params:
            url += '?%s' % urllib.urlencode(params)

        resp, body = self.get(url)
        body = json.loads(body)
        self.validate_response(_schema, resp, body)
        return rest_client.ResponseBody(resp, body) 
Example #11
Source File: vif_pool.py    From kuryr-kubernetes with Apache License 2.0 6 votes vote down vote up
def _get_in_use_ports(self):
        kubernetes = clients.get_kubernetes_client()
        in_use_ports = []
        running_pods = kubernetes.get(constants.K8S_API_BASE + '/pods')
        for pod in running_pods['items']:
            try:
                annotations = jsonutils.loads(pod['metadata']['annotations'][
                    constants.K8S_ANNOTATION_VIF])
                pod_state = utils.extract_pod_annotation(annotations)
            except KeyError:
                LOG.debug("Skipping pod without kuryr VIF annotation: %s",
                          pod)
            else:
                for vif in pod_state.vifs.values():
                    in_use_ports.append(vif.id)
        return in_use_ports 
Example #12
Source File: utils.py    From kuryr-kubernetes with Apache License 2.0 6 votes vote down vote up
def get_port_annot_pci_info(nodename, neutron_port):
    k8s = clients.get_kubernetes_client()
    annot_name = constants.K8S_ANNOTATION_NODE_PCI_DEVICE_INFO
    annot_name = annot_name + '-' + neutron_port

    node_info = k8s.get('/api/v1/nodes/{}'.format(nodename))
    annotations = node_info['metadata']['annotations']
    try:
        json_pci_info = annotations[annot_name]
        pci_info = jsonutils.loads(json_pci_info)
    except KeyError:
        pci_info = {}
    except Exception:
        LOG.exception('Exception when reading annotations '
                      '%s and converting from json', annot_name)
    return pci_info 
Example #13
Source File: dpdk.py    From kuryr-kubernetes with Apache License 2.0 6 votes vote down vote up
def _get_pod_details(self, selflink):
        k8s = clients.get_kubernetes_client()
        pod = k8s.get(selflink)
        annotations = pod['metadata']['annotations']
        resource_version = pod['metadata']['resourceVersion']
        labels = pod['metadata'].get('labels')
        try:
            annotations = annotations[constants.K8S_ANNOTATION_VIF]
            state_annotation = jsonutils.loads(annotations)
            state = utils.extract_pod_annotation(state_annotation)
        except KeyError:
            LOG.exception("No annotations %s", constants.K8S_ANNOTATION_VIF)
            raise
        except ValueError:
            LOG.exception("Unable encode annotations")
            raise
        LOG.info("Got VIFs from annotation: %s", state.vifs)
        return state, labels, resource_version 
Example #14
Source File: servers_client.py    From tempest-lib with Apache License 2.0 6 votes vote down vote up
def update_server(self, server_id, **kwargs):
        """Update server.

        Available params: see http://developer.openstack.org/
                              api-ref-compute-v2.1.html#updateServer

        Most parameters except the following are passed to the API without
        any changes.
        :param disk_config: The name is changed to OS-DCF:diskConfig
        """
        if kwargs.get('disk_config'):
            kwargs['OS-DCF:diskConfig'] = kwargs.pop('disk_config')

        post_body = json.dumps({'server': kwargs})
        resp, body = self.put("servers/%s" % server_id, post_body)
        body = json.loads(body)
        self.validate_response(schema.update_server, resp, body)
        return rest_client.ResponseBody(resp, body) 
Example #15
Source File: servers_client.py    From tempest-lib with Apache License 2.0 6 votes vote down vote up
def list_servers(self, detail=False, **params):
        """List servers.

        Available params: see http://developer.openstack.org/
                              api-ref-compute-v2.1.html#listServers
                          and http://developer.openstack.org/
                              api-ref-compute-v2.1.html#listDetailServers
        """

        url = 'servers'
        _schema = schema.list_servers

        if detail:
            url += '/detail'
            _schema = schema.list_servers_detail
        if params:
            url += '?%s' % urllib.urlencode(params)

        resp, body = self.get(url)
        body = json.loads(body)
        self.validate_response(_schema, resp, body)
        return rest_client.ResponseBody(resp, body) 
Example #16
Source File: servers_client.py    From tempest-lib with Apache License 2.0 5 votes vote down vote up
def attach_volume(self, server_id, **kwargs):
        """Attaches a volume to a server instance."""
        post_body = json.dumps({'volumeAttachment': kwargs})
        resp, body = self.post('servers/%s/os-volume_attachments' % server_id,
                               post_body)
        body = json.loads(body)
        self.validate_response(schema.attach_volume, resp, body)
        return rest_client.ResponseBody(resp, body) 
Example #17
Source File: servers_client.py    From tempest-lib with Apache License 2.0 5 votes vote down vote up
def set_server_metadata_item(self, server_id, key, meta):
        post_body = json.dumps({'meta': meta})
        resp, body = self.put('servers/%s/metadata/%s' % (server_id, key),
                              post_body)
        body = json.loads(body)
        self.validate_response(schema.set_show_server_metadata_item,
                               resp, body)
        return rest_client.ResponseBody(resp, body) 
Example #18
Source File: servers_client.py    From tempest-lib with Apache License 2.0 5 votes vote down vote up
def show_server_metadata_item(self, server_id, key):
        resp, body = self.get("servers/%s/metadata/%s" % (server_id, key))
        body = json.loads(body)
        self.validate_response(schema.set_show_server_metadata_item,
                               resp, body)
        return rest_client.ResponseBody(resp, body) 
Example #19
Source File: servers_client.py    From tempest-lib with Apache License 2.0 5 votes vote down vote up
def show_password(self, server_id):
        resp, body = self.get("servers/%s/os-server-password" %
                              server_id)
        body = json.loads(body)
        self.validate_response(schema.show_password, resp, body)
        return rest_client.ResponseBody(resp, body) 
Example #20
Source File: whitelist.py    From zun with Apache License 2.0 5 votes vote down vote up
def _parse_white_list_from_config(self, whitelists):
        """Parse and validate the pci whitelist from the zun config."""
        specs = []
        for jsonspec in whitelists:
            try:
                dev_spec = jsonutils.loads(jsonspec)
            except ValueError:
                raise exception.PciConfigInvalidWhitelist(
                    reason=_("Invalid entry: '%s'") % jsonspec)
            if isinstance(dev_spec, dict):
                dev_spec = [dev_spec]
            elif not isinstance(dev_spec, list):
                raise exception.PciConfigInvalidWhitelist(
                    reason=_("Invalid entry: '%s'; "
                             "Expecting list or dict") % jsonspec)

            for ds in dev_spec:
                if not isinstance(ds, dict):
                    raise exception.PciConfigInvalidWhitelist(
                        reason=_("Invalid entry: '%s'; "
                                 "Expecting dict") % ds)

                spec = devspec.PciDeviceSpec(ds)
                specs.append(spec)

        return specs 
Example #21
Source File: certificates_client.py    From tempest-lib with Apache License 2.0 5 votes vote down vote up
def show_certificate(self, certificate_id):
        url = "os-certificates/%s" % certificate_id
        resp, body = self.get(url)
        body = json.loads(body)
        self.validate_response(schema.get_certificate, resp, body)
        return rest_client.ResponseBody(resp, body) 
Example #22
Source File: servers_client.py    From tempest-lib with Apache License 2.0 5 votes vote down vote up
def action(self, server_id, action_name,
               schema=schema.server_actions_common_schema,
               **kwargs):
        post_body = json.dumps({action_name: kwargs})
        resp, body = self.post('servers/%s/action' % server_id,
                               post_body)
        if body:
            body = json.loads(body)
        self.validate_response(schema, resp, body)
        return rest_client.ResponseBody(resp, body) 
Example #23
Source File: servers_client.py    From tempest-lib with Apache License 2.0 5 votes vote down vote up
def set_server_metadata(self, server_id, meta, no_metadata_field=False):
        if no_metadata_field:
            post_body = ""
        else:
            post_body = json.dumps({'metadata': meta})
        resp, body = self.put('servers/%s/metadata' % server_id,
                              post_body)
        body = json.loads(body)
        self.validate_response(schema.set_server_metadata, resp, body)
        return rest_client.ResponseBody(resp, body) 
Example #24
Source File: servers_client.py    From tempest-lib with Apache License 2.0 5 votes vote down vote up
def list_volume_attachments(self, server_id):
        """Returns the list of volume attachments for a given instance."""
        resp, body = self.get('servers/%s/os-volume_attachments' % (
            server_id))
        body = json.loads(body)
        self.validate_response(schema.list_volume_attachments, resp, body)
        return rest_client.ResponseBody(resp, body) 
Example #25
Source File: servers_client.py    From tempest-lib with Apache License 2.0 5 votes vote down vote up
def list_virtual_interfaces(self, server_id):
        """List the virtual interfaces used in an instance."""
        resp, body = self.get('/'.join(['servers', server_id,
                              'os-virtual-interfaces']))
        body = json.loads(body)
        self.validate_response(schema.list_virtual_interfaces, resp, body)
        return rest_client.ResponseBody(resp, body) 
Example #26
Source File: servers_client.py    From tempest-lib with Apache License 2.0 5 votes vote down vote up
def show_server_diagnostics(self, server_id):
        """Get the usage data for a server."""
        resp, body = self.get("servers/%s/diagnostics" % server_id)
        return rest_client.ResponseBody(resp, json.loads(body)) 
Example #27
Source File: servers_client.py    From tempest-lib with Apache License 2.0 5 votes vote down vote up
def list_instance_actions(self, server_id):
        """List the provided server action."""
        resp, body = self.get("servers/%s/os-instance-actions" %
                              server_id)
        body = json.loads(body)
        self.validate_response(schema.list_instance_actions, resp, body)
        return rest_client.ResponseBody(resp, body) 
Example #28
Source File: floating_ips_bulk_client.py    From tempest-lib with Apache License 2.0 5 votes vote down vote up
def delete_floating_ips_bulk(self, ip_range):
        """Deletes the provided floating IPs in bulk."""
        post_body = json.dumps({'ip_range': ip_range})
        resp, body = self.put('os-floating-ips-bulk/delete', post_body)
        body = json.loads(body)
        self.validate_response(schema.delete_floating_ips_bulk, resp, body)
        return rest_client.ResponseBody(resp, body) 
Example #29
Source File: extensions_client.py    From tempest-lib with Apache License 2.0 5 votes vote down vote up
def list_extensions(self):
        url = 'extensions'
        resp, body = self.get(url)
        body = json.loads(body)
        self.validate_response(schema.list_extensions, resp, body)
        return rest_client.ResponseBody(resp, body) 
Example #30
Source File: servers_client.py    From tempest-lib with Apache License 2.0 5 votes vote down vote up
def show_instance_action(self, server_id, request_id):
        """Returns the action details of the provided server."""
        resp, body = self.get("servers/%s/os-instance-actions/%s" %
                              (server_id, request_id))
        body = json.loads(body)
        self.validate_response(schema.show_instance_action, resp, body)
        return rest_client.ResponseBody(resp, body)