Class: CfnDsl::OrchestrationTemplate
- Inherits:
-
JSONable
- Object
- JSONable
- CfnDsl::OrchestrationTemplate
show all
- Defined in:
- lib/cfndsl/orchestration_template.rb
Overview
Handles the overall template object rubocop:disable Metrics/ClassLength
Defined Under Namespace
Classes: RefHash
Constant Summary
collapse
- GLOBAL_REFS =
{
'AWS::NotificationARNs' => 1,
'AWS::Region' => 1,
'AWS::StackId' => 1,
'AWS::StackName' => 1,
'AWS::AccountId' => 1,
'AWS::NoValue' => 1,
'AWS::URLSuffix' => 1,
'AWS::Partition' => 1
}.freeze
Constants included
from Functions
Functions::FN_SUB_SCANNER
Class Method Summary
collapse
Instance Method Summary
collapse
Methods inherited from JSONable
#as_json, #declare, #external_parameters, external_parameters, #ref_children, #to_json
Methods included from Functions
#FnAnd, #FnBase64, #FnCidr, #FnEquals, #FnFindInMap, #FnGetAZs, #FnGetAtt, #FnIf, #FnImportValue, #FnJoin, #FnNot, #FnOr, #FnSelect, #FnSplit, #FnSub, #Ref
Methods included from RefCheck
#build_references, #ref_children
Constructor Details
Returns a new instance of OrchestrationTemplate.
176
177
178
179
180
|
# File 'lib/cfndsl/orchestration_template.rb', line 176
def initialize(description = nil, &block)
@AWSTemplateFormatVersion = '2010-09-09'
@Description = description if description
declare(&block) if block_given?
end
|
Class Method Details
.create_array_property_def(resource, pname, pclass, info) ⇒ Object
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
# File 'lib/cfndsl/orchestration_template.rb', line 86
def create_array_property_def(resource, pname, pclass, info)
singular_name = CfnDsl::Plurals.singularize pname
plural_name = singular_name == pname ? CfnDsl::Plurals.pluralize(pname) : pname
if singular_name == plural_name
plural_name = nil
elsif pname == plural_name && info['Properties'].include?(singular_name)
singular_name = plural_name
plural_name = nil
elsif pname == singular_name && info['Properties'].include?(plural_name)
plural_name = singular_name
end
create_property_def(resource, pname, Array, plural_name) if plural_name
create_singular_property_def(resource, pname, pclass, singular_name) if singular_name
end
|
.create_resource_accessor(accessor, resource, type) ⇒ Object
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
# File 'lib/cfndsl/orchestration_template.rb', line 114
def create_resource_accessor(accessor, resource, type)
class_eval do
CfnDsl.method_names(accessor) do |method|
define_method(method) do |name, *values, &block|
name = name.to_s
@Resources ||= {}
instance = @Resources[name]
if !instance
instance = resource.new(*values)
instance.instance_variable_set('@Type', type)
@Resources[name] = instance
elsif type != (other_type = instance.instance_variable_get('@Type'))
raise ArgumentError, "Resource #{name}<#{other_type}> exists, and is not a <#{type}>"
elsif !values.empty?
raise ArgumentError, "wrong number of arguments (given #{values.size + 1}, expected 1) as Resource #{name} already exists"
end
@Resources[name].instance_eval(&block) if block
instance
end
end
end
end
|
.create_resource_def(name, info) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
# File 'lib/cfndsl/orchestration_template.rb', line 62
def create_resource_def(name, info)
resource = Class.new ResourceDefinition do
def Type(type = nil)
return @Type unless type
raise CfnDsl::Error, "Cannot override previously defined Type #{@Type} with #{type}" unless type == @Type
super
end
end
resource_name = name.gsub(/::/, '_')
type_module.const_set(resource_name, resource)
info['Properties'].each_pair do |pname, ptype|
if ptype.is_a? Array
pclass = type_module.const_get ptype.first
create_array_property_def(resource, pname, pclass, info)
else
pclass = type_module.const_get ptype
create_property_def(resource, pname, pclass)
end
end
resource_name
end
|
.create_types ⇒ Object
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/cfndsl/orchestration_template.rb', line 40
def create_types
accessors = {}
types_mapping = {}
template_types['Resources'].each_pair do |resource, info|
resource_name = create_resource_def(resource, info)
parts = resource.split('::')
until parts.empty?
break if CfnDsl.reserved_items.include? parts.first
abreve_name = parts.join('_')
if accessors.key? abreve_name
accessors[abreve_name] = :duplicate else
accessors[abreve_name] = type_module.const_get resource_name
types_mapping[abreve_name] = resource
end
parts.shift
end
end
accessors.each_pair { |acc, res| create_resource_accessor(acc, res, types_mapping[acc]) unless res == :duplicate }
end
|
Instance Method Details
#_check_refs(container_name, method, source_containers) ⇒ Object
rubocop:disable Metrics/CyclomaticComplexity
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
# File 'lib/cfndsl/orchestration_template.rb', line 247
def _check_refs(container_name, method, source_containers)
container = instance_variable_get("@#{container_name}s")
return [] unless container
invalids = []
referred_by = RefHash.new { |h, k| h[k] = [] }
self_check = source_containers.first.eql?(container)
container.each_pair do |name, entry|
name = name.to_s
begin
refs = entry.build_references([], self_check && name, method)
refs.each { |r| referred_by[r.to_s] << name }
rescue RefCheck::SelfReference, RefCheck::NullReference => e
invalids.push("#{container_name} #{e.message}")
end
end
referred_by.each_pair do |ref, names|
unless valid_ref?(ref, source_containers)
invalids.push "Invalid Reference: #{container_name}s #{names} refer to unknown #{method == :condition_refs ? 'Condition' : 'Reference'} #{ref}"
end
end
begin
referred_by.tsort if self_check && invalids.empty? rescue TSort::Cyclic => e
invalids.push "Cyclic references found in #{container_name}s #{referred_by} - #{e.message}"
end
invalids
end
|
#_Condition ⇒ Object
182
|
# File 'lib/cfndsl/orchestration_template.rb', line 182
alias _Condition Condition
|
#check_condition_refs ⇒ Object
205
206
207
208
209
210
211
212
213
|
# File 'lib/cfndsl/orchestration_template.rb', line 205
def check_condition_refs
invalids = []
invalids.concat(_check_refs(:Condition, :condition_refs, [@Conditions]))
invalids.concat(_check_refs(:Condition, :all_refs, [GLOBAL_REFS, @Parameters]))
end
|
#check_output_refs ⇒ Object
223
224
225
226
227
|
# File 'lib/cfndsl/orchestration_template.rb', line 223
def check_output_refs
invalids = []
invalids.concat(_check_refs(:Output, :all_refs, [@Resources, GLOBAL_REFS, @Parameters]))
invalids.concat(_check_refs(:Output, :condition_refs, [@Conditions]))
end
|
#check_refs ⇒ Object
195
196
197
198
|
# File 'lib/cfndsl/orchestration_template.rb', line 195
def check_refs
invalids = check_condition_refs + check_resource_refs + check_output_refs + check_rule_refs
invalids unless invalids.empty?
end
|
#check_resource_refs ⇒ Object
215
216
217
218
219
220
221
|
# File 'lib/cfndsl/orchestration_template.rb', line 215
def check_resource_refs
invalids = []
invalids.concat(_check_refs(:Resource, :all_refs, [@Resources, GLOBAL_REFS, @Parameters]))
invalids.concat(_check_refs(:Resource, :condition_refs, [@Conditions]))
end
|
#check_rule_refs ⇒ Object
229
230
231
232
233
234
|
# File 'lib/cfndsl/orchestration_template.rb', line 229
def check_rule_refs
invalids = []
invalids.concat(_check_refs(:Rule, :all_refs, [@Resources, GLOBAL_REFS, @Parameters]))
invalids.concat(_check_refs(:Rule, :condition_refs, [@Conditions]))
invalids
end
|
#Condition(name, expression) ⇒ Object
#Condition(name) ⇒ Object
Condition has two usages at this level
187
188
189
190
191
192
193
|
# File 'lib/cfndsl/orchestration_template.rb', line 187
def Condition(name, expression = nil)
if expression
_Condition(name, expression)
else
{ Condition: ConditionDefinition.new(name) }
end
end
|
#valid_ref?(ref, ref_containers = [GLOBAL_REFS, @Resources, @Parameters]) ⇒ Boolean
200
201
202
203
|
# File 'lib/cfndsl/orchestration_template.rb', line 200
def valid_ref?(ref, ref_containers = [GLOBAL_REFS, @Resources, @Parameters])
ref = ref.to_s
ref_containers.any? { |c| c && c.key?(ref) }
end
|
#validate ⇒ Object
rubocop:enable Metrics/CyclomaticComplexity
282
283
284
285
286
287
|
# File 'lib/cfndsl/orchestration_template.rb', line 282
def validate
errors = check_refs || []
raise CfnDsl::Error, "#{errors.size} errors in template\n#{errors.join("\n")}" unless errors.empty?
self
end
|