Class: ForemanOpentofu::ComputeVM

Inherits:
Object
  • Object
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 (private)



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

#nameObject



29
30
31
# File 'app/models/foreman_opentofu/compute_vm.rb', line 29

def name
  self['name']
end

#persisted?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'app/models/foreman_opentofu/compute_vm.rb', line 33

def persisted?
  self['identity'].present? || self['id'].present?
end

#powerObject



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

Returns:

  • (Boolean)


25
26
27
# File 'app/models/foreman_opentofu/compute_vm.rb', line 25

def ready?
  power.to_s == 'on' || power.to_s == 'running'
end

#rebootObject



45
46
47
48
# File 'app/models/foreman_opentofu/compute_vm.rb', line 45

def reboot
  stop
  start
end

#resetObject



50
51
52
# File 'app/models/foreman_opentofu/compute_vm.rb', line 50

def reset
  reboot
end

#startObject



37
38
39
# File 'app/models/foreman_opentofu/compute_vm.rb', line 37

def start
  @provider.start_vm(name)
end

#stopObject



41
42
43
# File 'app/models/foreman_opentofu/compute_vm.rb', line 41

def stop
  @provider.stop_vm(name)
end

#to_hObject



16
17
18
# File 'app/models/foreman_opentofu/compute_vm.rb', line 16

def to_h
  unwrap(@attributes.to_dup)
end

#vm_ip_addressObject



54
55
56
# File 'app/models/foreman_opentofu/compute_vm.rb', line 54

def vm_ip_address
  @attributes['vm_ip_address']
end

#volumes_attributesObject



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)
  # TODO: I guess we have nothing to wait for
  # and we need to change the context of the given block
  instance_eval(&block)
end