Skip to content

Commit

Permalink
compute controller: list servers in OCCI
Browse files Browse the repository at this point in the history
With the new code the controllers should call the application
themselves, get the response, and "re-occi-fy" it. We do this in the
compute controller so we call nova, capture the response, parse it,
build the OCCI objects and return an OCCI collection so that it is
properly rendered.
  • Loading branch information
alvarolopez committed Mar 5, 2015
1 parent ad57496 commit 08c57d4
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
13 changes: 12 additions & 1 deletion ooi/api/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,21 @@
# under the License.

from ooi.api import base
from ooi.occi.core import collection
from ooi.occi.infrastructure import compute


class Controller(base.Controller):
def index(self, req):
tenant_id = req.environ["keystone.token_auth"].user.project_id
req = self._get_req(req, path="/%s/servers" % tenant_id)
return req.get_response(self.app)
response = req.get_response(self.app)

servers = response.json_body.get("servers", [])
occi_compute_resources = []
if servers:
for s in servers:
s = compute.ComputeResource(title=s["name"], id=s["id"])
occi_compute_resources.append(s)

return collection.Collection(resources=occi_compute_resources)
31 changes: 29 additions & 2 deletions ooi/tests/test_compute_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# under the License.

import json
import uuid

import mock
import webob
Expand All @@ -41,7 +42,7 @@ def create_fake_json_resp(data):


class TestComputeMiddleware(base.TestCase):
def test_list_vms_all(self):
def test_list_vms_empty(self):
d = {"servers": []}
fake_resp = create_fake_json_resp(d)

Expand All @@ -57,4 +58,30 @@ def test_list_vms_all(self):
self.assertEqual("/3dd7b3f6-c19d-11e4-8dfc-aa07a5b093db/servers",
req.environ["PATH_INFO"])

self.assertEqual(d, resp.json_body)
self.assertEqual(200, resp.status_code)
self.assertEqual("", resp.text)

def test_list_vms_one_vm(self):
tenant = uuid.uuid4().hex

d = {"servers": [{"id": uuid.uuid4().hex, "name": "foo"},
{"id": uuid.uuid4().hex, "name": "bar"},
{"id": uuid.uuid4().hex, "name": "baz"}]}

fake_resp = create_fake_json_resp(d)

app = wsgi.OCCIMiddleware(fake_app(fake_resp))
req = webob.Request.blank("/compute", method="GET")

m = mock.MagicMock()
m.user.project_id = tenant
req.environ["keystone.token_auth"] = m

resp = req.get_response(app)

self.assertEqual("/%s/servers" % tenant, req.environ["PATH_INFO"])

self.assertEqual(200, resp.status_code)
for s in d["servers"]:
expected = "X-OCCI-Location: /compute/%s" % s["id"]
self.assertIn(expected, resp.text)

0 comments on commit 08c57d4

Please sign in to comment.