Class: GeoEngineer::SubResource

Inherits:
Object
  • Object
show all
Includes:
HasAttributes, HasSubResources
Defined in:
lib/geoengineer/sub_resource.rb

Overview

SubResources are included in resources with their own rules

For example, ingress in aws_security_group is a subresource.

A SubResource can have arbitrary attributes

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HasSubResources

#assign_block, #attribute_missing, #delete_all_subresources, #delete_subresources_where, #subresources

Methods included from HasAttributes

#[], #[]=, #assign_attribute, #assign_block, #attribute_missing, #attribute_procs, #attributes, #delete, #eager_load_attributes, #method_missing, #reset_attributes, #retrieve_attribute, #terraform_attribute_ref, #terraform_attributes, #timeout

Constructor Details

#initialize(resource, type, &block) ⇒ SubResource

Returns a new instance of SubResource


14
15
16
17
18
# File 'lib/geoengineer/sub_resource.rb', line 14

def initialize(resource, type, &block)
  @resource = resource
  @type = type.to_s
  instance_exec(self, &block) if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class HasAttributes

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type


12
13
14
# File 'lib/geoengineer/sub_resource.rb', line 12

def type
  @type
end

Instance Method Details

#_terraform_idObject


20
21
22
# File 'lib/geoengineer/sub_resource.rb', line 20

def _terraform_id
  @resource._terraform_id
end

#to_terraformObject

Terraform methods


25
26
27
28
29
30
31
32
33
34
35
# File 'lib/geoengineer/sub_resource.rb', line 25

def to_terraform
  sb = ["  #{@type} { "]

  sb.concat terraform_attributes.map { |k, v|
    "  #{k.to_s.inspect} = #{v.inspect}"
  }

  sb.concat subresources.map(&:to_terraform)
  sb << " }"
  sb.join("\n")
end

#to_terraform_jsonObject


37
38
39
40
41
42
43
44
# File 'lib/geoengineer/sub_resource.rb', line 37

def to_terraform_json
  json = terraform_attributes
  subresources.map(&:to_terraform_json).each do |k, v|
    json[k] ||= []
    json[k] << v
  end
  [@type, json]
end