Пример #1
0
    def remove_fixed_ip_from_instance(self, context, instance, address,
                                      conductor_api=None):
        """Remove a fixed ip from the instance."""
        zone = 'compute:%s' % instance['availability_zone']
        search_opts = {'device_id': instance['uuid'],
                       'device_owner': zone,
                       'fixed_ips': 'ip_address=%s' % address}
        data = neutronv2.get_client(context).list_ports(**search_opts)
        ports = data['ports']
        for p in ports:
            fixed_ips = p['fixed_ips']
            new_fixed_ips = []
            for fixed_ip in fixed_ips:
                if fixed_ip['ip_address'] != address:
                    new_fixed_ips.append(fixed_ip)
            port_req_body = {'port': {'fixed_ips': new_fixed_ips}}
            try:
                neutronv2.get_client(context).update_port(p['id'],
                                                          port_req_body)
            except Exception as ex:
                msg = _("Unable to update port %(portid)s with"
                        " failure: %(exception)s")
                LOG.debug(msg, {'portid': p['id'], 'exception': ex})
            return

        raise exception.FixedIpNotFoundForSpecificInstance(
                instance_uuid=instance['uuid'], ip=address)
Пример #2
0
 def remove_fixed_ip_from_instance(self, context, instance_id, address):
     """Removes a fixed ip from an instance from specified network."""
     fixed_ips = self.db.fixed_ip_get_by_instance(context, instance_id)
     for fixed_ip in fixed_ips:
         if fixed_ip['address'] == address:
             self.deallocate_fixed_ip(context, address)
             return
     raise exception.FixedIpNotFoundForSpecificInstance(
                                 instance_id=instance_id, ip=address)
