Skip to content

Commit

Permalink
Merge 41ca581 into d03df10
Browse files Browse the repository at this point in the history
  • Loading branch information
enolfc committed May 6, 2015
2 parents d03df10 + 41ca581 commit 6d8585e
Showing 1 changed file with 72 additions and 1 deletion.
73 changes: 72 additions & 1 deletion ooi/api/storage.py
Expand Up @@ -14,9 +14,13 @@
# License for the specific language governing permissions and limitations
# under the License.

import json

from ooi.api import base
from ooi import exception
from ooi.occi.core import collection
from ooi.occi.infrastructure import storage
from ooi.occi import validator as occi_validator
from ooi.openstack import helpers


Expand Down Expand Up @@ -47,4 +51,71 @@ def show(self, id, req):
size=v["size"], state=state)
return [st]

# TODO(enolfc): delete, create
def create(self, req, body):
tenant_id = req.environ["keystone.token_auth"].user.project_id
parser = req.get_parser()(req.headers, req.body)
scheme = {"category": storage.StorageResource.kind}
obj = parser.parse()
validator = occi_validator.Validator(obj)
validator.validate(scheme)

attrs = obj.get("attributes", {})
name = attrs.get("occi.core.title", "OCCI Volume")
# TODO(enolfc): this should be handled by the validator
try:
size = attrs["occi.storage.size"]
except KeyError:
raise exception.Invalid()

req_body = {"volume": {
"display_name": name,
"size": size,
}}
req = self._get_req(req, path="/%s/os-volumes" % tenant_id,
body=json.dumps(req_body), method="POST")
response = req.get_response(self.app)
volume = self.get_from_response(response, "volume", {})

st = storage.StorageResource(title=volume["displayName"],
id=volume["id"],
size=volume["size"],
state=helpers.vol_state(volume["status"]))
return collection.Collection(resources=[st])

def _get_storage_ids(self, req):
tenant_id = req.environ["keystone.token_auth"].user.project_id
req = self._get_req(req,
path="/%s/os-volumes" % tenant_id,
method="GET")
response = req.get_response(self.app)
return [v["id"] for v in self.get_from_response(response,
"volumes", [])]

def _delete(self, req, ids):
tenant_id = req.environ["keystone.token_auth"].user.project_id
for id in ids:
req = self._get_req(req,
path="/%s/os-volumes/%s" % (tenant_id, id),
method="DELETE")
response = req.get_response(self.app)
if response.status_int not in [204]:
raise base.exception_from_response(response)
return []

# TODO(enolfc): these two methods could be in the base.Controller
# they are identical to the ones of the Compute
def delete(self, req, id):
return self._delete(req, [id])

def delete_all(self, req):
return self._delete(req, self._get_storage_ids(req))

# TODO(enolfc): implement the actions.
def run_action(self, req, id, body):
action = req.GET.get("action", None)
actions = [a.term for a in storage.StorageResource.actions]

if action is None or action not in actions:
raise exception.InvalidAction(action=action)

raise exception.NotImplemented

0 comments on commit 6d8585e

Please sign in to comment.