def dettach_vip(cls, context, instance_id): """ 1.dettach_vip; 2.release_vip if the vip not bind any instance; :param context: :param instance_id: :return: """ instance = BuiltInstance.load(context, instance_id) LOG.info("dettach_vip instance:%s", instance_id) # try dettach_vip try: instance_vip = DBInstanceVip.find_by(context, instance_id=instance.id, deleted=False) vip_id = instance_vip.vip_id InstanceVip.deallocate(context, instance.id, purge=False) except Exception as e: msg = "Failed dettach_vip from instance:%s, exception:%s" % (instance.id, e) LOG.error(msg) raise Exception(msg) # try release_vip if the vip not bind any instance try: vips = DBVips.find_by(context, id=vip_id, deleted=False) vip = vips.vip except Exception as e: LOG.warn("get vips error, vip_id:%s, exception:%s", vip_id, e) return try: inst_list = DBInstanceVip.find_all(vip_id=vip_id, deleted=False) LOG.debug("dettach_vip find_all by vip_id:%s, inst_list:%s", vip_id, inst_list) has = False for inst in inst_list: LOG.debug("inst:%s, vip_id:%s", inst.id, inst.vip_id) has = True if not has: InstanceVip.release_vip(context, vip) except Exception as e: msg = "Failed release_vip with vip:%s, exception:%s" % (vip, e) LOG.error(msg) raise Exception(msg) LOG.info("dettach_vip ok, instance:%s", instance)
def test_deallocate(self): instance_id = "instance_id" vip = fake() vip.id = "vip_id" vip.lb_id = "lb_id" vip.mid = "mid" def delete(): pass vip.delete = delete when(models.DBInstanceVip).find_by(any(),instance_id=any(),deleted=any()).thenReturn(vip) when(instance_vip).delete_member(any(),any()).thenReturn(None) self.assertEqual(None, vip_service.deallocate(self.context, instance_id))