Class: Momo::Stack

Inherits:
MomoScope show all
Defined in:
lib/momo/stack.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from MomoScope

#call, #lookup, #ref

Constructor Details

#initialize(&block) ⇒ Stack

Returns a new instance of Stack.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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

Instance Attribute Details

#conditionsObject

Returns the value of attribute conditions.



7
8
9
# File 'lib/momo/stack.rb', line 7

def conditions
  @conditions
end

#outputsObject

Returns the value of attribute outputs.



7
8
9
# File 'lib/momo/stack.rb', line 7

def outputs
  @outputs
end

#parametersObject

Returns the value of attribute parameters.



7
8
9
# File 'lib/momo/stack.rb', line 7

def parameters
  @parameters
end

#resourcesObject

Returns the value of attribute resources.



7
8
9
# File 'lib/momo/stack.rb', line 7

def resources
  @resources
end

Instance Method Details

#condition(cond_expr, &block) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Object



31
32
33
34
35
36
37
# 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) ⇒ Object



26
27
28
29
# 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) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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_stringObject



55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Object



103
104
105
# File 'lib/momo/stack.rb', line 103

def mapping(name, &block)
  @mappings[name] = Mapping.new(&block).major_keys
end

#output(name, res) ⇒ Object



99
100
101
# File 'lib/momo/stack.rb', line 99

def output(name, res)
  @outputs[name] = Momo.resolve(res, stack: self)
end

#param(name, options = {}) ⇒ Object



67
68
69
# File 'lib/momo/stack.rb', line 67

def param(name, options={})
  @parameters[name] = Parameter.new(name, self, options)
end

#templatize_conditionsObject



111
112
113
# File 'lib/momo/stack.rb', line 111

def templatize_conditions
  @conditions
end

#templatize_mappingsObject



107
108
109
# File 'lib/momo/stack.rb', line 107

def templatize_mappings
  @mappings
end

#templatize_outputsObject



142
143
144
145
146
147
148
# File 'lib/momo/stack.rb', line 142

def templatize_outputs
  temp = {}
  @outputs.each do |name, res|
    temp[name] = {"Value" => res}
  end
  temp
end

#templatize_paramsObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# 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_resourcesObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# 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.
      temp[name]["Metadata"] = res.
    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