Coverage for nova/api/openstack/compute/multinic.py: 100%

42 statements  

« prev     ^ index     » next       coverage.py v7.6.12, created at 2025-04-24 11:16 +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. 

15 

16"""The multinic extension.""" 

17 

18from webob import exc 

19 

20from nova.api.openstack import common 

21from nova.api.openstack.compute.schemas import multinic as schema 

22from nova.api.openstack import wsgi 

23from nova.api import validation 

24from nova.compute import api as compute 

25from nova import exception 

26from nova.policies import multinic as multinic_policies 

27 

28 

29class MultinicController(wsgi.Controller): 

30 """This API is deprecated from Microversion '2.44'.""" 

31 

32 def __init__(self): 

33 super(MultinicController, self).__init__() 

34 self.compute_api = compute.API() 

35 

36 @wsgi.Controller.api_version("2.1", "2.43") 

37 @wsgi.response(202) 

38 @wsgi.action('addFixedIp') 

39 @wsgi.expected_errors((400, 404)) 

40 @validation.schema(schema.add_fixed_ip) 

41 @validation.response_body_schema(schema.add_fixed_ip_response) 

42 def _add_fixed_ip(self, req, id, body): 

43 """Adds an IP on a given network to an instance.""" 

44 context = req.environ['nova.context'] 

45 instance = common.get_instance(self.compute_api, context, id) 

46 context.can(multinic_policies.BASE_POLICY_NAME % 'add', 

47 target={'project_id': instance.project_id}) 

48 

49 network_id = body['addFixedIp']['networkId'] 

50 try: 

51 self.compute_api.add_fixed_ip(context, instance, network_id) 

52 except exception.NoMoreFixedIps as e: 

53 raise exc.HTTPBadRequest(explanation=e.format_message()) 

54 

55 @wsgi.Controller.api_version("2.1", "2.43") 

56 @wsgi.response(202) 

57 @wsgi.action('removeFixedIp') 

58 @wsgi.expected_errors((400, 404)) 

59 @validation.schema(schema.remove_fixed_ip) 

60 @validation.response_body_schema(schema.remove_fixed_ip_response) 

61 def _remove_fixed_ip(self, req, id, body): 

62 """Removes an IP from an instance.""" 

63 context = req.environ['nova.context'] 

64 instance = common.get_instance(self.compute_api, context, id) 

65 context.can(multinic_policies.BASE_POLICY_NAME % 'remove', 

66 target={'project_id': instance.project_id}) 

67 

68 address = body['removeFixedIp']['address'] 

69 

70 try: 

71 self.compute_api.remove_fixed_ip(context, instance, address) 

72 except exception.FixedIpNotFoundForInstance as e: 

73 raise exc.HTTPBadRequest(explanation=e.format_message())