Exemple #1
0
    def fetch_subnet(self, floatingip):
        fip = netaddr.IPAddress(floatingip)
        network = self.neutron.show_network(self.network_id)['network']
        subnet_ids = network['subnets']

        for sub_id in subnet_ids:
            subnet = self.neutron.show_subnet(sub_id)['subnet']
            cidr = netaddr.IPNetwork(subnet['cidr'])

            # skip the subnet because it has not valid cidr for the floating ip
            if fip not in cidr:
                continue

            allocated_ip = netaddr.IPSet()

            allocated_ip.add(netaddr.IPAddress(subnet['gateway_ip']))
            for alloc in subnet['allocation_pools']:
                allocated_ip.add(netaddr.IPRange(alloc['start'], alloc['end']))

            if fip in allocated_ip:
                raise exceptions.NeutronUsesFloatingIP(floatingip=fip,
                                                       subnet=subnet['id'])
            else:
                self.subnet_id = subnet['id']
                return subnet

        raise exceptions.FloatingIPSubnetNotFound(fip=floatingip)
    def test_create_floatingip_with_invalid_ip(self):
        m = mock.MagicMock()
        m.fetch_subnet.side_effect = opst_exceptions.NeutronUsesFloatingIP()
        self.fip_pool.return_value = m

        fip_plugin = floatingip_plugin.FloatingIpPlugin()
        self.assertRaises(opst_exceptions.NeutronUsesFloatingIP,
                          fip_plugin.create_floatingip,
                          {'floating_ip_address': 'invalid-ip',
                           'floating_network_id': 'id'})