Class: Cfer::Core::Resource

Inherits:
BlockHash
  • Object
show all
Includes:
Hooks
Defined in:
lib/cfer/core/resource.rb

Defined Under Namespace

Classes: Handle

Constant Summary collapse

@@types =
{}

Constants inherited from BlockHash

BlockHash::NON_PROXIED_METHODS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Hooks

included, #post_block, #pre_block

Methods inherited from BlockHash

#method_missing, #respond_to?

Methods inherited from Block

#build_from_block, #build_from_file, #build_from_string, #include_file, #post_block, #pre_block

Constructor Details

#initialize(name, type, stack, options, &block) ⇒ Resource

Returns a new instance of Resource.



24
25
26
27
28
29
30
31
32
# File 'lib/cfer/core/resource.rb', line 24

def initialize(name, type, stack, options, &block)
  @name = name
  @stack = stack

  self[:Type] = type
  self.merge!(options)
  self[:Properties] = HashWithIndifferentAccess.new
  build_from_block(&block)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Cfer::BlockHash

Instance Attribute Details

#stackObject (readonly)

Returns the value of attribute stack.



22
23
24
# File 'lib/cfer/core/resource.rb', line 22

def stack
  @stack
end

Class Method Details

.after(type, **options, &block) ⇒ Object

Registers a hook that will be run after properties have been set on a resource

Parameters:

  • type (String)

    The type of resource, for example AWS::EC2::Instance



82
83
84
# File 'lib/cfer/core/resource.rb', line 82

def after(type, **options, &block)
  resource_class(type).post_hooks << options.merge(block: block)
end

.before(type, **options, &block) ⇒ Object

Registers a hook that will be run before properties are set on a resource

Parameters:

  • type (String)

    The type of resource, for example AWS::EC2::Instance



76
77
78
# File 'lib/cfer/core/resource.rb', line 76

def before(type, **options, &block)
  resource_class(type).pre_hooks << options.merge(block: block)
end

.extend_resource(type, &block) ⇒ Object

Patches code into DSL classes for CloudFormation resources

Parameters:

  • type (String)

    The type of resource, for example AWS::EC2::Instance



70
71
72
# File 'lib/cfer/core/resource.rb', line 70

def extend_resource(type, &block)
  resource_class(type).class_eval(&block)
end

.resource_class(type) ⇒ Class

Fetches the DSL class for a CloudFormation resource type

Parameters:

  • type (String)

    The type of resource, for example AWS::EC2::Instance

Returns:

  • (Class)

    The DSL class representing this resource type, including all extensions



64
65
66
# File 'lib/cfer/core/resource.rb', line 64

def resource_class(type)
  @@types[type] ||= "CferExt::#{type}".split('::').inject(Object) { |o, c| o.const_get c if o && o.const_defined?(c) } || Class.new(Cfer::Core::Resource)
end

Instance Method Details

#get_property(key) ⇒ Object

Gets the current value of a given property

Parameters:

  • key (String)

    The name of the property to fetch



56
57
58
# File 'lib/cfer/core/resource.rb', line 56

def get_property(key)
  self[:Properties].fetch key
end

#handleObject



34
35
36
# File 'lib/cfer/core/resource.rb', line 34

def handle
  @handle ||= Handle.new(@name)
end

#properties(keyvals = {}) ⇒ Object

Directly sets raw properties in the underlying CloudFormation structure.

Parameters:

  • keyvals (Hash) (defaults to: {})

    The properties to set on this object.



50
51
52
# File 'lib/cfer/core/resource.rb', line 50

def properties(keyvals = {})
  self[:Properties].merge!(keyvals)
end

#tag(k, v, **options) ⇒ Object

Sets a tag on this resource. The resource must support the CloudFormation Tags property.

Parameters:

  • k (String)

    The name of the tag to set

  • v (String)

    The value for this tag

  • options (Hash)

    An arbitrary set of additional properties to be added to this tag, for example PropagateOnLaunch on AWS::AutoScaling::AutoScalingGroup



42
43
44
45
46
# File 'lib/cfer/core/resource.rb', line 42

def tag(k, v, **options)
  self[:Properties][:Tags] ||= []
  self[:Properties][:Tags].delete_if { |kv| kv["Key"] == k }
  self[:Properties][:Tags].unshift({"Key" => k, "Value" => v}.merge(options))
end