momo 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 61d24aa3bc8b813807f31c3180fec6176ccbe55c
4
- data.tar.gz: a3aceaa9e405a1c969c18a5b7382c6210f589d99
3
+ metadata.gz: befe622c1ebf025fb049faf7fd4ac45599983c2e
4
+ data.tar.gz: e86b895df8edd1fb0314055fc30c767ba23f7393
5
5
  SHA512:
6
- metadata.gz: 5631dd9f8217a5a837bb51f09d76cfea9047871c993e31da9cf0ea780e099576d51a25351623d9c126f0ca40ca95089614ca0d0cc581250021550b668d7fd7a1
7
- data.tar.gz: 87555420670fc44dc2c0f41af18fe4e02a71ae4b96a8195c1ce3c568d24b7d1e41375fb1262463e9f3d6a3ae4e05974e6af4cf01a2230f19b617e4bbb6c9dae2
6
+ metadata.gz: ce1f018848a93e6170a89971ab7c18700305d12a80368446e5e620d5200fe69044bfe0c5ca6dd80f001eb592e566ad85ec6fc5c7db0c8fa961753c8f99a823a0
7
+ data.tar.gz: de168af0051e6894ea89b4ded43fb78982334e2b1f8efb4dd66f3b794ef70b6d6279f46304b2fbedb1eba5e74f1561a424b7cc97da8a9b59c8bb54fffe66ad49
data/lib/momo/cfl.rb CHANGED
@@ -14,6 +14,7 @@ module Momo
14
14
 
15
15
  template["Mappings"] = @stack.templatize_mappings
16
16
  template["Resources"] = @stack.templatize_resources
17
+ template["Conditions"] = @stack.templatize_conditions
17
18
  template["Parameters"] = @stack.templatize_params if @stack.parameters.length > 0
18
19
  template["Outputs"] = @stack.templatize_outputs if @stack.outputs.length > 0
19
20
 
data/lib/momo/funccall.rb CHANGED
@@ -1,11 +1,15 @@
1
1
 
2
2
  module Momo
3
3
  class FuncCall
4
- def initialize(name, *args)
4
+ def initialize(name, stack, *args)
5
5
  @name = name
6
6
  @args = []
7
+ @stack = stack
8
+ if !stack.is_a? Stack
9
+ raise "#{stack.inspect} is not a stack"
10
+ end
7
11
  args.each do |arg|
8
- @args << Momo.resolve(arg)
12
+ @args << Momo.resolve(arg, stack: stack)
9
13
  end
10
14
  if @args.length == 1
11
15
  @args = @args[0]
@@ -1,8 +1,9 @@
1
+ require "momo/funccall"
1
2
 
2
3
  module Momo
3
4
 
4
5
  def Momo.resolve(something, options={})
5
-
6
+
6
7
  if something.is_a? String or
7
8
  something.is_a? TrueClass or
8
9
  something.is_a? FalseClass or
@@ -11,19 +12,22 @@ module Momo
11
12
  elsif something.is_a? Array
12
13
  result = []
13
14
  something.each do |elem|
14
- result << Momo.resolve(elem)
15
+ result << Momo.resolve(elem, options)
15
16
  end
16
17
  return result
17
18
  elsif something.is_a? Hash
18
19
  result = {}
19
20
  something.each do |key, value|
20
- result[key] = Momo.resolve(value)
21
+ result[key] = Momo.resolve(value, options)
21
22
  end
22
23
  return result
23
24
  elsif something.is_a? Resource
24
25
  return { "Ref" => something.name }
25
26
  elsif something.is_a? Reference
26
27
  return something.representation
28
+ elsif something.is_a? BooleanValue
29
+ options[:stack].conditions[something.signature] = something.representation
30
+ return { "Condition" => something.signature }
27
31
  elsif something.is_a? FuncCall
28
32
  return something.representation
29
33
  elsif something.is_a? Parameter
@@ -34,9 +38,8 @@ module Momo
34
38
  end
35
39
 
36
40
  class MomoScope
