Skip to content

Commit

Permalink
Merge e273dbd into 8ab4be5
Browse files Browse the repository at this point in the history
  • Loading branch information
enolfc committed Apr 9, 2015
2 parents 8ab4be5 + e273dbd commit 4a49f48
Show file tree
Hide file tree
Showing 22 changed files with 789 additions and 65 deletions.
27 changes: 22 additions & 5 deletions ooi/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# License for the specific language governing permissions and limitations
# under the License.

import copy

from ooi import utils

import webob.exc
Expand All @@ -25,14 +27,29 @@ def __init__(self, app, openstack_version):
self.openstack_version = openstack_version

def _get_req(self, req, path=None, content_type=None, body=None):
req.script_name = self.openstack_version
"""Return a new Request object to interact with OpenStack.
This method will create a new request starting with the same WSGI
environment as the original request, prepared to interact with
OpenStack. Namely, it will override the script name to match the
OpenStack version. It will also override the path, content_type and
body of the request, if any of those keyword arguments are passed.
:param req: the original request
:param path: new path for the request
:param content_type: new content type for the request
:param body: new body for the request
:returns: a Request object
"""
new_req = webob.Request(copy.copy(req.environ))
new_req.script_name = self.openstack_version
if path is not None:
req.path_info = path
new_req.path_info = path
if content_type is not None:
req.content_type = content_type
new_req.content_type = content_type
if body is not None:
req.body = utils.utf8(body)
return req
new_req.body = utils.utf8(body)
return new_req

@staticmethod
def get_from_response(response, element, default):
Expand Down
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]
2 changes: 1 addition & 1 deletion ooi/occi/core/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Entity(object):
"occi.core.title"])

kind = kind.Kind(helpers.build_scheme('core'), 'entity',
'entity', attributes, '/entity/')
'entity', attributes, 'entity/')

def __init__(self, title, mixins, id=None):
helpers.check_type(mixins, mixin.Mixin)
Expand Down
6 changes: 3 additions & 3 deletions ooi/occi/core/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ class Link(entity.Entity):
"occi.core.target"])

kind = kind.Kind(helpers.build_scheme("core"), 'link', 'link',
attributes, '/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
2 changes: 1 addition & 1 deletion ooi/occi/core/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class Resource(entity.Entity):
attributes = attribute.AttributeCollection(["occi.core.summary"])

kind = kind.Kind(helpers.build_scheme('core'), 'resource',
'resource', attributes, '/resource/',
'resource', attributes, 'resource/',
related=[entity.Entity.kind])

def __init__(self, title, mixins, id=None, summary=None):
Expand Down
2 changes: 1 addition & 1 deletion ooi/occi/infrastructure/compute.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class ComputeResource(resource.Resource):
"occi.compute.state"])
actions = (start, stop, restart, suspend)
kind = kind.Kind(helpers.build_scheme('infrastructure'), 'compute',
'compute resource', attributes, '/compute/',
'compute resource', attributes, 'compute/',
actions=actions,
related=[resource.Resource.kind])

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
Loading

0 comments on commit 4a49f48

Please sign in to comment.