Python oslo_serialization.jsonutils.loads() Examples

The following are code examples for showing how to use oslo_serialization.jsonutils.loads(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: manager.py    Apache License 2.0 6 votes vote down vote up
def update_devices_from_hypervisor_resources(self, devices_json):
        """Sync the pci device tracker with hypervisor information.

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

        Devices should not be hot-plugged when assigned to a guest,
        but possibly the hypervisor 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 virt driver's
                             get_available_resource() call in the
                             pci_passthrough_devices key.
        """

        devices = []
        for dev in jsonutils.loads(devices_json):
            if self.dev_filter.device_assignable(dev):
                devices.append(dev)
        self._set_hvdevs(devices) 
Example 2
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: messaging.py    Apache License 2.0 6 votes vote down vote up
def message_from_json(self, json_message):
        """Turns a message in JSON format into an appropriate Message
        instance.  This is called when cells receive a message from
        another cell.
        """
        message_dict = jsonutils.loads(json_message)
        # Need to convert context back.
        ctxt = message_dict['ctxt']
        message_dict['ctxt'] = context.RequestContext.from_dict(ctxt)
        # NOTE(comstud): We also need to re-serialize any objects that
        # exist in 'method_kwargs'.
        method_kwargs = message_dict['method_kwargs']
        for k, v in method_kwargs.items():
            method_kwargs[k] = self.serializer.deserialize_entity(
                    message_dict['ctxt'], v)
        message_type = message_dict.pop('message_type')
        message_cls = _CELL_MESSAGE_TYPE_TO_MESSAGE_CLS[message_type]
        return message_cls(self, **message_dict) 
Example 3
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: instance_pci_requests.py    Apache License 2.0 6 votes vote down vote up
def obj_from_db(cls, context, instance_uuid, db_requests):
        self = cls(context=context, requests=[],
                   instance_uuid=instance_uuid)
        if db_requests is not None:
            requests = jsonutils.loads(db_requests)
        else:
            requests = []
        for request in requests:
            request_obj = InstancePCIRequest(
                count=request['count'], spec=request['spec'],
                alias_name=request['alias_name'], is_new=request['is_new'],
                request_id=request['request_id'])
            request_obj.obj_reset_changes()
            self.requests.append(request_obj)
        self.obj_reset_changes()
        return self 
Example 4
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: monitor_metric.py    Apache License 2.0 6 votes vote down vote up
def from_json(cls, metrics):
        """Converts a legacy json object into a list of MonitorMetric objs
        and finally returns of MonitorMetricList

        :param metrics: a string of json serialized objects
        :returns: a MonitorMetricList Object.
        """
        metrics = jsonutils.loads(metrics) if metrics else []

        # NOTE(suro-patz): While instantiating the MonitorMetric() from
        #                  JSON-ified string, we need to re-convert the
        #                  normalized metrics to avoid truncation to 0 by
        #                  typecasting into an integer.
        metric_list = []
        for metric in metrics:
            if ('value' in metric and metric['name'] in
                                      FIELDS_REQUIRING_CONVERSION):
                metric['value'] = metric['value'] * 100
            metric_list.append(MonitorMetric(**metric))

        return MonitorMetricList(objects=metric_list)

    # NOTE(jaypipes): This method exists to convert the object to the
    # format expected by the RPC notifier for metrics events. 
Example 5
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: instance.py    Apache License 2.0 6 votes vote down vote up
def _flavor_from_db(self, db_flavor):
        """Load instance flavor information from instance_extra."""

        flavor_info = jsonutils.loads(db_flavor)

        self.flavor = objects.Flavor.obj_from_primitive(flavor_info['cur'])
        if flavor_info['old']:
            self.old_flavor = objects.Flavor.obj_from_primitive(
                flavor_info['old'])
        else:
            self.old_flavor = None
        if flavor_info['new']:
            self.new_flavor = objects.Flavor.obj_from_primitive(
                flavor_info['new'])
        else:
            self.new_flavor = None
        self.obj_reset_changes(['flavor', 'old_flavor', 'new_flavor']) 
Example 6
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: pci_device_pool.py    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, six.string_types):
        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 'nova_object.namespace' in pci_stats:
            return objects.PciDevicePoolList.obj_from_primitive(pci_stats)
        else:
            # This can be either a dict or a list of dicts
            if isinstance(pci_stats, list):
                pools = [objects.PciDevicePool.from_dict(stat)
                         for stat in pci_stats]
            else:
                pools = [objects.PciDevicePool.from_dict(pci_stats)]
    return objects.PciDevicePoolList(objects=pools) 
Example 7
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: assisted_volume_snapshots.py    Apache License 2.0 6 votes vote down vote up
def delete(self, req, id):
        """Delete a snapshot."""
        context = req.environ['nova.context']
        authorize(context, action='delete')

        LOG.info(_LI("Delete snapshot with id: %s"), id, context=context)

        delete_metadata = {}
        delete_metadata.update(req.GET)

        try:
            delete_info = jsonutils.loads(delete_metadata['delete_info'])
            volume_id = delete_info['volume_id']
        except (KeyError, ValueError) as e:
            raise webob.exc.HTTPBadRequest(explanation=six.text_type(e))

        try:
            self.compute_api.volume_snapshot_delete(context, volume_id,
                    id, delete_info)
        except exception.NotFound:
            return webob.exc.HTTPNotFound()

        return webob.Response(status_int=204) 
Example 8
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: assisted_volume_snapshots.py    Apache License 2.0 6 votes vote down vote up
def delete(self, req, id):
        """Delete a snapshot."""
        context = req.environ['nova.context']
        authorize(context, action='delete')

        LOG.info(_LI("Delete snapshot with id: %s"), id, context=context)

        delete_metadata = {}
        delete_metadata.update(req.GET)

        try:
            delete_info = jsonutils.loads(delete_metadata['delete_info'])
            volume_id = delete_info['volume_id']
        except (KeyError, ValueError) as e:
            raise exc.HTTPBadRequest(explanation=six.text_type(e))

        try:
            self.compute_api.volume_snapshot_delete(context, volume_id,
                    id, delete_info)
        except (exception.VolumeBDMNotFound,
                exception.InvalidVolume) as error:
            raise exc.HTTPBadRequest(explanation=error.format_message())
        except exception.NotFound as e:
            return exc.HTTPNotFound(explanation=e.format_message()) 
Example 9
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: wsgi.py    Apache License 2.0 6 votes vote down vote up
def action_peek(body):
    """Determine action to invoke.

    This looks inside the json body and fetches out the action method
    name.
    """

    try:
        decoded = jsonutils.loads(body)
    except ValueError:
        msg = _("cannot understand JSON")
        raise exception.MalformedRequestBody(reason=msg)

    # Make sure there's exactly one key...
    if len(decoded) != 1:
        msg = _("too many body keys")
        raise exception.MalformedRequestBody(reason=msg)

    # Return the action name
    return list(decoded.keys())[0] 
