Skip to content

Commit

Permalink
Merge 2161eed into 8ab4be5
Browse files Browse the repository at this point in the history
  • Loading branch information
enolfc committed Apr 9, 2015
2 parents 8ab4be5 + 2161eed commit 27a3f5b
Show file tree
Hide file tree
Showing 15 changed files with 607 additions and 24 deletions.
9 changes: 8 additions & 1 deletion ooi/api/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import ooi.api.base
from ooi.occi.core import collection
from ooi.occi.infrastructure import compute
from ooi.occi.infrastructure import storage
from ooi.occi.infrastructure import storage_link
from ooi.openstack import helpers
from ooi.openstack import templates

Expand Down Expand Up @@ -103,6 +105,11 @@ def show(self, id, req):
cores=flavor["vcpus"],
hostname=s["name"],
memory=flavor["ram"],
state=helpers.occi_state(s["status"]),
state=helpers.vm_state(s["status"]),
mixins=[os_tpl, res_tpl])
# storage links
vols_attached = s.get("os-extended-volumes:volumes_attached", [])
for v in vols_attached:
st = storage.StorageResource(title="storage", id=v["id"])
comp._links.append(storage_link.StorageLink(comp, st))
return [comp]
7 changes: 7 additions & 0 deletions ooi/api/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from ooi.occi.core import link
from ooi.occi.core import resource
from ooi.occi.infrastructure import compute
from ooi.occi.infrastructure import storage
from ooi.occi.infrastructure import storage_link
from ooi.occi.infrastructure import templates as infra_templates
from ooi.openstack import mixins
from ooi.openstack import templates
Expand Down Expand Up @@ -63,6 +65,11 @@ def index(self, req):
l.append(compute.ComputeResource.kind)
l.extend(compute.ComputeResource.actions)

# OCCI infra Storage
l.append(storage.StorageResource.kind)
l.append(storage_link.StorageLink.kind)
l.extend(storage.StorageResource.actions)

# OCCI infra mixins
l.append(infra_templates.os_tpl)
l.append(infra_templates.resource_tpl)
Expand Down
48 changes: 48 additions & 0 deletions ooi/api/storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- 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.api import base
from ooi.occi.core import collection
from ooi.occi.infrastructure import storage
from ooi.openstack import helpers


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/os-volumes" % tenant_id)
response = req.get_response(self.app)
volumes = self.get_from_response(response, "volumes", [])
occi_storage_resources = []
if volumes:
for v in volumes:
s = storage.StorageResource(title=v["displayName"], id=v["id"])
occi_storage_resources.append(s)

return collection.Collection(resources=occi_storage_resources)

def show(self, id, req):
tenant_id = req.environ["keystone.token_auth"].user.project_id

# get info from server
req = self._get_req(req, path="/%s/os-volumes/%s" % (tenant_id, id))
response = req.get_response(self.app)
v = self.get_from_response(response, "volume", {})

state = helpers.vol_state(v["status"])
st = storage.StorageResource(title=v["displayName"], id=v["id"],
size=v["size"], state=state)
return [st]
4 changes: 2 additions & 2 deletions ooi/occi/core/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class Link(entity.Entity):
kind = kind.Kind(helpers.build_scheme("core"), 'link', 'link',
attributes, '/link/')

