Esempio n. 1
0
    def ListAllocatedIPs(self, ipblock_msg, context):
        """ Return a list of IPs allocated from a IP block

        Args:
            ipblock_msg (IPBlock): ip block to add. ipblock_msg has the
            type IPBlock, a protobuf message type for the gRPC interface.
            Internal representation of ip blocks uses the ipaddress.ip_network
            type and is named as ipblock.
        """
        resp = ListAllocatedIPsResponse()

        ipblock = self._ipblock_msg_to_ipblock(ipblock_msg, context)
        if ipblock is None:
            return resp

        if ipblock_msg.version == IPBlock.IPV4:
            try:
                ips = self._ipv4_allocator.list_allocated_ips(ipblock)
                ip_msg_list = [
                    IPAddress(version=IPAddress.IPV4, address=ip.packed)
                    for ip in ips
                ]

                resp.ip_list.extend(ip_msg_list)
            except IPBlockNotFoundError:
                context.set_details('IP block not found: %s' % ipblock)
                context.set_code(grpc.StatusCode.FAILED_PRECONDITION)
        else:
            self._unimplemented_ip_version_error(context)

        return resp
Esempio n. 2
0
    def test_release_ip_address(self):
        """ test ReleaseIPAddress """
        self._stub.AddIPBlock(self._block_msg)

        alloc_request0 = AllocateIPRequest(sid=self._sid0,
                                           version=AllocateIPRequest.IPV4,
                                           apn=self._apn0)
        ip_msg0 = self._stub.AllocateIPAddress(alloc_request0)
        alloc_request1 = AllocateIPRequest(sid=self._sid1,
                                           version=AllocateIPRequest.IPV4,
                                           apn=self._apn0)
        ip_msg1 = self._stub.AllocateIPAddress(alloc_request1)

        # release ip_msg0
        release_request0 = ReleaseIPRequest(sid=self._sid0,
                                            ip=ip_msg0.ip_list[0],
                                            apn=self._apn0)
        resp = self._stub.ReleaseIPAddress(release_request0)
        self.assertEqual(resp, Void())
        resp = self._stub.ListAllocatedIPs(self._block_msg)
        tmp = ListAllocatedIPsResponse()
        tmp.ip_list.extend([ip_msg1.ip_list[0]])
        self.assertEqual(resp, tmp)

        # release ip_msg1
        release_request1 = ReleaseIPRequest(sid=self._sid1,
                                            ip=ip_msg1.ip_list[0],
                                            apn=self._apn0)
        resp = self._stub.ReleaseIPAddress(release_request1)
        resp = self._stub.ListAllocatedIPs(self._block_msg)
        self.assertEqual(len(resp.ip_list), 0)
Esempio n. 3
0
    def test_list_allocated_ips(self):
        """ test list allocated IPs from a IP block """
        self._stub.AddIPBlock(self._block_msg)

        # list empty allocated IPs
        resp = self._stub.ListAllocatedIPs(self._block_msg)
        self.assertEqual(len(resp.ip_list), 0)

        # list after allocating one IP
        request = AllocateIPRequest(sid=self._sid0,
                                    version=AllocateIPRequest.IPV4)
        ip_msg0 = self._stub.AllocateIPAddress(request)
        resp = self._stub.ListAllocatedIPs(self._block_msg)
        self.assertNotEqual(resp, None)
        tmp = ListAllocatedIPsResponse()
        tmp.ip_list.extend([ip_msg0])
        self.assertEqual(resp, tmp)