Ejemplo n.º 1
0
    def __init__(
        self,
        config,
        scopegroup_id,
        address_id,
        reservation_id=None,
        options=None,
        mac=None,
    ):
        """
        Create a new high level Reservation object

        :param ns1.config.Config config: config object
        :param int scopegroup_id: id of the scope group
        :param int address_id: id of the address the reservation is associated with
        :param int reservation_id: id of the reservation
        :param list options: dhcp options of the reservation
        :param str mac: mac address of the reservation
        """
        self._rest = Reservations(config)
        self.config = config
        self.id = reservation_id
        self.scopegroup_id = scopegroup_id
        self.address_id = address_id
        self.mac = mac
        self.data = None

        if options is None:
            options = DHCPOptions("dhcpv4", {})
        self.options = options.option_list["options"]
Ejemplo n.º 2
0
    def reservations(self, callback=None, errback=None):
        if not self.data:
            raise ScopegroupException("Scope Group not loaded")

        reservations_config = Reservations(self.config)

        return reservations_config.list(self.id,
                                        callback=callback,
                                        errback=errback)
Ejemplo n.º 3
0
class Reservation(object):
    def __init__(
        self,
        config,
        scopegroup_id,
        address_id,
        reservation_id=None,
        options=None,
        mac=None,
    ):
        """
        Create a new high level Reservation object

        :param ns1.config.Config config: config object
        :param int scopegroup_id: id of the scope group
        :param int address_id: id of the address the reservation is associated with
        :param int reservation_id: id of the reservation
        :param list options: dhcp options of the reservation
        :param str mac: mac address of the reservation
        """
        self._rest = Reservations(config)
        self.config = config
        self.id = reservation_id
        self.scopegroup_id = scopegroup_id
        self.address_id = address_id
        self.mac = mac
        self.data = None

        if options is None:
            options = DHCPOptions("dhcpv4", {})
        self.options = options.option_list["options"]

    def __repr__(self):
        return "<Reservation scopegroup=%s, address=%s, mac=%s>" % (
            self.scopegroup_id,
            self.address_id,
            self.mac,
        )

    def __getitem__(self, item):
        if item == "scopegroup_id":
            return self.scopegroup_id

        if item == "address_id":
            return self.address_id

        return self.data.get(item, None)

    def reload(self, callback=None, errback=None):
        """
        Reload Reservation data from the API.
        """

        return self.load(reload=True, callback=callback, errback=errback)

    def load(self, callback=None, errback=None, reload=False):
        """
        Load Reservation data from the API.
        """

        if not reload and self.data:
            raise ReservationException("Reservation already loaded")

        def success(result, *args):
            self.data = result
            self.address_id = result["address_id"]
            self.mac = result["mac"]
            self.options = result["options"]

            if callback:
                return callback(self)
            else:
                return self

        if self.id is None:
            raise ReservationException("Must specify a reservation_id")

        return self._rest.retrieve(self.id, callback=success, errback=errback)

    def delete(self, callback=None, errback=None):
        """
        Delete the Reservation
        """

        return self._rest.delete(self.id, callback=callback, errback=errback)

    def create(self, callback=None, errback=None, **kwargs):
        """
        Create a new Reservation. Pass a list of keywords and their values to
        configure. For the list of keywords available for address configuration, see :attr:`ns1.rest.ipam.Reservations.INT_FIELDS` and :attr:`ns1.rest.ipam.Reservations.PASSTHRU_FIELDS`
        """

        if self.data:
            raise ReservationException("Reservation already loaded")

        def success(result, *args):
            self.data = result
            self.id = result["id"]
            self.address_id = result["address_id"]
            self.mac = result["mac"]
            self.options = result["options"]

            if callback:
                return callback(self)
            else:
                return self

        return self._rest.create(self.scopegroup_id,
                                 self.address_id,
                                 options=self.options,
                                 mac=self.mac,
                                 callback=success,
                                 errback=errback,
                                 **kwargs)

    def update(self,
               options,
               callback=None,
               errback=None,
               parent=True,
               **kwargs):
        """
        Update reservation configuration. Pass a list of keywords and their values to
        update. For the list of keywords available for address configuration, see :attr:`ns1.rest.ipam.Reservations.INT_FIELDS` and :attr:`ns1.rest.ipam.Reservations.PASSTHRU_FIELDS`
        """

        if not self.data:
            raise ReservationException("Reservation not loaded")

        def success(result, *args):
            self.data = result
            self.id = result["id"]
            self.address_id = result["address_id"]
            self.mac = result["mac"]
            self.options = result["options"]

            if callback:
                return callback(self)
            else:
                return self

        return self._rest.update(self.id,
                                 options,
                                 callback=success,
                                 errback=errback,
                                 parent=parent,
                                 **kwargs)