Return collections instead of object lists

Every time we need to return more than one object a collection should
be used instead of a list of those objects. This allows to render the
objects taking into account their context. If returning a single
object controllers must return the object instead of a list.

Change-Id: I0caa3334dbd14374f39bf41acf45528fc7befa45
This commit is contained in:
Enol Fernandez 2016-06-23 13:22:08 +02:00 committed by Enol Fernández
parent 43995a96dc
commit 38cccfe3c5
8 changed files with 86 additions and 80 deletions

View File

@ -264,7 +264,7 @@ class Controller(ooi.api.base.Controller):
net_id = "FIXED"
comp.add_link(_create_network_link(addr, comp, net_id))
return [comp]
return comp
def _get_server_floating_ips(self, req, server_id):
s = self.os_helper.get_server(req, server_id)

View File

@ -15,6 +15,7 @@
from ooi.api import base
import ooi.api.helpers
from ooi.occi.core import collection
from ooi.occi.core import entity
from ooi.occi.core import link
from ooi.occi.core import resource
@ -72,42 +73,46 @@ class Controller(base.Controller):
return occi_ip_pools
def index(self, req):
l = []
# OCCI Core Kinds:
l.append(entity.Entity.kind)
l.append(resource.Resource.kind)
l.append(link.Link.kind)
kinds = []
actions = []
mixins = []
kinds.append(entity.Entity.kind)
kinds.append(resource.Resource.kind)
kinds.append(link.Link.kind)
# OCCI infra Compute:
l.append(compute.ComputeResource.kind)
l.extend(compute.ComputeResource.actions)
kinds.append(compute.ComputeResource.kind)
actions.extend(compute.ComputeResource.actions)
# OCCI infra Storage
l.append(storage.StorageResource.kind)
l.append(storage_link.StorageLink.kind)
l.extend(storage.StorageResource.actions)
kinds.append(storage.StorageResource.kind)
kinds.append(storage_link.StorageLink.kind)
actions.extend(storage.StorageResource.actions)
# OCCI infra network
l.append(network.NetworkResource.kind)
l.extend(network.NetworkResource.actions)
kinds.append(network.NetworkResource.kind)
actions.extend(network.NetworkResource.actions)
if self.neutron_ooi_endpoint:
l.append(os_network.neutron_network)
l.append(network.ip_network)
l.append(network_link.NetworkInterface.kind)
l.append(network_link.ip_network_interface)
mixins.append(os_network.neutron_network)
mixins.append(network.ip_network)
kinds.append(network_link.NetworkInterface.kind)
mixins.append(network_link.ip_network_interface)
# OCCI infra compute mixins
l.append(infra_templates.os_tpl)
l.append(infra_templates.resource_tpl)
mixins.append(infra_templates.os_tpl)
mixins.append(infra_templates.resource_tpl)
# OpenStack flavors & images
l.extend(self._resource_tpls(req))
l.extend(self._os_tpls(req))
mixins.extend(self._resource_tpls(req))
mixins.extend(self._os_tpls(req))
# OpenStack Contextualization
l.append(contextualization.user_data)
l.append(contextualization.public_key)
mixins.append(contextualization.user_data)
mixins.append(contextualization.public_key)
# OpenStack Floating IP Pools
l.extend(self._ip_pools(req))
return l
mixins.extend(self._ip_pools(req))
return collection.Collection(kinds=kinds,
mixins=mixins,
actions=actions)

View File

@ -45,7 +45,7 @@ class Controller(base.Controller):
state = helpers.vol_state(v["status"])
st = storage.StorageResource(title=v["displayName"], id=v["id"],
size=v["size"], state=state)
return [st]
return st
def create(self, req, body):
parser = req.get_parser()(req.headers, req.body)

View File

@ -61,7 +61,7 @@ class Controller(base.Controller):
v = self._get_attachment_from_id(req, id)
c = compute.ComputeResource(title="Compute", id=v["serverId"])
s = storage.StorageResource(title="Storage", id=v["volumeId"])
return [storage_link.StorageLink(c, s, deviceid=v["device"])]
return storage_link.StorageLink(c, s, deviceid=v["device"])
def create(self, req, body):
parser = req.get_parser()(req.headers, req.body)

View File

@ -198,7 +198,7 @@ class TestComputeController(base.TestController):
ret = self.controller.show(None, server["id"])
# FIXME(aloga): Should we test the resource?
self.assertIsInstance(ret[0], occi_compute.ComputeResource)
self.assertIsInstance(ret, occi_compute.ComputeResource)
m_server.assert_called_with(None, server["id"])
m_flavor.assert_called_with(None, flavor["id"])
m_image.assert_called_with(None, image["id"])
@ -229,7 +229,7 @@ class TestComputeController(base.TestController):
ret = self.controller.show(None, server["id"])
# FIXME(aloga): Should we test the resource?
self.assertIsInstance(ret[0], occi_compute.ComputeResource)
self.assertIsInstance(ret, occi_compute.ComputeResource)
m_server.assert_called_with(None, server["id"])
m_flavor.assert_called_with(None, flavor["id"])
m_image.assert_called_with(None, image["id"])