Пример #3
0
class FixedIpTestV21(test.NoDBTestCase):
    def setUp(self):
        super(FixedIpTestV21, self).setUp()
        fakes.stub_out_networking(self.stubs)
        fakes.stub_out_rate_limiting(self.stubs)
        self.stubs.Set(compute.api.API, "add_fixed_ip",
                       compute_api_add_fixed_ip)
        self.stubs.Set(compute.api.API, "remove_fixed_ip",
                       compute_api_remove_fixed_ip)
        self.stubs.Set(compute.api.API, 'get', compute_api_get)
        self.app = self._get_app()

    def _get_app(self):
        return fakes.wsgi_app_v21(init_only=('servers', 'os-multinic'))

    def _get_url(self):
        return '/v2/fake'

    def test_add_fixed_ip(self):
        global last_add_fixed_ip
        last_add_fixed_ip = (None, None)

        body = dict(addFixedIp=dict(networkId='test_net'))
        req = webob.Request.blank(self._get_url() +
                                  '/servers/%s/action' % UUID)
        req.method = 'POST'
        req.body = jsonutils.dumps(body)
        req.headers['content-type'] = 'application/json'

        resp = req.get_response(self.app)
        self.assertEqual(resp.status_int, 202)
        self.assertEqual(last_add_fixed_ip, (UUID, 'test_net'))

    def _test_add_fixed_ip_bad_request(self, body):
        req = webob.Request.blank(self._get_url() +
                                  '/servers/%s/action' % UUID)
        req.method = 'POST'
        req.body = jsonutils.dumps(body)
        req.headers['content-type'] = 'application/json'
        resp = req.get_response(self.app)
        self.assertEqual(400, resp.status_int)

    def test_add_fixed_ip_empty_network_id(self):
        body = {'addFixedIp': {'network_id': ''}}
        self._test_add_fixed_ip_bad_request(body)

    def test_add_fixed_ip_network_id_bigger_than_36(self):
        body = {'addFixedIp': {'network_id': 'a' * 37}}
        self._test_add_fixed_ip_bad_request(body)

    def test_add_fixed_ip_no_network(self):
        global last_add_fixed_ip
        last_add_fixed_ip = (None, None)

        body = dict(addFixedIp=dict())
        req = webob.Request.blank(self._get_url() +
                                  '/servers/%s/action' % UUID)
        req.method = 'POST'
        req.body = jsonutils.dumps(body)
        req.headers['content-type'] = 'application/json'

        resp = req.get_response(self.app)
        self.assertEqual(resp.status_int, 400)
        self.assertEqual(last_add_fixed_ip, (None, None))

    @mock.patch.object(compute.api.API, 'add_fixed_ip')
    def test_add_fixed_ip_no_more_ips_available(self, mock_add_fixed_ip):
        mock_add_fixed_ip.side_effect = exception.NoMoreFixedIps(net='netid')

        body = dict(addFixedIp=dict(networkId='test_net'))
        req = webob.Request.blank(self._get_url() +
                                  '/servers/%s/action' % UUID)
        req.method = 'POST'
        req.body = jsonutils.dumps(body)
        req.headers['content-type'] = 'application/json'

        resp = req.get_response(self.app)
        self.assertEqual(resp.status_int, 400)

    def test_remove_fixed_ip(self):
        global last_remove_fixed_ip
        last_remove_fixed_ip = (None, None)

        body = dict(removeFixedIp=dict(address='10.10.10.1'))
        req = webob.Request.blank(self._get_url() +
                                  '/servers/%s/action' % UUID)
        req.method = 'POST'
        req.body = jsonutils.dumps(body)
        req.headers['content-type'] = 'application/json'

        resp = req.get_response(self.app)
        self.assertEqual(resp.status_int, 202)
        self.assertEqual(last_remove_fixed_ip, (UUID, '10.10.10.1'))

    def test_remove_fixed_ip_no_address(self):
        global last_remove_fixed_ip
        last_remove_fixed_ip = (None, None)

        body = dict(removeFixedIp=dict())
        req = webob.Request.blank(self._get_url() +
                                  '/servers/%s/action' % UUID)
        req.method = 'POST'
        req.body = jsonutils.dumps(body)
        req.headers['content-type'] = 'application/json'

        resp = req.get_response(self.app)
        self.assertEqual(resp.status_int, 400)
        self.assertEqual(last_remove_fixed_ip, (None, None))

    def test_remove_fixed_ip_invalid_address(self):
        body = {'remove_fixed_ip': {'address': ''}}
        req = webob.Request.blank(self._get_url() +
                                  '/servers/%s/action' % UUID)
        req.method = 'POST'
        req.body = jsonutils.dumps(body)
        req.headers['content-type'] = 'application/json'
        resp = req.get_response(self.app)
        self.assertEqual(400, resp.status_int)

    @mock.patch.object(
        compute.api.API,
        'remove_fixed_ip',
        side_effect=exception.FixedIpNotFoundForSpecificInstance(
            instance_uuid=UUID, ip='10.10.10.1'))
    def test_remove_fixed_ip_not_found(self, _remove_fixed_ip):

        body = {'remove_fixed_ip': {'address': '10.10.10.1'}}
        req = webob.Request.blank(self._get_url() +
                                  '/servers/%s/action' % UUID)
        req.method = 'POST'
        req.body = jsonutils.dumps(body)
        req.headers['content-type'] = 'application/json'

        resp = req.get_response(self.app)
        self.assertEqual(400, resp.status_int)
