module Skywriter::Resource
Attributes
Public Class Methods
@param [String] logical_name
The logical name of this Resource
. Will be
used as the hash key in the 'Resources' hash
@param [Hash] options Options hash. Valid values depend on the
implementing class - see the AWS documentation at http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html for details. Additionally, you can pass `:additional_dependencies` as described in the README. And `:deletion_policy`, `:metadata` and `:update_policy`.
# File lib/skywriter/resource.rb, line 48 def initialize(logical_name, options = {}) @logical_name = logical_name @additional_dependencies = Set.new(Array(delete_liberally(options, :additional_dependencies))) @deletion_policy = delete_liberally(options, :deletion_policy) @metadata = delete_liberally(options, :metadata) @update_policy = delete_liberally(options, :update_policy) @options = options.freeze end
Private Class Methods
# File lib/skywriter/resource.rb, line 143 def self.included(base) base.extend(DSL) end
Public Instance Methods
Sugar for using an attribute of this resource in another resource.
@example
i = EC2::Instance.new("myInstance", ....) dns = Route53::RecordSet.new("masterDNS", ResourceRecords: [{ i[:PublicDnsName] }], ...)
@param attribute_name [Symbol,String] The attribute the function
retrieves from the resource.
@return [Hash] A hash describing a GetAtt Function
# File lib/skywriter/resource.rb, line 126 def [](attribute_name) Function.get_att(logical_name, attribute_name.to_s) end
Returns a hash representing the Resource
@return [Hash] A JSON-able hash
# File lib/skywriter/resource.rb, line 63 def as_json(*) Thread.current[:skywriter_as_json_context] = self { logical_name => { 'Type' => type, 'Properties' => properties.as_json, 'DependsOn' => all_dependencies, 'Metadata' => metadata, 'DeletionPolicy' => deletion_policy, 'UpdatePolicy' => update_policy, }.reject { |key, value| value.nil? } } ensure Thread.current[:skywriter_as_json_context] = nil end
Returns a pointer to this Resource
Pointers represent references to resources defined elsewhere in a template. In most cases pointers serialize to something like
@example
{ "Ref" => "logical_name" }
In some cases, you might need to refer to a resource using its logical name directly (as opposed to nested in a hash as shown), in which case you can tell `as_pointer` to point with the resource's logical name:
@example
resource.as_pointer(with: :logical_name)
@param with [:ref, :logical_name] How this pointer should be
rendered in JSON. Use `:ref` to generate {"Ref": "foo"}, and `:logical_name` to generate "foo"
@return [Skywriter::Resource::Pointer] A pointer to this resource
# File lib/skywriter/resource.rb, line 102 def as_pointer(options = {}) with = options[:with] || :ref case with when :ref Skywriter::Resource::RefPointer.new(self) when :logical_name Skywriter::Resource::LogicalNamePointer.new(self) else raise ArgumentError, "Unrecognized 'with' value '#{with}'" end end
@api private
@param dependency [Skywriter::Resource] A Resource
upon which this resource depends.
# File lib/skywriter/resource.rb, line 134 def register_dependency(dependency) magical_dependencies << dependency.logical_name end
Private Instance Methods
# File lib/skywriter/resource.rb, line 147 def all_dependencies (additional_dependencies + magical_dependencies).to_a end
# File lib/skywriter/resource.rb, line 151 def delete_liberally(hash, key) hash.delete(key.to_sym) || hash.delete(key.to_s) || hash.delete(key.to_s.camelcase) || hash.delete(key.to_s.camelcase.to_sym) end
# File lib/skywriter/resource.rb, line 158 def magical_dependencies @magical_dependencies ||= Set.new end
# File lib/skywriter/resource.rb, line 162 def properties @properties ||= property_definitions.each_with_object({}) do |property_definition, hash| if (value = property_value(property_definition)) hash[property_definition.name] = value end end end
# File lib/skywriter/resource.rb, line 177 def property_definitions self.class.property_definitions end
# File lib/skywriter/resource.rb, line 170 def property_value(property_definition) options[property_definition.key.to_sym] || options[property_definition.key.to_s] || options[property_definition.name.to_sym] || options[property_definition.name.to_s] end