Python oslo_serialization.jsonutils.to_primitive() Examples

The following are code examples for showing how to use oslo_serialization.jsonutils.to_primitive(). 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: rpcapi.py    Apache License 2.0 6 votes vote down vote up
def prep_resize(self, ctxt, image, instance, instance_type, host,
                    reservations=None, request_spec=None,
                    filter_properties=None, node=None,
                    clean_shutdown=True):
        image_p = jsonutils.to_primitive(image)
        msg_args = {'instance': instance,
                    'instance_type': instance_type,
                    'image': image_p,
                    'reservations': reservations,
                    'request_spec': request_spec,
                    'filter_properties': filter_properties,
                    'node': node,
                    'clean_shutdown': clean_shutdown}
        version = '4.1'
        if not self.client.can_send_version(version):
            version = '4.0'
            msg_args['instance_type'] = objects_base.obj_to_primitive(
                                            instance_type)
        cctxt = self.client.prepare(server=host, version=version)
        cctxt.cast(ctxt, 'prep_resize', **msg_args) 
Example 2
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: rpcapi.py    Apache License 2.0 6 votes vote down vote up
def allocate_for_instance(self, ctxt, instance_id, project_id, host,
                              rxtx_factor, vpn, requested_networks, macs=None,
                              dhcp_options=None):
        version = '1.13'
        if not self.client.can_send_version(version):
            version = '1.9'
            if requested_networks:
                requested_networks = requested_networks.as_tuples()

        if CONF.multi_host:
            cctxt = self.client.prepare(version=version, server=host)
        else:
            cctxt = self.client.prepare(version=version)
        return cctxt.call(ctxt, 'allocate_for_instance',
                          instance_id=instance_id, project_id=project_id,
                          host=host, rxtx_factor=rxtx_factor, vpn=vpn,
                          requested_networks=requested_networks,
                          macs=jsonutils.to_primitive(macs)) 
Example 3
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_rpcapi.py    Apache License 2.0 6 votes vote down vote up
def setUp(self):
        super(ComputeRpcAPITestCase, self).setUp()
        self.context = context.get_admin_context()
        self.fake_flavor_obj = fake_flavor.fake_flavor_obj(self.context)
        self.fake_flavor = jsonutils.to_primitive(self.fake_flavor_obj)
        instance_attr = {'host': 'fake_host',
                         'instance_type_id': self.fake_flavor_obj['id'],
                         'instance_type': self.fake_flavor_obj}
        self.fake_instance_obj = fake_instance.fake_instance_obj(self.context,
                                                   **instance_attr)
        self.fake_instance = jsonutils.to_primitive(self.fake_instance_obj)
        self.fake_volume_bdm = objects_block_dev.BlockDeviceMapping(
                **fake_block_device.FakeDbBlockDeviceDict(
                    {'source_type': 'volume', 'destination_type': 'volume',
                     'instance_uuid': self.fake_instance_obj.uuid,
                     'volume_id': 'fake-volume-id'})) 
Example 4
Project: nova   Author: ZhanHan   File: rpcapi.py    Apache License 2.0 6 votes vote down vote up
def prep_resize(self, ctxt, instance, image, instance_type, host,
                    reservations=None, request_spec=None,
                    filter_properties=None, node=None,
                    clean_shutdown=True):
        image_p = jsonutils.to_primitive(image)
        msg_args = {'instance': instance,
                    'instance_type': instance_type,
                    'image': image_p,
                    'reservations': reservations,
                    'request_spec': request_spec,
                    'filter_properties': filter_properties,
                    'node': node,
                    'clean_shutdown': clean_shutdown}
        version = '4.1'
        client = self.router.by_host(ctxt, host)
        if not client.can_send_version(version):
            version = '4.0'
            msg_args['instance_type'] = objects_base.obj_to_primitive(
                                            instance_type)
        cctxt = client.prepare(server=host, version=version)
        cctxt.cast(ctxt, 'prep_resize', **msg_args) 
Example 5
Project: nova   Author: ZhanHan   File: rpcapi.py    Apache License 2.0 6 votes vote down vote up
def resize_instance(self, ctxt, instance, extra_instance_updates,
                       scheduler_hint, flavor, reservations,
                       clean_shutdown=True,
                       request_spec=None):
        # NOTE(sbauza): Since Cells v1 is quite feature-frozen, we don't want
        # to pass down request_spec to the manager and rather keep the
        # cell conductor providing a new RequestSpec like the original
        # behaviour
        flavor_p = jsonutils.to_primitive(flavor)
        version = '1.33'
        msg_args = {'instance': instance,
                    'flavor': flavor_p,
                    'extra_instance_updates': extra_instance_updates,
                    'clean_shutdown': clean_shutdown}
        if not self.client.can_send_version(version):
            del msg_args['clean_shutdown']
            version = '1.20'

        cctxt = self.client.prepare(version=version)
        cctxt.cast(ctxt, 'resize_instance', **msg_args) 
Example 6
Project: nova   Author: ZhanHan   File: rpcapi.py    Apache License 2.0 6 votes vote down vote up
def allocate_for_instance(self, ctxt, instance_id, project_id, host,
                              rxtx_factor, vpn, requested_networks, macs=None,
                              dhcp_options=None):
        version = '1.13'
        if not self.client.can_send_version(version):
            version = '1.9'
            if requested_networks:
                requested_networks = requested_networks.as_tuples()

        if CONF.multi_host:
            cctxt = self.client.prepare(version=version, server=host)
        else:
            cctxt = self.client.prepare(version=version)
        return cctxt.call(ctxt, 'allocate_for_instance',
                          instance_id=instance_id, project_id=project_id,
                          host=host, rxtx_factor=rxtx_factor, vpn=vpn,
                          requested_networks=requested_networks,
                          macs=jsonutils.to_primitive(macs)) 
Example 7
Project: nova   Author: ZhanHan   File: test_instance_pci_requests.py    Apache License 2.0 6 votes vote down vote up
def test_from_request_spec_instance_props(self):
        requests = objects.InstancePCIRequests(
            requests=[objects.InstancePCIRequest(count=1,
                                                 request_id=FAKE_UUID,
                                                 spec=[{'vendor_id': '8086',
                                                        'device_id': '1502'}])
                      ],
            instance_uuid=FAKE_UUID)
        result = jsonutils.to_primitive(requests)
        result = objects.InstancePCIRequests.from_request_spec_instance_props(
                                                                        result)
        self.assertEqual(1, len(result.requests))
        self.assertEqual(1, result.requests[0].count)
        self.assertEqual(FAKE_UUID, result.requests[0].request_id)
        self.assertEqual([{'vendor_id': '8086', 'device_id': '1502'}],
                          result.requests[0].spec) 
Example 8
Project: nova   Author: ZhanHan   File: manager.py    Apache License 2.0 6 votes vote down vote up
def select_destinations(self, ctxt,
                            request_spec=None, filter_properties=None,
                            spec_obj=_sentinel):
        """Returns destinations(s) best suited for this RequestSpec.

        The result should be a list of dicts with 'host', 'nodename' and
        'limits' as keys.
        """

        # TODO(sbauza): Change the method signature to only accept a spec_obj
        # argument once API v5 is provided.
        if spec_obj is self._sentinel:
            spec_obj = objects.RequestSpec.from_primitives(ctxt,
                                                           request_spec,
                                                           filter_properties)
        dests = self.driver.select_destinations(ctxt, spec_obj)
        return jsonutils.to_primitive(dests) 
Example 9
Project: mistral-lib   Author: openstack   File: serialization.py    Apache License 2.0 6 votes vote down vote up
def serialize(self, entity):
        if entity is None:
            return None

        key = self._get_serialization_key(type(entity))

        # Primitive or not registered type.
        if not key:
            return jsonutils.dumps(
                jsonutils.to_primitive(entity, convert_instances=True)
            )

        serializer = self.serializers.get(key)

        if not serializer:
            raise RuntimeError(
                "Failed to find a serializer for the key: %s" % key
            )

        result = {
            '__serial_key': key,
            '__serial_data': serializer.serialize(entity)
        }

        return jsonutils.dumps(result) 
Example 10
Project: stx-nova   Author: starlingx-staging   File: rpcapi.py    Apache License 2.0 6 votes vote down vote up
def allocate_for_instance(self, ctxt, instance_id, project_id, host,
                              rxtx_factor, vpn, requested_networks, macs=None,
                              dhcp_options=None):
        # NOTE(mriedem): dhcp_options should be removed in version 2.0
        version = '1.13'
        if not self.client.can_send_version(version):
            version = '1.9'
            if requested_networks:
                requested_networks = requested_networks.as_tuples()

        if CONF.multi_host:
            cctxt = self.client.prepare(version=version, server=host)
        else:
            cctxt = self.client.prepare(version=version)
        return cctxt.call(ctxt, 'allocate_for_instance',
                          instance_id=instance_id, project_id=project_id,
                          host=host, rxtx_factor=rxtx_factor, vpn=vpn,
                          requested_networks=requested_networks,
                          macs=jsonutils.to_primitive(macs)) 