Example 10
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: rbd_utils.py    Apache License 2.0 6 votes vote down vote up
def get_mon_addrs(self):
        args = ['ceph', 'mon', 'dump', '--format=json'] + self.ceph_args()
        out, _ = utils.execute(*args)
        lines = out.split('\n')
        if lines[0].startswith('dumped monmap epoch'):
            lines = lines[1:]
        monmap = jsonutils.loads('\n'.join(lines))
        addrs = [mon['addr'] for mon in monmap['mons']]
        hosts = []
        ports = []
        for addr in addrs:
            host_port = addr[:addr.rindex('/')]
            host, port = host_port.rsplit(':', 1)
            hosts.append(host.strip('[]'))
            ports.append(port)
        return hosts, ports 
Example 11
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: block_device.py    Apache License 2.0 6 votes vote down vote up
def _transform(self):
        if (not self._bdm_obj.source_type == self._valid_source
                or not self._bdm_obj.destination_type ==
                self._valid_destination):
            raise _InvalidType

        self.update(
            {k: v for k, v in six.iteritems(self._bdm_obj)
             if k in self._new_fields | set(['delete_on_termination'])}
        )
        self['mount_device'] = self._bdm_obj.device_name
        try:
            self['connection_info'] = jsonutils.loads(
                self._bdm_obj.connection_info)
        except TypeError:
            self['connection_info'] = None 
Example 12
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: host.py    Apache License 2.0 6 votes vote down vote up
def call_xenhost(session, method, arg_dict):
    """There will be several methods that will need this general
    handling for interacting with the xenhost plugin, so this abstracts
    out that behavior.
    """
    # Create a task ID as something that won't match any instance ID
    try:
        result = session.call_plugin('xenhost', method, args=arg_dict)
        if not result:
            return ''
        return jsonutils.loads(result)
    except ValueError:
        LOG.exception(_LE("Unable to get updated status"))
        return None
    except session.XenAPI.Failure as e:
        LOG.error(_LE("The call to %(method)s returned "
                      "an error: %(e)s."), {'method': method, 'e': e})
        return e.details[1] 
Example 13
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: api_samples_test_base.py    Apache License 2.0 6 votes vote down vote up
def objectify(data):
    if not data:
        return {}
    # NOTE(sdague): templates will contain values like %(foo)s
    # throughout them. If these are inside of double quoted
    # strings, life is good, and we can treat it just like valid
    # json to load it to python.
    #
    # However we've got some fields which are ints, like
    # aggregate_id. This means we've got a snippet in the sample
    # that looks like:
    #
    #     "id": %(aggregate_id)s,
    #
    # which is not valid json, and will explode. We do a quick and
    # dirty transform of this to:
    #
    #     "id": "%(int:aggregate_id)s",
    #
    # That makes it valid data to convert to json, but keeps
    # around the information that we need to drop those strings
    # later. The regex anchors from the ': ', as all of these will
    # be top rooted keys.
    data = re.sub(r'(\: )%\((.+)\)s([^"])', r'\1"%(int:\2)s"\3', data)
    return jsonutils.loads(data) 
Example 14
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: notification_sample_base.py    Apache License 2.0 6 votes vote down vote up
def _verify_notification(self, sample_file_name, replacements=None):
        """Assert if the generated notification matches with the stored sample

        :param sample_file_name: The name of the sample file to match relative
                                 to doc/notification_samples
        :param replacements: A dict of key value pairs that is used to update
                             the payload field of the sample data before it is
                             matched against the generated notification.
                             The 'x.y':'new-value' key-value pair selects the
                             ["payload"]["nova_object.data"]["x"]
                             ["nova_object.data"]["y"] value from the sample
                             data and overrides it with 'new-value'.
        """

        self.assertEqual(1, len(fake_notifier.VERSIONED_NOTIFICATIONS))
        notification = fake_notifier.VERSIONED_NOTIFICATIONS[0]

        with open(self._get_notification_sample(sample_file_name)) as sample:
            sample_data = sample.read()

        sample_obj = jsonutils.loads(sample_data)
        self._apply_replacements(replacements, sample_obj)

        self.assertJsonEqual(sample_obj, notification) 
Example 15
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_request_spec.py    Apache License 2.0 6 votes vote down vote up
def _check_update_primitive(self, req_obj, changes):
        self.assertEqual(req_obj.instance_uuid, changes['instance_uuid'])
        serialized_obj = objects.RequestSpec.obj_from_primitive(
                jsonutils.loads(changes['spec']))

        # primitive fields
        for field in ['instance_uuid', 'num_instances', 'ignore_hosts',
                'project_id', 'scheduler_hints', 'force_hosts',
                'availability_zone', 'force_nodes']:
            self.assertEqual(getattr(req_obj, field),
                    getattr(serialized_obj, field))

        # object fields
        for field in ['image', 'numa_topology', 'pci_requests', 'flavor',
                'retry', 'limits', 'instance_group']:
            self.assertDictEqual(
                    getattr(req_obj, field).obj_to_primitive(),
                    getattr(serialized_obj, field).obj_to_primitive()) 
Example 16
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_disk_config.py    Apache License 2.0 6 votes vote down vote up
def test_create_server_with_auto_disk_config(self):
        req = fakes.HTTPRequest.blank('/fake/servers')
        req.method = 'POST'
        req.content_type = 'application/json'
        body = {'server': {
                  'name': 'server_test',
                  'imageRef': 'cedef40a-ed67-4d10-800e-17455edce175',
                  'flavorRef': '1',
                  API_DISK_CONFIG: 'AUTO'
               }}
        old_create = compute_api.API.create

        def create(*args, **kwargs):
            self.assertIn('auto_disk_config', kwargs)
            self.assertTrue(kwargs['auto_disk_config'])
            return old_create(*args, **kwargs)

        self.stubs.Set(compute_api.API, 'create', create)

        req.body = jsonutils.dump_as_bytes(body)
        res = req.get_response(self.app)
        server_dict = jsonutils.loads(res.body)['server']
        self.assertDiskConfig(server_dict, 'AUTO') 
Example 17
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_disk_config.py    Apache License 2.0 6 votes vote down vote up
def test_rebuild_server_with_auto_disk_config(self):
        req = fakes.HTTPRequest.blank(
            '/fake/servers/%s/action' % AUTO_INSTANCE_UUID)
        req.method = 'POST'
        req.content_type = 'application/json'
        body = {"rebuild": {
                  'imageRef': 'cedef40a-ed67-4d10-800e-17455edce175',
                  API_DISK_CONFIG: 'AUTO'
               }}

        def rebuild(*args, **kwargs):
            self.assertIn('auto_disk_config', kwargs)
            self.assertTrue(kwargs['auto_disk_config'])

        self.stubs.Set(compute_api.API, 'rebuild', rebuild)

        req.body = jsonutils.dump_as_bytes(body)
        res = req.get_response(self.app)
        server_dict = jsonutils.loads(res.body)['server']
        self.assertDiskConfig(server_dict, 'AUTO') 
Example 18
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_extensions.py    Apache License 2.0 6 votes vote down vote up
def test_bad_request(self):
        res_ext = base_extensions.ResourceExtension('tweedles',
                                  StubController(response_body))
        manager = StubExtensionManager(res_ext)
        app = compute.APIRouter(manager)
        request = webob.Request.blank("/fake/tweedles")
        request.method = "POST"
        response = request.get_response(app)
        self.assertEqual(400, response.status_int)
        self.assertEqual('application/json', response.content_type)
        body = jsonutils.loads(response.body)
        expected = {
            "badRequest": {
                "message": "All aboard the fail train!",
                "code": 400
            }
        }
        self.assertThat(expected, matchers.DictMatches(body)) 