View File

@ -55,50 +55,51 @@ class TestQueryController(base.TestController):
ip_pool = os_network.OSFloatingIPPool("foo")
m_pools.return_value = [ip_pool]
expected = [
res_tpl,
os_tpl,
ip_pool,
# OCCI Core Kinds:
expected_kinds = [
entity.Entity.kind,
resource.Resource.kind,
link.Link.kind,
# OCCI infra Compute:
compute.ComputeResource.kind,
storage.StorageResource.kind,
storage_link.StorageLink.kind,
network.NetworkResource.kind,
network_link.NetworkInterface.kind,
]
expected_mixins = [
res_tpl,
os_tpl,
ip_pool,
network.ip_network,
network_link.ip_network_interface,
infra_templates.os_tpl,
infra_templates.resource_tpl,
contextualization.user_data,
contextualization.public_key,
]
expected_actions = [
compute.start,
compute.stop,
compute.restart,
compute.suspend,
# OCCI infra Storage
storage.StorageResource.kind,
storage_link.StorageLink.kind,
storage.online,
storage.offline,
storage.backup,
storage.snapshot,
storage.resize,
# OCCI infra network
network.NetworkResource.kind,
network.up,
network.down,
network.ip_network,
network_link.NetworkInterface.kind,
network_link.ip_network_interface,
# OCCI infra compute mixins
infra_templates.os_tpl,
infra_templates.resource_tpl,
# OpenStack Contextualization
contextualization.user_data,
contextualization.public_key,
]
ret = self.controller.index(req)
self.assertItemsEqual(expected, ret)
self.assertItemsEqual(expected_kinds, ret.kinds)
self.assertItemsEqual(expected_mixins, ret.mixins)
self.assertItemsEqual(expected_actions, ret.actions)
self.assertEqual([], ret.resources)
self.assertEqual([], ret.links)
@mock.patch.object(query.Controller, "_os_tpls")
@mock.patch.object(query.Controller, "_resource_tpls")
@ -120,51 +121,52 @@ class TestQueryController(base.TestController):
ip_pool = os_network.OSFloatingIPPool("foo")
m_pools.return_value = [ip_pool]
expected = [
res_tpl,
os_tpl,
ip_pool,
# OCCI Core Kinds:
expected_kinds = [
entity.Entity.kind,
resource.Resource.kind,
link.Link.kind,
# OCCI infra Compute:
compute.ComputeResource.kind,
storage.StorageResource.kind,
storage_link.StorageLink.kind,
network.NetworkResource.kind,
network_link.NetworkInterface.kind,
]
expected_mixins = [
res_tpl,
os_tpl,
ip_pool,
os_network.neutron_network,
network.ip_network,
network_link.ip_network_interface,
infra_templates.os_tpl,
infra_templates.resource_tpl,
contextualization.user_data,
contextualization.public_key,
]
expected_actions = [
compute.start,
compute.stop,
compute.restart,
compute.suspend,
# OCCI infra Storage
storage.StorageResource.kind,
storage_link.StorageLink.kind,
storage.online,
storage.offline,
storage.backup,
storage.snapshot,
storage.resize,
# OCCI infra network
network.NetworkResource.kind,
network.up,
network.down,
os_network.neutron_network,
network.ip_network,
network_link.NetworkInterface.kind,
network_link.ip_network_interface,
# OCCI infra compute mixins
infra_templates.os_tpl,
infra_templates.resource_tpl,
# OpenStack Contextualization
contextualization.user_data,
contextualization.public_key,
]
ret = neutron_controller.index(req)
self.assertItemsEqual(expected, ret)
self.assertItemsEqual(expected_kinds, ret.kinds)
self.assertItemsEqual(expected_mixins, ret.mixins)
self.assertItemsEqual(expected_actions, ret.actions)
self.assertEqual([], ret.resources)
self.assertEqual([], ret.links)
@mock.patch.object(helpers.OpenStackHelper, "get_flavors")
def test_get_resource_tpls(self, m_get_flavors):

View File

@ -74,7 +74,7 @@ class TestStorageController(base.TestController):
vols = fakes.volumes[tenant["id"]]
for idx, vol in enumerate(vols):
m_vol.return_value = vol
ret = self.controller.show(None, vol["id"])[0]
ret = self.controller.show(None, vol["id"])
self.assertIsInstance(ret, storage.StorageResource)
self.assertEqual(vol["id"], ret.id)
self.assertEqual(vol["displayName"], ret.title)

View File

@ -74,8 +74,7 @@ class TestStorageLinkController(base.TestController):
"volumeId": vol_id,
"device": "/dev/sda",
}
ret = self.controller.show(None, link_id)
link = ret.pop()
link = self.controller.show(None, link_id)
self.assertIsInstance(link, storage_link.StorageLink)
self.assertIsInstance(link.source, compute.ComputeResource)
self.assertIsInstance(link.target, storage.StorageResource)