Example 11
Project: stx-nova   Author: starlingx-staging   File: selection.py    Apache License 2.0 6 votes vote down vote up
def to_dict(self):
        if self.limits is not None:
            limits = self.limits.to_dict()
        else:
            limits = {}
        # The NUMATopologyFilter can set 'numa_topology' in the limits dict to
        # a NUMATopologyLimits object which we need to convert to a primitive
        # before this hits jsonutils.to_primitive(). We only check for that
        # known case specifically as we don't care about handling out of tree
        # filters or drivers injecting non-serializable things in the limits
        # dict.
        numa_limit = limits.get("numa_topology")
        if numa_limit is not None:
            limits['numa_topology'] = numa_limit.obj_to_primitive()
        return {
            'host': self.service_host,
            'nodename': self.nodename,
            'limits': limits,
        } 
Example 12
Project: stx-nova   Author: starlingx-staging   File: rpcapi.py    Apache License 2.0 6 votes vote down vote up
def schedule_and_build_instances(self, context, build_requests,
                                     request_specs,
                                     image, admin_password, injected_files,
                                     requested_networks,
                                     block_device_mapping,
                                     tags=None):
        version = '1.17'
        kw = {'build_requests': build_requests,
              'request_specs': request_specs,
              'image': jsonutils.to_primitive(image),
              'admin_password': admin_password,
              'injected_files': injected_files,
              'requested_networks': requested_networks,
              'block_device_mapping': block_device_mapping,
              'tags': tags}

        if not self.client.can_send_version(version):
            version = '1.16'
            del kw['tags']

        cctxt = self.client.prepare(version=version)
        cctxt.cast(context, 'schedule_and_build_instances', **kw) 
Example 13
Project: stx-nova   Author: starlingx-staging   File: test_instance_pci_requests.py    Apache License 2.0 6 votes vote down vote up
def test_from_request_spec_instance_props(self):
        requests = objects.InstancePCIRequests(
            requests=[objects.InstancePCIRequest(count=1,
                                                 request_id=FAKE_UUID,
                                                 spec=[{'vendor_id': '8086',
                                                        'device_id': '1502'}])
                      ],
            instance_uuid=FAKE_UUID)
        result = jsonutils.to_primitive(requests)
        result = objects.InstancePCIRequests.from_request_spec_instance_props(
                                                                        result)
        self.assertEqual(1, len(result.requests))
        self.assertEqual(1, result.requests[0].count)
        self.assertEqual(FAKE_UUID, result.requests[0].request_id)
        self.assertEqual([{'vendor_id': '8086', 'device_id': '1502'}],
                          result.requests[0].spec) 
Example 14
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: rpcapi.py    Apache License 2.0 5 votes vote down vote up
def get_instance_diagnostics(self, ctxt, instance):
        # TODO(danms): This needs to be fixed for objects
        instance_p = jsonutils.to_primitive(instance)
        kwargs = {'instance': instance_p}
        version = '4.0'
        cctxt = self.client.prepare(server=_compute_host(None, instance),
                version=version)
        return cctxt.call(ctxt, 'get_instance_diagnostics', **kwargs) 
Example 15
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: messaging.py    Apache License 2.0 5 votes vote down vote up
def task_log_get_all(self, message, task_name, period_beginning,
                         period_ending, host, state):
        """Get task logs from the DB.  The message could have
        directly targeted this cell, or it could have been a broadcast
        message.

        If 'host' is not None, filter by host.
        If 'state' is not None, filter by state.
        """
        task_logs = self.db.task_log_get_all(message.ctxt, task_name,
                                             period_beginning,
                                             period_ending,
                                             host=host,
                                             state=state)
        return jsonutils.to_primitive(task_logs) 
Example 16
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: messaging.py    Apache License 2.0 5 votes vote down vote up
def actions_get(self, message, instance_uuid):
        actions = self.db.actions_get(message.ctxt, instance_uuid)
        return jsonutils.to_primitive(actions) 
Example 17
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: messaging.py    Apache License 2.0 5 votes vote down vote up
def action_get_by_request_id(self, message, instance_uuid, request_id):
        action = self.db.action_get_by_request_id(message.ctxt, instance_uuid,
                                                  request_id)
        return jsonutils.to_primitive(action) 
Example 18
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: rpcapi.py    Apache License 2.0 5 votes vote down vote up
def build_instances(self, ctxt, **kwargs):
        """Build instances."""
        build_inst_kwargs = kwargs
        instances = build_inst_kwargs['instances']
        build_inst_kwargs['image'] = jsonutils.to_primitive(
                build_inst_kwargs['image'])

        version = '1.34'
        if self.client.can_send_version('1.34'):
            build_inst_kwargs.pop('legacy_bdm', None)
        else:
            bdm_p = objects_base.obj_to_primitive(
                    build_inst_kwargs['block_device_mapping'])
            build_inst_kwargs['block_device_mapping'] = bdm_p
            version = '1.32'
        if not self.client.can_send_version('1.32'):
            instances_p = [jsonutils.to_primitive(inst) for inst in instances]
            build_inst_kwargs['instances'] = instances_p
            version = '1.30'
        if not self.client.can_send_version('1.30'):
            if 'filter_properties' in build_inst_kwargs:
                filter_properties = build_inst_kwargs['filter_properties']
                flavor = filter_properties['instance_type']
                flavor_p = objects_base.obj_to_primitive(flavor)
                filter_properties['instance_type'] = flavor_p
            version = '1.8'
        cctxt = self.client.prepare(version=version)
        cctxt.cast(ctxt, 'build_instances',
                   build_inst_kwargs=build_inst_kwargs) 
Example 19
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: rpcapi.py    Apache License 2.0 5 votes vote down vote up
def instance_delete_everywhere(self, ctxt, instance, delete_type):
        """Delete instance everywhere.  delete_type may be 'soft'
        or 'hard'.  This is generally only used to resolve races
        when API cell doesn't know to what cell an instance belongs.
        """
        if self.client.can_send_version('1.27'):
            version = '1.27'
        else:
            version = '1.0'
            instance = jsonutils.to_primitive(instance)
        cctxt = self.client.prepare(version=version)
        cctxt.cast(ctxt, 'instance_delete_everywhere', instance=instance,
                delete_type=delete_type) 
Example 20
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: rpcapi.py    Apache License 2.0 5 votes vote down vote up
def instance_fault_create_at_top(self, ctxt, instance_fault):
        """Create an instance fault at the top."""
        instance_fault_p = jsonutils.to_primitive(instance_fault)
        self.client.cast(ctxt, 'instance_fault_create_at_top',
                         instance_fault=instance_fault_p) 
Example 21
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: rpcapi.py    Apache License 2.0 5 votes vote down vote up
def resize_instance(self, ctxt, instance, extra_instance_updates,
                       scheduler_hint, flavor, reservations,
                       clean_shutdown=True):
        flavor_p = jsonutils.to_primitive(flavor)
        version = '1.33'
        msg_args = {'instance': instance,
                    'flavor': flavor_p,
                    'extra_instance_updates': extra_instance_updates,
                    'clean_shutdown': clean_shutdown}
        if not self.client.can_send_version(version):
            del msg_args['clean_shutdown']
            version = '1.20'

        cctxt = self.client.prepare(version=version)
        cctxt.cast(ctxt, 'resize_instance', **msg_args) 
Example 22
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: rpcapi.py    Apache License 2.0 5 votes vote down vote up
def migrate_server(self, context, instance, scheduler_hint, live, rebuild,
                  flavor, block_migration, disk_over_commit,
                  reservations=None, clean_shutdown=True, request_spec=None):
        kw = {'instance': instance, 'scheduler_hint': scheduler_hint,
              'live': live, 'rebuild': rebuild, 'flavor': flavor,
              'block_migration': block_migration,
              'disk_over_commit': disk_over_commit,
              'reservations': reservations,
              'clean_shutdown': clean_shutdown,
              'request_spec': request_spec,
              }
        version = '1.13'
        if not self.client.can_send_version(version):
            del kw['request_spec']
            version = '1.11'
        if not self.client.can_send_version(version):
            del kw['clean_shutdown']
            version = '1.10'
        if not self.client.can_send_version(version):
            kw['flavor'] = objects_base.obj_to_primitive(flavor)
            version = '1.6'
        if not self.client.can_send_version(version):
            kw['instance'] = jsonutils.to_primitive(
                    objects_base.obj_to_primitive(instance))
            version = '1.4'
        cctxt = self.client.prepare(version=version)
        return cctxt.call(context, 'migrate_server', **kw) 