Example 19
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_extensions.py    Apache License 2.0 6 votes vote down vote up
def test_non_exist_resource(self):
        res_ext = base_extensions.ResourceExtension('tweedles',
                                               StubController(response_body))
        manager = StubExtensionManager(res_ext)
        app = compute.APIRouter(manager)
        request = webob.Request.blank("/fake/tweedles/1")
        response = request.get_response(app)
        self.assertEqual(404, response.status_int)
        self.assertEqual('application/json', response.content_type)
        body = jsonutils.loads(response.body)
        expected = {
            "itemNotFound": {
                "message": "The resource could not be found.",
                "code": 404
            }
        }
        self.assertThat(expected, matchers.DictMatches(body)) 
Example 20
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_flavorextradata.py    Apache License 2.0 6 votes vote down vote up
def test_show(self):
        expected = {
            'flavor': {
                'id': '1',
                'name': 'test',
                'ram': 512,
                'vcpus': 1,
                'disk': 1,
                'OS-FLV-EXT-DATA:ephemeral': 1,
            }
        }

        url = self.base_url + '/1'
        req = webob.Request.blank(url)
        req.headers['Content-Type'] = 'application/json'
        res = req.get_response(self.app)
        body = jsonutils.loads(res.body)
        self._verify_flavor_response(body['flavor'], expected['flavor']) 
Example 21
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: manager.py    Apache License 2.0 5 votes vote down vote up
def _driver_detach_volume(self, context, instance, bdm):
        """Do the actual driver detach using block device mapping."""
        mp = bdm.device_name
        volume_id = bdm.volume_id

        LOG.info(_LI('Detach volume %(volume_id)s from mountpoint %(mp)s'),
                  {'volume_id': volume_id, 'mp': mp},
                  context=context, instance=instance)

        connection_info = jsonutils.loads(bdm.connection_info)
        # NOTE(vish): We currently don't use the serial when disconnecting,
        #             but added for completeness in case we ever do.
        if connection_info and 'serial' not in connection_info:
            connection_info['serial'] = volume_id
        try:
            if not self.driver.instance_exists(instance):
                LOG.warning(_LW('Detaching volume from unknown instance'),
                            context=context, instance=instance)

            encryption = encryptors.get_encryption_metadata(
                context, self.volume_api, volume_id, connection_info)

            self.driver.detach_volume(connection_info,
                                      instance,
                                      mp,
                                      encryption=encryption)
        except exception.DiskNotFound as err:
            LOG.warning(_LW('Ignoring DiskNotFound exception while detaching '
                            'volume %(volume_id)s from %(mp)s: %(err)s'),
                        {'volume_id': volume_id, 'mp': mp, 'err': err},
                        instance=instance)
        except Exception:
            with excutils.save_and_reraise_exception():
                LOG.exception(_LE('Failed to detach volume %(volume_id)s '
                                  'from %(mp)s'),
                              {'volume_id': volume_id, 'mp': mp},
                              context=context, instance=instance)
                self.volume_api.roll_detaching(context, volume_id)

        return connection_info 
Example 22
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: manager.py    Apache License 2.0 5 votes vote down vote up
def _init_volume_connection(self, context, new_volume_id,
                                old_volume_id, connector, instance, bdm):

        new_cinfo = self.volume_api.initialize_connection(context,
                                                          new_volume_id,
                                                          connector)
        old_cinfo = jsonutils.loads(bdm['connection_info'])
        if old_cinfo and 'serial' not in old_cinfo:
            old_cinfo['serial'] = old_volume_id
        new_cinfo['serial'] = old_cinfo['serial']
        return (old_cinfo, new_cinfo) 
Example 23
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: whitelist.py    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 nova 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 24
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: request.py    Apache License 2.0 5 votes vote down vote up
def _get_alias_from_config():
    """Parse and validate PCI aliases from the nova config."""
    jaliases = CONF.pci_alias
    aliases = {}  # map alias name to alias spec list
    try:
        for jsonspecs in jaliases:
            spec = jsonutils.loads(jsonspecs)
            jsonschema.validate(spec, _ALIAS_SCHEMA)
            name = spec.pop("name")
            dev_type = spec.pop('device_type', None)
            if dev_type:
                spec['dev_type'] = dev_type
            if name not in aliases:
                aliases[name] = [spec]
            else:
                if aliases[name][0]["dev_type"] == spec["dev_type"]:
                    aliases[name].append(spec)
                else:
                    reason = _("Device type mismatch for alias '%s'") % name
                    raise exception.PciInvalidAlias(reason=reason)

    except exception.PciInvalidAlias:
        raise
    except Exception as e:
        raise exception.PciInvalidAlias(reason=six.text_type(e))

    return aliases 
Example 25
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: manager.py    Apache License 2.0 5 votes vote down vote up
def check_token(self, context, token):
        token_str = self.mc.get(token.encode('UTF-8'))
        token_valid = (token_str is not None)
        LOG.info(_LI("Checking Token: %(token)s, %(token_valid)s"),
                  {'token': token, 'token_valid': token_valid})
        if token_valid:
            token = jsonutils.loads(token_str)
            if self._validate_token(context, token):
                return token 
Example 26
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: messaging.py    Apache License 2.0 5 votes vote down vote up
def run_compute_api_method(self, message, method_info):
        """Run a method in the compute api class."""
        method = method_info['method']
        fn = getattr(self.compute_api, method, None)
        if not fn:
            detail = _("Unknown method '%(method)s' in compute API")
            raise exception.CellServiceAPIMethodNotFound(
                    detail=detail % {'method': method})
        args = list(method_info['method_args'])
        # 1st arg is instance_uuid that we need to turn into the
        # instance object.
        instance_uuid = args[0]
        # NOTE: compute/api.py loads these when retrieving an instance for an
        # API request, so there's a good chance that this is what was loaded.
        expected_attrs = ['metadata', 'system_metadata', 'security_groups',
                          'info_cache']

        try:
            instance = objects.Instance.get_by_uuid(message.ctxt,
                    instance_uuid, expected_attrs=expected_attrs)
            args[0] = instance
        except exception.InstanceNotFound:
            with excutils.save_and_reraise_exception():
                # Must be a race condition.  Let's try to resolve it by
                # telling the top level cells that this instance doesn't
                # exist.
                instance = objects.Instance(context=message.ctxt,
                                            uuid=instance_uuid)
                self.msg_runner.instance_destroy_at_top(message.ctxt,
                                                        instance)
        return fn(message.ctxt, *args, **method_info['method_kwargs']) 
Example 27
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: messaging.py    Apache License 2.0 5 votes vote down vote up
def from_json(cls, ctxt, json_message):
        _dict = jsonutils.loads(json_message)
        if _dict['failure']:
            resp_value = deserialize_remote_exception(_dict['value'],
                                                      rpc.get_allowed_exmods())
            _dict['value'] = resp_value
        response = cls(ctxt, **_dict)
        response.value = response.serializer.deserialize_entity(
            response.ctxt, response.value)
        return response 