37
-
38
41
  def call(name, *args)
39
- FuncCall.new(name, *args)
42
+ FuncCall.new(name, @stack, *args)
40
43
  end
41
44
 
42
45
  def ref(resource)
@@ -47,4 +50,53 @@ module Momo
47
50
  call("Fn::FindInMap", map_name, key, item)
48
51
  end
49
52
  end
50
- end
53
+
54
+ class BooleanValue < FuncCall
55
+
56
+ def initialize(operator, stack, *args)
57
+ super(operator, stack, args)
58
+ end
59
+
60
+ def not()
61
+ BooleanValue.new("Fn::Not", @stack, self)
62
+ end
63
+
64
+ def signature_of(something)
65
+ if something.is_a? String or
66
+ something.is_a? TrueClass or
67
+ something.is_a? FalseClass or
68
+ something.is_a? Numeric
69
+ return something
70
+ elsif something.is_a? Hash
71
+ return something["Ref"] || something["Condition"]
72
+ elsif something.is_a? BooleanValue
73
+ return something.signature
74
+ elsif something.is_a? Resource
75
+ return something.name
76
+ end
77
+ end
78
+
79
+ def signature()
80
+ match = /Fn::(?<name>[a-z]+)/i.match(@name)
81
+ "#{signature_of(@args[0])}#{match[:name]}#{signature_of(@args[1])}"
82
+ end
83
+
84
+ end
85
+
86
+ module MomoCondition
87
+
88
+ def equals(another)
89
+ BooleanValue.new("Fn::Equals", @stack, self, another)
90
+ end
91
+
92
+ def and(another)
93
+ BooleanValue.new("Fn::And", @stack, self, another)
94
+ end
95
+
96
+ def or(another)
97
+ BooleanValue.new("Fn::Or", @stack, self, another)
98
+ end
99
+
100
+ end
101
+
102
+ end
@@ -3,8 +3,11 @@ module Momo
3
3
  class Parameter
4
4
  attr_accessor :name, :options
5
5
 
6
- def initialize(name, options={})
6
+ include MomoCondition
7
+
8
+ def initialize(name, stack, options={})
7
9
  @name = name
10
+ @stack = stack
8
11
  @options = options
9
12
  end
10
13
  end
data/lib/momo/resource.rb CHANGED
@@ -2,16 +2,20 @@ module Momo
2
2
 
3
3
  class Resource < MomoScope
4
4
 
5
- attr_accessor :type, :name, :props, :metadata, :dependencies, :deletion_policy
5
+ include MomoCondition
6
6
 
7
- def initialize(type, name)
7
+ attr_accessor :type, :name, :props, :metadata, :condition, :dependencies, :deletion_policy
8
+
9
+ def initialize(type, name, stack)
8
10
  @type = type
9
11
  @name = name
10
12
  @metadata = nil
11
13
  @props = {}
14
+ @condition = nil
12
15
  @dependencies = []
13
16
  @complete = false
14
17
  @deletion_policy = nil
18
+ @stack = stack
15
19
  end
16
20
 
17
21
  def method_missing(name, *args, &block)
@@ -21,7 +25,7 @@ module Momo
21
25
  end
22
26
 
23
27
  if !@complete
24
- @props[name] = Momo.resolve(args[0], resource: name)
28
+ @props[name] = Momo.resolve(args[0], resource: name, stack: @stack)
25
29
  else
26
30
  MemberReference.new @name, name
27
31
  end
@@ -126,7 +130,7 @@ module Momo
126
130
  info["group"] = options[:group] if options[:group]
127
131
  info["mode"] = options[:mode] if options[:mode]
128
132
  info["owner"] = options[:owner] if options[:owner]
129
- info["context"] = Momo.resolve(options[:context]) if options[:context]
133
+ info["context"] = Momo.resolve(options[:context], stack: @stack, resource: name) if options[:context]
130
134
 
131
135
  set_thing2("files", name, info)
132
136
  end