class FixedIpTestV21(test.NoDBTestCase):
    controller_class = multinic_v21
    validation_error = exception.ValidationError

    def setUp(self):
        super(FixedIpTestV21, self).setUp()
        fakes.stub_out_networking(self.stubs)
        fakes.stub_out_rate_limiting(self.stubs)
        self.stubs.Set(compute.api.API, "add_fixed_ip",
                       compute_api_add_fixed_ip)
        self.stubs.Set(compute.api.API, "remove_fixed_ip",
                       compute_api_remove_fixed_ip)
        self.stubs.Set(compute.api.API, 'get', compute_api_get)
        self.controller = self.controller_class.MultinicController()
        self.fake_req = fakes.HTTPRequest.blank('')

    def test_add_fixed_ip(self):
        global last_add_fixed_ip
        last_add_fixed_ip = (None, None)

        body = dict(addFixedIp=dict(networkId='test_net'))
        resp = self.controller._add_fixed_ip(self.fake_req, UUID, body=body)
        # NOTE: on v2.1, http status code is set as wsgi_code of API
        # method instead of status_int in a response object.
        if isinstance(self.controller, multinic_v21.MultinicController):
            status_int = self.controller._add_fixed_ip.wsgi_code
        else:
            status_int = resp.status_int
        self.assertEqual(status_int, 202)
        self.assertEqual(last_add_fixed_ip, (UUID, 'test_net'))

    def _test_add_fixed_ip_bad_request(self, body):
        self.assertRaises(self.validation_error,
                          self.controller._add_fixed_ip,
                          self.fake_req,
                          UUID,
                          body=body)

    def test_add_fixed_ip_empty_network_id(self):
        body = {'addFixedIp': {'network_id': ''}}
        self._test_add_fixed_ip_bad_request(body)

    def test_add_fixed_ip_network_id_bigger_than_36(self):
        body = {'addFixedIp': {'network_id': 'a' * 37}}
        self._test_add_fixed_ip_bad_request(body)

    def test_add_fixed_ip_no_network(self):
        global last_add_fixed_ip
        last_add_fixed_ip = (None, None)

        body = dict(addFixedIp=dict())
        self._test_add_fixed_ip_bad_request(body)
        self.assertEqual(last_add_fixed_ip, (None, None))

    @mock.patch.object(compute.api.API, 'add_fixed_ip')
    def test_add_fixed_ip_no_more_ips_available(self, mock_add_fixed_ip):
        mock_add_fixed_ip.side_effect = exception.NoMoreFixedIps(net='netid')

        body = dict(addFixedIp=dict(networkId='test_net'))
        self.assertRaises(webob.exc.HTTPBadRequest,
                          self.controller._add_fixed_ip,
                          self.fake_req,
                          UUID,
                          body=body)

    def test_remove_fixed_ip(self):
        global last_remove_fixed_ip
        last_remove_fixed_ip = (None, None)

        body = dict(removeFixedIp=dict(address='10.10.10.1'))
        resp = self.controller._remove_fixed_ip(self.fake_req, UUID, body=body)
        # NOTE: on v2.1, http status code is set as wsgi_code of API
        # method instead of status_int in a response object.
        if isinstance(self.controller, multinic_v21.MultinicController):
            status_int = self.controller._remove_fixed_ip.wsgi_code
        else:
            status_int = resp.status_int
        self.assertEqual(status_int, 202)
        self.assertEqual(last_remove_fixed_ip, (UUID, '10.10.10.1'))

    def test_remove_fixed_ip_no_address(self):
        global last_remove_fixed_ip
        last_remove_fixed_ip = (None, None)

        body = dict(removeFixedIp=dict())
        self.assertRaises(self.validation_error,
                          self.controller._remove_fixed_ip,
                          self.fake_req,
                          UUID,
                          body=body)
        self.assertEqual(last_remove_fixed_ip, (None, None))

    def test_remove_fixed_ip_invalid_address(self):
        body = {'removeFixedIp': {'address': ''}}
        self.assertRaises(self.validation_error,
                          self.controller._remove_fixed_ip,
                          self.fake_req,
                          UUID,
                          body=body)

    @mock.patch.object(
        compute.api.API,
        'remove_fixed_ip',
        side_effect=exception.FixedIpNotFoundForSpecificInstance(
            instance_uuid=UUID, ip='10.10.10.1'))
    def test_remove_fixed_ip_not_found(self, _remove_fixed_ip):

        body = {'removeFixedIp': {'address': '10.10.10.1'}}
        self.assertRaises(webob.exc.HTTPBadRequest,
                          self.controller._remove_fixed_ip,
                          self.fake_req,
                          UUID,
                          body=body)