Example 23
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: rpcapi.py    Apache License 2.0 5 votes vote down vote up
def build_instances(self, context, instances, image, filter_properties,
            admin_password, injected_files, requested_networks,
            security_groups, block_device_mapping, legacy_bdm=True):
        image_p = jsonutils.to_primitive(image)
        version = '1.10'
        if not self.client.can_send_version(version):
            version = '1.9'
            if 'instance_type' in filter_properties:
                flavor = filter_properties['instance_type']
                flavor_p = objects_base.obj_to_primitive(flavor)
                filter_properties = dict(filter_properties,
                                         instance_type=flavor_p)
        kw = {'instances': instances, 'image': image_p,
               'filter_properties': filter_properties,
               'admin_password': admin_password,
               'injected_files': injected_files,
               'requested_networks': requested_networks,
               'security_groups': security_groups}
        if not self.client.can_send_version(version):
            version = '1.8'
            kw['requested_networks'] = kw['requested_networks'].as_tuples()
        if not self.client.can_send_version('1.7'):
            version = '1.5'
            bdm_p = objects_base.obj_to_primitive(block_device_mapping)
            kw.update({'block_device_mapping': bdm_p,
                       'legacy_bdm': legacy_bdm})

        cctxt = self.client.prepare(version=version)
        cctxt.cast(context, 'build_instances', **kw) 
Example 24
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_host_api.py    Apache License 2.0 5 votes vote down vote up
def _test_service_get_all(self, fake_filters, **kwargs):
        service_attrs = dict(test_service.fake_service)
        del service_attrs['version']
        services = [
            cells_utils.ServiceProxy(
                objects.Service(**dict(service_attrs, id=1,
                                topic='compute', host='host1')),
                'cell1'),
            cells_utils.ServiceProxy(
                objects.Service(**dict(service_attrs, id=2,
                                topic='compute', host='host2')),
                'cell1')]
        exp_services = []
        for service in services:
            exp_service = copy.copy(service)
            exp_service.update({'availability_zone': 'nova'})
            exp_services.append(exp_service)

        self.mox.StubOutWithMock(self.host_api.cells_rpcapi,
                                 'service_get_all')
        self.host_api.cells_rpcapi.service_get_all(self.ctxt,
                filters=fake_filters).AndReturn(services)
        self.mox.ReplayAll()
        result = self.host_api.service_get_all(self.ctxt,
                                               filters=fake_filters,
                                               **kwargs)
        self.mox.VerifyAll()
        self.assertEqual(jsonutils.to_primitive(exp_services),
                         jsonutils.to_primitive(result)) 
Example 25
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 get_primitive_instance_by_uuid(context, instance_uuid):
    """Helper method to get an instance and then convert it to
    a primitive form using jsonutils.
    """
    instance = db.instance_get_by_uuid(context, instance_uuid)
    return jsonutils.to_primitive(instance) 
Example 26
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 test_resize_instance_driver_rollback(self):
        # Ensure instance status set to Running after rollback.

        def throw_up(*args, **kwargs):
            raise exception.InstanceFaultRollback(test.TestingException())

        self.stubs.Set(self.compute.driver, 'migrate_disk_and_power_off',
                       throw_up)

        instance = self._create_fake_instance_obj()
        instance_type = flavors.get_default_flavor()
        reservations = self._ensure_quota_reservations_rolledback(instance)
        self.compute.build_and_run_instance(self.context, instance, {}, {}, {},
                                            block_device_mapping=[])
        instance.host = 'foo'
        instance.save()
        self.compute.prep_resize(self.context, instance=instance,
                                 instance_type=instance_type, image={},
                                 reservations=reservations, request_spec={},
                                 filter_properties={}, node=None,
                                 clean_shutdown=True)
        instance.task_state = task_states.RESIZE_PREP
        instance.save()

        migration = objects.Migration.get_by_instance_and_status(
                self.context.elevated(),
                instance.uuid, 'pre-migrating')

        self.assertRaises(test.TestingException, self.compute.resize_instance,
                          self.context, instance=instance,
                          migration=migration, image={},
                          reservations=reservations,
                          instance_type=jsonutils.to_primitive(instance_type),
                          clean_shutdown=True)
        # NOTE(comstud): error path doesn't use objects, so our object
        # is not updated.  Refresh and compare against the DB.
        instance.refresh()
        self.assertEqual(instance.vm_state, vm_states.ACTIVE)
        self.assertIsNone(instance.task_state)
        self.compute.terminate_instance(self.context, instance, [], []) 
Example 27
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 test_resize_instance_handles_migration_error(self):
        # Ensure vm_state is ERROR when error occurs.
        def raise_migration_failure(*args):
            raise test.TestingException()
        self.stubs.Set(self.compute.driver,
                'migrate_disk_and_power_off',
                raise_migration_failure)

        instance = self._create_fake_instance_obj()
        reservations = self._ensure_quota_reservations_rolledback(instance)

        instance_type = flavors.get_default_flavor()

        self.compute.build_and_run_instance(self.context, instance, {}, {}, {},
                                            block_device_mapping=[])
        instance.host = 'foo'
        instance.save()
        self.compute.prep_resize(self.context, instance=instance,
                                 instance_type=instance_type,
                                 image={}, reservations=reservations,
                                 request_spec={}, filter_properties={},
                                 node=None, clean_shutdown=True)
        migration = objects.Migration.get_by_instance_and_status(
                self.context.elevated(),
                instance.uuid, 'pre-migrating')
        instance.task_state = task_states.RESIZE_PREP
        instance.save()
        self.assertRaises(test.TestingException, self.compute.resize_instance,
                          self.context, instance=instance,
                          migration=migration, image={},
                          reservations=reservations,
                          instance_type=jsonutils.to_primitive(instance_type),
                          clean_shutdown=True)
        # NOTE(comstud): error path doesn't use objects, so our object
        # is not updated.  Refresh and compare against the DB.
        instance.refresh()
        self.assertEqual(instance.vm_state, vm_states.ERROR)
        self.compute.terminate_instance(self.context, instance, [], []) 
Example 28
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 test_add_aggregate_host(self):
        def fake_driver_add_to_aggregate(context, aggregate, host, **_ignore):
            fake_driver_add_to_aggregate.called = True
            return {"foo": "bar"}
        self.stubs.Set(self.compute.driver, "add_to_aggregate",
                       fake_driver_add_to_aggregate)

        self.compute.add_aggregate_host(self.context, host="host",
                aggregate=jsonutils.to_primitive(self.aggr), slave_info=None)
        self.assertTrue(fake_driver_add_to_aggregate.called) 
Example 29
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 test_remove_aggregate_host(self):
        def fake_driver_remove_from_aggregate(context, aggregate, host,
                                              **_ignore):
            fake_driver_remove_from_aggregate.called = True
            self.assertEqual("host", host, "host")
            return {"foo": "bar"}
        self.stubs.Set(self.compute.driver, "remove_from_aggregate",
                       fake_driver_remove_from_aggregate)

        self.compute.remove_aggregate_host(self.context,
                aggregate=jsonutils.to_primitive(self.aggr), host="host",
                slave_info=None)
        self.assertTrue(fake_driver_remove_from_aggregate.called) 
Example 30
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 test_remove_from_aggregate_passes_slave_info_to_driver(self):
        def driver_remove_from_aggregate(context, aggregate, host, **kwargs):
            self.assertEqual(self.context, context)
            self.assertEqual(aggregate['id'], self.aggr['id'])
            self.assertEqual(host, "the_host")
            self.assertEqual("SLAVE_INFO", kwargs.get("slave_info"))

        self.stubs.Set(self.compute.driver, "remove_from_aggregate",
                       driver_remove_from_aggregate)

        self.compute.remove_aggregate_host(self.context,
                aggregate=jsonutils.to_primitive(self.aggr), host="the_host",
                slave_info="SLAVE_INFO") 
Example 31
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_cells_messaging.py    Apache License 2.0 5 votes vote down vote up
def test_actions_get(self):
        fake_uuid = fake_server_actions.FAKE_UUID
        fake_req_id = fake_server_actions.FAKE_REQUEST_ID1
        fake_act = fake_server_actions.FAKE_ACTIONS[fake_uuid][fake_req_id]

        self.mox.StubOutWithMock(self.tgt_db_inst, 'actions_get')
        self.tgt_db_inst.actions_get(self.ctxt,
                                     'fake-uuid').AndReturn([fake_act])
        self.mox.ReplayAll()

        response = self.src_msg_runner.actions_get(self.ctxt,
                                                   self.tgt_cell_name,
                                                   'fake-uuid')
        result = response.value_or_raise()
        self.assertEqual([jsonutils.to_primitive(fake_act)], result) 