@@ -156,6 +160,5 @@ module Momo
156
160
  raise "Invalid argument to depends_on: #{resource[0]}"
157
161
  end
158
162
  end
159
-
160
163
  end
161
164
  end
data/lib/momo/stack.rb CHANGED
@@ -4,7 +4,7 @@ module Momo
4
4
 
5
5
  class Stack < MomoScope
6
6
 
7
- attr_accessor :resources, :parameters, :outputs
7
+ attr_accessor :resources, :parameters, :conditions, :outputs
8
8
 
9
9
  def initialize(&block)
10
10
  raise "Stack expects a block" unless block
@@ -14,6 +14,8 @@ module Momo
14
14
  @parameters = {}
15
15
  @outputs = {}
16
16
  @mappings = {}
17
+ @conditions = {}
18
+ @stack = self
17
19
 
18
20
  @names = {}
19
21
 
@@ -33,7 +35,7 @@ module Momo
33
35
  @description
34
36
  end
35
37
  end
36
-
38
+
37
39
  def make_default_resource_name (type)
38
40
  match = /\:?\:?([a-zA-Z]+)$/.match(type)
39
41
  name = match.captures[0]
@@ -49,18 +51,31 @@ module Momo
49
51
 
50
52
  def make_random_string
51
53
  id = ""
52
- loop do
54
+ loop do
53
55
  o = [('A'..'Z'), ('0'..'9')].map { |i| i.to_a }.flatten
54
56
  id = (1...20).map { o[rand(o.length)] }.join
55
57
  break if !@ids.has_key?(id)
56
- end
58
+ end
57
59
 
58
60
  @ids[id] = true
59
61
  id
60
62
  end
61
63
 
62
64
  def param(name, options={})
63
- @parameters[name] = Parameter.new(name, options)
65
+ @parameters[name] = Parameter.new(name, self, options)
66
+ end
67
+
68
+ def condition(cond_expr, &block)
69
+ Momo.resolve(cond_expr, stack: self, resource: cond_expr.signature)
70
+ previous_condition = @current_condition
71
+
72
+ if previous_condition
73
+ cond_expr = BooleanValue.new("Fn::And", self, {"Condition" => previous_condition}, {"Condition" => cond_expr.signature})
74
+ Momo.resolve(cond_expr, stack: self, resource: cond_expr.signature)
75
+ end
76
+ @current_condition = cond_expr.signature
77
+ instance_eval(&block)
78
+ @current_condition = previous_condition
64
79
  end
65
80
 
66
81
  def make(type, options = {}, &block)
@@ -68,7 +83,8 @@ module Momo
68
83
  name = options[:name]
69
84
  name = make_default_resource_name(type) if !name
70
85
 
71
- resource = Resource.new(type, name)
86
+ resource = Resource.new(type, name, self)
87
+ resource.condition = @current_condition
72
88
  resource.instance_eval(&block) if block
73
89
  resource.complete!
74
90
 
@@ -78,7 +94,7 @@ module Momo
78
94
  end
79
95
 
80
96
  def output(name, res)
81
- @outputs[name] = Momo.resolve(res)
97
+ @outputs[name] = Momo.resolve(res, stack: self)
82
98
  end
83
99
 
84
100
  def mapping(name, &block)
@@ -89,6 +105,10 @@ module Momo
89
105
  @mappings
90
106
  end
91
107
 
108
+ def templatize_conditions
109
+ @conditions
110
+ end
111
+
92
112
  def templatize_resources
93
113
  temp = {}
94
114
  @resources.each do |name, res|
@@ -101,6 +121,10 @@ module Momo
101
121
  temp[name]["Metadata"] = res.metadata
102
122
  end
103
123
 
124
+ if res.condition
125
+ temp[name]["Condition"] = res.condition
126
+ end
127
+
104
128
  if res.dependencies.length != 0
105
129
  temp[name]["DependsOn"] = res.dependencies
106
130
  end
data/lib/momo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Momo
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: momo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Siaw
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-07-28 00:00:00.000000000 Z
11
+ date: 2016-08-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport