Coverage for nova/api/openstack/compute/ips.py: 100%
33 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-04-17 15:08 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-04-17 15:08 +0000
1# Copyright 2011 OpenStack Foundation
2# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
16from webob import exc
18from nova.api.openstack import common
19from nova.api.openstack.compute.schemas import addresses as schema
20from nova.api.openstack.compute.views import addresses as views_addresses
21from nova.api.openstack import wsgi
22from nova.api import validation
23from nova.compute import api as compute
24from nova.i18n import _
25from nova.policies import ips as ips_policies
28class IPsController(wsgi.Controller):
29 """The servers addresses API controller for the OpenStack API."""
30 _view_builder_class = views_addresses.ViewBuilder
32 def __init__(self):
33 super(IPsController, self).__init__()
34 self._compute_api = compute.API()
36 @wsgi.expected_errors(404)
37 @validation.query_schema(schema.index_query)
38 def index(self, req, server_id):
39 context = req.environ["nova.context"]
40 instance = common.get_instance(self._compute_api, context, server_id)
41 context.can(ips_policies.POLICY_ROOT % 'index',
42 target={'project_id': instance.project_id})
43 networks = common.get_networks_for_instance(context, instance)
44 return self._view_builder.index(req, networks)
46 @wsgi.expected_errors(404)
47 @validation.query_schema(schema.show_query)
48 def show(self, req, server_id, id):
49 context = req.environ["nova.context"]
50 instance = common.get_instance(self._compute_api, context, server_id)
51 context.can(ips_policies.POLICY_ROOT % 'show',
52 target={'project_id': instance.project_id})
53 networks = common.get_networks_for_instance(context, instance)
54 if id not in networks:
55 msg = _("Instance is not a member of specified network")
56 raise exc.HTTPNotFound(explanation=msg)
58 return self._view_builder.show(req, networks[id], id)