Module: ForemanOpentofu::NicHelpers

Includes:
HclFormat
Included in:
Concerns::BaseTemplateScopeExtensions
Defined in:
app/lib/foreman_opentofu/nic_helpers.rb

Instance Method Summary collapse

Methods included from HclFormat

#array_to_hcl, #block_to_hcl, #default_opts, #format_value, #hash_to_hcl, #prefix_hcl, #to_hcl

Instance Method Details

#nic_attributes(block_name = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/lib/foreman_opentofu/nic_helpers.rb', line 5

def nic_attributes(block_name = nil)
  nic_defs = @compute_resource.available_attributes('nic')
  interfaces = normalize_interfaces(@cr_attrs['interfaces'] || @cr_attrs['interfaces_attributes'])
  res = ''
  interfaces.each do |iface|
    missing_attrs = nic_defs.select { |name, cfg| cfg[:mandatory] && iface[name].blank? }
    # TODO: log the fact that we skip this due to missing mandatory attributes
    next unless missing_attrs.empty?

    res << if block_given?
             yield(iface, nic_defs)
           else
             block_to_hcl([block_name], sanitize_attributes(iface, nic_defs), depth: 1)
           end
  end
  res
end

#normalize_interfaces(interfaces) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'app/lib/foreman_opentofu/nic_helpers.rb', line 23

def normalize_interfaces(interfaces)
  if interfaces.is_a?(Hash)
    if interfaces.keys.all? { |k| k.to_s =~ /^\d+$/ }
      interfaces.values
    else
      [interfaces]
    end
  else
    Array(interfaces)
  end
end

#sanitize_attributes(attrs, defs) ⇒ Object



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

def sanitize_attributes(attrs, defs)
  data = {}
  attrs.each do |k, v|
    next if v.blank?

    next unless defs[k]

    data[k] = format_value(v, defs.dig(k, :type))
  end
  data
end