Example 28
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: messaging.py    Apache License 2.0 5 votes vote down vote up
def deserialize_remote_exception(data, allowed_remote_exmods):
    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 != 'exceptions' and module not in allowed_remote_exmods:
        return messaging.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('args', []), **failure.get('kwargs', {}))
    except (AttributeError, TypeError, ImportError):
        return messaging.RemoteError(name, failure.get('message'), trace)

    ex_type = type(failure)
    str_override = lambda self: message
    new_ex_type = type(ex_type.__name__ + _REMOTE_POSTFIX, (ex_type,),
                       {'__str__': str_override, '__unicode__': str_override})
    new_ex_type.__module__ = '%s%s' % (module, _REMOTE_POSTFIX)
    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 
Example 29
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: model.py    Apache License 2.0 5 votes vote down vote up
def hydrate(cls, network_info):
        if isinstance(network_info, six.string_types):
            network_info = jsonutils.loads(network_info)
        return cls([VIF.hydrate(vif) for vif in network_info]) 
Example 30
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: numa.py    Apache License 2.0 5 votes vote down vote up
def obj_from_db_obj(cls, db_obj):
        return cls.obj_from_primitive(
            jsonutils.loads(db_obj)) 
Example 31
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: migrate_data.py    Apache License 2.0 5 votes vote down vote up
def connection_info(self):
        return jsonutils.loads(self.connection_info_json) 
Example 32
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: request_spec.py    Apache License 2.0 5 votes vote down vote up
def _from_db_object(context, spec, db_spec):
        spec_obj = spec.obj_from_primitive(jsonutils.loads(db_spec['spec']))
        for key in spec.fields:
            # Load these from the db model not the serialized object within,
            # though they should match.
            if key in ['id', 'instance_uuid']:
                setattr(spec, key, db_spec[key])
            else:
                setattr(spec, key, getattr(spec_obj, key))
        spec._context = context
        spec.obj_reset_changes()
        return spec 
Example 33
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: instance_numa_topology.py    Apache License 2.0 5 votes vote down vote up
def obj_from_db_obj(cls, instance_uuid, db_obj):
        primitive = jsonutils.loads(db_obj)
        obj_topology = cls.obj_from_primitive(primitive)

        if 'nova_object.name' not in db_obj:
            obj_topology.instance_uuid = instance_uuid
            # No benefit to store a list of changed fields
            obj_topology.obj_reset_changes()

        return obj_topology

    # TODO(ndipanov) Remove this method on the major version bump to 2.0 
Example 34
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: build_request.py    Apache License 2.0 5 votes vote down vote up
def _load_info_cache(self, db_info_cache):
        self.info_cache = objects.InstanceInfoCache.obj_from_primitive(
                jsonutils.loads(db_info_cache)) 
Example 35
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: build_request.py    Apache License 2.0 5 votes vote down vote up
def _load_security_groups(self, db_sec_group):
        self.security_groups = objects.SecurityGroupList.obj_from_primitive(
                jsonutils.loads(db_sec_group)) 
Example 36
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: build_request.py    Apache License 2.0 5 votes vote down vote up
def _from_db_object(context, req, db_req):
        for key in req.fields:
            if isinstance(req.fields[key], fields.ObjectField):
                try:
                    getattr(req, '_load_%s' % key)(db_req[key])
                except AttributeError:
                    LOG.exception(_LE('No load handler for %s'), key)
            elif key in JSON_FIELDS and db_req[key] is not None:
                setattr(req, key, jsonutils.loads(db_req[key]))
            else:
                setattr(req, key, db_req[key])
        req.obj_reset_changes()
        req._context = context
        return req 
Example 37
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: instance.py    Apache License 2.0 5 votes vote down vote up
def _load_vcpu_model(self, db_vcpu_model=None):
        if db_vcpu_model is None:
            self.vcpu_model = objects.VirtCPUModel.get_by_instance_uuid(
                self._context, self.uuid)
        else:
            db_vcpu_model = jsonutils.loads(db_vcpu_model)
            self.vcpu_model = objects.VirtCPUModel.obj_from_primitive(
                db_vcpu_model) 
Example 38
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: migration_context.py    Apache License 2.0 5 votes vote down vote up
def obj_from_db_obj(cls, db_obj):
        primitive = jsonutils.loads(db_obj)
        return cls.obj_from_primitive(primitive) 
Example 39
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: pci_device.py    Apache License 2.0 5 votes vote down vote up
def _from_db_object(context, pci_device, db_dev):
        for key in pci_device.fields:
            if key != 'extra_info':
                setattr(pci_device, key, db_dev[key])
            else:
                extra_info = db_dev.get("extra_info")
                pci_device.extra_info = jsonutils.loads(extra_info)
        pci_device._context = context
        pci_device.obj_reset_changes()
        # NOTE(ndipanov): As long as there is PF data in the old location, we
        # want to load it as it may have be the only place we have it
        if 'phys_function' in pci_device.extra_info:
            pci_device.parent_addr = pci_device.extra_info['phys_function']

        return pci_device 
Example 40
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: vcpu_model.py    Apache License 2.0 5 votes vote down vote up
def get_by_instance_uuid(cls, context, instance_uuid):
        db_extra = db.instance_extra_get_by_instance_uuid(
                context, instance_uuid, columns=['vcpu_model'])
        if not db_extra or not db_extra['vcpu_model']:
            return None
        return cls.obj_from_primitive(jsonutils.loads(db_extra['vcpu_model'])) 
Example 41
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: limits.py    Apache License 2.0 5 votes vote down vote up
def __call__(self, request):
        """Handles a call to this application.

        Returns 204 if the request is acceptable to the limiter, else a 403
        is returned with a relevant header indicating when the request *will*
        succeed.
        """
        if request.method != "POST":
            raise webob.exc.HTTPMethodNotAllowed()

        try:
            info = dict(jsonutils.loads(request.body))
        except ValueError:
            raise webob.exc.HTTPBadRequest()

        username = request.path_info_pop()
        verb = info.get("verb")
        path = info.get("path")

        delay, error = self._limiter.check_for_delay(verb, path, username)

        if delay:
            headers = {"X-Wait-Seconds": "%.2f" % delay}
            return webob.exc.HTTPForbidden(headers=headers, explanation=error)
        else:
            return webob.exc.HTTPNoContent() 
Example 42
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: wsgi.py    Apache License 2.0 5 votes vote down vote up
def _from_json(self, datastring):
        try:
            return jsonutils.loads(datastring)
        except ValueError:
            msg = _("cannot understand JSON")
            raise exception.MalformedRequestBody(reason=msg) 
Example 43
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: storage_users.py    Apache License 2.0 5 votes vote down vote up
def register_storage_use(storage_path, hostname):
    """Identify the id of this instance storage."""

    LOCK_PATH = os.path.join(CONF.instances_path, 'locks')

    @utils.synchronized('storage-registry-lock', external=True,
                        lock_path=LOCK_PATH)
    def do_register_storage_use(storage_path, hostname):
        # NOTE(mikal): this is required to determine if the instance storage is
        # shared, which is something that the image cache manager needs to
        # know. I can imagine other uses as well though.

        d = {}
        id_path = os.path.join(storage_path, 'compute_nodes')
        if os.path.exists(id_path):
            with open(id_path) as f:
                try:
                    d = jsonutils.loads(f.read())
                except ValueError:
                    LOG.warning(_LW("Cannot decode JSON from %(id_path)s"),
                                {"id_path": id_path})

        d[hostname] = time.time()

        with open(id_path, 'w') as f:
            f.write(jsonutils.dumps(d))

    return do_register_storage_use(storage_path, hostname) 