Example 32
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_cells_messaging.py    Apache License 2.0 5 votes vote down vote up
def test_action_get_by_request_id(self):
        fake_uuid = fake_server_actions.FAKE_UUID
        fake_req_id = fake_server_actions.FAKE_REQUEST_ID1
        fake_act = fake_server_actions.FAKE_ACTIONS[fake_uuid][fake_req_id]

        self.mox.StubOutWithMock(self.tgt_db_inst, 'action_get_by_request_id')
        self.tgt_db_inst.action_get_by_request_id(self.ctxt,
                'fake-uuid', 'req-fake').AndReturn(fake_act)
        self.mox.ReplayAll()

        response = self.src_msg_runner.action_get_by_request_id(self.ctxt,
                self.tgt_cell_name, 'fake-uuid', 'req-fake')
        result = response.value_or_raise()
        self.assertEqual(jsonutils.to_primitive(fake_act), result) 
Example 33
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_cells_messaging.py    Apache License 2.0 5 votes vote down vote up
def test_action_events_get(self):
        fake_action_id = fake_server_actions.FAKE_ACTION_ID1
        fake_events = fake_server_actions.FAKE_EVENTS[fake_action_id]

        self.mox.StubOutWithMock(self.tgt_db_inst, 'action_events_get')
        self.tgt_db_inst.action_events_get(self.ctxt,
                                     'fake-action').AndReturn(fake_events)
        self.mox.ReplayAll()

        response = self.src_msg_runner.action_events_get(self.ctxt,
                                                         self.tgt_cell_name,
                                                         'fake-action')
        result = response.value_or_raise()
        self.assertEqual(jsonutils.to_primitive(fake_events), result) 
Example 34
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_driver.py    Apache License 2.0 5 votes vote down vote up
def test_connection_to_primitive(self):
        # Test bug 962840.
        import nova.virt.libvirt.driver as libvirt_driver
        drvr = libvirt_driver.LibvirtDriver('')
        drvr.set_host_enabled = mock.Mock()
        jsonutils.to_primitive(drvr._conn, convert_instances=True) 
Example 35
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_xenapi.py    Apache License 2.0 5 votes vote down vote up
def test_slave_asks_master_to_add_slave_to_pool(self):
        slave = ResourcePoolWithStubs()

        slave.add_to_aggregate("CONTEXT", self.fake_aggregate, "slave")

        self.assertIn(
            (slave.compute_rpcapi.add_aggregate_host,
            "CONTEXT", jsonutils.to_primitive(self.fake_aggregate),
            "slave", "master", "SLAVE_INFO"),
            slave.compute_rpcapi._mock_calls) 
Example 36
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_hardware.py    Apache License 2.0 5 votes vote down vote up
def test_instance_serialized_by_build_request_spec(self):
        host = objects.ComputeNode(numa_topology=self.hosttopo._to_json())
        fake_uuid = str(uuid.uuid4())
        instance = objects.Instance(context=self.context, id=1, uuid=fake_uuid,
                numa_topology=self.instancetopo)
        # NOTE (ndipanov): This emulates scheduler.utils.build_request_spec
        # We can remove this test once we no longer use that method.
        instance_raw = jsonutils.to_primitive(
                base_obj.obj_to_primitive(instance))
        res = hw.get_host_numa_usage_from_instance(host, instance_raw)
        self.assertIsInstance(res, six.string_types)
        self._check_usage(objects.NUMATopology.obj_from_db_obj(res)) 
Example 37
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: fake_notifier.py    Apache License 2.0 5 votes vote down vote up
def _notify(self, priority, ctxt, event_type, payload):
        payload = self._serializer.serialize_entity(ctxt, payload)
        # NOTE(sileht): simulate the kombu serializer
        # this permit to raise an exception if something have not
        # been serialized correctly
        jsonutils.to_primitive(payload)
        # NOTE(melwitt): Try to serialize the context, as the rpc would.
        #                An exception will be raised if something is wrong
        #                with the context.
        self._serializer.serialize_context(ctxt)
        msg = FakeMessage(self.publisher_id, priority, event_type,
                          payload, ctxt)
        NOTIFICATIONS.append(msg) 
Example 38
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: utils.py    Apache License 2.0 5 votes vote down vote up
def build_request_spec(ctxt, image, instances, instance_type=None):
    """Build a request_spec for the scheduler.

    The request_spec assumes that all instances to be scheduled are the same
    type.
    """
    instance = instances[0]
    if instance_type is None:
        if isinstance(instance, obj_instance.Instance):
            instance_type = instance.get_flavor()
        else:
            instance_type = flavors.extract_flavor(instance)

    if isinstance(instance, obj_instance.Instance):
        instance = obj_base.obj_to_primitive(instance)
        # obj_to_primitive doesn't copy this enough, so be sure
        # to detach our metadata blob because we modify it below.
        instance['system_metadata'] = dict(instance.get('system_metadata', {}))

    if isinstance(instance_type, objects.Flavor):
        instance_type = obj_base.obj_to_primitive(instance_type)
        # NOTE(danms): Replicate this old behavior because the
        # scheduler RPC interface technically expects it to be
        # there. Remove this when we bump the scheduler RPC API to
        # v5.0
        try:
            flavors.save_flavor_info(instance.get('system_metadata', {}),
                                     instance_type)
        except KeyError:
            # If the flavor isn't complete (which is legit with a
            # flavor object, just don't put it in the request spec
            pass

    request_spec = {
            'image': image or {},
            'instance_properties': instance,
            'instance_type': instance_type,
            'num_instances': len(instances)}
    return jsonutils.to_primitive(request_spec) 
Example 39
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: baserpc.py    Apache License 2.0 5 votes vote down vote up
def ping(self, context, arg, timeout=None):
        arg_p = jsonutils.to_primitive(arg)
        cctxt = self.client.prepare(timeout=timeout)
        return cctxt.call(context, 'ping', arg=arg_p) 
Example 40
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: baserpc.py    Apache License 2.0 5 votes vote down vote up
def ping(self, context, arg):
        resp = {'service': self.service_name, 'arg': arg}
        return jsonutils.to_primitive(resp) 
Example 41
Project: zun   Author: openstack   File: rpc.py    Apache License 2.0 5 votes vote down vote up
def serialize_entity(context, entity):
        return json.to_primitive(entity, convert_instances=True) 
Example 42
Project: nova   Author: ZhanHan   File: messaging.py    Apache License 2.0 5 votes vote down vote up
def task_log_get_all(self, message, task_name, period_beginning,
                         period_ending, host, state):
        """Get task logs from the DB.  The message could have
        directly targeted this cell, or it could have been a broadcast
        message.

        If 'host' is not None, filter by host.
        If 'state' is not None, filter by state.
        """
        task_logs = self.db.task_log_get_all(message.ctxt, task_name,
                                             period_beginning,
                                             period_ending,
                                             host=host,
                                             state=state)
        return jsonutils.to_primitive(task_logs) 
Example 43
Project: nova   Author: ZhanHan   File: messaging.py    Apache License 2.0 5 votes vote down vote up
def actions_get(self, message, instance_uuid):
        actions = self.db.actions_get(message.ctxt, instance_uuid)
        return jsonutils.to_primitive(actions) 
Example 44
Project: nova   Author: ZhanHan   File: messaging.py    Apache License 2.0 5 votes vote down vote up
def action_get_by_request_id(self, message, instance_uuid, request_id):
        action = self.db.action_get_by_request_id(message.ctxt, instance_uuid,
                                                  request_id)
        return jsonutils.to_primitive(action) 
Example 45
Project: nova   Author: ZhanHan   File: messaging.py    Apache License 2.0 5 votes vote down vote up
def action_events_get(self, message, action_id):
        action_events = self.db.action_events_get(message.ctxt, action_id)
        return jsonutils.to_primitive(action_events) 
Example 46
Project: nova   Author: ZhanHan   File: rpcapi.py    Apache License 2.0 5 votes vote down vote up
def instance_delete_everywhere(self, ctxt, instance, delete_type):
        """Delete instance everywhere.  delete_type may be 'soft'
        or 'hard'.  This is generally only used to resolve races
        when API cell doesn't know to what cell an instance belongs.
        """
        if self.client.can_send_version('1.27'):
            version = '1.27'
        else:
            version = '1.0'
            instance = jsonutils.to_primitive(instance)
        cctxt = self.client.prepare(version=version)
        cctxt.cast(ctxt, 'instance_delete_everywhere', instance=instance,
                delete_type=delete_type) 
Example 47
Project: nova   Author: ZhanHan   File: rpcapi.py    Apache License 2.0 5 votes vote down vote up
def instance_fault_create_at_top(self, ctxt, instance_fault):
        """Create an instance fault at the top."""
        instance_fault_p = jsonutils.to_primitive(instance_fault)
        self.client.cast(ctxt, 'instance_fault_create_at_top',
                         instance_fault=instance_fault_p) 
