diff options
| author | Jenkins <jenkins@review.openstack.org> | 2012-12-20 23:41:39 +0000 |
|---|---|---|
| committer | Gerrit Code Review <review@openstack.org> | 2012-12-20 23:41:39 +0000 |
| commit | b827dc2983b2604bb00d79f372c8a696370d83c8 (patch) | |
| tree | 242a4386d692dbc00fad7b8ef86c820c6c0e2783 /nova/api | |
| parent | d7673f7655c631cafbaf5e95439c1c07bd267714 (diff) | |
| parent | b3bbd09131e127e7540f4ccdb1376c10bace8b7a (diff) | |
Merge "Add extension to allow hiding of addresses"
Diffstat (limited to 'nova/api')
| -rw-r--r-- | nova/api/openstack/compute/contrib/hide_server_addresses.py | 89 |
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())] |