Example 44
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: imagecache.py    Apache License 2.0 5 votes vote down vote up
def _read_possible_json(serialized, info_file):
    try:
        d = jsonutils.loads(serialized)

    except ValueError as e:
        LOG.error(_LE('Error reading image info file %(filename)s: '
                      '%(error)s'),
                  {'filename': info_file,
                   'error': e})
        d = {}

    return d 
Example 45
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: driver.py    Apache License 2.0 5 votes vote down vote up
def _detach_encrypted_volumes(self, instance, block_device_info):
        """Detaches encrypted volumes attached to instance."""
        disks = jsonutils.loads(self.get_instance_disk_info(instance,
                                                            block_device_info))
        encrypted_volumes = filter(dmcrypt.is_encrypted,
                                   [disk['path'] for disk in disks])
        for path in encrypted_volumes:
            dmcrypt.delete_volume(path) 
Example 46
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: driver.py    Apache License 2.0 5 votes vote down vote up
def _request_vtpm_uuid(self):
        """Request a UUID from the vtpmmgr domain
        TODO: The request URL needs to point to our REST API
        """
        import oslo_serialization.jsonutils as jsonutils
        import urllib2
        request_url = "http://keylime_vm:8081"
        request = urllib2.urlopen(request_url)
        status_code = request.getcode()
        if(status_code == 200):
            json_str = request.read()
            uuid = jsonutils.loads(json_str)['uuid']
        else:
            raise exception.VtpmFailure(status_code=str(status_code))
        return uuid 
Example 47
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: driver.py    Apache License 2.0 5 votes vote down vote up
def _is_shared_block_storage(self, instance, dest_check_data,
                                 block_device_info=None):
        """Check if all block storage of an instance can be shared
        between source and destination of a live migration.

        Returns true if the instance is volume backed and has no local disks,
        or if the image backend is the same on source and destination and the
        backend shares block storage between compute nodes.

        :param instance: nova.objects.instance.Instance object
        :param dest_check_data: dict with boolean fields image_type,
                                is_shared_instance_path, and is_volume_backed
        """
        if (dest_check_data.obj_attr_is_set('image_type') and
                CONF.libvirt.images_type == dest_check_data.image_type and
                self.image_backend.backend().is_shared_block_storage()):
            # NOTE(dgenin): currently true only for RBD image backend
            return True

        if (dest_check_data.is_shared_instance_path and
                self.image_backend.backend().is_file_in_instance_path()):
            # NOTE(angdraug): file based image backends (Raw, Qcow2)
            # place block device files under the instance path
            return True

        if (dest_check_data.is_volume_backed and
                not bool(jsonutils.loads(
                    self.get_instance_disk_info(instance,
                                                block_device_info)))):
            return True

        return False 
Example 48
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: firewall.py    Apache License 2.0 5 votes vote down vote up
def _plugin_execute(self, *cmd, **kwargs):
        # Prepare arguments for plugin call
        args = {}
        args.update(map(lambda x: (x, str(kwargs[x])), kwargs))
        args['cmd_args'] = jsonutils.dumps(cmd)
        ret = self._session.call_plugin('xenhost', 'iptables_config', args)
        json_ret = jsonutils.loads(ret)
        return (json_ret['out'], json_ret['err']) 
Example 49
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: api_samples_test_base.py    Apache License 2.0 5 votes vote down vote up
def pretty_data(data):
    data = jsonutils.dumps(jsonutils.loads(data), sort_keys=True,
                           indent=4)
    return '\n'.join(line.rstrip() for line in data.split('\n')).strip() 
Example 50
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_console_auth_tokens.py    Apache License 2.0 5 votes vote down vote up
def _get_console_url(self, data):
        return jsonutils.loads(data)["console"]["url"] 
Example 51
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_tenant_networks.py    Apache License 2.0 5 votes vote down vote up
def test_delete_network(self):
        response = self._do_post('os-tenant-networks', "networks-post-req", {})
        net = jsonutils.loads(response.content)
        response = self._do_delete('os-tenant-networks/%s' %
                                                net["network"]["id"])
        self.assertEqual(202, response.status_code)
        self.assertEqual("", response.content) 
Example 52
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: client.py    Apache License 2.0 5 votes vote down vote up
def _decode_json(self, response):
        resp = APIResponse(status=response.status_code)
        if response.content:
            resp.body = jsonutils.loads(response.content)
        return resp 
Example 53
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_compute.py    Apache License 2.0 5 votes vote down vote up
def setUp(self):
        super(ComputeVolumeTestCase, self).setUp()
        self.fetched_attempts = 0
        self.instance = {
            'id': 'fake',
            'uuid': uuids.instance,
            'name': 'fake',
            'root_device_name': '/dev/vda',
        }
        self.fake_volume = fake_block_device.FakeDbBlockDeviceDict(
                {'source_type': 'volume', 'destination_type': 'volume',
                 'volume_id': uuids.volume_id, 'device_name': '/dev/vdb'})
        self.instance_object = objects.Instance._from_db_object(
                self.context, objects.Instance(),
                fake_instance.fake_db_instance())
        self.stubs.Set(self.compute.volume_api, 'get', lambda *a, **kw:
                       {'id': uuids.volume_id, 'size': 4,
                        'attach_status': 'detached'})
        self.stubs.Set(self.compute.driver, 'get_volume_connector',
                       lambda *a, **kw: None)
        self.stubs.Set(self.compute.volume_api, 'initialize_connection',
                       lambda *a, **kw: {})
        self.stubs.Set(self.compute.volume_api, 'terminate_connection',
                       lambda *a, **kw: None)
        self.stubs.Set(self.compute.volume_api, 'attach',
                       lambda *a, **kw: None)
        self.stubs.Set(self.compute.volume_api, 'detach',
                       lambda *a, **kw: None)
        self.stubs.Set(self.compute.volume_api, 'check_attach',
                       lambda *a, **kw: None)
        self.stubs.Set(greenthread, 'sleep',
                       lambda *a, **kw: None)

        def store_cinfo(context, *args, **kwargs):
            self.cinfo = jsonutils.loads(args[-1].get('connection_info'))
            return self.fake_volume

        self.stub_out('nova.db.block_device_mapping_create', store_cinfo)
        self.stub_out('nova.db.block_device_mapping_update', store_cinfo) 
Example 54
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_extended_availability_zone.py    Apache License 2.0 5 votes vote down vote up
def _get_server(self, body):
        return jsonutils.loads(body).get('server') 
Example 55
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_extended_availability_zone.py    Apache License 2.0 5 votes vote down vote up
def _get_servers(self, body):
        return jsonutils.loads(body).get('servers') 
Example 56
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_disk_config.py    Apache License 2.0 5 votes vote down vote up
def test_show_server(self):
        req = fakes.HTTPRequest.blank(
            '/fake/servers/%s' % MANUAL_INSTANCE_UUID)
        res = req.get_response(self.app)
        server_dict = jsonutils.loads(res.body)['server']
        self.assertDiskConfig(server_dict, 'MANUAL')

        req = fakes.HTTPRequest.blank(
            '/fake/servers/%s' % AUTO_INSTANCE_UUID)
        res = req.get_response(self.app)
        server_dict = jsonutils.loads(res.body)['server']
        self.assertDiskConfig(server_dict, 'AUTO') 
Example 57
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_disk_config.py    Apache License 2.0 5 votes vote down vote up
def test_detail_servers(self):
        req = fakes.HTTPRequest.blank('/fake/servers/detail')
        res = req.get_response(self.app)
        server_dicts = jsonutils.loads(res.body)['servers']

        expectations = ['MANUAL', 'AUTO']
        for server_dict, expected in zip(server_dicts, expectations):
            self.assertDiskConfig(server_dict, expected) 
Example 58
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_disk_config.py    Apache License 2.0 5 votes vote down vote up
def test_show_image(self):
        self.flags(group='glance', api_servers=['http://localhost:9292'])

        req = fakes.HTTPRequest.blank(
            '/fake/images/a440c04b-79fa-479c-bed1-0b816eaec379')
        res = req.get_response(self.app)
        image_dict = jsonutils.loads(res.body)['image']
        self.assertDiskConfig(image_dict, 'MANUAL')

        req = fakes.HTTPRequest.blank(
            '/fake/images/70a599e0-31e7-49b7-b260-868f441e862b')
        res = req.get_response(self.app)
        image_dict = jsonutils.loads(res.body)['image']
        self.assertDiskConfig(image_dict, 'AUTO') 
Example 59
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_disk_config.py    Apache License 2.0 5 votes vote down vote up
def test_detail_image(self):
        req = fakes.HTTPRequest.blank('/fake/images/detail')
        res = req.get_response(self.app)
        image_dicts = jsonutils.loads(res.body)['images']

        expectations = ['MANUAL', 'AUTO']
        for image_dict, expected in zip(image_dicts, expectations):
            # NOTE(sirp): image fixtures 6 and 7 are setup for
            # auto_disk_config testing
            if image_dict['id'] in (6, 7):
                self.assertDiskConfig(image_dict, expected) 
Example 60
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_disk_config.py    Apache License 2.0 5 votes vote down vote up
def test_create_server_override_auto(self):
        req = fakes.HTTPRequest.blank('/fake/servers')
        req.method = 'POST'
        req.content_type = 'application/json'
        body = {'server': {
                  'name': 'server_test',
                  'imageRef': 'cedef40a-ed67-4d10-800e-17455edce175',
                  'flavorRef': '1',
                  API_DISK_CONFIG: 'AUTO'
               }}

        req.body = jsonutils.dump_as_bytes(body)
        res = req.get_response(self.app)
        server_dict = jsonutils.loads(res.body)['server']
        self.assertDiskConfig(server_dict, 'AUTO') 
Example 61
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_disk_config.py    Apache License 2.0 5 votes vote down vote up
def test_create_server_detect_from_image(self):
        """If user doesn't pass in diskConfig for server, use image metadata
        to specify AUTO or MANUAL.
        """
        req = fakes.HTTPRequest.blank('/fake/servers')
        req.method = 'POST'
        req.content_type = 'application/json'
        body = {'server': {
                  'name': 'server_test',
                  'imageRef': 'a440c04b-79fa-479c-bed1-0b816eaec379',
                  'flavorRef': '1',
               }}

        req.body = jsonutils.dump_as_bytes(body)
        res = req.get_response(self.app)
        server_dict = jsonutils.loads(res.body)['server']
        self.assertDiskConfig(server_dict, 'MANUAL')

        req = fakes.HTTPRequest.blank('/fake/servers')
        req.method = 'POST'
        req.content_type = 'application/json'
        body = {'server': {
                  'name': 'server_test',
                  'imageRef': '70a599e0-31e7-49b7-b260-868f441e862b',
                  'flavorRef': '1',
               }}

        req.body = jsonutils.dump_as_bytes(body)
        res = req.get_response(self.app)
        server_dict = jsonutils.loads(res.body)['server']
        self.assertDiskConfig(server_dict, 'AUTO') 
Example 62
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_disk_config.py    Apache License 2.0 5 votes vote down vote up
def test_create_server_detect_from_image_disabled_goes_to_manual(self):
        req = fakes.HTTPRequest.blank('/fake/servers')
        req.method = 'POST'
        req.content_type = 'application/json'
        body = {'server': {
                  'name': 'server_test',
                  'imageRef': '88580842-f50a-11e2-8d3a-f23c91aec05e',
                  'flavorRef': '1',
               }}

        req.body = jsonutils.dump_as_bytes(body)
        res = req.get_response(self.app)
        server_dict = jsonutils.loads(res.body)['server']
        self.assertDiskConfig(server_dict, 'MANUAL') 
Example 63
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_disk_config.py    Apache License 2.0 5 votes vote down vote up
def test_create_server_when_disabled_and_manual(self):
        req = fakes.HTTPRequest.blank('/fake/servers')
        req.method = 'POST'
        req.content_type = 'application/json'
        body = {'server': {
                  'name': 'server_test',
                  'imageRef': '88580842-f50a-11e2-8d3a-f23c91aec05e',
                  'flavorRef': '1',
                  API_DISK_CONFIG: 'MANUAL'
               }}

        req.body = jsonutils.dump_as_bytes(body)
        res = req.get_response(self.app)
        server_dict = jsonutils.loads(res.body)['server']
        self.assertDiskConfig(server_dict, 'MANUAL') 
Example 64
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_disk_config.py    Apache License 2.0 5 votes vote down vote up
def _test_update_server_disk_config(self, uuid, disk_config):
        req = fakes.HTTPRequest.blank(
            '/fake/servers/%s' % uuid)
        req.method = 'PUT'
        req.content_type = 'application/json'
        body = {'server': {API_DISK_CONFIG: disk_config}}
        req.body = jsonutils.dump_as_bytes(body)
        res = req.get_response(self.app)
        server_dict = jsonutils.loads(res.body)['server']
        self.assertDiskConfig(server_dict, disk_config) 
Example 65
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_disk_config.py    Apache License 2.0 5 votes vote down vote up
def test_update_server_invalid_disk_config(self):
        # Return BadRequest if user passes an invalid diskConfig value.
        req = fakes.HTTPRequest.blank(
            '/fake/servers/%s' % MANUAL_INSTANCE_UUID)
        req.method = 'PUT'
        req.content_type = 'application/json'
        body = {'server': {API_DISK_CONFIG: 'server_test'}}
        req.body = jsonutils.dump_as_bytes(body)
        res = req.get_response(self.app)
        self.assertEqual(res.status_int, 400)
        expected_msg = self._get_expected_msg_for_invalid_disk_config()
        expected_msg = expected_msg.format(API_DISK_CONFIG, 'server_test')

        self.assertEqual(jsonutils.loads(expected_msg),
                         jsonutils.loads(res.body)) 
Example 66
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_flavor_rxtx.py    Apache License 2.0 5 votes vote down vote up
def _get_flavor(self, body):
        return jsonutils.loads(body).get('flavor') 
Example 67
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_flavor_rxtx.py    Apache License 2.0 5 votes vote down vote up
def _get_flavors(self, body):
        return jsonutils.loads(body).get('flavors') 
Example 68
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_plugin_framework.py    Apache License 2.0 5 votes vote down vote up
def test_plugin_framework_index(self, mock_namespace):
        mock_namespace.return_value = 'nova.api.v21.test_extensions'

        app = fakes.wsgi_app_v21(init_only='test-basic')
        req = fakes.HTTPRequest.blank('/v2/fake/test')
        res = req.get_response(app)
        self.assertEqual(200, res.status_int)
        resp_json = jsonutils.loads(res.body)
        self.assertEqual('val', resp_json['param']) 
Example 69
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_extensions.py    Apache License 2.0 5 votes vote down vote up
def test_get_extension_json(self):
        app = compute.APIRouter(init_only=('extensions',))
        request = webob.Request.blank("/fake/extensions/FOXNSOX")
        response = request.get_response(app)
        self.assertEqual(200, response.status_int)

        data = jsonutils.loads(response.body)
        self.assertEqual({
            "namespace": "http://www.fox.in.socks/api/ext/pie/v1.0",
            "name": "Fox In Socks",
            "updated": "2011-01-22T13:25:27-06:00",
            "description": "The Fox In Socks Extension.",
            "alias": "FOXNSOX",
            "links": []}, data['extension']) 
Example 70
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_extensions.py    Apache License 2.0 5 votes vote down vote up
def test_invalid_action(self):
        body = dict(blah=dict(name="test"))  # Doesn't exist
        url = "/fake/servers/abcd/action"
        response = self._send_server_action_request(url, body)
        self.assertEqual(400, response.status_int)
        self.assertEqual('application/json', response.content_type)
        body = jsonutils.loads(response.body)
        expected = {
            "badRequest": {
                "message": "There is no such action: blah",
                "code": 400
            }
        }
        self.assertThat(expected, matchers.DictMatches(body)) 
Example 71
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_extensions.py    Apache License 2.0 5 votes vote down vote up
def test_failed_action(self):
        body = dict(fail=dict(name="test"))
        url = "/fake/servers/abcd/action"
        response = self._send_server_action_request(url, body)
        self.assertEqual(400, response.status_int)
        self.assertEqual('application/json', response.content_type)
        body = jsonutils.loads(response.body)
        expected = {
            "badRequest": {
                "message": "Tweedle fail",
                "code": 400
            }
        }
        self.assertThat(expected, matchers.DictMatches(body)) 
Example 72
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_extensions.py    Apache License 2.0 5 votes vote down vote up
def test_get_resources_with_mgr(self):

        app = fakes.wsgi_app(init_only=('flavors',))
        request = webob.Request.blank("/v2/fake/flavors/1?chewing=newblue")
        request.environ['api.version'] = '2'
        response = request.get_response(app)
        self.assertEqual(200, response.status_int)
        response_data = jsonutils.loads(response.body)
        self.assertEqual('newblue', response_data['flavor']['googoose'])
        self.assertEqual("Pig Bands!", response_data['big_bands']) 
Example 73
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_api.py    Apache License 2.0 5 votes vote down vote up
def test_vendor_content_type_json(self):
        ctype = 'application/vnd.openstack.compute+json'

        req = webob.Request.blank('/')
        req.headers['Accept'] = ctype

        res = req.get_response(self.wsgi_app)
        self.assertEqual(res.status_int, 200)
        self.assertEqual(res.content_type, ctype)

        jsonutils.loads(res.body) 
Example 74
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_server_usage.py    Apache License 2.0 5 votes vote down vote up
def _get_server(self, body):
        return jsonutils.loads(body).get('server') 
Example 75
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_server_usage.py    Apache License 2.0 5 votes vote down vote up
def _get_servers(self, body):
        return jsonutils.loads(body).get('servers') 
Example 76
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: security_groups.py    Apache License 2.0 4 votes vote down vote up
def _extend_servers(self, req, servers):
        # TODO(arosen) this function should be refactored to reduce duplicate
        # code and use get_instance_security_groups instead of get_db_instance.
        if not len(servers):
            return
        key = "security_groups"
        context = _authorize_context(req)
        if not openstack_driver.is_neutron_security_groups():
            for server in servers:
                instance = req.get_db_instance(server['id'])
                groups = instance.get(key)
                if groups:
                    server[key] = [{"name": group["name"]} for group in groups]
        else:
            # If method is a POST we get the security groups intended for an
            # instance from the request. The reason for this is if using
            # neutron security groups the requested security groups for the
            # instance are not in the db and have not been sent to neutron yet.
            if req.method != 'POST':
                sg_instance_bindings = (
                        self.security_group_api
                        .get_instances_security_groups_bindings(context,
                                                                servers))
                for server in servers:
                    groups = sg_instance_bindings.get(server['id'])
                    if groups:
                        server[key] = groups

            # In this section of code len(servers) == 1 as you can only POST
            # one server in an API request.
            else:
                try:
                    # try converting to json
                    req_obj = jsonutils.loads(req.body)
                    # Add security group to server, if no security group was in
                    # request add default since that is the group it is part of
                    servers[0][key] = req_obj['server'].get(
                        key, [{'name': 'default'}])
                except ValueError:
                    root = minidom.parseString(req.body)
                    sg_root = root.getElementsByTagName(key)
                    groups = []
                    if sg_root:
                        security_groups = sg_root[0].getElementsByTagName(
                            'security_group')
                        for security_group in security_groups:
                            groups.append(
                                {'name': security_group.getAttribute('name')})
                    if not groups:
                        groups = [{'name': 'default'}]

                    servers[0][key] = groups 
Example 77
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: auth.py    Apache License 2.0 4 votes vote down vote up
def __call__(self, req):
        user_id = req.headers.get('X_USER')
        user_id = req.headers.get('X_USER_ID', user_id)
        if user_id is None:
            LOG.debug("Neither X_USER_ID nor X_USER found in request")
            return webob.exc.HTTPUnauthorized()

        roles = self._get_roles(req)

        if 'X_TENANT_ID' in req.headers:
            # This is the new header since Keystone went to ID/Name
            project_id = req.headers['X_TENANT_ID']
        else:
            # This is for legacy compatibility
            project_id = req.headers['X_TENANT']
        project_name = req.headers.get('X_TENANT_NAME')
        user_name = req.headers.get('X_USER_NAME')

        req_id = req.environ.get(request_id.ENV_REQUEST_ID)

        # Get the auth token
        auth_token = req.headers.get('X_AUTH_TOKEN',
                                     req.headers.get('X_STORAGE_TOKEN'))

        # Build a context, including the auth_token...
        remote_address = req.remote_addr
        if CONF.use_forwarded_for:
            remote_address = req.headers.get('X-Forwarded-For', remote_address)

        service_catalog = None
        if req.headers.get('X_SERVICE_CATALOG') is not None:
            try:
                catalog_header = req.headers.get('X_SERVICE_CATALOG')
                service_catalog = jsonutils.loads(catalog_header)
            except ValueError:
                raise webob.exc.HTTPInternalServerError(
                          _('Invalid service catalog json.'))

        # NOTE(jamielennox): This is a full auth plugin set by auth_token
        # middleware in newer versions.
        user_auth_plugin = req.environ.get('keystone.token_auth')

        ctx = context.RequestContext(user_id,
                                     project_id,
                                     user_name=user_name,
                                     project_name=project_name,
                                     roles=roles,
                                     auth_token=auth_token,
                                     remote_address=remote_address,
                                     service_catalog=service_catalog,
                                     request_id=req_id,
                                     user_auth_plugin=user_auth_plugin)

        req.environ['nova.context'] = ctx
        return self.application 
