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
167
168
169
170
171
172
173
174
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 167
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)
nil
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
48
49
50
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 48
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
60
61
62
63
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 60
def reboot
stop
start
end
|
#reset ⇒ Object
65
66
67
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 65
def reset
reboot
end
|
#start ⇒ Object
52
53
54
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 52
def start
@provider.start_vm(name)
end
|
#stop ⇒ Object
56
57
58
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 56
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.deep_dup)
end
|
#vm_description ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 33
def vm_description
return nil unless @provider.respond_to?(:tofu_provider)
vm_attrs = @provider.tofu_provider.attributes('vm')
values = vm_attrs.filter_map do |attr|
key = attr['name'].to_s
value = attribute_value(key)
next if value.blank?
label = attr['label'].presence || key.humanize
"#{label}: #{value}"
end
values.join(', ') if values.present?
end
|
#vm_ip_address ⇒ Object
69
70
71
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 69
def vm_ip_address
@attributes['vm_ip_address']
end
|
#volumes_attributes ⇒ Object
79
80
81
82
83
84
85
86
87
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 79
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
73
74
75
76
77
|
# File 'app/models/foreman_opentofu/compute_vm.rb', line 73
def wait_for(&block)
instance_eval(&block)
end
|