Refactor: remove _items() in nova/api/openstack/compute/attach_interfaces.py

removing _items() as this method is called only once and also to
ensure policy checks happen in the API methods.

blueprint policy-docs

Change-Id: I43683e53d1d277f3ed6cc5a274b5b940fcfc55cb
This commit is contained in:
Sujitha 2017-03-20 21:22:15 +00:00 committed by Stephen Finucane
parent 1d45863758
commit 53f1385115
1 changed files with 18 additions and 22 deletions

View File

@ -55,8 +55,24 @@ class InterfaceAttachmentController(wsgi.Controller):
@extensions.expected_errors((404, 501))
def index(self, req, server_id):
"""Returns the list of interface attachments for a given instance."""
return self._items(req, server_id,
entity_maker=_translate_interface_attachment_view)
context = req.environ['nova.context']
context.can(ai_policies.BASE_POLICY_NAME)
instance = common.get_instance(self.compute_api, context, server_id)
search_opts = {'device_id': instance.uuid}
try:
data = self.network_api.list_ports(context, **search_opts)
except exception.NotFound as e:
raise exc.HTTPNotFound(explanation=e.format_message())
except NotImplementedError:
common.raise_feature_not_supported()
ports = data.get('ports', [])
entity_maker = _translate_interface_attachment_view
results = [entity_maker(port) for port in ports]
return {'interfaceAttachments': results}
@extensions.expected_errors((403, 404))
def show(self, req, server_id, id):
@ -162,26 +178,6 @@ class InterfaceAttachmentController(wsgi.Controller):
common.raise_http_conflict_for_instance_invalid_state(state_error,
'detach_interface', server_id)
def _items(self, req, server_id, entity_maker):
"""Returns a list of attachments, transformed through entity_maker."""
context = req.environ['nova.context']
context.can(ai_policies.BASE_POLICY_NAME)
instance = common.get_instance(self.compute_api, context, server_id)
search_opts = {'device_id': instance.uuid}
try:
data = self.network_api.list_ports(context, **search_opts)
except exception.NotFound as e:
raise exc.HTTPNotFound(explanation=e.format_message())
except NotImplementedError:
common.raise_feature_not_supported()
ports = data.get('ports', [])
results = [entity_maker(port) for port in ports]
return {'interfaceAttachments': results}
class AttachInterfaces(extensions.V21APIExtensionBase):
"""Attach interface support."""