Merge pull request #8 from alvarolopez/query

Update query controller

Add the compute controller related stuff into the query controller.
This commit is contained in:
Alvaro Lopez Garcia 2015-03-31 10:52:34 +02:00
commit e443785026
5 changed files with 199 additions and 1 deletions

View File

@ -15,11 +15,63 @@
# under the License.
from ooi.api import base
from ooi.occi.core import entity
from ooi.occi.core import link
from ooi.occi.core import resource
from ooi.occi.infrastructure import compute
from ooi.occi.infrastructure import templates as infra_templates
from ooi.openstack import mixins
from ooi.openstack import templates
class Controller(base.Controller):
def _resource_tpls(self, req):
tenant_id = req.environ["keystone.token_auth"].user.project_id
req = self._get_req(req, path="/%s/flavors/detail" % tenant_id)
response = req.get_response(self.app)
flavors = response.json_body.get("flavors", [])
occi_resource_templates = []
if flavors:
for f in flavors:
tpl = templates.OpenStackResourceTemplate(f["name"],
f["vcpus"],
f["ram"],
f["disk"])
occi_resource_templates.append(tpl)
return occi_resource_templates
def _os_tpls(self, req):
tenant_id = req.environ["keystone.token_auth"].user.project_id
req = self._get_req(req, path="/%s/images/detail" % tenant_id)
response = req.get_response(self.app)
images = response.json_body.get("images", [])
occi_os_templates = []
if images:
for i in images:
tpl = templates.OpenStackOSTemplate(i["id"], i["name"])
occi_os_templates.append(tpl)
return occi_os_templates
def index(self, req):
l = []
# OCCI Core Kinds:
l.append(entity.Entity.kind)
l.append(resource.Resource.kind)
l.append(link.Link.kind)
# OCCI infra Compute:
l.append(compute.ComputeResource.kind)
l.extend(compute.ComputeResource.actions)
# OCCI infra mixins
l.append(infra_templates.os_tpl)
l.append(infra_templates.resource_tpl)
# OpenStack flavors & images
l.extend(self._resource_tpls(req))
l.extend(self._os_tpls(req))
# OpenStack mixins (contextualization)
l.append(mixins.user_data)
l.append(mixins.public_key)
return l

69
ooi/openstack/mixins.py Normal file
View File

@ -0,0 +1,69 @@
# -*- coding: utf-8 -*-
# Copyright 2015 Spanish National Research Council
#
# 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 ooi.occi.core import attribute
from ooi.occi.core import mixin
from ooi.openstack import helpers
class OpenStackUserData(mixin.Mixin):
def __init__(self, user_data=None):
attrs = [
attribute.InmutableAttribute("org.openstack.compute.user_data",
user_data),
]
attrs = attribute.AttributeCollection({a.name: a for a in attrs})
super(OpenStackUserData, self).__init__(
helpers.build_scheme("compute/instance"),
"user_data", "Contextualization extension - user_data",
attributes=attrs)
@property
def user_data(self):
return self.attributes["org.openstack.compute.user_data"].value
class OpenStackPublicKey(mixin.Mixin):
def __init__(self, name=None, data=None):
attrs = [
attribute.InmutableAttribute(
"org.openstack.credentials.publickey.name", name),
attribute.InmutableAttribute(
"org.openstack.credentials.publickey.data", data),
]
attrs = attribute.AttributeCollection({a.name: a for a in attrs})
super(OpenStackPublicKey, self).__init__(
helpers.build_scheme("instance/credentials"),
"public_key", "Contextualization extension - public_key",
attributes=attrs)
@property
def name(self):
attr = "org.openstack.credentials.publickey.name"
return self.attributes[attr].value
@property
def data(self):
attr = "org.openstack.credentials.publickey.data"
return self.attributes[attr].value
user_data = OpenStackUserData()
public_key = OpenStackPublicKey()

View File

