Module: ForemanOpentofu::Concerns::BaseTemplateScopeExtensions

Extended by:
ActiveSupport::Concern, ApipieDSL::Module
Includes:
HclFormat, NicHelpers
Defined in:
app/lib/foreman_opentofu/concerns/base_template_scope_extensions.rb

Instance Method Summary collapse

Methods included from NicHelpers

#nic_attributes, #normalize_interfaces, #sanitize_attributes

Methods included from HclFormat

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

Instance Method Details

#backend_blockObject



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/lib/foreman_opentofu/concerns/base_template_scope_extensions.rb', line 85

def backend_block
  if @token
    data = {
      address: "#{Setting[:foreman_url]}/api/v2/tf_states/#{@host_name}",
      headers: {
        'Authorization' => "Token #{@token}",
      },
    }
    block_to_hcl(%w[backend http], data, depth: 1)
  else
    ''
  end
end

#build_disksObject



99
100
101
102
103
104
105
106
# File 'app/lib/foreman_opentofu/concerns/base_template_scope_extensions.rb', line 99

def build_disks
  disk_render_source.each_with_index.filter_map do |disk, index|
    # drop removed disks; tofu will drop them automatically, if they are no longer defined
    next if disk.respond_to?(:[]) && disk['_delete'].to_i == 1

    rendered_disk(index, disk)
  end.join("\n")
end

#build_nicsObject



108
109
110
111
112
113
# File 'app/lib/foreman_opentofu/concerns/base_template_scope_extensions.rb', line 108

def build_nics
  nics_from_cr_attrs.each_with_index.map do |nic, index|
    data = @compute_resource.render_nic(nic, self, index)
    render_provider_data(data)
  end.join("\n")
end

#resource_block(resource) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'app/lib/foreman_opentofu/concerns/base_template_scope_extensions.rb', line 30

def resource_block(resource)
  block = ''
  path = ['data', resource[:name], 'all']

  # data "<%= @resource[:name] %>" "all" {
  # <% @resource.dig(:options, 'data_source', 'arguments')&.each do |key, value| %>
  #   <%= key %> = <%= value.inspect %>
  # <% end %>
  # }
  block << block_to_hcl(path, resource.dig(:options, 'data_source', 'arguments') || {})

  # output "resources" {
  #   value = [ for e in data.<%= @resource[:name] %>.all.<%= @resource.dig(:options, 'output_path_postfix') %>: {
  #     id = e.<%= @resource.dig(:options, 'entity', 'id') || 'id' %>
  #     name = e.<%= @resource.dig(:options, 'entity', 'name') || 'name' %>
  #     # obj = e
  #     } ]
  # }
  block << block_to_hcl(%w[output resources])
  block << '{' << "\n"
  block << "  value = [ for e in data.#{resource[:name]}.all.#{resource.dig(:options, 'output_path_postfix')}: {\n"
  params = resource.dig(:options, 'entity') || {}
  params[:id] = 'id' unless params.key? :id
  params[:name] = 'name' unless params.key? :name
  params.each do |key, value|
    block << "    #{key} = e.#{value}\n"
  end
  block << '  } ]' << "\n"
  block << '}' << "\n"
end

#terraform_block(data) ⇒ Object

e.g. terraform_block({ ‘nutanix’ => { ‘source’ => ‘nutanix/nutanix’, ‘version’ => ‘2.4.0’ })



19
20
21
22
23
24
25
# File 'app/lib/foreman_opentofu/concerns/base_template_scope_extensions.rb', line 19

def terraform_block(data)
  block = block_to_hcl(['terraform'])
  block << '{'
  block << block_to_hcl(['required_providers'], data, depth: 1)
  block << backend_block
  block << "\n}"
end

#vm_attributes(skip_list = []) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/lib/foreman_opentofu/concerns/base_template_scope_extensions.rb', line 65

def vm_attributes(skip_list = [])
  available_attributes = @compute_resource.available_attributes
  data = {}
  res = ''
  @cr_attrs.each do |key, value|
    next if skip_list.include? key

    conf = available_attributes[key]
    if conf.nil?
      Rails.logger.warn("Attribute #{key.inspect} is not supported.")
      next
    end
    next if conf['group'] != 'vm'
    next if value.blank? && !conf['mandatory']

    data[key] = format_value(value, conf['type'])
  end
  res << to_hcl(data, snippet: true)
end