Class: ForemanOpentofu::ComputeVM
- Inherits:
-
Object
- Object
- ForemanOpentofu::ComputeVM
show all
- Includes:
- ActiveModel::Attributes, ActiveModel::Model
- Defined in:
- app/models/foreman_opentofu/compute_vm.rb
Instance Method Summary
collapse
Constructor Details
#initialize(provider, attrs = {}) ⇒ ComputeVM
Returns a new instance of ComputeVM.
6
7
8
9
10
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 6
def initialize(provider, attrs = {})
@attributes = flatten_attrs(attrs.deep_stringify_keys)
@provider = provider
define_dynamic_readers!
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object
141
142
143
144
145
146
147
148
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 141
def method_missing(method_name, *args)
return super unless args.empty?
key = method_name.to_s
return attribute_value(key) if dynamic_attribute_keys.include?(key)
super
end
|
Instance Method Details
#[](key) ⇒ Object
12
13
14
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 12
def [](key)
@attributes[key.to_s]
end
|
#name ⇒ Object
29
30
31
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 29
def name
self['name']
end
|
#persisted? ⇒ Boolean
33
34
35
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 33
def persisted?
self['identity'].present? || self['id'].present?
end
|
#power ⇒ Object
20
21
22
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 20
def power
self['status'] || self['power'] || self['power_state']
end
|
#ready? ⇒ Boolean
TODO: add definitions for different power on/off values
25
26
27
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 25
def ready?
power.to_s == 'on' || power.to_s == 'running'
end
|
#reboot ⇒ Object
45
46
47
48
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 45
def reboot
stop
start
end
|
#reset ⇒ Object
50
51
52
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 50
def reset
reboot
end
|
#start ⇒ Object
37
38
39
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 37
def start
@provider.start_vm(name)
end
|
#stop ⇒ Object
41
42
43
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 41
def stop
@provider.stop_vm(name)
end
|
#to_h ⇒ Object
16
17
18
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 16
def to_h
unwrap(@attributes.to_dup)
end
|
#vm_ip_address ⇒ Object
54
55
56
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 54
def vm_ip_address
@attributes['vm_ip_address']
end
|
#volumes_attributes ⇒ Object
64
65
66
67
68
69
70
71
72
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 64
def volumes_attributes
vols_attrs = attribute_value('volumes_attributes')
return vols_attrs if vols_attrs.is_a?(Hash)
list = vols_attrs.presence || attribute_value('volumes')
return {} if list.blank?
Array(list).each_with_index.to_h { |vol, idx| [idx.to_s, vol] }
end
|
#wait_for(&block) ⇒ Object
58
59
60
61
62
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 58
def wait_for(&block)
instance_eval(&block)
end
|