Example 48
Project: nova   Author: ZhanHan   File: rpc.py    Apache License 2.0 5 votes vote down vote up
def serialize_entity(context, entity):
        return jsonutils.to_primitive(entity, convert_instances=True) 
Example 49
Project: nova   Author: ZhanHan   File: rpcapi.py    Apache License 2.0 5 votes vote down vote up
def build_instances(self, context, instances, image, filter_properties,
            admin_password, injected_files, requested_networks,
            security_groups, block_device_mapping, legacy_bdm=True):
        image_p = jsonutils.to_primitive(image)
        version = '1.10'
        if not self.client.can_send_version(version):
            version = '1.9'
            if 'instance_type' in filter_properties:
                flavor = filter_properties['instance_type']
                flavor_p = objects_base.obj_to_primitive(flavor)
                filter_properties = dict(filter_properties,
                                         instance_type=flavor_p)
        kw = {'instances': instances, 'image': image_p,
               'filter_properties': filter_properties,
               'admin_password': admin_password,
               'injected_files': injected_files,
               'requested_networks': requested_networks,
               'security_groups': security_groups}
        if not self.client.can_send_version(version):
            version = '1.8'
            kw['requested_networks'] = kw['requested_networks'].as_tuples()
        if not self.client.can_send_version('1.7'):
            version = '1.5'
            bdm_p = objects_base.obj_to_primitive(block_device_mapping)
            kw.update({'block_device_mapping': bdm_p,
                       'legacy_bdm': legacy_bdm})

        cctxt = self.client.prepare(version=version)
        cctxt.cast(context, 'build_instances', **kw) 
Example 50
Project: nova   Author: ZhanHan   File: test_host_api.py    Apache License 2.0 5 votes vote down vote up
def _test_service_get_all(self, fake_filters, **kwargs):
        service_attrs = dict(test_service.fake_service)
        del service_attrs['version']
        services = [
            cells_utils.ServiceProxy(
                objects.Service(**dict(service_attrs, id=1,
                                topic='compute', host='host1')),
                'cell1'),
            cells_utils.ServiceProxy(
                objects.Service(**dict(service_attrs, id=2,
                                topic='compute', host='host2')),
                'cell1')]
        exp_services = []
        for service in services:
            exp_service = copy.copy(service)
            exp_service.update({'availability_zone': 'nova'})
            exp_services.append(exp_service)

        @mock.patch.object(self.host_api.cells_rpcapi, 'service_get_all')
        def _do_test(mock_service_get_all):
            mock_service_get_all.return_value = services
            result = self.host_api.service_get_all(self.ctxt,
                                                   filters=fake_filters,
                                                   **kwargs)
            mock_service_get_all.assert_called_once_with(self.ctxt,
                                                         filters=fake_filters)
            self.assertEqual(jsonutils.to_primitive(exp_services),
                             jsonutils.to_primitive(result))

        _do_test() 
Example 51
Project: nova   Author: ZhanHan   File: test_compute.py    Apache License 2.0 5 votes vote down vote up
def get_primitive_instance_by_uuid(context, instance_uuid):
    """Helper method to get an instance and then convert it to
    a primitive form using jsonutils.
    """
    instance = db.instance_get_by_uuid(context, instance_uuid)
    return jsonutils.to_primitive(instance) 
Example 52
Project: nova   Author: ZhanHan   File: test_compute.py    Apache License 2.0 5 votes vote down vote up
def test_resize_instance_handles_migration_error(self, mock_rollback):
        # Ensure vm_state is ERROR when error occurs.
        def raise_migration_failure(*args):
            raise test.TestingException()
        self.stub_out('nova.virt.fake.FakeDriver.migrate_disk_and_power_off',
                raise_migration_failure)

        instance = self._create_fake_instance_obj()
        reservations = list('fake_res')

        instance_type = flavors.get_default_flavor()

        self.compute.build_and_run_instance(self.context, instance, {}, {}, {},
                                            block_device_mapping=[])
        instance.host = 'foo'
        instance.save()
        self.compute.prep_resize(self.context, instance=instance,
                                 instance_type=instance_type,
                                 image={}, reservations=reservations,
                                 request_spec={}, filter_properties={},
                                 node=None, clean_shutdown=True)
        migration = objects.Migration.get_by_instance_and_status(
                self.context.elevated(),
                instance.uuid, 'pre-migrating')
        instance.task_state = task_states.RESIZE_PREP
        instance.save()
        self.assertRaises(test.TestingException, self.compute.resize_instance,
                          self.context, instance=instance,
                          migration=migration, image={},
                          reservations=reservations,
                          instance_type=jsonutils.to_primitive(instance_type),
                          clean_shutdown=True)
        # NOTE(comstud): error path doesn't use objects, so our object
        # is not updated.  Refresh and compare against the DB.
        instance.refresh()
        self.assertEqual(instance.vm_state, vm_states.ERROR)
        self.compute.terminate_instance(self.context, instance, [], [])
        self._ensure_quota_reservations(instance, reservations,
                                        mock_rollback) 
Example 53
Project: nova   Author: ZhanHan   File: test_compute.py    Apache License 2.0 5 votes vote down vote up
def test_add_aggregate_host(self):
        def fake_driver_add_to_aggregate(self, context, aggregate, host,
                                         **_ignore):
            fake_driver_add_to_aggregate.called = True
            return {"foo": "bar"}
        self.stub_out("nova.virt.fake.FakeDriver.add_to_aggregate",
                       fake_driver_add_to_aggregate)

        self.compute.add_aggregate_host(self.context, host="host",
                aggregate=jsonutils.to_primitive(self.aggr), slave_info=None)
        self.assertTrue(fake_driver_add_to_aggregate.called) 
Example 54
Project: nova   Author: ZhanHan   File: test_compute.py    Apache License 2.0 5 votes vote down vote up
def test_remove_aggregate_host(self):
        def fake_driver_remove_from_aggregate(cls, context, aggregate, host,
                                              **_ignore):
            fake_driver_remove_from_aggregate.called = True
            self.assertEqual("host", host, "host")
            return {"foo": "bar"}
        self.stub_out("nova.virt.fake.FakeDriver.remove_from_aggregate",
                       fake_driver_remove_from_aggregate)

        self.compute.remove_aggregate_host(self.context,
                aggregate=jsonutils.to_primitive(self.aggr), host="host",
                slave_info=None)
        self.assertTrue(fake_driver_remove_from_aggregate.called) 
Example 55
Project: nova   Author: ZhanHan   File: test_compute.py    Apache License 2.0 5 votes vote down vote up
def test_add_aggregate_host_passes_slave_info_to_driver(self):
        def driver_add_to_aggregate(cls, context, aggregate, host, **kwargs):
            self.assertEqual(self.context, context)
            self.assertEqual(aggregate['id'], self.aggr['id'])
            self.assertEqual(host, "the_host")
            self.assertEqual("SLAVE_INFO", kwargs.get("slave_info"))

        self.stub_out("nova.virt.fake.FakeDriver.add_to_aggregate",
                       driver_add_to_aggregate)

        self.compute.add_aggregate_host(self.context, host="the_host",
                slave_info="SLAVE_INFO",
                aggregate=jsonutils.to_primitive(self.aggr)) 
Example 56
Project: nova   Author: ZhanHan   File: test_compute.py    Apache License 2.0 5 votes vote down vote up
def test_remove_from_aggregate_passes_slave_info_to_driver(self):
        def driver_remove_from_aggregate(cls, context, aggregate, host,
                                         **kwargs):
            self.assertEqual(self.context, context)
            self.assertEqual(aggregate['id'], self.aggr['id'])
            self.assertEqual(host, "the_host")
            self.assertEqual("SLAVE_INFO", kwargs.get("slave_info"))

        self.stub_out("nova.virt.fake.FakeDriver.remove_from_aggregate",
                       driver_remove_from_aggregate)

        self.compute.remove_aggregate_host(self.context,
                aggregate=jsonutils.to_primitive(self.aggr), host="the_host",
                slave_info="SLAVE_INFO") 
Example 57
Project: nova   Author: ZhanHan   File: test_cells_messaging.py    Apache License 2.0 5 votes vote down vote up
def test_actions_get(self):
        fake_uuid = fake_server_actions.FAKE_UUID
        fake_req_id = fake_server_actions.FAKE_REQUEST_ID1
        fake_act = fake_server_actions.FAKE_ACTIONS[fake_uuid][fake_req_id]

        self.mox.StubOutWithMock(self.tgt_db_inst, 'actions_get')
        self.tgt_db_inst.actions_get(self.ctxt,
                                     fake_uuid).AndReturn([fake_act])
        self.mox.ReplayAll()

        response = self.src_msg_runner.actions_get(self.ctxt,
                                                   self.tgt_cell_name,
                                                   fake_uuid)
        result = response.value_or_raise()
        self.assertEqual([jsonutils.to_primitive(fake_act)], result) 
