def deallocate_for_instance(self, context, **kwargs):
        instance_id = kwargs.get('instance_id')
        project_id = kwargs.pop('project_id', None)
        admin_context = context.elevated()
        networks = self._get_networks_for_instance(admin_context, instance_id,
                                                                  project_id)
        vifs = self.db.virtual_interface_get_by_instance(context, instance_id)
        for n in networks:
            vif_id = "nova-" + str(instance_id) + "-" + str(n['id'])
            # Un-attach the vif and delete the port
            tenant_id = project_id or FLAGS.quantum_default_tenant_id
            quantum_net_id = n['bridge']
            LOG.debug("Using quantum_net_id: %s" % quantum_net_id)
            attachment = vif_id
            port_id = quantum.get_port_by_attachment(tenant_id,
                                            quantum_net_id, attachment)

            # FIXME: tell Quantum that this interface-binding is no
            # longer valid.

            if not port_id:
                LOG.error("Unable to find port with attachment: %s" % \
                                                        (attachment))
            else:
                quantum.unplug_iface(tenant_id, quantum_net_id, port_id)
                quantum.delete_port(tenant_id, quantum_net_id, port_id)

            vif = filter(lambda vif: vif['network_id'] == n['id'], vifs)[0]
            melange.deallocate_ips(n['id'], vif['id'],
                                   project_id=n['project_id'])

        self.db.virtual_interface_delete_by_instance(context, instance_id)
    def test_deallocate_ip_without_a_project_id(self):
        network_id = "network1"
        vif_id = "vif1"
        mock_client = setup_mock_client(self.mox)
        mock_client.delete("/v0.1/ipam/networks/network1/"
                           "interfaces/vif1/ip_allocations",
                           headers=json_content_type())
        self.mox.ReplayAll()

        melange_client.deallocate_ips(network_id, vif_id)
    def test_deallocates_ips_from_melange(self):
        quantum_mgr = QuantumManager()
        admin_context = context.get_admin_context()
        project_id = "project1"

        instance_id = db_api.instance_create(admin_context, dict())['id']
        network1 = db_api.network_create_safe(admin_context,
                                             dict(instance_id=instance_id,
                                                  priority=1,
                                                  project_id=project_id))
        network2 = db_api.network_create_safe(admin_context,
                                             dict(instance_id=instance_id,
                                                  priority=2,
                                                  project_id=project_id))

        vif1 = db_api.virtual_interface_create(admin_context,
                                              dict(instance_id=instance_id,
                                              network_id=network1['id'],
                                              project_id=project_id))
        vif2 = db_api.virtual_interface_create(admin_context,
                                              dict(instance_id=instance_id,
                                              network_id=network2['id'],
                                              project_id=project_id))
        self._setup_quantum_mocks()

        self.mox.StubOutWithMock(melange_client, "deallocate_ips")
        melange_client.deallocate_ips(network1['id'], vif1['id'],
                                      project_id=project_id)
        melange_client.deallocate_ips(network2['id'], vif2['id'],
                                      project_id=project_id)

        self.mox.ReplayAll()

        quantum_mgr.deallocate_for_instance(admin_context,
                                            instance_id=instance_id,
                                            project_id=project_id)

        vifs_left = db_api.virtual_interface_get_by_instance(admin_context,
                                                             instance_id)
        self.assertEqual(len(vifs_left), 0)