def __init__(self, title, mixins, source, target):
super(Link, self).__init__(title, mixins)
def __init__(self, title, mixins, source, target, id=None):
super(Link, self).__init__(title, mixins, id)
self.attributes["occi.core.source"] = attribute.MutableAttribute(
"occi.core.source", source)
self.attributes["occi.core.target"] = attribute.MutableAttribute(
Expand Down
67 changes: 67 additions & 0 deletions ooi/occi/infrastructure/storage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# -*- 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 action
from ooi.occi.core import attribute as attr
from ooi.occi.core import kind
from ooi.occi.core import resource
from ooi.occi import helpers

online = action.Action(helpers.build_scheme('infrastructure/storage/action'),
"online", "online storage instance")

offline = action.Action(helpers.build_scheme('infrastructure/storage/action'),
"offline", "offline storage instance")

backup = action.Action(helpers.build_scheme('infrastructure/storage/action'),
"backup", "backup storage instance")

snapshot = action.Action(helpers.build_scheme('infrastructure/storage/action'),
"snapshot", "snapshot storage instance")

resize = action.Action(helpers.build_scheme('infrastructure/storage/action'),
"resize", "resize storage instance")


class StorageResource(resource.Resource):
attributes = attr.AttributeCollection(["occi.storage.size",
"occi.storage.state"])
actions = (online, offline, backup, snapshot, resize)
kind = kind.Kind(helpers.build_scheme('infrastructure'), 'storage',
'storage resource', attributes, '/storage/',
actions=actions,
related=[resource.Resource.kind])

def __init__(self, title, summary=None, id=None, size=None, state=None):
mixins = []
super(StorageResource, self).__init__(title, mixins, summary=summary,
id=id)
self.attributes["occi.storage.size"] = attr.MutableAttribute(
"occi.storage.size", size)
self.attributes["occi.storage.state"] = attr.InmutableAttribute(
"occi.storage.state", state)

@property
def size(self):
return self.attributes["occi.storage.size"].value

@size.setter
def size(self, value):
self.attributes["occi.storage.size"].value = value

@property
def state(self):
return self.attributes["occi.storage.state"].value
63 changes: 63 additions & 0 deletions ooi/occi/infrastructure/storage_link.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# -*- 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 as attr
from ooi.occi.core import kind
from ooi.occi.core import link
from ooi.occi import helpers


class StorageLink(link.Link):
attributes = attr.AttributeCollection(["occi.storagelink.deviceid",
"occi.storagelink.mountpoint",
"occi.storagelink.state"])
kind = kind.Kind(helpers.build_scheme('infrastructure'), 'storagelink',
'storage link resource', attributes, '/storagelink/',
related=[link.Link.kind])

def __init__(self, source, target, deviceid=None, mountpoint=None,
state=None):

# TODO(enolfc): is this a valid link id?
link_id = '_'.join([source.id, target.id])
super(StorageLink, self).__init__(None, [], source, target, link_id)

self.attributes["occi.storagelink.deviceid"] = attr.MutableAttribute(
"occi.storagelink.deviceid", deviceid)
self.attributes["occi.storagelink.mountpoint"] = attr.MutableAttribute(
"occi.storagelink.mountpoint", mountpoint)
self.attributes["occi.storagelink.state"] = attr.InmutableAttribute(
"occi.storagelink.state", state)

@property
def deviceid(self):
return self.attributes["occi.storagelink.deviceid"].value

@deviceid.setter
def deviceid(self, value):
self.attributes["occi.storagelink.deviceid"].value = value

@property
def mountpoint(self):
return self.attributes["occi.storagelink.mountpoint"].value

@mountpoint.setter
def mountpoint(self, value):
self.attributes["occi.storagelink.mountpoint"].value = value

@property
def state(self):
return self.attributes["occi.storagelink.state"].value
32 changes: 27 additions & 5 deletions ooi/occi/rendering/headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from ooi.occi.core import action
from ooi.occi.core import collection
from ooi.occi.core import entity
from ooi.occi.core import kind
from ooi.occi.core import mixin
from ooi.occi.core import resource
Expand Down Expand Up @@ -86,15 +87,38 @@ def render(self, env={}):


class AttributeRenderer(HeaderRenderer):
def render(self, env={}):
def render_attr(self, env={}):
value_str = ''
if isinstance(self.obj.value, six.string_types):
value_str = '"%s"' % self.obj.value
elif isinstance(self.obj.value, bool):
value_str = '"%s"' % str(self.obj.value).lower()
elif isinstance(self.obj.value, entity.Entity):
value_str = '"%s"' % self.obj.value.id
else:
value_str = "%s" % self.obj.value
return [('X-OCCI-Attribute', '%s=%s' % (self.obj.name, value_str))]
return '%s=%s' % (self.obj.name, value_str)

def render(self, env={}):
return [('X-OCCI-Attribute', self.render_attr(env))]


class LinkRenderer(HeaderRenderer):
def render(self, env={}):
ret = []
url = env.get("application_url", "")
url = utils.join_url(url, self.obj.location)
d = {"location": url,
"scheme": self.obj.target.kind.scheme,
"term": self.obj.target.kind.term,
"self": url}
link = '<%(location)s>; rel="%(scheme)s#%(term)s"; self="%(self)s"' % d
ret.append(link)
for a in self.obj.attributes:
if self.obj.attributes[a].value is None:
continue
ret.append(AttributeRenderer(self.obj.attributes[a]).render_attr())
return [('Link', '; '.join(ret))]


class ResourceRenderer(HeaderRenderer):
Expand All @@ -111,9 +135,7 @@ def render(self, env={}):
for a in self.obj.actions:
ret.extend(ActionRenderer(a).render(instance=self.obj.id))
for l in self.obj.links:
pass
# FIXME(aloga): we need to fix this
# ret.append(LinkRenderer(l))
ret.extend(LinkRenderer(l).render())
return ret


Expand Down
7 changes: 6 additions & 1 deletion ooi/openstack/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,15 @@ def build_scheme(category):


# TODO(enolfc): Check the correct names of nova states
def occi_state(nova_status):
def vm_state(nova_status):
if nova_status in ["ACTIVE"]:
return "active"
elif nova_status in ["PAUSED", "SUSPENDED", "STOPPED"]:
return "suspended"
else:
return "inactive"


# TODO(enolfc): Do really implement this.
def vol_state(nova_status):
return "online"
Loading

0 comments on commit 27a3f5b

Please sign in to comment.