@ -85,6 +85,22 @@ servers = {
def fake_query_results():
cats = []
cats.append(
'compute; '
'scheme="http://schemas.ogf.org/occi/infrastructure"; '
'class="kind"')
cats.append(
'link; '
'scheme="http://schemas.ogf.org/occi/core"; '
'class="kind"')
cats.append(
'resource; '
'scheme="http://schemas.ogf.org/occi/core"; '
'class="kind"')
cats.append(
'entity; '
'scheme="http://schemas.ogf.org/occi/core"; '
'class="kind"')
cats.append(
'start; '
'scheme="http://schemas.ogf.org/occi/infrastructure/compute/action"; '
@ -101,6 +117,38 @@ def fake_query_results():
'suspend; '
'scheme="http://schemas.ogf.org/occi/infrastructure/compute/action"; '
'class="action"')
cats.append(
'bar; '
'scheme="http://schemas.openstack.org/template/os"; '
'class="mixin"')
cats.append(
'bar; '
'scheme="http://schemas.openstack.org/template/resource"; '
'class="mixin"')
cats.append(
'foo; '
'scheme="http://schemas.openstack.org/template/os"; '
'class="mixin"')
cats.append(
'foo; '
'scheme="http://schemas.openstack.org/template/resource"; '
'class="mixin"')
cats.append(
'os_tpl; '
'scheme="http://schemas.ogf.org/occi/infrastructure"; '
'class="mixin"')
cats.append(
'resource_tpl; '
'scheme="http://schemas.ogf.org/occi/infrastructure"; '
'class="mixin"')
cats.append(
'user_data; '
'scheme="http://schemas.openstack.org/compute/instance"; '
'class="mixin"')
cats.append(
'public_key; '
'scheme="http://schemas.openstack.org/instance/credentials"; '
'class="mixin"')
result = []
for c in cats:
@ -125,7 +173,10 @@ class FakeApp(object):
def _populate(self, path_base, obj_name, obj_list):
objs_name = "%ss" % obj_name
objs_path = "%s/%s" % (path_base, objs_name)
objs_details_path = "%s/%s/detail" % (path_base, objs_name)
self.routes[objs_path] = create_fake_json_resp({objs_name: obj_list})
self.routes[objs_details_path] = create_fake_json_resp(
{objs_name: obj_list})
for o in obj_list:
obj_path = "%s/%s" % (objs_path, o["id"])

View File

@ -23,7 +23,8 @@ class TestQueryController(test_middleware.TestMiddleware):
"""Test OCCI query controller."""
def test_query(self):
result = self._build_req("/-/", "tenant").get_response(self.get_app())
tenant_id = fakes.tenants["bar"]["id"]
result = self._build_req("/-/", tenant_id).get_response(self.get_app())
self.assertContentType(result)
self.assertExpectedResult(fakes.fake_query_results(), result)
self.assertEqual(200, result.status_code)

View File

@ -18,6 +18,7 @@ import uuid
from ooi.occi.infrastructure import templates as occi_templates
from ooi.openstack import helpers
from ooi.openstack import mixins
from ooi.openstack import templates
from ooi.tests import base
@ -69,3 +70,27 @@ class TestHelpers(base.TestCase):
self.assertEqual("suspended", helpers.occi_state("SUSPENDED"))
self.assertEqual("suspended", helpers.occi_state("STOPPED"))
self.assertEqual("inactive", helpers.occi_state("BUILDING"))
class TestOpenStackUserData(base.TestCase):
def test_os_userdata(self):
user_data = "foobar"
mxn = mixins.OpenStackUserData(user_data)
self.assertEqual("user_data", mxn.term)
self.assertTrue(mxn.scheme.startswith(helpers._PREFIX))
self.assertEqual(user_data, mxn.user_data)
class TestOpenStackPublicKey(base.TestCase):
def test_os_userdata(self):
key_name = "foobar"
key_data = "1234"
mxn = mixins.OpenStackPublicKey(key_name, key_data)
self.assertEqual("public_key", mxn.term)
self.assertTrue(mxn.scheme.startswith(helpers._PREFIX))
self.assertEqual(key_name, mxn.name)
self.assertEqual(key_data, mxn.data)