Module: Pangea::Resources::ResourceBuilder::ClassMethods

Defined in:
lib/pangea/resources/resource_builder.rb

Instance Method Summary collapse

Instance Method Details

#data_definitionsObject



84
85
86
# File 'lib/pangea/resources/resource_builder.rb', line 84

def data_definitions
  @_data_definitions || {}
end

#define_data(tf_type, attributes_class:, outputs: { id: :id }, map: [], map_present: [], map_bool: [], tags: nil, labels: nil, &custom_block) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/pangea/resources/resource_builder.rb', line 57

def define_data(tf_type, attributes_class:, outputs: { id: :id },
                map: [], map_present: [], map_bool: [],
                tags: nil, labels: nil, &custom_block)
  method_name = :"data_#{tf_type}"
  @_data_definitions ||= {}
  @_data_definitions[tf_type] = _store_definition(attributes_class, outputs, map, map_present, map_bool, tags, labels)

  define_method(method_name) do |name, attributes = {}|
    if attributes.is_a?(Hash)
      known = attributes_class.schema.map { |k| k.name }.to_set
      unknown = attributes.keys.map(&:to_sym) - known.to_a
      unless unknown.empty?
        raise ArgumentError,
          "data.#{tf_type}: unknown attributes #{unknown.inspect}. " \
          "Valid attributes: #{known.to_a.sort.inspect}."
      end
    end
    input = ResourceInput.partition(attributes_class, attributes)
    _synthesize_block(:data, tf_type, name, input, map, map_present, map_bool, tags, labels, custom_block)
    _build_reference("data.#{tf_type}", name, input, outputs)
  end
end

#define_resource(tf_type, attributes_class:, outputs: { id: :id }, map: [], map_present: [], map_bool: [], tags: nil, labels: nil, &custom_block) ⇒ Object



24
25
26
27
28
29
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
# File 'lib/pangea/resources/resource_builder.rb', line 24

def define_resource(tf_type, attributes_class:, outputs: { id: :id },
                    map: [], map_present: [], map_bool: [],
                    tags: nil, labels: nil, &custom_block)
  @_resource_definitions ||= {}
  @_resource_definitions[tf_type] = _store_definition(attributes_class, outputs, map, map_present, map_bool, tags, labels)

  define_method(tf_type) do |name, attributes = {}|
    meta_args = {}
    resource_attrs = attributes
    if attributes.is_a?(Hash)
      meta_keys = %i[lifecycle depends_on count for_each provider provisioner]
      meta_args = attributes.select { |k, _| meta_keys.include?(k.to_sym) }
      resource_attrs = attributes.reject { |k, _| meta_keys.include?(k.to_sym) }

      # Unknown key detection runs on ALL keys (including ref-carrying ones)
      known = attributes_class.schema.map { |k| k.name }.to_set
      unknown = resource_attrs.keys.map(&:to_sym) - known.to_a
      unless unknown.empty?
        raise ArgumentError,
          "#{tf_type}: unknown attributes #{unknown.inspect}. " \
          "Valid attributes: #{known.to_a.sort.inspect}. " \
          "Typo? Check Terraform docs for the correct attribute name."
      end
    end

    # Partition into validated literals + opaque refs.
    # Types stay pure. Refs handled at serialization boundary.
    input = ResourceInput.partition(attributes_class, resource_attrs)
    _synthesize_block(:resource, tf_type, name, input, map, map_present, map_bool, tags, labels, custom_block, meta_args)
    _build_reference(tf_type.to_s, name, input, outputs)
  end
end

#resource_definitionsObject



80
81
82
# File 'lib/pangea/resources/resource_builder.rb', line 80

def resource_definitions
  @_resource_definitions || {}
end