def can_delete_shutoff(nc, instance): """ Check instance actions to see if an instance was shutdown by an admin over three months ago """ try: actions = instance_action.InstanceActionManager(nc).list(instance.id) except Exception as e: LOG.error('Failed to get instance actions: %s' % e) return False if not actions: return False last_action = actions[0] three_months_ago = datetime.now() - relativedelta(days=90) action_date = datetime.strptime(last_action.start_time, ACTION_DATE_FORMAT) allowed_actions = ['stop', 'suspend', 'delete'] admin_projects = ['1', '2', None] if last_action.action in allowed_actions and \ last_action.project_id in admin_projects and \ action_date < three_months_ago: LOG.debug("\tinstance %s has been shutdown >3 months ago" % instance.id) return True if last_action.action not in allowed_actions: LOG.info("\tinstance %s: cannot delete, shutdown last action was %s" % (instance.id, last_action.action)) if last_action.project_id not in admin_projects: LOG.info("\tinstance %s: cannot delete, shutdown last action was by " "project %s" % (instance.id, last_action.project_id)) if action_date > three_months_ago: LOG.debug("\tinstance %s cannot delete, shutdown last action date " "was %s" % (instance.id, action_date)) return False
def instance_action_list(request, instance_id): return nova_instance_action.InstanceActionManager( novaclient(request)).list(instance_id)
def get_last_action(nc, instance): last_actions = instance_action.InstanceActionManager(nc).list(instance.id) return last_actions[0]