Merge "Add get_count_by_vm_state() to InstanceList object"

This commit is contained in:
Jenkins 2017-06-27 19:43:36 +00:00 committed by Gerrit Code Review
commit f27ac38f00
3 changed files with 60 additions and 2 deletions

View File

@ -1195,7 +1195,8 @@ class InstanceList(base.ObjectListBase, base.NovaObject):
# Version 2.0: Initial Version
# Version 2.1: Add get_uuids_by_host()
# Version 2.2: Pagination for get_active_by_window_joined()
VERSION = '2.2'
# Version 2.3: Add get_count_by_vm_state()
VERSION = '2.3'
fields = {
'objects': fields.ListOfObjectsField('Instance'),
@ -1381,6 +1382,21 @@ class InstanceList(base.ObjectListBase, base.NovaObject):
columns_to_join=[])
return [inst['uuid'] for inst in db_instances]
@staticmethod
@db_api.pick_context_manager_reader
def _get_count_by_vm_state_in_db(context, project_id, user_id, vm_state):
return context.session.query(models.Instance.id).\
filter_by(deleted=0).\
filter_by(project_id=project_id).\
filter_by(user_id=user_id).\
filter_by(vm_state=vm_state).\
count()
@base.remotable_classmethod
def get_count_by_vm_state(cls, context, project_id, user_id, vm_state):
return cls._get_count_by_vm_state_in_db(context, project_id, user_id,
vm_state)
@db_api.pick_context_manager_writer
def _migrate_instance_keypairs(ctxt, count):

View File

@ -0,0 +1,42 @@
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
from nova.compute import vm_states
from nova import context
from nova import objects
from nova import test
class InstanceObjectTestCase(test.TestCase):
def setUp(self):
super(InstanceObjectTestCase, self).setUp()
self.context = context.RequestContext('fake-user', 'fake-project')
def _create_instance(self, **values):
inst = objects.Instance(context=self.context,
project_id=self.context.project_id,
user_id=self.context.user_id)
inst.update(values)
inst.create()
return inst
def test_get_count_by_vm_state(self):
# _create_instance() creates an instance with project_id and user_id
# from self.context by default
self._create_instance()
self._create_instance(vm_state=vm_states.ACTIVE)
self._create_instance(vm_state=vm_states.ACTIVE, project_id='foo')
self._create_instance(vm_state=vm_states.ACTIVE, user_id='bar')
count = objects.InstanceList.get_count_by_vm_state(
self.context, self.context.project_id, self.context.user_id,
vm_states.ACTIVE)
self.assertEqual(1, count)

View File

@ -1112,7 +1112,7 @@ object_data = {
'InstanceGroup': '1.10-1a0c8c7447dc7ecb9da53849430c4a5f',
'InstanceGroupList': '1.8-90f8f1a445552bb3bbc9fa1ae7da27d4',
'InstanceInfoCache': '1.5-cd8b96fefe0fc8d4d337243ba0bf0e1e',
'InstanceList': '2.2-ff71772c7bf6d72f6ef6eee0199fb1c9',
'InstanceList': '2.3-7a3c541e6e7b5a75afe7afe125f9712d',
'InstanceMapping': '1.0-65de80c491f54d19374703c0753c4d47',
'InstanceMappingList': '1.2-ee638619aa3d8a82a59c0c83bfa64d78',
'InstanceNUMACell': '1.4-7c1eb9a198dee076b4de0840e45f4f55',