def add(self, excludeType=None, excludeId=None): if excludeId is None: print("id option is mandatory") return if excludeType not in VmExcludeCommands.EXCLUSION_TYPES: print("type is not valid") return repositories.setup_database_engine_and_factory() repo = repositories.get_vmexclude_repository() entity = VmExclude() entity.exclude_type = VmExcludeCommands.EXCLUSION_TYPES[excludeType] entity.exclude_id = excludeId try: entity = repo.create_exclude(entity) except Exception as e: print(str(e)) return repositories.commit() headers = ['id', 'type', 'exclude_id'] pt = prettytable.PrettyTable(headers) pt.add_row([ entity.id, VmExcludeCommands.EXCLUSION_MAP[entity.exclude_type], entity.exclude_id ]) if six.PY3: print(encodeutils.safe_encode(pt.get_string()).decode()) else: print(encodeutils.safe_encode(pt.get_string()))
def list(self, excludeType=None): repositories.setup_database_engine_and_factory() repo = repositories.get_vmexclude_repository() exclude_type = None if excludeType is not None: print("Filter by exclude type %s" % (excludeType)) if excludeType == 'domain': exclude_type = 0 elif excludeType == 'project': exclude_type = 1 elif excludeType == 'user': exclude_type = 2 excludes = repo.get_type_entities(exclude_type=exclude_type) headers = ['id', 'type', 'exclude_id'] pt = prettytable.PrettyTable(headers) for instance in excludes: pt.add_row([ instance.id, VmExcludeCommands.EXCLUSION_MAP[instance.exclude_type], instance.exclude_id ]) if six.PY3: print(encodeutils.safe_encode(pt.get_string()).decode()) else: print(encodeutils.safe_encode(pt.get_string()))
def create_vmexclude(exclude_id=None, exclude_type=0, session=None): exclude = models.VmExclude() exclude.exclude_id = exclude_id exclude.exclude_type = exclude_type container_repo = repositories.get_vmexclude_repository() container_repo.create_exclude(exclude, session=session) return exclude
def tearDown(self): super(WhenTestingVmExpiresResource, self).tearDown() repo = repositories.get_vmexpire_repository() repo.delete_all_entities() repositories.commit() exclude_repo = repositories.get_vmexclude_repository() exclude_repo.delete_all_entities() repositories.commit()
def delete(self, excludeId=None): if excludeId is None: print("Missing mandatory id parameter") return repositories.setup_database_engine_and_factory() repo = repositories.get_vmexclude_repository() repo.delete_entity_by_id(excludeId) repositories.commit() print("Exclude deleted")
def info(self, ctxt, publisher_id, event_type, payload, metadata): if event_type == 'instance.create.end': LOG.debug(event_type + ':' + payload['nova_object.data']['uuid']) repo = repositories.get_vmexpire_repository() instance = None instance_uuid = str(payload['nova_object.data']['uuid']) try: instance = repo.get_by_instance(instance_uuid) except Exception: LOG.debug("Fine, instance does not already exists") if instance: LOG.warn("InstanceAlreadyExists:" + instance_uuid + ", deleting first") repo.delete_entity_by_id(entity_id=instance.id) entity = models.VmExpire() entity.instance_id = instance_uuid if payload['nova_object.data']['display_name']: display_name = payload['nova_object.data']['display_name'] entity.instance_name = display_name else: entity.instance_name = instance_uuid entity.project_id = payload['nova_object.data']['tenant_id'] entity.user_id = payload['nova_object.data']['user_id'] entity.expire = int( time.mktime(datetime.datetime.now().timetuple()) + (CONF.max_vm_duration * 3600 * 24)) entity.notified = False entity.notified_last = False project_domain = None try: project_domain = get_project_domain(entity.project_id) except Exception: LOG.exception('Failed to get domain for project') exclude_repo = repositories.get_vmexclude_repository() if project_domain: exclude_id = exclude_repo.get_exclude_by_id(project_domain) if exclude_id: LOG.debug('domain %s is excluded, skipping' % (project_domain)) return exclude_id = exclude_repo.get_exclude_by_id(entity.project_id) if exclude_id: LOG.debug('project %s is excluded, skipping' % (entity.project_id)) return exclude_id = exclude_repo.get_exclude_by_id(entity.user_id) if exclude_id: LOG.debug('user %s is excluded, skipping' % (entity.user_id)) return instance = repo.create_from(entity) LOG.debug("NewInstanceExpiration:" + instance_uuid) elif event_type == 'instance.delete.end': instance_uuid = str(payload['nova_object.data']['uuid']) LOG.debug(event_type + ':' + instance_uuid) repo = repositories.get_vmexpire_repository() try: instance = repo.get_by_instance(instance_uuid) repo.delete_entity_by_id(entity_id=instance.id) LOG.debug("Delete id:" + instance.id) except Exception: LOG.warn('Failed to delete: ' + instance_uuid) LOG.debug(publisher_id) LOG.debug(event_type) LOG.debug(str(metadata)) LOG.debug(json.dumps(payload, indent=4))
def create_vmexclude(entity): repo = repositories.get_vmexclude_repository() instance = repo.create_exclude(entity) repositories.commit() return instance
def __init__(self, project_id): self.project_id = str(project_id) self.vmexclude_repo = repo.get_vmexclude_repository()