Implement db get_[un]associated_nodes

These method were just stubs.
This patch implements them in db/sqlalchemy/api.py, and a subsequent
patch will expose this functionality via the API.

Change-Id: Iae68cbe3bf4e84a0f47a0770695bed9dae78aa95
This commit is contained in:
Devananda van der Veen 2013-10-17 11:25:11 -07:00
parent 5f664f279c
commit 982b8ba204
4 changed files with 53 additions and 11 deletions

View File

@ -196,11 +196,15 @@ class Connection(api.Connection):
@objects.objectify(objects.Node)
def get_associated_nodes(self):
pass
query = model_query(models.Node).\
filter(models.Node.instance_uuid != None)
return query.all()
@objects.objectify(objects.Node)
def get_unassociated_nodes(self):
pass
query = model_query(models.Node).\
filter(models.Node.instance_uuid == None)
return query.all()
@objects.objectify(objects.Node)
def reserve_nodes(self, tag, nodes):
@ -277,11 +281,8 @@ class Connection(api.Connection):
@objects.objectify(objects.Node)
def get_node_by_instance(self, instance):
query = model_query(models.Node)
if uuidutils.is_uuid_like(instance):
query = query.filter_by(instance_uuid=instance)
else:
query = query.filter_by(instance_name=instance)
query = model_query(models.Node).\
filter_by(instance_uuid=instance)
try:
result = query.one()

View File

@ -44,6 +44,28 @@ class DbNodeTestCase(base.DbTestCase):
uuids.sort()
return uuids
def _create_associated_nodes(self):
uuids = []
uuids_with_instance = []
for i in xrange(1, 5):
uuid = uuidutils.generate_uuid()
uuids.append(unicode(uuid))
if i < 3:
instance_uuid = uuidutils.generate_uuid()
uuids_with_instance.append(unicode(uuid))
else:
instance_uuid = None
n = utils.get_test_node(id=i,
uuid=uuid,
instance_uuid=instance_uuid)
self.dbapi.create_node(n)
uuids.sort()
uuids_with_instance.sort()
return (uuids, uuids_with_instance)
def test_create_node(self):
self._create_test_node()
@ -98,7 +120,7 @@ class DbNodeTestCase(base.DbTestCase):
self.assertEqual(uuids.sort(), res_uuids.sort())
def test_get_node_by_instance(self):
n = self._create_test_node()
n = self._create_test_node(instance_uuid='fake-uuid-1234')
res = self.dbapi.get_node_by_instance(n['instance_uuid'])
self.assertEqual(n['uuid'], res['uuid'])
@ -295,3 +317,21 @@ class DbNodeTestCase(base.DbTestCase):
for uuid in uuids:
res = self.dbapi.get_node(uuid)
self.assertEqual(None, res['reservation'])
def test_get_associated_nodes(self):
(uuids, uuids_with_instance) = self._create_associated_nodes()
res = self.dbapi.get_associated_nodes()
res_uuids = [r.uuid for r in res]
res_uuids.sort()
self.assertEqual(uuids_with_instance, res_uuids)
def test_get_unassociated_nodes(self):
(uuids, uuids_with_instance) = self._create_associated_nodes()
uuids_without_instance = list(set(uuids) - set(uuids_with_instance))
uuids_without_instance.sort()
res = self.dbapi.get_unassociated_nodes()
res_uuids = [r.uuid for r in res]
res_uuids.sort()
self.assertEqual(uuids_without_instance, res_uuids)

View File

@ -73,8 +73,7 @@ def get_test_node(**kw):
'provision_state': kw.get('provision_state', states.NOSTATE),
'target_provision_state': kw.get('target_provision_state',
states.NOSTATE),
'instance_uuid': kw.get('instance_uuid',
'8227348d-5f1d-4488-aad1-7c92b2d42504'),
'instance_uuid': kw.get('instance_uuid', None),
'driver': kw.get('driver', 'fake'),
'driver_info': kw.get('driver_info', fake_info),
'properties': kw.get('properties', properties),

View File

@ -31,6 +31,8 @@ commands =
commands = {posargs}
[flake8]
ignore = E12
# E711: ignored because it is normal to use "column == None" in sqlalchemy
ignore = E12,E711
builtins = _
exclude = .venv,.git,.tox,dist,doc,*openstack/common*,*lib/python*,*egg,build,*ironic/nova*,tools