Example 78
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: hardware.py    Apache License 2.0 4 votes vote down vote up
def instance_topology_from_instance(instance):
    """Convenience method for getting the numa_topology out of instances

    Since we may get an Instance as either a dict, a db object, or an actual
    Instance object, this makes sure we get beck either None, or an instance
    of objects.InstanceNUMATopology class.
    """
    if isinstance(instance, obj_instance.Instance):
        # NOTE (ndipanov): This may cause a lazy-load of the attribute
        instance_numa_topology = instance.numa_topology
    else:
        if 'numa_topology' in instance:
            instance_numa_topology = instance['numa_topology']
        elif 'uuid' in instance:
            try:
                instance_numa_topology = (
                    objects.InstanceNUMATopology.get_by_instance_uuid(
                            context.get_admin_context(), instance['uuid'])
                    )
            except exception.NumaTopologyNotFound:
                instance_numa_topology = None
        else:
            instance_numa_topology = None

    if instance_numa_topology:
        if isinstance(instance_numa_topology, six.string_types):
            instance_numa_topology = (
                objects.InstanceNUMATopology.obj_from_primitive(
                    jsonutils.loads(instance_numa_topology)))

        elif isinstance(instance_numa_topology, dict):
            # NOTE (ndipanov): A horrible hack so that we can use
            # this in the scheduler, since the
            # InstanceNUMATopology object is serialized raw using
            # the obj_base.obj_to_primitive, (which is buggy and
            # will give us a dict with a list of InstanceNUMACell
            # objects), and then passed to jsonutils.to_primitive,
            # which will make a dict out of those objects. All of
            # this is done by scheduler.utils.build_request_spec
            # called in the conductor.
            #
            # Remove when request_spec is a proper object itself!
            dict_cells = instance_numa_topology.get('cells')
            if dict_cells:
                cells = [objects.InstanceNUMACell(
                    id=cell['id'],
                    cpuset=set(cell['cpuset']),
                    memory=cell['memory'],
                    pagesize=cell.get('pagesize'),
                    cpu_pinning=cell.get('cpu_pinning_raw'),
                    cpu_policy=cell.get('cpu_policy'),
                    cpu_thread_policy=cell.get('cpu_thread_policy'))
                         for cell in dict_cells]
                instance_numa_topology = objects.InstanceNUMATopology(
                    cells=cells)

    return instance_numa_topology


# TODO(ndipanov): Remove when all code paths are using objects 
Example 79
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: driver.py    Apache License 2.0 4 votes vote down vote up
def _compare_cpu(self, guest_cpu, host_cpu_str):
        """Check the host is compatible with the requested CPU

        :param guest_cpu: nova.objects.VirtCPUModel or None
        :param host_cpu_str: JSON from _get_cpu_info() method

        If the 'guest_cpu' parameter is not None, this will be
        validated for migration compatibility with the host.
        Otherwise the 'host_cpu_str' JSON string will be used for
        validation.

        :returns:
            None. if given cpu info is not compatible to this server,
            raise exception.
        """

        # NOTE(berendt): virConnectCompareCPU not working for Xen
        if CONF.libvirt.virt_type not in ['qemu', 'kvm']:
            return

        if guest_cpu is None:
            info = jsonutils.loads(host_cpu_str)
            LOG.info(_LI('Instance launched has CPU info: %s'), host_cpu_str)
            cpu = vconfig.LibvirtConfigCPU()
            cpu.arch = info['arch']
            cpu.model = info['model']
            cpu.vendor = info['vendor']
            cpu.sockets = info['topology']['sockets']
            cpu.cores = info['topology']['cores']
            cpu.threads = info['topology']['threads']
            for f in info['features']:
                cpu.add_feature(vconfig.LibvirtConfigCPUFeature(f))
        else:
            cpu = self._vcpu_model_to_cpu_config(guest_cpu)

        u = ("http://libvirt.org/html/libvirt-libvirt-host.html#"
             "virCPUCompareResult")
        m = _("CPU doesn't have compatibility.\n\n%(ret)s\n\nRefer to %(u)s")
        # unknown character exists in xml, then libvirt complains
        try:
            ret = self._host.compare_cpu(cpu.to_xml())
        except libvirt.libvirtError as e:
            error_code = e.get_error_code()
            if error_code == libvirt.VIR_ERR_NO_SUPPORT:
                LOG.debug("URI %(uri)s does not support cpu comparison. "
                          "It will be proceeded though. Error: %(error)s",
                          {'uri': self._uri(), 'error': e})
                return
            else:
                LOG.error(m, {'ret': e, 'u': u})
                raise exception.MigrationPreCheckError(
                    reason=m % {'ret': e, 'u': u})

        if ret <= 0:
            LOG.error(m, {'ret': ret, 'u': u})
            raise exception.InvalidCPUInfo(reason=m % {'ret': ret, 'u': u}) 
Example 80
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: imagebackend.py    Apache License 2.0 4 votes vote down vote up
def resolve_driver_format(self):
        """Return the driver format for self.path.

        First checks self.disk_info_path for an entry.
        If it's not there, calls self._get_driver_format(), and then
        stores the result in self.disk_info_path

        See https://bugs.launchpad.net/nova/+bug/1221190
        """
        def _dict_from_line(line):
            if not line:
                return {}
            try:
                return jsonutils.loads(line)
            except (TypeError, ValueError) as e:
                msg = (_("Could not load line %(line)s, got error "
                        "%(error)s") %
                        {'line': line, 'error': e})
                raise exception.InvalidDiskInfo(reason=msg)

        @utils.synchronized(self.disk_info_path, external=False,
                            lock_path=self.lock_path)
        def write_to_disk_info_file():
            # Use os.open to create it without group or world write permission.
            fd = os.open(self.disk_info_path, os.O_RDONLY | os.O_CREAT, 0o644)
            with os.fdopen(fd, "r") as disk_info_file:
                line = disk_info_file.read().rstrip()
                dct = _dict_from_line(line)

            if self.path in dct:
                msg = _("Attempted overwrite of an existing value.")
                raise exception.InvalidDiskInfo(reason=msg)
            dct.update({self.path: driver_format})

            tmp_path = self.disk_info_path + ".tmp"
            fd = os.open(tmp_path, os.O_WRONLY | os.O_CREAT, 0o644)
            with os.fdopen(fd, "w") as tmp_file:
                tmp_file.write('%s\n' % jsonutils.dumps(dct))
            os.rename(tmp_path, self.disk_info_path)

        try:
            if (self.disk_info_path is not None and
                        os.path.exists(self.disk_info_path)):
                with open(self.disk_info_path) as disk_info_file:
                    line = disk_info_file.read().rstrip()
                    dct = _dict_from_line(line)
                    for path, driver_format in six.iteritems(dct):
                        if path == self.path:
                            return driver_format
            driver_format = self._get_driver_format()
            if self.disk_info_path is not None:
                fileutils.ensure_tree(os.path.dirname(self.disk_info_path))
                write_to_disk_info_file()
        except OSError as e:
            raise exception.DiskInfoReadWriteFail(reason=six.text_type(e))
        return driver_format