def get_snapshot_list(self, volume_name): """Retrieve list of snapshots for volume""" volume = Volume(volume_name, self.aggregate) data, error_message = volume.get_snapshots() if error_message: logging.error(error_message) return None, error_message if not data: return [], None tmp = dict(data) snaps = tmp['result']['records'] return [{ "snapshot_name": x['name'], "timestamp": x['access_timestamp'] } for x in snaps]
def create_snapshot(self, vol_name, snapshot_name): """Create snapshot for a volume""" volume = Volume(vol_name, self.aggregate) status, error_message, _ = volume.make_snapshot(snapshot_name) if status != "COMPLETED": if error_message == "Failed to create snapshot %s of volume %s on Vserver %s. " \ "Reason: Snapshot already exists. (errno=13020)" \ % (snapshot_name, vol_name, self.aggregate.svm_name): snap_status = self.set_status(200, "Snapshot", snapshot_name) else: snap_status = self.set_status(400, "Snapshot", snapshot_name, error_message) else: snap_status = self.set_status(201, "Snapshot", snapshot_name) return [snap_status]
def get_size_used(self, volume_name): '''Retrieve storage usage for Admin Dashboard''' volume = Volume(volume_name, self.aggregate) # convert bytes to MB try: size_used = int(volume.get_size_used()) / (1024 * 1024) except KeyError: size_used = 0 if size_used < 1024: storage = str(round(size_used, 2)) + "MB" else: # convert to GB size_used = size_used / 1024 storage = str(round(size_used, 2)) + "GB" return storage
def delete_snapshot(self, volume_name, snapshot_name): """Delete a snapshot, will fail if a clone is in use""" volume = Volume(volume_name, self.aggregate) status, error_message = volume.delete_snapshot(snapshot_name) if status == "COMPLETED": snap_status = self.set_status(201, "Snapshot", snapshot_name) else: if "has not expired or is locked" in error_message: logging.warning( "Failed to delete snapshot %s. Most likely clone is in use. error: %s", snapshot_name, error_message) else: logging.error( "Failed to delete snapshot %s, unexpected error: %s", snapshot_name, error_message) snap_status = self.set_status(400, "Snapshot", snapshot_name, error_message) return [snap_status]
def delete_volume(self, volume_name): '''Delete a volume''' volume = Volume(volume_name, self.aggregate) snapshots = self.get_snapshot_list(volume_name) if snapshots is None: logging.error("get_snapshot_list for %s returned 0", volume_name) else: for snapshot in snapshots: #snapshot has snapshot_name and timestamp self.delete_snapshot(volume_name, snapshot['snapshot_name']) status, error, _ = volume.unmount_offline_delete_volume() if status == "COMPLETED": vol_status = self.set_status(201, "Volume", volume_name) else: vol_status = self.set_status(400, "Volume", volume_name, error) return [vol_status]
def get_snapdiff_and_delete(self, volume_name, count): ''' delete workspace if greater than count days old with no changes''' volume = Volume(volume_name, self.aggregate) recent_snapshot, old_snapshot, error = self.get_oldest_and_latest_snapshots( volume_name, count) if error: return False, error if recent_snapshot is None or old_snapshot is None: logging.info("Workspace is less than %s days old", count) return False, "Workspace is less than %s days old" % count snapdiff = volume.get_snapdiff(recent_snapshot, old_snapshot) if snapdiff == 0: self.delete_volume(volume_name) logging.info("Deleted inactive workspace %s", volume_name) return True, "Workspace %s has been inactive for %s days\ and has been deleted" % (volume_name, count) # workspace is still active logging.info("Workspace %s is active", volume_name) return False, "Workspace %s is active" % volume_name
def create_clone(self, vol_name, uid, gid, clone_name, snapshot_name=None): """Create clone of snapshot for a developer workspace""" volume = Volume(vol_name, self.aggregate) vol_size = "" if snapshot_name is None: status, error_message, clone = volume.make_clone_without_snapshot( clone_name, uid, gid) else: status, error_message, clone = volume.make_clone( snapshot_name, clone_name, uid, gid) if status != "COMPLETED": if error_message == "Duplicate volume name %s. (errno=17)" % clone_name: clone_status = self.set_status(200, "Clone", clone_name) else: clone_status = self.set_status(400, "Clone", clone_name, error_message) return [clone_status], vol_size else: clone_status = self.set_status(201, "Clone", clone_name) if clone.check_vol(): status, error_message, junction_name = clone.mount() if status != "COMPLETED": junction_status = self.set_status(400, "Junction", junction_name, error_message) return [clone_status, junction_status], vol_size else: junction_status = self.set_status(201, "Junction", junction_name) if self.aggregate.check_vol_junction(clone_name, junction_name): vol_size = volume.get_size() return [clone_status, junction_status], vol_size else: clone_status = self.set_status(500, "Clone", clone_name, "Check_vol failed") return [clone_status], vol_size
def create_volume(self, vol_name, vol_size, uid, gid, export_policy='default'): '''Create ONTAP volume''' volume = Volume(vol_name, self.aggregate) status, error_message = volume.make_volume(vol_size, uid, gid, export_policy) if status != "COMPLETED": if error_message == "Duplicate volume name %s. (errno=17)" % vol_name: vol_status = self.set_status(200, "Volume", vol_name) else: vol_status = self.set_status(400, "Volume", vol_name, error_message) else: vol_status = self.set_status(201, "Volume", vol_name) vol_size = "" if vol_status['code'] != 400: if volume.check_vol(): vol_size = volume.get_size() else: vol_status = self.set_status(500, "Volume", vol_name, "Check_vol failed") return [vol_status], vol_size
def modify_volume_ssl(self, volume_name, ssl_name): '''Modify the storage service level for volume''' volume = Volume(volume_name, self.aggregate) volume.modify_ssl(ssl_name)