Class: Pvectl::Models::Vm

Inherits:
Base
  • Object
show all
Defined in:
lib/pvectl/models/vm.rb

Overview

Represents a QEMU virtual machine in the Proxmox cluster.

Immutable domain model containing VM attributes and status predicates. Created by Repositories::Vm from API data. Display/formatting methods are in Presenters::Vm.

Examples:

Creating a VM model

vm = Vm.new(vmid: 100, name: "web", status: "running", node: "pve1")
vm.running? #=> true
vm.template? #=> false

From API response

data = { "vmid" => 100, "name" => "web", "status" => "running" }
vm = Vm.new(data)

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Vm

Creates a new VM model from attributes.

Parameters:

  • attrs (Hash) (defaults to: {})

    VM attributes from API (string or symbol keys)



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/pvectl/models/vm.rb', line 84

def initialize(attrs = {})
  super(attrs)
  # Use @attributes which has normalized symbol keys from Base
  @vmid = @attributes[:vmid]
  @name = @attributes[:name]
  @status = @attributes[:status]
  @node = @attributes[:node]
  @cpu = @attributes[:cpu]
  @maxcpu = @attributes[:maxcpu]
  @mem = @attributes[:mem]
  @maxmem = @attributes[:maxmem]
  @disk = @attributes[:disk]
  @maxdisk = @attributes[:maxdisk]
  @uptime = @attributes[:uptime]
  @template = @attributes[:template]
  @tags = @attributes[:tags]
  @hastate = @attributes[:hastate]
  @netin = @attributes[:netin]
  @netout = @attributes[:netout]
  @describe_data = @attributes[:describe_data]
  @pool = @attributes[:pool]
  @type = @attributes[:type]
end

Instance Attribute Details

#cpuFloat? (readonly)

Returns current CPU usage (0-1 scale).

Returns:

  • (Float, nil)

    current CPU usage (0-1 scale)



37
38
39
# File 'lib/pvectl/models/vm.rb', line 37

def cpu
  @cpu
end

#describe_dataHash? (readonly)

Returns raw API responses for describe command.

Returns:

  • (Hash, nil)

    raw API responses for describe command



73
74
75
# File 'lib/pvectl/models/vm.rb', line 73

def describe_data
  @describe_data
end

#diskInteger? (readonly)

Returns current disk usage in bytes.

Returns:

  • (Integer, nil)

    current disk usage in bytes



49
50
51
# File 'lib/pvectl/models/vm.rb', line 49

def disk
  @disk
end

#hastateString? (readonly)

Returns HA state.

Returns:

  • (String, nil)

    HA state



64
65
66
# File 'lib/pvectl/models/vm.rb', line 64

def hastate
  @hastate
end

#maxcpuInteger? (readonly)

Returns maximum CPU cores.

Returns:

  • (Integer, nil)

    maximum CPU cores



40
41
42
# File 'lib/pvectl/models/vm.rb', line 40

def maxcpu
  @maxcpu
end

#maxdiskInteger? (readonly)

Returns maximum disk size in bytes.

Returns:

  • (Integer, nil)

    maximum disk size in bytes



52
53
54
# File 'lib/pvectl/models/vm.rb', line 52

def maxdisk
  @maxdisk
end

#maxmemInteger? (readonly)

Returns maximum memory in bytes.

Returns:

  • (Integer, nil)

    maximum memory in bytes



46
47
48
# File 'lib/pvectl/models/vm.rb', line 46

def maxmem
  @maxmem
end

#memInteger? (readonly)

Returns current memory usage in bytes.

Returns:

  • (Integer, nil)

    current memory usage in bytes



43
44
45
# File 'lib/pvectl/models/vm.rb', line 43

def mem
  @mem
end

#nameString? (readonly)

Returns VM name.

Returns:

  • (String, nil)

    VM name



28
29
30
# File 'lib/pvectl/models/vm.rb', line 28

def name
  @name
end

#netinInteger? (readonly)

Returns network input bytes.

Returns:

  • (Integer, nil)

    network input bytes



67
68
69
# File 'lib/pvectl/models/vm.rb', line 67

def netin
  @netin
end

#netoutInteger? (readonly)

Returns network output bytes.

Returns:

  • (Integer, nil)

    network output bytes



70
71
72
# File 'lib/pvectl/models/vm.rb', line 70

def netout
  @netout
end

#nodeString (readonly)

Returns node name where VM runs.

Returns:

  • (String)

    node name where VM runs



34
35
36
# File 'lib/pvectl/models/vm.rb', line 34

def node
  @node
end

#poolString? (readonly)

Returns resource pool name.

Returns:

  • (String, nil)

    resource pool name



76
77
78
# File 'lib/pvectl/models/vm.rb', line 76

def pool
  @pool
end

#statusString (readonly)

Returns VM status (running, stopped, paused).

Returns:

  • (String)

    VM status (running, stopped, paused)



31
32
33
# File 'lib/pvectl/models/vm.rb', line 31

def status
  @status
end

#tagsString? (readonly)

Returns semicolon-separated tags.

Returns:

  • (String, nil)

    semicolon-separated tags



61
62
63
# File 'lib/pvectl/models/vm.rb', line 61

def tags
  @tags
end

#templateInteger? (readonly)

Returns template flag (1 if template, 0 otherwise).

Returns:

  • (Integer, nil)

    template flag (1 if template, 0 otherwise)



58
59
60
# File 'lib/pvectl/models/vm.rb', line 58

def template
  @template
end

#typeString? (readonly)

Returns resource type from API (“qemu”).

Returns:

  • (String, nil)

    resource type from API (“qemu”)



79
80
81
# File 'lib/pvectl/models/vm.rb', line 79

def type
  @type
end

#uptimeInteger? (readonly)

Returns uptime in seconds.

Returns:

  • (Integer, nil)

    uptime in seconds



55
56
57
# File 'lib/pvectl/models/vm.rb', line 55

def uptime
  @uptime
end

#vmidInteger (readonly)

Returns unique VM identifier.

Returns:

  • (Integer)

    unique VM identifier



25
26
27
# File 'lib/pvectl/models/vm.rb', line 25

def vmid
  @vmid
end

Instance Method Details

#paused?Boolean

Checks if the VM is paused.

Returns:

  • (Boolean)

    true if status is “paused”



125
126
127
# File 'lib/pvectl/models/vm.rb', line 125

def paused?
  status == "paused"
end

#running?Boolean

Checks if the VM is running.

Returns:

  • (Boolean)

    true if status is “running”



111
112
113
# File 'lib/pvectl/models/vm.rb', line 111

def running?
  status == "running"
end

#stopped?Boolean

Checks if the VM is stopped.

Returns:

  • (Boolean)

    true if status is “stopped”



118
119
120
# File 'lib/pvectl/models/vm.rb', line 118

def stopped?
  status == "stopped"
end

#template?Boolean

Checks if the VM is a template.

Returns:

  • (Boolean)

    true if template flag is 1



132
133
134
# File 'lib/pvectl/models/vm.rb', line 132

def template?
  template == 1
end