Class: ForemanOpentofu::ProviderType

Inherits:
Object
  • Object
show all
Defined in:
app/services/foreman_opentofu/provider_type.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ ProviderType

Returns a new instance of ProviderType.



7
8
9
10
11
12
# File 'app/services/foreman_opentofu/provider_type.rb', line 7

def initialize(id)
  @id = id.to_sym
  @name = id.capitalize
  @capabilities = [:build]
  @provider_attrs = []
end

Instance Attribute Details

#capabilitiesObject

Returns the value of attribute capabilities.



4
5
6
# File 'app/services/foreman_opentofu/provider_type.rb', line 4

def capabilities
  @capabilities
end

#default_attributesObject (readonly)

Returns the value of attribute default_attributes.



3
4
5
# File 'app/services/foreman_opentofu/provider_type.rb', line 3

def default_attributes
  @default_attributes
end

#default_interfacesObject (readonly)

Returns the value of attribute default_interfaces.



3
4
5
# File 'app/services/foreman_opentofu/provider_type.rb', line 3

def default_interfaces
  @default_interfaces
end

#default_volumesObject (readonly)

Returns the value of attribute default_volumes.



3
4
5
# File 'app/services/foreman_opentofu/provider_type.rb', line 3

def default_volumes
  @default_volumes
end

#disk_rendererObject

Returns the value of attribute disk_renderer.



4
5
6
# File 'app/services/foreman_opentofu/provider_type.rb', line 4

def disk_renderer
  @disk_renderer
end

#idObject (readonly)

Returns the value of attribute id.



3
4
5
# File 'app/services/foreman_opentofu/provider_type.rb', line 3

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'app/services/foreman_opentofu/provider_type.rb', line 3

def name
  @name
end

#nic_rendererObject

Returns the value of attribute nic_renderer.



4
5
6
# File 'app/services/foreman_opentofu/provider_type.rb', line 4

def nic_renderer
  @nic_renderer
end

#recreate_type_allow_listObject

Returns the value of attribute recreate_type_allow_list.



4
5
6
# File 'app/services/foreman_opentofu/provider_type.rb', line 4

def recreate_type_allow_list
  @recreate_type_allow_list
end

Instance Method Details

#attributes(group = nil) ⇒ Object



68
69
70
71
72
73
74
# File 'app/services/foreman_opentofu/provider_type.rb', line 68

def attributes(group = nil)
  return [] unless attributes?

  return @provider_attrs if group.nil?

  @provider_attrs.select { |e| e['group'] == group }
end

#attributes?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'app/services/foreman_opentofu/provider_type.rb', line 64

def attributes?
  @provider_attrs.present?
end

#available_attributes(group = nil) ⇒ Object

returns hash of available-attributes with attr-name as key



58
59
60
61
62
# File 'app/services/foreman_opentofu/provider_type.rb', line 58

def available_attributes(group = nil)
  raise "No available-attributes found for #{name}" unless attributes?

  attributes(group).index_by { |e| e['name'] }.with_indifferent_access
end

#available_images(compute_resource) ⇒ Object

if necessary, select-parameter named ‘available_images’ must be specified!

Raises:

  • (NotImplementedError)


21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/foreman_opentofu/provider_type.rb', line 21

def available_images(compute_resource)
  attribute = find_attr_by('name', 'available_images')
  raise NotImplementedError if attribute.nil?

  query_opts = attribute['options']
  case query_opts
  when nil then raise NotImplementedError
  # TODO: Check if Array really works!
  when Array then query_opts
  when Hash then compute_resource.available_resource(query_opts.dig('data_source', 'name'), query_opts)
  else raise 'available_images in ProviderType config is of unknown type.'
  end
end

#available_ssh_keys(compute_resource) ⇒ Object

if necessary, select-parameter named ‘available_ssh_keys’ must be specified!



36
37
38
39
40
41
42
43
44
45
# File 'app/services/foreman_opentofu/provider_type.rb', line 36

def available_ssh_keys(compute_resource)
  attribute = find_attr_by('name', 'available_ssh_keys')
  return [] if attribute.nil? || attribute['options'].nil?

  query_opts = attribute['options']
  case query_opts
  when Hash then compute_resource.available_resource(query_opts.dig('data_source', 'name'), query_opts)
  else raise 'available_ssh_keys in ProviderType config is of unknown type.'
  end
end

#filter_resource_changes(resources) ⇒ Object



113
114
115
116
117
118
119
120
121
# File 'app/services/foreman_opentofu/provider_type.rb', line 113

def filter_resource_changes(resources)
  return [] if resources.blank?

  result = resources.clone

  result.reject! { |res| recreate_type_allow_list.include?(res['type']) } if recreate_type_allow_list.respond_to? :include?

  result
end

#find_attr_by(key, value, group = nil) ⇒ Object

return Hash of attribute that has ‘key` set to `value`. If multiple exists, first in list is returned Returns `nil` if none is found Optional: limited to `group`



86
87
88
# File 'app/services/foreman_opentofu/provider_type.rb', line 86

def find_attr_by(key, value, group = nil)
  search_attr_by(key, value, group).first
end

#normalize_interfaces(vm_attrs) ⇒ Object

Normalize provider-specific NIC data to map to Foreman’s expected interfaces_attributes shape.



109
110
111
# File 'app/services/foreman_opentofu/provider_type.rb', line 109

def normalize_interfaces(vm_attrs)
  vm_attrs
end

#provided_attributesObject



90
91
92
93
94
95
# File 'app/services/foreman_opentofu/provider_type.rb', line 90

def provided_attributes
  # TODO: maybe we need to do something more sophisticated, here.
  #       network-based deployment needs MAC to set-up DHCP, but
  #       on image-based deployment we usually only get IPv4/6-address
  { mac: :mac }
end

#provider_attrs=(input) ⇒ Object



14
15
16
17
18
# File 'app/services/foreman_opentofu/provider_type.rb', line 14

def provider_attrs=(input)
  @provider_attrs = Array(input).map do |attr|
    ActiveSupport::HashWithIndifferentAccess.new(attr)
  end
end

#render_disk(disk, context, *args) ⇒ Object



97
98
99
100
# File 'app/services/foreman_opentofu/provider_type.rb', line 97

def render_disk(disk, context, *args)
  return '' unless disk_renderer
  context.instance_exec(disk, *args, &disk_renderer)
end

#render_nic(nic, context, *args) ⇒ Object



102
103
104
105
# File 'app/services/foreman_opentofu/provider_type.rb', line 102

def render_nic(nic, context, *args)
  return '' unless nic_renderer
  context.instance_exec(nic, *args, &nic_renderer)
end

#reset_cached_ssh_keys(compute_resource) ⇒ Object



47
48
49
50
51
52
53
54
55
# File 'app/services/foreman_opentofu/provider_type.rb', line 47

def reset_cached_ssh_keys(compute_resource)
  attribute = find_attr_by('name', 'available_ssh_keys')
  return if attribute.nil? || attribute['options'].nil?

  query_opts = attribute['options']
  return unless query_opts.is_a? Hash

  compute_resource.cache_delete(query_opts.dig('data_source', 'name'))
end

#search_attr_by(key, value, group = nil) ⇒ Object

return Array of Hashes of all attributes that have ‘key` set to `value`. Optional: limited to `group`



78
79
80
# File 'app/services/foreman_opentofu/provider_type.rb', line 78

def search_attr_by(key, value, group = nil)
  attributes(group).select { |attr| attr[key] == value }
end