Example 58
Project: nova   Author: ZhanHan   File: test_cells_messaging.py    Apache License 2.0 5 votes vote down vote up
def test_action_get_by_request_id(self):
        fake_uuid = fake_server_actions.FAKE_UUID
        fake_req_id = fake_server_actions.FAKE_REQUEST_ID1
        fake_act = fake_server_actions.FAKE_ACTIONS[fake_uuid][fake_req_id]

        self.mox.StubOutWithMock(self.tgt_db_inst, 'action_get_by_request_id')
        self.tgt_db_inst.action_get_by_request_id(self.ctxt,
                fake_uuid, 'req-fake').AndReturn(fake_act)
        self.mox.ReplayAll()

        response = self.src_msg_runner.action_get_by_request_id(self.ctxt,
                self.tgt_cell_name, fake_uuid, 'req-fake')
        result = response.value_or_raise()
        self.assertEqual(jsonutils.to_primitive(fake_act), result) 
Example 59
Project: nova   Author: ZhanHan   File: test_cells_messaging.py    Apache License 2.0 5 votes vote down vote up
def test_action_events_get(self):
        fake_action_id = fake_server_actions.FAKE_ACTION_ID1
        fake_events = fake_server_actions.FAKE_EVENTS[fake_action_id]

        self.mox.StubOutWithMock(self.tgt_db_inst, 'action_events_get')
        self.tgt_db_inst.action_events_get(self.ctxt,
                                     'fake-action').AndReturn(fake_events)
        self.mox.ReplayAll()

        response = self.src_msg_runner.action_events_get(self.ctxt,
                                                         self.tgt_cell_name,
                                                         'fake-action')
        result = response.value_or_raise()
        self.assertEqual(jsonutils.to_primitive(fake_events), result) 
Example 60
Project: nova   Author: ZhanHan   File: test_driver.py    Apache License 2.0 5 votes vote down vote up
def test_connection_to_primitive(self):
        # Test bug 962840.
        import nova.virt.libvirt.driver as libvirt_driver
        drvr = libvirt_driver.LibvirtDriver('')
        drvr.set_host_enabled = mock.Mock()
        jsonutils.to_primitive(drvr._conn, convert_instances=True) 
Example 61
Project: nova   Author: ZhanHan   File: test_hardware.py    Apache License 2.0 5 votes vote down vote up
def test_instance_serialized_by_build_request_spec(self):
        host = objects.ComputeNode(numa_topology=self.hosttopo._to_json())
        fake_uuid = uuids.fake
        instance = objects.Instance(context=self.context, id=1, uuid=fake_uuid,
                numa_topology=self.instancetopo)
        # NOTE (ndipanov): This emulates scheduler.utils.build_request_spec
        # We can remove this test once we no longer use that method.
        instance_raw = jsonutils.to_primitive(
                base_obj.obj_to_primitive(instance))
        res = hw.get_host_numa_usage_from_instance(host, instance_raw)
        self.assertIsInstance(res, six.string_types)
        self._check_usage(objects.NUMATopology.obj_from_db_obj(res)) 
Example 62
Project: nova   Author: ZhanHan   File: fake_notifier.py    Apache License 2.0 5 votes vote down vote up
def _notify(self, priority, ctxt, event_type, payload):
        payload = self._serializer.serialize_entity(ctxt, payload)
        # NOTE(sileht): simulate the kombu serializer
        # this permit to raise an exception if something have not
        # been serialized correctly
        jsonutils.to_primitive(payload)
        # NOTE(melwitt): Try to serialize the context, as the rpc would.
        #                An exception will be raised if something is wrong
        #                with the context.
        self._serializer.serialize_context(ctxt)
        msg = FakeMessage(self.publisher_id, priority, event_type,
                          payload, ctxt)
        NOTIFICATIONS.append(msg) 
Example 63
Project: nova   Author: ZhanHan   File: utils.py    Apache License 2.0 5 votes vote down vote up
def build_request_spec(ctxt, image, instances, instance_type=None):
    """Build a request_spec for the scheduler.

    The request_spec assumes that all instances to be scheduled are the same
    type.
    """
    instance = instances[0]
    if instance_type is None:
        if isinstance(instance, obj_instance.Instance):
            instance_type = instance.get_flavor()
        else:
            instance_type = flavors.extract_flavor(instance)

    if isinstance(instance, obj_instance.Instance):
        instance = obj_base.obj_to_primitive(instance)
        # obj_to_primitive doesn't copy this enough, so be sure
        # to detach our metadata blob because we modify it below.
        instance['system_metadata'] = dict(instance.get('system_metadata', {}))

    if isinstance(instance_type, objects.Flavor):
        instance_type = obj_base.obj_to_primitive(instance_type)
        # NOTE(danms): Replicate this old behavior because the
        # scheduler RPC interface technically expects it to be
        # there. Remove this when we bump the scheduler RPC API to
        # v5.0
        try:
            flavors.save_flavor_info(instance.get('system_metadata', {}),
                                     instance_type)
        except KeyError:
            # If the flavor isn't complete (which is legit with a
            # flavor object, just don't put it in the request spec
            pass

    request_spec = {
            'image': image or {},
            'instance_properties': instance,
            'instance_type': instance_type,
            'num_instances': len(instances)}
    return jsonutils.to_primitive(request_spec) 
Example 64
Project: nova   Author: ZhanHan   File: baserpc.py    Apache License 2.0 5 votes vote down vote up
def ping(self, context, arg, timeout=None):
        arg_p = jsonutils.to_primitive(arg)
        cctxt = self.client.prepare(timeout=timeout)
        return cctxt.call(context, 'ping', arg=arg_p) 
Example 65
Project: mistral-lib   Author: openstack   File: serialization.py    Apache License 2.0 5 votes vote down vote up
def serialize(self, entity):
        if entity is None:
            return None

        entity_dict = self.serialize_to_dict(entity)

        return jsonutils.dumps(
            jsonutils.to_primitive(entity_dict, convert_instances=True)
        ) 
Example 66
Project: stx-nova   Author: starlingx-staging   File: rpc.py    Apache License 2.0 5 votes vote down vote up
def fallback(obj):
        """Serializer fallback

        This method is used to serialize an object which jsonutils.to_primitive
        does not otherwise know how to handle.

        This is mostly only needed in tests because of the use of the nova
        CheatingSerializer fixture which keeps some non-serializable fields
        on the RequestContext, like db_connection.
        """
        if isinstance(obj, nova.context.RequestContext):
            # This matches RequestContextSerializer.serialize_context().
            return obj.to_dict()
        # The default fallback in jsonutils.to_primitive() is six.text_type.
        return six.text_type(obj) 
Example 67
Project: stx-nova   Author: starlingx-staging   File: rpc.py    Apache License 2.0 5 votes vote down vote up
def serialize_entity(self, context, entity):
        return jsonutils.to_primitive(entity, convert_instances=True,
                                      fallback=self.fallback) 
Example 68
Project: stx-nova   Author: starlingx-staging   File: rpcapi.py    Apache License 2.0 5 votes vote down vote up
def migrate_server(self, context, instance, scheduler_hint, live, rebuild,
                  flavor, block_migration, disk_over_commit,
                  reservations=None, clean_shutdown=True, request_spec=None,
                  host_list=None):
        kw = {'instance': instance, 'scheduler_hint': scheduler_hint,
              'live': live, 'rebuild': rebuild, 'flavor': flavor,
              'block_migration': block_migration,
              'disk_over_commit': disk_over_commit,
              'reservations': reservations,
              'clean_shutdown': clean_shutdown,
              'request_spec': request_spec,
              'host_list': host_list,
              }
        version = '1.20'
        if not self.client.can_send_version(version):
            del kw['host_list']
            version = '1.13'
        if not self.client.can_send_version(version):
            del kw['request_spec']
            version = '1.11'
        if not self.client.can_send_version(version):
            del kw['clean_shutdown']
            version = '1.10'
        if not self.client.can_send_version(version):
            kw['flavor'] = objects_base.obj_to_primitive(flavor)
            version = '1.6'
        if not self.client.can_send_version(version):
            kw['instance'] = jsonutils.to_primitive(
                    objects_base.obj_to_primitive(instance))
            version = '1.4'
        cctxt = self.client.prepare(version=version)
        return cctxt.call(context, 'migrate_server', **kw) 
