def purge_snapshots_by_volume(volume, purge_limit): """ Purge snapshots per volume @return: count of snapshots purged """ config = helpers.get_db_config() ontap = OntapService(config['ontap_api'], config['ontap_apiuser'], config['ontap_apipass'], config['ontap_svm_name'], config['ontap_aggr_name'], config['ontap_data_ip']) ontap_snapshot_list = ontap.get_snapshot_list(volume) if ontap_snapshot_list is None: return 0 delete_count = len(ontap_snapshot_list) - purge_limit if delete_count <= 0: return 0 database = helpers.connect_db() sorted_by_timestamp = sorted(ontap_snapshot_list, key=lambda snap: snap['timestamp']) delete_snapshot_list = sorted_by_timestamp[:delete_count] for snap in delete_snapshot_list: status = ontap.delete_snapshot(volume, snap['snapshot_name']) if helpers.verify_successful_response(status): # delete snapshot document from db doc = Database.get_document_by_name(database, snap['snapshot_name']) if not doc: # if snapshot to be deleted is not found in DB logging.info("Purge: snapshot document not found for %s", snap['snapshot_name']) else: database.delete(doc) logging.info("Purge: snapshot deleted from DB and ONTAP: %s", snap['snapshot_name']) return delete_count
def purge_inconsistent_snapshots(volume): """ Snapshot consistency check - ONTAP vs DB Purge inconsistent snapshot documents from DB i.e. snapshots in DB that do not exist in ONTAP @return: count of snapshots deleted from DB """ config = helpers.get_db_config() database = helpers.connect_db() snapshots_in_db = Database.get_snapshots_by_volume(database, volume=volume) ontap = OntapService(config['ontap_api'], config['ontap_apiuser'], config['ontap_apipass'], config['ontap_svm_name'], config['ontap_aggr_name'], config['ontap_data_ip']) ontap_snapshot_data = ontap.get_snapshot_list(volume) if not ontap_snapshot_data: # purge all snapshots from DB and return: return purge_snapshots_from_db(snapshots_ontap=[], snapshots_db=snapshots_in_db) if not snapshots_in_db: # return if there are no snapshot documents in db return 0 ontap_snapshots = [snap['snapshot_name'] for snap in ontap_snapshot_data] return purge_snapshots_from_db(snapshots_ontap=ontap_snapshots, snapshots_db=snapshots_in_db)
def snapshot_list(volume_name): """ List all snapshots --- tags: - snapshot parameters: - in: path name: volume_name required: true description: parent volume name to list snapshots type: string responses: 200: description: snapshot was created successfully """ database = helpers.connect_db() config_document = helpers.get_db_config() if not config_document: raise GenericException( 500, "Customer config doc not found, please contact your administrator", "Database Exception") ontap = OntapService(config_document['ontap_api'], config_document['ontap_apiuser'], config_document['ontap_apipass'], config_document['ontap_svm_name'], config_document['ontap_aggr_name'], config_document['ontap_data_ip']) snapshots = ontap.get_snapshot_list(volume_name) return jsonify(snapshots)