Пример #5
0
class FixedIpTestV21(test.NoDBTestCase):
    controller_class = multinic_v21
    validation_error = exception.ValidationError

    def setUp(self):
        super(FixedIpTestV21, self).setUp()
        fakes.stub_out_networking(self)
        self.stub_out('nova.compute.api.API.add_fixed_ip',
                      compute_api_add_fixed_ip)
        self.stub_out('nova.compute.api.API.remove_fixed_ip',
                      compute_api_remove_fixed_ip)
        self.stub_out('nova.compute.api.API.get', compute_api_get)
        self.controller = self.controller_class.MultinicController()
        self.fake_req = fakes.HTTPRequest.blank('')

    def test_add_fixed_ip(self):
        global last_add_fixed_ip
        last_add_fixed_ip = (None, None)

        body = dict(addFixedIp=dict(networkId='test_net'))
        self.controller._add_fixed_ip(self.fake_req, UUID, body=body)
        self.assertEqual(last_add_fixed_ip, (UUID, 'test_net'))

    def _test_add_fixed_ip_bad_request(self, body):
        self.assertRaises(self.validation_error,
                          self.controller._add_fixed_ip,
                          self.fake_req,
                          UUID,
                          body=body)

    def test_add_fixed_ip_empty_network_id(self):
        body = {'addFixedIp': {'network_id': ''}}
        self._test_add_fixed_ip_bad_request(body)

    def test_add_fixed_ip_network_id_bigger_than_36(self):
        body = {'addFixedIp': {'network_id': 'a' * 37}}
        self._test_add_fixed_ip_bad_request(body)

    def test_add_fixed_ip_no_network(self):
        global last_add_fixed_ip
        last_add_fixed_ip = (None, None)

        body = dict(addFixedIp=dict())
        self._test_add_fixed_ip_bad_request(body)
        self.assertEqual(last_add_fixed_ip, (None, None))

    @mock.patch.object(compute.api.API, 'add_fixed_ip')
    def test_add_fixed_ip_no_more_ips_available(self, mock_add_fixed_ip):
        mock_add_fixed_ip.side_effect = exception.NoMoreFixedIps(net='netid')

        body = dict(addFixedIp=dict(networkId='test_net'))
        self.assertRaises(webob.exc.HTTPBadRequest,
                          self.controller._add_fixed_ip,
                          self.fake_req,
                          UUID,
                          body=body)

    def test_remove_fixed_ip(self):
        global last_remove_fixed_ip
        last_remove_fixed_ip = (None, None)

        body = dict(removeFixedIp=dict(address='10.10.10.1'))
        self.controller._remove_fixed_ip(self.fake_req, UUID, body=body)
        self.assertEqual(last_remove_fixed_ip, (UUID, '10.10.10.1'))

    def test_remove_fixed_ip_no_address(self):
        global last_remove_fixed_ip
        last_remove_fixed_ip = (None, None)

        body = dict(removeFixedIp=dict())
        self.assertRaises(self.validation_error,
                          self.controller._remove_fixed_ip,
                          self.fake_req,
                          UUID,
                          body=body)
        self.assertEqual(last_remove_fixed_ip, (None, None))

    def test_remove_fixed_ip_invalid_address(self):
        body = {'removeFixedIp': {'address': ''}}
        self.assertRaises(self.validation_error,
                          self.controller._remove_fixed_ip,
                          self.fake_req,
                          UUID,
                          body=body)

    @mock.patch.object(
        compute.api.API,
        'remove_fixed_ip',
        side_effect=exception.FixedIpNotFoundForSpecificInstance(
            instance_uuid=UUID, ip='10.10.10.1'))
    def test_remove_fixed_ip_not_found(self, _remove_fixed_ip):

        body = {'removeFixedIp': {'address': '10.10.10.1'}}
        self.assertRaises(webob.exc.HTTPBadRequest,
                          self.controller._remove_fixed_ip,
                          self.fake_req,
                          UUID,
                          body=body)