Example 69
Project: stx-nova   Author: starlingx-staging   File: test_compute.py    Apache License 2.0 5 votes vote down vote up
def get_primitive_instance_by_uuid(context, instance_uuid):
    """Helper method to get an instance and then convert it to
    a primitive form using jsonutils.
    """
    instance = db.instance_get_by_uuid(context, instance_uuid)
    return jsonutils.to_primitive(instance) 
Example 70
Project: stx-nova   Author: starlingx-staging   File: test_compute.py    Apache License 2.0 5 votes vote down vote up
def test_resize_instance_driver_error(self):
        # Ensure instance status set to Error on resize error.

        def throw_up(*args, **kwargs):
            raise test.TestingException()

        self.stub_out('nova.virt.fake.FakeDriver.migrate_disk_and_power_off',
                       throw_up)

        instance = self._create_fake_instance_obj()

        self.compute.build_and_run_instance(self.context, instance, {}, {}, {},
                                            block_device_mapping=[])
        instance.host = 'foo'
        instance.save()
        self.compute.prep_resize(self.context, instance=instance,
                                 instance_type=self.default_flavor, image={},
                                 request_spec={},
                                 filter_properties={}, node=None,
                                 clean_shutdown=True, migration=None,
                                 host_list=[])
        instance.task_state = task_states.RESIZE_PREP
        instance.save()
        migration = objects.Migration.get_by_instance_and_status(
                self.context.elevated(),
                instance.uuid, 'pre-migrating')

        # verify
        self.assertRaises(test.TestingException, self.compute.resize_instance,
                          self.context, instance=instance,
                          migration=migration, image={},
                          instance_type=jsonutils.to_primitive(
                              self.default_flavor),
                          clean_shutdown=True)
        # NOTE(comstud): error path doesn't use objects, so our object
        # is not updated.  Refresh and compare against the DB.
        instance.refresh()
        self.assertEqual(instance.vm_state, vm_states.ERROR)
        self.compute.terminate_instance(self.context, instance, []) 
Example 71
Project: stx-nova   Author: starlingx-staging   File: test_compute.py    Apache License 2.0 5 votes vote down vote up
def test_resize_instance_driver_rollback(self):
        # Ensure instance status set to Running after rollback.

        def throw_up(*args, **kwargs):
            raise exception.InstanceFaultRollback(test.TestingException())

        self.stub_out('nova.virt.fake.FakeDriver.migrate_disk_and_power_off',
                       throw_up)

        instance = self._create_fake_instance_obj()
        self.compute.build_and_run_instance(self.context, instance, {}, {}, {},
                                            block_device_mapping=[])
        instance.host = 'foo'
        instance.save()
        self.compute.prep_resize(self.context, instance=instance,
                                 instance_type=self.default_flavor, image={},
                                 request_spec={},
                                 filter_properties={}, node=None,
                                 migration=None, host_list=[],
                                 clean_shutdown=True)
        instance.task_state = task_states.RESIZE_PREP
        instance.save()

        migration = objects.Migration.get_by_instance_and_status(
                self.context.elevated(),
                instance.uuid, 'pre-migrating')

        self.assertRaises(test.TestingException, self.compute.resize_instance,
                          self.context, instance=instance,
                          migration=migration, image={},
                          instance_type=jsonutils.to_primitive(
                              self.default_flavor),
                          clean_shutdown=True)
        # NOTE(comstud): error path doesn't use objects, so our object
        # is not updated.  Refresh and compare against the DB.
        instance.refresh()
        self.assertEqual(instance.vm_state, vm_states.ACTIVE)
        self.assertIsNone(instance.task_state)
        self.compute.terminate_instance(self.context, instance, []) 
Example 72
Project: stx-nova   Author: starlingx-staging   File: test_rpcapi.py    Apache License 2.0 5 votes vote down vote up
def setUp(self):
        super(ComputeRpcAPITestCase, self).setUp()
        self.context = context.get_admin_context()
        self.fake_flavor_obj = fake_flavor.fake_flavor_obj(self.context)
        self.fake_flavor = jsonutils.to_primitive(self.fake_flavor_obj)
        instance_attr = {'host': 'fake_host',
                         'instance_type_id': self.fake_flavor_obj['id'],
                         'instance_type': self.fake_flavor_obj}
        self.fake_instance_obj = fake_instance.fake_instance_obj(self.context,
                                                   **instance_attr)
        self.fake_instance = jsonutils.to_primitive(self.fake_instance_obj)
        self.fake_volume_bdm = objects_block_dev.BlockDeviceMapping(
                **fake_block_device.FakeDbBlockDeviceDict(
                    {'source_type': 'volume', 'destination_type': 'volume',
                     'instance_uuid': self.fake_instance_obj.uuid,
                     'volume_id': 'fake-volume-id'}))
        # FIXME(melwitt): Temporary while things have no mappings
        self.patcher1 = mock.patch('nova.objects.InstanceMapping.'
                                   'get_by_instance_uuid')
        self.patcher2 = mock.patch('nova.objects.HostMapping.get_by_host')
        mock_inst_mapping = self.patcher1.start()
        mock_host_mapping = self.patcher2.start()
        mock_inst_mapping.side_effect = exception.InstanceMappingNotFound(
                uuid=self.fake_instance_obj.uuid)
        mock_host_mapping.side_effect = exception.HostMappingNotFound(
                name=self.fake_instance_obj.host) 
Example 73
Project: stx-nova   Author: starlingx-staging   File: test_conductor.py    Apache License 2.0 5 votes vote down vote up
def test_schedule_and_build_instances_with_tags(self):

        build_request = fake_build_request.fake_req_obj(self.context)
        request_spec = objects.RequestSpec(
            instance_uuid=build_request.instance_uuid)
        image = {'fake_data': 'should_pass_silently'}
        admin_password = 'fake_password'
        injected_file = 'fake'
        requested_network = None
        block_device_mapping = None
        tags = ['fake_tag']
        version = '1.17'
        cctxt_mock = mock.MagicMock()

        @mock.patch.object(self.conductor.client, 'can_send_version',
                           return_value=True)
        @mock.patch.object(self.conductor.client, 'prepare',
                          return_value=cctxt_mock)
        def _test(prepare_mock, can_send_mock):
            self.conductor.schedule_and_build_instances(
                self.context, build_request, request_spec, image,
                admin_password, injected_file, requested_network,
                block_device_mapping, tags=tags)
            prepare_mock.assert_called_once_with(version=version)
            kw = {'build_requests': build_request,
                  'request_specs': request_spec,
                  'image': jsonutils.to_primitive(image),
                  'admin_password': admin_password,
                  'injected_files': injected_file,
                  'requested_networks': requested_network,
                  'block_device_mapping': block_device_mapping,
                  'tags': tags}
            cctxt_mock.cast.assert_called_once_with(
                self.context, 'schedule_and_build_instances', **kw)
        _test() 
Example 74
Project: stx-nova   Author: starlingx-staging   File: test_conductor.py    Apache License 2.0 5 votes vote down vote up
def test_schedule_and_build_instances_with_tags_cannot_send(self):

        build_request = fake_build_request.fake_req_obj(self.context)
        request_spec = objects.RequestSpec(
            instance_uuid=build_request.instance_uuid)
        image = {'fake_data': 'should_pass_silently'}
        admin_password = 'fake_password'
        injected_file = 'fake'
        requested_network = None
        block_device_mapping = None
        tags = ['fake_tag']
        cctxt_mock = mock.MagicMock()

        @mock.patch.object(self.conductor.client, 'can_send_version',
                           return_value=False)
        @mock.patch.object(self.conductor.client, 'prepare',
                           return_value=cctxt_mock)
        def _test(prepare_mock, can_send_mock):
            self.conductor.schedule_and_build_instances(
                self.context, build_request, request_spec, image,
                admin_password, injected_file, requested_network,
                block_device_mapping, tags=tags)
            prepare_mock.assert_called_once_with(version='1.16')
            kw = {'build_requests': build_request,
                  'request_specs': request_spec,
                  'image': jsonutils.to_primitive(image),
                  'admin_password': admin_password,
                  'injected_files': injected_file,
                  'requested_networks': requested_network,
                  'block_device_mapping': block_device_mapping}
            cctxt_mock.cast.assert_called_once_with(
                self.context, 'schedule_and_build_instances', **kw)
        _test() 
Example 75
Project: stx-nova   Author: starlingx-staging   File: test_driver.py    Apache License 2.0 5 votes vote down vote up
def test_connection_to_primitive(self):
        # Test bug 962840.
        import nova.virt.libvirt.driver as libvirt_driver
        drvr = libvirt_driver.LibvirtDriver('')
        drvr.set_host_enabled = mock.Mock()
        jsonutils.to_primitive(drvr._conn, convert_instances=True) 
