class Momo::Stack

Attributes

conditions[RW]
outputs[RW]
parameters[RW]
resources[RW]

Public Class Methods

new(&block) click to toggle source
# File lib/momo/stack.rb, line 9
def initialize(&block)
        raise "Stack expects a block" unless block

        @description = "No description"
        @resources = {}
        @parameters = {}
        @outputs = {}
        @mappings = {}
        @conditions = {}
        @stack = self

        @names = {}

        @ids = {}
        instance_eval(&block)
end

Public Instance Methods

condition(cond_expr, &block) click to toggle source
# File lib/momo/stack.rb, line 71
def condition(cond_expr, &block)
        Momo.resolve(cond_expr, stack: self, resource: cond_expr.signature)
        previous_condition = @current_condition

        if previous_condition
                cond_expr = BooleanValue.new("Fn::And", self, {"Condition" => previous_condition}, {"Condition" => cond_expr.signature})
                Momo.resolve(cond_expr, stack: self, resource: cond_expr.signature)
        end
        @current_condition = cond_expr.signature
        instance_eval(&block)
        @current_condition = previous_condition
end
description(*args) click to toggle source
# File lib/momo/stack.rb, line 31
def description(*args)
        if (args.length == 1)
                @description = args[0]
        else
                @description
        end
end
inject(module_object=nil, &block) click to toggle source
# File lib/momo/stack.rb, line 26
def inject(module_object=nil, &block)
        extend module_object if module_object
        instance_eval &block if block
end
make(type, options = {}, &block) click to toggle source
# File lib/momo/stack.rb, line 84
def make(type, options = {}, &block)

        name = options[:name]
        name = make_default_resource_name(type) if !name

        resource = Resource.new(type, name, self)
        resource.condition = @current_condition
        resource.instance_eval(&block) if block
        resource.complete!

        raise "Resource #{name} already exists" if @resources.has_key? resource.name
        @resources[resource.name] = resource
        resource
end
make_default_resource_name(type) click to toggle source
# File lib/momo/stack.rb, line 39
def make_default_resource_name (type)
        match = /\:?\:?([a-zA-Z0-9]+)$/.match(type)
        if match == nil
                raise "Invalid resource name: #{type}"
        end
        name = match.captures[0]

        if !@names[name]
                @names[name] = 1
        else
                @names[name] += 1
        end

        "#{name}#{@names[name]}"
end
make_random_string() click to toggle source
# File lib/momo/stack.rb, line 55
def make_random_string
        id = ""
        loop do
                o = [('A'..'Z'), ('0'..'9')].map { |i| i.to_a }.flatten
                id = (1...20).map { o[rand(o.length)] }.join
                break if !@ids.has_key?(id)
        end

        @ids[id] = true
        id
end
mapping(name, &block) click to toggle source
# File lib/momo/stack.rb, line 103
def mapping(name, &block)
        @mappings[name] = Mapping.new(&block).major_keys
end
output(name, res) click to toggle source
# File lib/momo/stack.rb, line 99
def output(name, res)
        @outputs[name] = Momo.resolve(res, stack: self)
end
param(name, options={}) click to toggle source
# File lib/momo/stack.rb, line 67
def param(name, options={})
        @parameters[name] = Parameter.new(name, self, options)
end
templatize_conditions() click to toggle source
# File lib/momo/stack.rb, line 111
def templatize_conditions
        @conditions
end
templatize_mappings() click to toggle source
# File lib/momo/stack.rb, line 107
def templatize_mappings
        @mappings
end
templatize_outputs() click to toggle source
# File lib/momo/stack.rb, line 142
def templatize_outputs
        temp = {}
        @outputs.each do |name, res|
                temp[name] = {"Value" => res}
        end
        temp
end
templatize_params() click to toggle source
# File lib/momo/stack.rb, line 150
def templatize_params
        temp = {}
        @parameters.each do |name, param|
                typeConv = {
                        string: "String",
                        number: "Number",
                        list: "List"
                }

                temp[name] = {"Type" => typeConv[param.options[:type]]}
                temp[name]["NoEcho"] = true unless param.options[:no_echo] == false
                temp[name]["Description"] = param.options[:description] if param.options.has_key? :description
                temp[name]["Default"] = param.options[:default] if param.options.has_key? :default
                temp[name]["AllowedValues"] = param.options[:allowed] if param.options.has_key? :allowed
        end
        temp
end
templatize_resources() click to toggle source
# File lib/momo/stack.rb, line 115
def templatize_resources
        temp = {}
        @resources.each do |name, res|
                temp[name] = {"Type" => res.type, "Properties" => {}}
                res.props.each do |propname, prop|
                        temp[name]["Properties"][propname] = prop
                end

                if res.metadata
                        temp[name]["Metadata"] = res.metadata
                end

                if res.condition
                        temp[name]["Condition"] = res.condition
                end

                if res.dependencies.length != 0
                        temp[name]["DependsOn"] = res.dependencies
                end
                
                if res.deletion_policy
                        temp[name]["DeletionPolicy"] = res.deletion_policy
                end
        end
        temp
end