~lamont/maas/bug-1599223-2.0

« back to all changes in this revision

Viewing changes to src/maasserver/models/staticipaddress.py

  • Committer: Mike Pontillo
  • Date: 2015-12-04 20:10:02 UTC
  • mto: This revision was merged to the branch mainline in revision 4547.
  • Revision ID: mike.pontillo@canonical.com-20151204201002-1k3t8erytvtsy7tp
Merge revision 4520 from MAAS 1.9 (IP allocation fixes, bug #1519090, #1519077)

Show diffs side-by-side

added added

removed removed

Lines of Context:
166
166
            dynamic_range_low, dynamic_range_high,
167
167
            alloc_type=IPADDRESS_TYPE.AUTO, user=None,
168
168
            requested_address=None, hostname=None, subnet=None,
169
 
            exclude_addresses=[]):
 
169
            exclude_addresses=[], in_use_ipset=set()):
170
170
        """Return a new StaticIPAddress.
171
171
 
172
172
        :param network: The network the address should be allocated in.
226
226
                requested_address = self._async_find_free_ip(
227
227
                    static_range_low, static_range_high, static_range,
228
228
                    alloc_type, user,
229
 
                    exclude_addresses=exclude_addresses).wait(30)
 
229
                    exclude_addresses=exclude_addresses,
 
230
                    in_use_ipset=in_use_ipset).wait(30)
230
231
                try:
231
232
                    return self._attempt_allocation(
232
233
                        requested_address, alloc_type, user,
272
273
 
273
274
    def _find_free_ip(
274
275
            self, range_low, range_high, static_range, alloc_type,
275
 
            user, exclude_addresses):
 
276
            user, exclude_addresses, in_use_ipset=set()):
276
277
        """Helper function that finds a free IP address using a lock."""
277
278
        # The set of _allocated_ addresses in the range is going to be
278
279
        # smaller or at least no bigger than the set of addresses in the
297
298
            })
298
299
        # Now find the first free address in the range.
299
300
        for requested_address in static_range:
300
 
            if requested_address not in existing:
 
301
            if (requested_address not in existing and
 
302
                    requested_address not in in_use_ipset):
301
303
                return requested_address
302
304
        else:
303
305
            raise StaticIPAddressExhaustion(
840
842
            else:
841
843
                # (2) and (3): the Subnet has changed (could be to None)
842
844
                subnet = Subnet.objects.get_best_subnet_for_ip(ipaddr)
 
845
                # We must save here, otherwise it's possible that we can't
 
846
                # traverse the interface_set many-to-many.
 
847
                self.save()
843
848
                self._set_subnet(subnet, interfaces=self.interface_set.all())