summaryrefslogtreecommitdiffstats
path: root/nova/api
diff options
context:
space:
mode:
authorJason Kölker <jason@koelker.net>2012-12-11 15:12:29 -0600
committerJason Kölker <jason@koelker.net>2012-12-20 15:43:15 -0600
commitb3bbd09131e127e7540f4ccdb1376c10bace8b7a (patch)
tree4997aefd85852ae4e1abbce74d7bf465a84c464c /nova/api
parent276fded4972a357db40f7a72e1bf67d4f846a95a (diff)
Add extension to allow hiding of addresses
* Servers in certain states will have network_info but it may change, (eg. rescheduled build on another host). The extension allows the operator to hide the address information in those states * Fixes bug LP 1089092 Change-Id: Ie843e34a41c77903b201b45c5b67a6f75334cb5e
Diffstat (limited to 'nova/api')
-rw-r--r--nova/api/openstack/compute/contrib/hide_server_addresses.py89
1 files changed, 89 insertions, 0 deletions
diff --git a/nova/api/openstack/compute/contrib/hide_server_addresses.py b/nova/api/openstack/compute/contrib/hide_server_addresses.py
new file mode 100644
index 000000000..bb8ee553a
--- /dev/null
+++ b/nova/api/openstack/compute/contrib/hide_server_addresses.py
@@ -0,0 +1,89 @@
+# Copyright 2012 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.
+
+"""Extension for hiding server addresses in certain states."""
+
+
+from nova.api.openstack import extensions
+from nova.api.openstack import wsgi
+from nova.compute import vm_states
+from nova.openstack.common import cfg
+from nova.openstack.common import log as logging
+
+opts = [
+ cfg.ListOpt('osapi_hide_server_address_states',
+ default=[vm_states.BUILDING],
+ help='List of instance states that should hide network info'),
+]
+
+
+CONF = cfg.CONF
+CONF.register_opts(opts)
+LOG = logging.getLogger(__name__)
+
+authorize = extensions.soft_extension_authorizer('compute',
+ 'hide_server_addresses')
+
+
+class Controller(wsgi.Controller):
+ def __init__(self, *args, **kwargs):
+ super(Controller, self).__init__(*args, **kwargs)
+ hidden_states = CONF.osapi_hide_server_address_states
+
+ # NOTE(jkoelker) _ is not considered uppercase ;)
+ valid_vm_states = [getattr(vm_states, state)
+ for state in dir(vm_states)
+ if state.isupper()]
+ self.hide_address_states = [state.lower()
+ for state in hidden_states
+ if state in valid_vm_states]
+
+ def _perhaps_hide_addresses(self, instance, resp_server):
+ if instance.get('vm_state') in self.hide_address_states:
+ resp_server['addresses'] = {}
+
+ @wsgi.extends
+ def show(self, req, resp_obj, id):
+ resp = resp_obj
+ if not authorize(req.environ['nova.context']):
+ return
+
+ if 'server' in resp.obj and 'addresses' in resp.obj['server']:
+ instance = req.get_db_instance(id)
+ self._perhaps_hide_addresses(instance, resp.obj['server'])
+
+ @wsgi.extends
+ def detail(self, req, resp_obj):
+ resp = resp_obj
+ if not authorize(req.environ['nova.context']):
+ return
+
+ for server in list(resp.obj['servers']):
+ if 'addresses' in server:
+ instance = req.get_db_instance(server['id'])
+ self._perhaps_hide_addresses(instance, server)
+
+
+class Hide_server_addresses(extensions.ExtensionDescriptor):
+ """Support hiding server addresses in certain states."""
+
+ name = 'HideServerAddresses'
+ alias = 'os-hide-server-addresses'
+ namespace = ('http://docs.openstack.org/compute/ext/'
+ 'hide_server_addresses/api/v1.1')
+ updated = '2012-12-11T00:00:00+00:00'
+
+ def get_controller_extensions(self):
+ return [extensions.ControllerExtension(self, 'servers', Controller())]