def test_list_with_tenant(self):
        device1_id, device2_id = uuid.uuid4(), uuid.uuid4()
        tenant1_id, tenant2_id = uuid.uuid4(), uuid.uuid4()
        block = factory.create_block(cidr="30.1.1.1/24",
                                      tenant_id=self.tenant_id)
        tenant1_ip1 = factory.create_ip(block_id=block['id'],
                                         address="30.1.1.2",
                                         used_by_device=device1_id,
                                         tenant_id=self.tenant_id,
                                         used_by_tenant=tenant1_id,)
        tenant1_ip2 = factory.create_ip(block_id=block['id'],
                                         address="30.1.1.3",
                                         used_by_device=device1_id,
                                         tenant_id=self.tenant_id,
                                         used_by_tenant=tenant1_id,)
        tenant2_ip1 = factory.create_ip(block_id=block['id'],
                                         address="30.1.1.4",
                                         used_by_device=device2_id,
                                         tenant_id=self.tenant_id,
                                         used_by_tenant=tenant2_id)

        list_res = functional.run("allocated_ip list -t %s" % tenant1_id)

        self.assertEqual(sorted([tenant1_ip1, tenant1_ip2]),
                         sorted(yaml.load(list_res['out'])['ip_addresses']))
    def test_crud(self):
        network_id = uuid.uuid4()
        block = factory.create_block(cidr="20.1.1.0/24",
                                     network_id=network_id,
                                     tenant_id=self.tenant_id)
        iface = factory.create_interface(network_id=network_id,
                                         tenant_id=self.tenant_id)
        ip = factory.create_ip(address="20.1.1.2",
                               block_id=block['id'],
                               used_by_tenant=self.tenant_id,
                               tenant_id=self.tenant_id)
        another_ip = factory.create_ip(address="20.1.1.3",
                                       block_id=block['id'],
                                       used_by_tenant=self.tenant_id,
                                       tenant_id=self.tenant_id)

        ip_res = self.command("allowed_ip create",
                              interface_id=iface['id'],
                              network_id=network_id,
                              ip_address=ip['address'])
        allowed_ip = factory.model("ip_address", ip_res)
        self.assertShows("ip_address",
                         allowed_ip,
                         parameters="interface_id=%s ip_address=%s" %
                                    (iface['id'], ip['address']),
                         command_name="allowed_ip")

        another_ip_res = self.command("allowed_ip create",
                                      interface_id=iface['id'],
                                      network_id=network_id,
                                      ip_address=another_ip['address'])

        another_allowed_ip = factory.model('ip_address', another_ip_res)

        list_res = self.command("allowed_ip list", interface_id=iface['id'])
        actual_allowed_ips = yaml.load(list_res['out'])['ip_addresses']
        self.assertTrue(allowed_ip in actual_allowed_ips)
        self.assertTrue(another_allowed_ip in actual_allowed_ips)

        self.command("allowed_ip delete",
                     interface_id=iface['id'],
                     ip_address=ip['address'])
        show_res = self.command("allowed_ip show",
                                interface_id=iface['id'],
                                ip_address=ip['address'],
                                raise_error=False)
        expected_error = ("Ip Address %s hasnt been allowed on interface %s" %
                          (ip['address'], iface['id']))
        self.assertTrue(expected_error in show_res['out'])
    def test_list(self):
        device1_id, device2_id = uuid.uuid4(), uuid.uuid4()
        block = factory.create_block(cidr="30.1.1.1/24",
                                      tenant_id=self.tenant_id)
        ip1 = factory.create_ip(block_id=block['id'],
                                 address="30.1.1.2",
                                 used_by_device=device1_id,
                                 tenant_id=self.tenant_id)
        ip2 = factory.create_ip(block_id=block['id'],
                                 address="30.1.1.3",
                                 used_by_device=device2_id,
                                 tenant_id=self.tenant_id)

        list_res = self.command("allocated_ip list",
                                 is_tenanted=False,
                                 used_by_device=device1_id)

        self.assertEqual([ip1], yaml.load(list_res['out'])['ip_addresses'])
    def test_crud(self):
        block = factory.create_block(cidr="10.1.1.0/24",
                                      tenant_id=self.tenant_id)

        ip = factory.create_ip(block_id=block['id'],
                                address="10.1.1.2",
                                tenant_id=self.tenant_id)

        self._assert_ip_shows(ip)

        another_ip = factory.create_ip(block_id=block['id'],
                                        address="10.1.1.3",
                                        tenant_id=self.tenant_id)

        list_res = self.command("ip_address list",
                                ip_block_id=block['id'])

        self.assertEqual(sorted([ip, another_ip]),
                         sorted(yaml.load(list_res['out'])['ip_addresses']))

        self.command("ip_address delete",
                     ip_block_id=block['id'],
                     address="10.1.1.2")