Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions simplyblock_cli/cli-reference.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2226,6 +2226,10 @@ commands:
dest: with_details
type: bool
action: store_true
- name: "--pool"
help: "List snapshots in particular pool id or name."
dest: pool
type: str
- name: delete
help: "Deletes a snapshot."
arguments:
Expand Down
1 change: 1 addition & 0 deletions simplyblock_cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -905,6 +905,7 @@ def init_snapshot__list(self, subparser):
argument = subcommand.add_argument('--all', help='List soft deleted snapshots.', dest='all', action='store_true')
argument = subcommand.add_argument('--cluster-id', help='Filter snapshots by cluster UUID', type=str, dest='cluster_id', required=False)
argument = subcommand.add_argument('--with-details', help='List snapshots with replicate and chaining details', dest='with_details', action='store_true')
argument = subcommand.add_argument('--pool', help='List snapshots in particular pool id or name.', type=str, dest='pool')

def init_snapshot__delete(self, subparser):
subcommand = self.add_sub_command(subparser, 'delete', 'Deletes a snapshot.')
Expand Down
2 changes: 1 addition & 1 deletion simplyblock_cli/clibase.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,7 +785,7 @@ def snapshot__backup(self, sub_command, args):
return True

def snapshot__list(self, sub_command, args):
return snapshot_controller.list(args.all, args.cluster_id, args.with_details)
return snapshot_controller.list(args.all, args.cluster_id, args.with_details, args.pool)

def snapshot__delete(self, sub_command, args):
return snapshot_controller.delete(args.snapshot_id, args.force)
Expand Down
17 changes: 15 additions & 2 deletions simplyblock_core/controllers/snapshot_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -329,8 +329,21 @@ def add(lvol_id, snapshot_name, backup=False, lock=True):
return snap.uuid, False


def list(all=False, cluster_id=None, with_details=False):
snaps = db_controller.get_snapshots(cluster_id)
def list(all=False, cluster_id=None, with_details=False, pool_id_or_name=None):
if pool_id_or_name:
try:
pool = (
db_controller.get_pool_by_id(pool_id_or_name)
if utils.UUID_PATTERN.match(pool_id_or_name) is not None
else db_controller.get_pool_by_name(pool_id_or_name)
)
snaps = db_controller.get_snapshots_by_pool_id(pool.get_id())
except KeyError:
logger.error("Can not find pool with provided pool_id_or_name: %s", pool_id_or_name)
return False
else:
snaps = db_controller.get_snapshots(cluster_id)

snaps = sorted(snaps, key=lambda snap: snap.created_at)

# Build set of lvol UUIDs with active migrations (single DB scan)
Expand Down
8 changes: 8 additions & 0 deletions simplyblock_core/db_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,14 @@ def get_snapshots_by_node_id(self, node_id) -> List[SnapShot]:
ret.append(snap)
return sorted(ret, key=lambda x: x.create_dt)

def get_snapshots_by_pool_id(self, pool_id) -> List[SnapShot]:
ret = []
snaps = SnapShot().read_from_db(self.kv_store)
for snap in snaps:
if snap.pool_uuid == pool_id:
ret.append(snap)
return sorted(ret, key=lambda x: x.create_dt)

def get_snapshots_by_lvol_id(self, lvol_id) -> List[SnapShot]:
return [s for s in self.get_snapshots() if s.lvol and s.lvol.get_id() == lvol_id]

Expand Down
Loading