Class: Cfer::Core::Resource

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

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.



9
10
11
12
13
14
15
16
17
# File 'lib/cfer/core/resource.rb', line 9

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.



7
8
9
# File 'lib/cfer/core/resource.rb', line 7

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



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

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



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

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



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

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



45
46
47
# File 'lib/cfer/core/resource.rb', line 45

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



37
38
39
# File 'lib/cfer/core/resource.rb', line 37

def get_property(key)
  self[:Properties].fetch key
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.



31
32
33
# File 'lib/cfer/core/resource.rb', line 31

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



23
24
25
26
27
# File 'lib/cfer/core/resource.rb', line 23

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