Example 76
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 77
Project: Trusted-Platform-Module-nova   Author: BU-NU-CLOUD-SP16   File: test_compute.py    Apache License 2.0 4 votes vote down vote up
def _test_resize_instance(self, clean_shutdown=True):
        # Ensure instance can be migrated/resized.
        instance = self._create_fake_instance_obj()
        instance_type = flavors.get_default_flavor()

        self.compute.build_and_run_instance(self.context, instance, {}, {}, {},
                                            block_device_mapping=[])
        instance.host = 'foo'
        instance.save()
        self.compute.prep_resize(self.context, instance=instance,
                instance_type=instance_type, image={}, reservations=[],
                request_spec={}, filter_properties={}, node=None,
                clean_shutdown=True)

        # verify 'old_vm_state' was set on system_metadata
        instance.refresh()
        sys_meta = instance.system_metadata
        self.assertEqual(vm_states.ACTIVE, sys_meta['old_vm_state'])

        self._stub_out_resize_network_methods()

        instance.task_state = task_states.RESIZE_PREP
        instance.save()

        migration = objects.Migration.get_by_instance_and_status(
                self.context.elevated(),
                instance.uuid, 'pre-migrating')

        with test.nested(
            mock.patch.object(objects.BlockDeviceMappingList,
                'get_by_instance_uuid', return_value='fake_bdms'),
            mock.patch.object(
                self.compute, '_get_instance_block_device_info',
                return_value='fake_bdinfo'),
            mock.patch.object(self.compute, '_terminate_volume_connections'),
            mock.patch.object(self.compute, '_get_power_off_values',
                return_value=(1, 2))
        ) as (mock_get_by_inst_uuid, mock_get_instance_vol_bdinfo,
                mock_terminate_vol_conn, mock_get_power_off_values):
            self.compute.resize_instance(self.context, instance=instance,
                    migration=migration, image={}, reservations=[],
                    instance_type=jsonutils.to_primitive(instance_type),
                    clean_shutdown=clean_shutdown)
            mock_get_instance_vol_bdinfo.assert_called_once_with(
                    self.context, instance, bdms='fake_bdms')
            mock_terminate_vol_conn.assert_called_once_with(self.context,
                    instance, 'fake_bdms')
            mock_get_power_off_values.assert_called_once_with(self.context,
                    instance, clean_shutdown)
            self.assertEqual(migration.dest_compute, instance.host)
            self.compute.terminate_instance(self.context, instance, [], []) 
Example 78
Project: nova   Author: ZhanHan   File: test_compute.py    Apache License 2.0 4 votes vote down vote up
def test_resize_instance_driver_error(self, mock_rollback):
        # Ensure instance status set to Error on resize error.

        def throw_up(*args, **kwargs):
            raise test.TestingException()

        self.stub_out('nova.virt.fake.FakeDriver.migrate_disk_and_power_off',
                       throw_up)

        instance = self._create_fake_instance_obj()
        instance_type = flavors.get_default_flavor()

        reservations = list('fake_res')

        self.compute.build_and_run_instance(self.context, instance, {}, {}, {},
                                            block_device_mapping=[])
        instance.host = 'foo'
        instance.save()
        self.compute.prep_resize(self.context, instance=instance,
                                 instance_type=instance_type, image={},
                                 reservations=reservations, request_spec={},
                                 filter_properties={}, node=None,
                                 clean_shutdown=True)
        instance.task_state = task_states.RESIZE_PREP
        instance.save()
        migration = objects.Migration.get_by_instance_and_status(
                self.context.elevated(),
                instance.uuid, 'pre-migrating')

        # verify
        self.assertRaises(test.TestingException, self.compute.resize_instance,
                          self.context, instance=instance,
                          migration=migration, image={},
                          reservations=reservations,
                          instance_type=jsonutils.to_primitive(instance_type),
                          clean_shutdown=True)
        # NOTE(comstud): error path doesn't use objects, so our object
        # is not updated.  Refresh and compare against the DB.
        instance.refresh()
        self.assertEqual(instance.vm_state, vm_states.ERROR)
        self.compute.terminate_instance(self.context, instance, [], [])
        self._ensure_quota_reservations(instance, reservations,
                                        mock_rollback) 
Example 79
Project: nova   Author: ZhanHan   File: test_compute.py    Apache License 2.0 4 votes vote down vote up
def test_resize_instance_driver_rollback(self, mock_rollback):
        # Ensure instance status set to Running after rollback.

        def throw_up(*args, **kwargs):
            raise exception.InstanceFaultRollback(test.TestingException())

        self.stub_out('nova.virt.fake.FakeDriver.migrate_disk_and_power_off',
                       throw_up)

        instance = self._create_fake_instance_obj()
        instance_type = flavors.get_default_flavor()
        reservations = list('fake_res')
        self.compute.build_and_run_instance(self.context, instance, {}, {}, {},
                                            block_device_mapping=[])
        instance.host = 'foo'
        instance.save()
        self.compute.prep_resize(self.context, instance=instance,
                                 instance_type=instance_type, image={},
                                 reservations=reservations, request_spec={},
                                 filter_properties={}, node=None,
                                 clean_shutdown=True)
        instance.task_state = task_states.RESIZE_PREP
        instance.save()

        migration = objects.Migration.get_by_instance_and_status(
                self.context.elevated(),
                instance.uuid, 'pre-migrating')

        self.assertRaises(test.TestingException, self.compute.resize_instance,
                          self.context, instance=instance,
                          migration=migration, image={},
                          reservations=reservations,
                          instance_type=jsonutils.to_primitive(instance_type),
                          clean_shutdown=True)
        # NOTE(comstud): error path doesn't use objects, so our object
        # is not updated.  Refresh and compare against the DB.
        instance.refresh()
        self.assertEqual(instance.vm_state, vm_states.ACTIVE)
        self.assertIsNone(instance.task_state)
        self.compute.terminate_instance(self.context, instance, [], [])
        self._ensure_quota_reservations(instance, reservations,
                                        mock_rollback) 
Example 80
Project: stx-nova   Author: starlingx-staging   File: test_compute.py    Apache License 2.0 4 votes vote down vote up
def _test_resize_instance(self, clean_shutdown=True):
        # Ensure instance can be migrated/resized.
        instance = self._create_fake_instance_obj()

        self.compute.build_and_run_instance(self.context, instance, {}, {}, {},
                                            block_device_mapping=[])
        instance.host = 'foo'
        instance.save()
        self.compute.prep_resize(self.context, instance=instance,
                instance_type=self.default_flavor, image={},
                request_spec={}, filter_properties={}, node=None,
                clean_shutdown=True, migration=None, host_list=[])

        # verify 'old_vm_state' was set on system_metadata
        instance.refresh()
        sys_meta = instance.system_metadata
        self.assertEqual(vm_states.ACTIVE, sys_meta['old_vm_state'])

        self._stub_out_resize_network_methods()

        instance.task_state = task_states.RESIZE_PREP
        instance.save()

        migration = objects.Migration.get_by_instance_and_status(
                self.context.elevated(),
                instance.uuid, 'pre-migrating')

        with test.nested(
            mock.patch.object(nova.compute.utils,
                'notify_about_instance_action'),
            mock.patch.object(objects.BlockDeviceMappingList,
                'get_by_instance_uuid', return_value='fake_bdms'),
            mock.patch.object(
                self.compute, '_get_instance_block_device_info',
                return_value='fake_bdinfo'),
            mock.patch.object(self.compute, '_terminate_volume_connections'),
            mock.patch.object(self.compute, '_get_power_off_values',
                return_value=(1, 2)),
            mock.patch.object(nova.compute.utils, 'is_volume_backed_instance',
                return_value=False)
        ) as (mock_notify_action, mock_get_by_inst_uuid,
                mock_get_instance_vol_bdinfo,
                mock_terminate_vol_conn, mock_get_power_off_values,
                mock_check_is_bfv):
            self.compute.resize_instance(self.context, instance=instance,
                    migration=migration, image={},
                    instance_type=jsonutils.to_primitive(self.default_flavor),
                    clean_shutdown=clean_shutdown)
            mock_notify_action.assert_has_calls([
                mock.call(self.context, instance, 'fake-mini',
                      action='resize', phase='start', bdms='fake_bdms'),
                mock.call(self.context, instance, 'fake-mini',
                      action='resize', phase='end', bdms='fake_bdms')])
            mock_get_instance_vol_bdinfo.assert_called_once_with(
                    self.context, instance, bdms='fake_bdms')
            mock_terminate_vol_conn.assert_called_once_with(self.context,
                    instance, 'fake_bdms')
            mock_get_power_off_values.assert_called_once_with(self.context,
                    instance, clean_shutdown)
            self.assertEqual(migration.dest_compute, instance.host)
            self.compute.terminate_instance(self.context, instance, [])