cumuliform 0.5.1 → 0.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,220 +0,0 @@
1
- require_relative 'error'
2
-
3
- module Cumuliform
4
- module Functions
5
- # implements wrappers for the intrinsic functions Fn::*
6
- class IntrinsicFunctions
7
- attr_reader :template
8
-
9
- # @api private
10
- def initialize(template)
11
- @template = template
12
- end
13
-
14
- # Wraps Fn::FindInMap
15
- #
16
- # see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-findinmap.html
17
- #
18
- # @param mapping_logical_id [String] The logical ID of the mapping we
19
- # want to look up a value from
20
- # @param level_1_key [String] Key 1
21
- # @param level_2_key [String] Key 2
22
- # @return [Hash] the Fn::FindInMap object
23
- def find_in_map(mapping_logical_id, level_1_key, level_2_key)
24
- template.verify_mapping_logical_id!(mapping_logical_id)
25
- {"Fn::FindInMap" => [mapping_logical_id, level_1_key, level_2_key]}
26
- end
27
-
28
- # Wraps Fn::GetAtt
29
- #
30
- # see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-getatt.html
31
- #
32
- # @param resource_logical_id [String] The Logical ID of resource we want
33
- # to get an attribute of
34
- # @param attr_name [String] The name of the attribute to get the value of
35
- # @return [Hash] the Fn::GetAtt object
36
- def get_att(resource_logical_id, attr_name)
37
- template.verify_resource_logical_id!(resource_logical_id)
38
- {"Fn::GetAtt" => [resource_logical_id, attr_name]}
39
- end
40
-
41
- # Wraps Fn::Join
42
- #
43
- # see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-join.html
44
- #
45
- # @param separator [String] The separator string to join the array
46
- # elements with
47
- # @param args [Array<String>] The array of strings to join
48
- # @return [Hash] the Fn::Join object
49
- def join(separator, args)
50
- raise ArgumentError, "Second argument must be an Array" unless args.is_a?(Array)
51
- {"Fn::Join" => [separator, args]}
52
- end
53
-
54
- # Wraps Fn::Base64
55
- #
56
- # see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-base64.html
57
- #
58
- # The argument should either be a string or an intrinsic function that
59
- # evaluates to a string when CloudFormation executes the template
60
- #
61
- # see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-base64.html
62
- #
63
- # @param value [String, Hash<string-returning instrinsic function>]
64
- # The separator string to join the array elements with
65
- # @return [Hash] the Fn::Base64 object
66
- def base64(value)
67
- {"Fn::Base64" => value}
68
- end
69
-
70
- # Wraps Fn::GetAZs
71
- #
72
- # CloudFormation evaluates this to an array of availability zone names.
73
- #
74
- # see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-getavailabilityzones.html
75
- # @param value [String, Hash<ref('AWS::Region')>] The AWS region to get
76
- # the array of Availability Zones of. Empty string (the default) is
77
- # equivalent to specifying `ref('AWS::Region')` which evaluates to the
78
- # region the stack is being created in
79
- def get_azs(value = "")
80
- {"Fn::GetAZs" => value}
81
- end
82
-
83
- # Wraps Fn::Equals
84
- #
85
- # see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html#d0e86148
86
- #
87
- # The arguments should be the literal values or refs you want to
88
- # compare. Returns true or false when CloudFormation evaluates the
89
- # template.
90
- # @param value [String, Hash<value-returning ref>]
91
- # @param other_value [String, Hash<value-returning ref>]
92
- # @return [Hash] the Fn::Equals object
93
- def equals(value, other_value)
94
- {"Fn::Equals" => [value, other_value]}
95
- end
96
-
97
- # Wraps Fn::If
98
- #
99
- # see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html#d0e86223
100
- #
101
- # CloudFormation evaluates the Condition referred to the logical ID in
102
- # the <tt>condition</tt> arg and returns the <tt>true_value</tt> if
103
- # <tt>true</tt> and <tt>false_value</tt> otherwise. <tt>condition</tt>
104
- # cannot be an <tt>Fn::Ref</tt>, but you can use our <tt>xref()</tt>
105
- # helper to ensure the logical ID is valid.
106
- #
107
- # @param condition[String] the Logical ID of the Condition to be checked
108
- # @param true_value the value to be returned if <tt>condition</tt>
109
- # evaluates true
110
- # @param false_value the value to be returned if <tt>condition</tt>
111
- # evaluates false
112
- # @return [Hash] the Fn::If object
113
- def if(condition, true_value, false_value)
114
- {"Fn::If" => [condition, true_value, false_value]}
115
- end
116
-
117
- # Wraps Fn::Select
118
- #
119
- # see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-select.html
120
- #
121
- # CloudFormation evaluates the <tt>index</tt> (which can be an
122
- # integer-as-a-string or a <tt>ref</tt> which evaluates to a number) and
123
- # returns the corresponding item from the array (which can be an array
124
- # literal, or the result of <tt>Fn::GetAZs</tt>, or one of
125
- # <tt>Fn::GetAtt</tt>, <tt>Fn::If</tt>, and <tt>Ref</tt> (if they would
126
- # return an Array).
127
- #
128
- # @param index [Integer, Hash<value-returning ref>] The index to
129
- # retrieve from <tt>array</tt>
130
- # @param array [Array, Hash<array-returning ref of intrinsic function>]
131
- # The array to retrieve from
132
- def select(index, array)
133
- ref_style_index = index.is_a?(Hash) && index.has_key?("Fn::Ref")
134
- positive_int_style_index = index.is_a?(Integer) && index >= 0
135
- unless ref_style_index || positive_int_style_index
136
- raise ArgumentError, "index must be a positive integer or Fn::Ref"
137
- end
138
- if positive_int_style_index
139
- if array.is_a?(Array) && index >= array.length
140
- raise IndexError, "index must be in the range 0 <= index < array.length"
141
- end
142
- index = index.to_s
143
- end
144
- {"Fn::Select" => [index, array]}
145
- end
146
-
147
- # Wraps Fn::And
148
- #
149
- # see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html#d0e86066
150
- #
151
- # Behaves as a logical AND operator for CloudFormation conditions. Arguments should be other conditions or things that will evaluate to <tt>true</tt> or <tt>false</tt>.
152
- #
153
- # @param condition_1 [Hash<boolean-returning ref, intrinsic function, or condition>] Condition / value to be ANDed
154
- # @param condition_n [Hash<boolean-returning ref, intrinsic function, or condition>] Condition / value to be ANDed (min 2, max 10 condition args)
155
- def and(*conditions)
156
- unless (2..10).cover?(conditions.length)
157
- raise ArgumentError, "You must specify AT LEAST 2 and AT MOST 10 conditions"
158
- end
159
- {"Fn::And" => conditions}
160
- end
161
-
162
- # Wraps Fn::Or
163
- #
164
- # see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html#d0e86490
165
- #
166
- # Behaves as a logical OR operator for CloudFormation conditions. Arguments should be other conditions or things that will evaluate to <tt>true</tt> or <tt>false</tt>.
167
- #
168
- # @param condition_1 [Hash<boolean-returning ref, intrinsic function, or condition>] Condition / value to be ORed
169
- # @param condition_n [Hash<boolean-returning ref, intrinsic function, or condition>] Condition / value to be ORed (min 2, max 10 condition args)
170
- def or(*conditions)
171
- unless (2..10).cover?(conditions.length)
172
- raise ArgumentError, "You must specify AT LEAST 2 and AT MOST 10 conditions"
173
- end
174
- {"Fn::Or" => conditions}
175
- end
176
-
177
- # Wraps Fn::Not
178
- #
179
- # see http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/intrinsic-function-reference-conditions.html#d0e86402
180
- #
181
- # Behaves as a logical NOT operator for CloudFormation conditions. The argument should be another condition or something that will evaluate to <tt>true</tt> or <tt>false</tt>
182
- # @param condition [Hash<boolean-returning ref, intrinsic function, or condition>] Condition / value to be NOTed
183
- def not(condition)
184
- {"Fn::Not" => [condition]}
185
- end
186
- end
187
-
188
- # Checks <tt>logical_id</tt> is present and either returns <tt>logical_id</tt> or raises
189
- # Cumuliform::Error::NoSuchLogicalId.
190
- #
191
- # You can use it anywhere you need a string Logical ID and want the
192
- # protection of having it be verified, for example in the <tt>cfn-init</tt>
193
- # invocation in a Cfn::Init metadata block or the condition name field
194
- # of, e.g. Fn::And.
195
- #
196
- # @param logical_id [String] the logical ID you want to check
197
- # @return [String] the logical_id param
198
- def xref(logical_id)
199
- unless has_logical_id?(logical_id)
200
- raise Error::NoSuchLogicalId, logical_id
201
- end
202
- logical_id
203
- end
204
-
205
- # Wraps Ref
206
- #
207
- # CloudFormation evaluates the <tt>Ref</tt> and returns the value of the Parameter or Resource with Logical ID <tt>logical_id</tt>.
208
- #
209
- # @param logical_id [String] The logical ID of the parameter or resource
210
- def ref(logical_id)
211
- {"Ref" => xref(logical_id)}
212
- end
213
-
214
- # returns an instance of IntrinsicFunctions which provides wrappers for
215
- # Fn::* functions
216
- def fn
217
- @fn ||= IntrinsicFunctions.new(self)
218
- end
219
- end
220
- end