summaryrefslogtreecommitdiffstats
path: root/nova/api
diff options
context:
space:
mode:
authorEd Leafe <ed@leafe.com>2011-08-08 14:07:03 +0000
committerEd Leafe <ed@leafe.com>2011-08-08 14:07:03 +0000
commit10ab2e76b1ea8bbbb6bff4ccaf506bfdd5b57388 (patch)
treec61d801ff6e5754e0994fadd8d4db0f3661f4c53 /nova/api
parentf1f86d229cff084a3f6257565a991c7ffe010907 (diff)
parentdcac4bc6c7be9832704e37cca7ce815e083974f5 (diff)
downloadnova-10ab2e76b1ea8bbbb6bff4ccaf506bfdd5b57388.tar.gz
nova-10ab2e76b1ea8bbbb6bff4ccaf506bfdd5b57388.tar.xz
nova-10ab2e76b1ea8bbbb6bff4ccaf506bfdd5b57388.zip
Updated with code changes on LP
Diffstat (limited to 'nova/api')
-rw-r--r--nova/api/openstack/contrib/admin_only.py30
-rw-r--r--nova/api/openstack/contrib/hosts.py48
2 files changed, 57 insertions, 21 deletions
diff --git a/nova/api/openstack/contrib/admin_only.py b/nova/api/openstack/contrib/admin_only.py
new file mode 100644
index 000000000..e821c9e1f
--- /dev/null
+++ b/nova/api/openstack/contrib/admin_only.py
@@ -0,0 +1,30 @@
+# Copyright (c) 2011 Openstack, LLC.
+# All Rights Reserved.
+#
+# 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.
+
+"""Decorator for limiting extensions that should be admin-only."""
+
+from functools import wraps
+from nova import flags
+FLAGS = flags.FLAGS
+
+
+def admin_only(fnc):
+ @wraps(fnc)
+ def _wrapped(self, *args, **kwargs):
+ if FLAGS.allow_admin_api:
+ return fnc(self, *args, **kwargs)
+ return []
+ _wrapped.func_name = fnc.func_name
+ return _wrapped
diff --git a/nova/api/openstack/contrib/hosts.py b/nova/api/openstack/contrib/hosts.py
index c90a889d5..cdf8760d5 100644
--- a/nova/api/openstack/contrib/hosts.py
+++ b/nova/api/openstack/contrib/hosts.py
@@ -24,6 +24,7 @@ from nova import log as logging
from nova.api.openstack import common
from nova.api.openstack import extensions
from nova.api.openstack import faults
+from nova.api.openstack.contrib import admin_only
from nova.scheduler import api as scheduler_api
@@ -70,7 +71,7 @@ class HostController(object):
key = raw_key.lower().strip()
val = raw_val.lower().strip()
# NOTE: (dabo) Right now only 'status' can be set, but other
- # actions may follow.
+ # settings may follow.
if key == "status":
if val[:6] in ("enable", "disabl"):
return self._set_enabled_status(req, id,
@@ -78,20 +79,6 @@ class HostController(object):
else:
explanation = _("Invalid status: '%s'") % raw_val
raise webob.exc.HTTPBadRequest(explanation=explanation)
- elif key == "power_state":
- if val == "startup":
- # The only valid values for 'state' are 'reboot' or
- # 'shutdown'. For completeness' sake there is the
- # 'startup' option to start up a host, but this is not
- # technically feasible now, as we run the host on the
- # XenServer box.
- msg = _("Host startup on XenServer is not supported.")
- raise webob.exc.HTTPBadRequest(explanation=msg)
- elif val in ("reboot", "shutdown"):
- return self._set_powerstate(req, id, val)
- else:
- explanation = _("Invalid powerstate: '%s'") % raw_val
- raise webob.exc.HTTPBadRequest(explanation=explanation)
else:
explanation = _("Invalid update setting: '%s'") % raw_key
raise webob.exc.HTTPBadRequest(explanation=explanation)
@@ -108,12 +95,28 @@ class HostController(object):
raise webob.exc.HTTPBadRequest(explanation=result)
return {"host": host, "status": result}
- def _set_powerstate(self, req, host, state):
+ def _host_power_action(self, req, host, action):
"""Reboots or shuts down the host."""
context = req.environ['nova.context']
- result = self.compute_api.set_host_powerstate(context, host=host,
- state=state)
- return {"host": host, "power_state": result}
+ result = self.compute_api.host_power_action(context, host=host,
+ action=action)
+ return {"host": host, "power_action": result}
+
+ def startup(self, req, id):
+ """The only valid values for 'action' are 'reboot' or
+ 'shutdown'. For completeness' sake there is the
+ 'startup' option to start up a host, but this is not
+ technically feasible now, as we run the host on the
+ XenServer box.
+ """
+ msg = _("Host startup on XenServer is not supported.")
+ raise webob.exc.HTTPBadRequest(explanation=msg)
+
+ def shutdown(self, req, id):
+ return self._host_power_action(req, host=id, action="shutdown")
+
+ def reboot(self, req, id):
+ return self._host_power_action(req, host=id, action="reboot")
class Hosts(extensions.ExtensionDescriptor):
@@ -132,7 +135,10 @@ class Hosts(extensions.ExtensionDescriptor):
def get_updated(self):
return "2011-06-29T00:00:00+00:00"
+ @admin_only.admin_only
def get_resources(self):
- resources = [extensions.ResourceExtension('os-hosts', HostController(),
- collection_actions={'update': 'PUT'}, member_actions={})]
+ resources = [extensions.ResourceExtension('os-hosts',
+ HostController(), collection_actions={'update': 'PUT'},
+ member_actions={"startup": "GET", "shutdown": "GET",
+ "reboot": "GET"})]
return resources