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)



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

#nameObject



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

def name
  self['name']
end

#persisted?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'app/models/foreman_opentofu/compute_vm.rb', line 48

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



60
61
62
63
# File 'app/models/foreman_opentofu/compute_vm.rb', line 60

def reboot
  stop
  start
end

#resetObject



65
66
67
# File 'app/models/foreman_opentofu/compute_vm.rb', line 65

def reset
  reboot
end

#startObject



52
53
54
# File 'app/models/foreman_opentofu/compute_vm.rb', line 52

def start
  @provider.start_vm(name)
end

#stopObject



56
57
58
# File 'app/models/foreman_opentofu/compute_vm.rb', line 56

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.deep_dup)
end

#vm_descriptionObject



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_addressObject



69
70
71
# File 'app/models/foreman_opentofu/compute_vm.rb', line 69

def vm_ip_address
  @attributes['vm_ip_address']
end

#volumes_attributesObject



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