Class: Pvectl::Repositories::Snapshot

Inherits:
Base
  • Object
show all
Defined in:
lib/pvectl/repositories/snapshot.rb,
sig/pvectl/repositories/snapshot.rbs

Overview

Repository for VM/container snapshots.

Handles listing snapshots for both QEMU VMs and LXC containers. Filters out the "current" snapshot which represents the live state.

Examples:

Listing snapshots for a VM

repo = Snapshot.new(connection)
snapshots = repo.list(100, "pve1", :qemu)
snapshots.each { |s| puts "#{s.name}: #{s.description}" }

Listing snapshots for a container

snapshots = repo.list(101, "pve1", :lxc)

See Also:

Instance Attribute Summary

Attributes inherited from Base

#connection

Instance Method Summary collapse

Methods inherited from Base

#extract_data, #get, #initialize, #models_from, #unwrap

Constructor Details

This class inherits a constructor from Pvectl::Repositories::Base

Instance Method Details

#build_model(data, vmid, node, resource_type) ⇒ Models::Snapshot

Builds Snapshot model from API response data.



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/pvectl/repositories/snapshot.rb', line 119

def build_model(data, vmid, node, resource_type)
  Models::Snapshot.new(
    name: data[:name],
    snaptime: data[:snaptime],
    description: data[:description],
    vmstate: data[:vmstate],
    parent: data[:parent],
    vmid: vmid,
    node: node,
    resource_type: resource_type
  )
end

#create(vmid, node, resource_type, name:, description: nil, vmstate: false) ⇒ String

Creates a snapshot for a VM or container.

Uses POST /nodes/{node}/qemu|lxc/{vmid}/snapshot with parameters:

  • snapname: Name of the snapshot
  • description: Optional description
  • vmstate: Include VM RAM state (QEMU only, ignored for LXC)


57
58
59
60
61
62
63
64
# File 'lib/pvectl/repositories/snapshot.rb', line 57

def create(vmid, node, resource_type, name:, description: nil, vmstate: false)
  endpoint = resource_endpoint(resource_type)
  params = { snapname: name }
  params[:description] = description if description
  params[:vmstate] = vmstate if vmstate && resource_type == :qemu

  connection.client["nodes/#{node}/#{endpoint}/#{vmid}/snapshot"].post(params)
end

#delete(vmid, node, resource_type, snapname, force: false) ⇒ String

Deletes a snapshot.

Uses DELETE /nodes/{node}/qemu|lxc/{vmid}/snapshot/{snapname}.



76
77
78
79
80
81
82
# File 'lib/pvectl/repositories/snapshot.rb', line 76

def delete(vmid, node, resource_type, snapname, force: false)
  endpoint = resource_endpoint(resource_type)
  params = {}
  params[:force] = true if force

  connection.client["nodes/#{node}/#{endpoint}/#{vmid}/snapshot/#{snapname}"].delete(params)
end

#list(vmid, node, resource_type) ⇒ Array<Models::Snapshot>

Lists all snapshots for a VM or container.

Uses /nodes/{node}/qemu/{vmid}/snapshot for VMs or /nodes/{node}/lxc/{vmid}/snapshot for containers. Filters out the "current" snapshot which represents live state.



32
33
34
35
36
37
38
39
40
41
# File 'lib/pvectl/repositories/snapshot.rb', line 32

def list(vmid, node, resource_type)
  endpoint = resource_endpoint(resource_type)
  response = connection.client["nodes/#{node}/#{endpoint}/#{vmid}/snapshot"].get

  response
    .reject { |s| s[:name] == "current" }
    .map { |data| build_model(data, vmid, node, resource_type) }
rescue StandardError
  []
end

#resource_endpoint(resource_type) ⇒ String

Returns the API endpoint prefix for the resource type.



108
109
110
# File 'lib/pvectl/repositories/snapshot.rb', line 108

def resource_endpoint(resource_type)
  resource_type == :lxc ? "lxc" : "qemu"
end

#rollback(vmid, node, resource_type, snapname, start: false) ⇒ String

Rolls back to a snapshot.

Uses POST /nodes/{node}/qemu|lxc/{vmid}/snapshot/{snapname}/rollback.



94
95
96
97
98
99
100
# File 'lib/pvectl/repositories/snapshot.rb', line 94

def rollback(vmid, node, resource_type, snapname, start: false)
  endpoint = resource_endpoint(resource_type)
  params = {}
  params[:start] = true if start

  connection.client["nodes/#{node}/#{endpoint}/#{vmid}/snapshot/#{snapname}/rollback"].post(params)
end