Пример #1
0
    def remove_ip(self, ip_address):
        '''Removes an IP from an interface. Full details are looked up via
            get_full_ip_info for removal

            Args:
                ip_address - IP address to remove

            Raises:
                ValueError
                    The IP address provided was invalid
                IPNotFound
                    The IP is not configured on this interface
                InterfaceConfigurationError
                    The IP address was valid, but the IP was not successfully removed
        '''

        # San check
        ip_address = check.validate_and_normalize_ip(ip_address)

        # Get the full set of IP information
        ip_info = self.get_full_ip_info(ip_address)

        # Attempt to delete
        if ip_info['family'] == AF_INET:
            self.iproute_api.addr('delete',
                                  index=self.interface_index,
                                  address=ip_info['ip_address'],
                                  broadcast=ip_info['broadcast'],
                                  prefixlen=ip_info['prefix_length'])

        if ip_info['family'] == AF_INET6:
            self.iproute_api.addr('delete',
                                  index=self.interface_index,
                                  address=ip_info['ip_address'],
                                  prefixlen=ip_info['prefix_length'])

        # Confirm the delete. get_full_ip_info will throw an exception if it can't find it
        try:
            self.get_full_ip_info(ip_address)
        except IPNotFound:
            # We got it!
            return

        # Didn't get it. Throw an exception and bail
        raise InterfaceConfigurationError("IP deletion failure")
Пример #2
0
    def set_ip_status(self, ip_address, status, allocation, machine):
        """Sets an IP status to reserved in the database"""

        # Sanity check our input
        ip_address = check.validate_and_normalize_ip(ip_address)
        if not isinstance(allocation, AllocationServerSide):
            raise ValueError("Invalid Allocation object")
        if not isinstance(machine, Machine):
            raise ValueError("Invalid Machine object")
        if not (status == "UNMANAGED" or status == "RESERVED" or status == "STANDBY" or status == "ACTIVE_UTILIZATION"):
            raise ValueError("Invalid status for IP")

        # We use REPLACE to make sure statuses are always accurate to the server. In case
        # of conflict, the server is always the correct source of information
        query = """REPLACE INTO ip_allocations (from_allocation, allocated_to, ip_address,
                   status, reservation_expires) VALUES (%s, %s, %s, %s, %s)"""

        # reservation status is null unless we're going to/from RESERVED
        reservation_status = "NULL"
        self._do_insert(query, (allocation.get_id(), machine.get_id(), ip_address, status, reservation_status))
Пример #3
0
    def get_full_ip_info(self, wanted_address):
        '''Returns an ip_dict for an individual IP address. Format identical to get_ips()

        Args:
            wanted_address - a valid v4 or v6 address to search for. Value is normalized
                             by ipaddress.ip_address when returning

        Raises:
            ValueError - wanted_address is not a valid IP address
            IPNotFound - this interface doesn't have this IP address
        '''
        wanted_address = check.validate_and_normalize_ip(wanted_address)

        ips = self.get_ips()

        # Walk the IP table and find the specific IP we want
        for ip_address in ips:
            if ip_address['ip_address'] == wanted_address:
                return ip_address

        # If we get here, the IP wasn't found
        raise IPNotFound("IP not found on interface")