Skip to content

Instantly share code, notes, and snippets.

@mgagne
Created April 30, 2018 17:49
Show Gist options
  • Save mgagne/ed1fd3251997b2e60564fed43f47d96f to your computer and use it in GitHub Desktop.
Save mgagne/ed1fd3251997b2e60564fed43f47d96f to your computer and use it in GitHub Desktop.
Add user_id to RequestSpec
From 825af3f12d9e1e1dbefd4c09e2ad2022f7ca90a5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mathieu=20Gagne=CC=81?= <[email protected]>
Date: Tue, 15 Aug 2017 16:52:25 -0400
Subject: [PATCH] Add user_id to RequestSpec
Change-Id: I3e174ae76931f8279540e92328c7c36a7bcaabc0
---
nova/objects/request_spec.py | 16 +++++++++++++---
nova/tests/unit/fake_request_spec.py | 1 +
nova/tests/unit/objects/test_objects.py | 2 +-
nova/tests/unit/objects/test_request_spec.py | 14 ++++++++++++--
4 files changed, 27 insertions(+), 6 deletions(-)
diff --git a/nova/objects/request_spec.py b/nova/objects/request_spec.py
index 573d96c0a7..73b319baa0 100644
--- a/nova/objects/request_spec.py
+++ b/nova/objects/request_spec.py
@@ -13,6 +13,7 @@
# under the License.
from oslo_serialization import jsonutils
+from oslo_utils import versionutils
import six
from nova.db.sqlalchemy import api as db
@@ -33,7 +34,8 @@ class RequestSpec(base.NovaObject):
# Version 1.3: InstanceGroup version 1.10
# Version 1.4: ImageMeta version 1.7
# Version 1.5: Added get_by_instance_uuid(), create(), save()
- VERSION = '1.5'
+ # Version 1.5.1: Added user_id field
+ VERSION = '1.5.1'
fields = {
'id': fields.IntegerField(),
@@ -43,6 +45,7 @@ class RequestSpec(base.NovaObject):
'pci_requests': fields.ObjectField('InstancePCIRequests',
nullable=True),
'project_id': fields.StringField(nullable=True),
+ 'user_id': fields.StringField(nullable=True),
'availability_zone': fields.StringField(nullable=True),
'flavor': fields.ObjectField('Flavor', nullable=False),
'num_instances': fields.IntegerField(default=1),
@@ -79,6 +82,11 @@ def ephemeral_gb(self):
def swap(self):
return self.flavor.swap
+ def obj_make_compatible(self, primitive, target_version):
+ target_version = versionutils.convert_version_to_tuple(target_version)
+ if target_version < (1, 5, 1) and 'user_id' in primitive:
+ del primitive['user_id']
+
def _image_meta_from_image(self, image):
if isinstance(image, objects.ImageMeta):
self.image = image
@@ -108,7 +116,7 @@ def _from_instance(self, instance):
return
instance_fields = ['numa_topology', 'pci_requests', 'uuid',
- 'project_id', 'availability_zone']
+ 'project_id', 'user_id', 'availability_zone']
for field in instance_fields:
if field == 'uuid':
setattr(self, 'instance_uuid', getter(instance, field))
@@ -251,7 +259,8 @@ def _to_legacy_instance(self):
# fields, we can only return a dict.
instance = {}
instance_fields = ['numa_topology', 'pci_requests',
- 'project_id', 'availability_zone', 'instance_uuid']
+ 'project_id', 'user_id', 'availability_zone',
+ 'instance_uuid']
for field in instance_fields:
if not self.obj_attr_is_set(field):
continue
@@ -353,6 +362,7 @@ def from_components(cls, context, instance_uuid, image, flavor,
if spec_obj.instance_group is None and filter_properties:
spec_obj._populate_group_info(filter_properties)
spec_obj.project_id = context.project_id
+ spec_obj.user_id = context.user_id
spec_obj._image_meta_from_image(image)
spec_obj._from_flavor(flavor)
spec_obj._from_instance_pci_requests(pci_requests)
diff --git a/nova/tests/unit/fake_request_spec.py b/nova/tests/unit/fake_request_spec.py
index 8d6e8d96a5..3431dad8d9 100644
--- a/nova/tests/unit/fake_request_spec.py
+++ b/nova/tests/unit/fake_request_spec.py
@@ -79,6 +79,7 @@ def fake_spec_obj(remove_id=False):
req_obj.limits = objects.SchedulerLimits()
req_obj.instance_group = objects.InstanceGroup()
req_obj.project_id = 'fake'
+ req_obj.user_id = 'fake-user'
req_obj.num_instances = 1
req_obj.availability_zone = None
req_obj.ignore_hosts = ['host2', 'host4']
diff --git a/nova/tests/unit/objects/test_objects.py b/nova/tests/unit/objects/test_objects.py
index 9660f44d73..2378ea0bca 100644
--- a/nova/tests/unit/objects/test_objects.py
+++ b/nova/tests/unit/objects/test_objects.py
@@ -1171,7 +1171,7 @@ def obj_name(cls):
'PciDevicePoolList': '1.1-15ecf022a68ddbb8c2a6739cfc9f8f5e',
'Quotas': '1.2-1fe4cd50593aaf5d36a6dc5ab3f98fb3',
'QuotasNoOp': '1.2-e041ddeb7dc8188ca71706f78aad41c1',
- 'RequestSpec': '1.5-576a249869c161e17b7cd6d55f9d85f3',
+ 'RequestSpec': '1.5.1-4952029a2a47c7319bbf477d3e44ab36',
'ResourceProvider': '1.0-57a9a344b0faed9cf6d6811835b6deb6',
'S3ImageMapping': '1.0-7dd7366a890d82660ed121de9092276e',
'SchedulerLimits': '1.0-249c4bd8e62a9b327b7026b7f19cc641',
diff --git a/nova/tests/unit/objects/test_request_spec.py b/nova/tests/unit/objects/test_request_spec.py
index 836d6a5588..509e714850 100644
--- a/nova/tests/unit/objects/test_request_spec.py
+++ b/nova/tests/unit/objects/test_request_spec.py
@@ -29,6 +29,13 @@
class _TestRequestSpecObject(object):
+ def test_obj_make_compatible(self):
+ spec = objects.RequestSpec(user_id='1')
+ primitive = spec.obj_to_primitive()
+ self.assertIn('user_id', primitive['nova_object.data'])
+ spec.obj_make_compatible(primitive['nova_object.data'], '1.5.0')
+ self.assertNotIn('user_id', primitive['nova_object.data'])
+
def test_image_meta_from_image_as_object(self):
# Just isolating the test for the from_dict() method
image_meta = objects.ImageMeta(name='foo')
@@ -75,12 +82,13 @@ def test_from_instance_as_object(self):
instance.numa_topology = None
instance.pci_requests = None
instance.project_id = '1'
+ instance.user_id = '2'
instance.availability_zone = 'nova'
spec = objects.RequestSpec()
spec._from_instance(instance)
instance_fields = ['numa_topology', 'pci_requests', 'uuid',
- 'project_id', 'availability_zone']
+ 'project_id', 'user_id', 'availability_zone']
for field in instance_fields:
if field == 'uuid':
self.assertEqual(getattr(instance, field),
@@ -94,12 +102,13 @@ def test_from_instance_as_dict(self):
numa_topology=None,
pci_requests=None,
project_id='1',
+ user_id='2',
availability_zone='nova')
spec = objects.RequestSpec()
spec._from_instance(instance)
instance_fields = ['numa_topology', 'pci_requests', 'uuid',
- 'project_id', 'availability_zone']
+ 'project_id', 'user_id', 'availability_zone']
for field in instance_fields:
if field == 'uuid':
self.assertEqual(instance.get(field),
@@ -278,6 +287,7 @@ def test_from_primitives(self, mock_limits):
numa_topology=None,
pci_requests=None,
project_id=1,
+ user_id=2,
availability_zone='nova')}
filt_props = {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment