Class: CfnDsl::OrchestrationTemplate

Inherits:
JSONable
  • Object
show all
Defined in:
lib/cfndsl/orchestration_template.rb

Overview

Handles the overall template object rubocop:disable Metrics/ClassLength

Direct Known Subclasses

CloudFormationTemplate, HeatTemplate

Constant Summary collapse

GLOBAL_REFS =
{
  'AWS::NotificationARNs' => 1,
  'AWS::Region' => 1,
  'AWS::StackId' => 1,
  'AWS::StackName' => 1,
  'AWS::AccountId' => 1,
  'AWS::NoValue' => 1
}.freeze

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, #FnFormat, #FnGetAZs, #FnGetAtt, #FnIf, #FnImportValue, #FnJoin, #FnNot, #FnOr, #FnSelect, #FnSplit, #FnSub, #Ref

Methods included from RefCheck

#build_references, #ref_children

Constructor Details

#initializeOrchestrationTemplate

Returns a new instance of OrchestrationTemplate.



113
114
115
# File 'lib/cfndsl/orchestration_template.rb', line 113

def initialize
  @AWSTemplateFormatVersion = '2010-09-09'
end

Class Method Details

.create_array_property_def(resource, pname, pclass) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/cfndsl/orchestration_template.rb', line 76

def create_array_property_def(resource, pname, pclass)
  create_property_def(resource, pname, Array)

  sname = CfnDsl::Plurals.singularize pname

  return if sname == pname

  resource.class_eval do
    CfnDsl.method_names(sname) do |method|
      define_method(method) do |value = nil, &block|
        @Properties ||= {}
        @Properties[pname] ||= PropertyDefinition.new([])
        value ||= pclass.new
        @Properties[pname].value.push value
        value.instance_eval(&block) if block
        value
      end
    end
  end
end

.create_property_def(resource, pname, pclass) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/cfndsl/orchestration_template.rb', line 62

def create_property_def(resource, pname, pclass)
  resource.class_eval do
    CfnDsl.method_names(pname) do |method|
      define_method(method) do |*values, &block|
        values.push pclass.new if values.empty?
        @Properties ||= {}
        @Properties[pname] = PropertyDefinition.new(*values)
        @Properties[pname].value.instance_eval(&block) if block
        @Properties[pname].value
      end
    end
  end
end

.create_resource_accessor(accessor, resource, type) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/cfndsl/orchestration_template.rb', line 97

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 ||= {}
        @Resources[name] ||= resource.new(*values)
        @Resources[name].instance_eval(&block) if block
        @Resources[name].instance_variable_set('@Type', type)
        @Resources[name]
      end
    end
  end
end

.create_resource_def(name, info) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cfndsl/orchestration_template.rb', line 46

def create_resource_def(name, info)
  resource = Class.new ResourceDefinition
  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)
    else
      pclass = type_module.const_get ptype
      create_property_def(resource, pname, pclass)
    end
  end
  resource_name
end

.create_typesObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/cfndsl/orchestration_template.rb', line 24

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.delete abreve_name # Delete potentially ambiguous names
      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]) }
end

Instance Method Details

#check_output_refsObject



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/cfndsl/orchestration_template.rb', line 153

def check_output_refs
  invalids = []
  output_refs = {}
  if @Outputs
    @Outputs.each_key do |resource|
      output_refs[resource.to_s] = @Outputs[resource].build_references({})
    end
    output_refs.each_key do |origin|
      output_refs[origin].each_key do |ref|
        invalids.push "Invalid Reference: Output #{origin} refers to #{ref}" unless valid_ref?(ref)
      end
    end
  end
  invalids
end

#check_refsObject

rubocop:enable Metrics/PerceivedComplexity



132
133
134
135
# File 'lib/cfndsl/orchestration_template.rb', line 132

def check_refs
  invalids = check_resource_refs + check_output_refs
  invalids unless invalids.empty?
end

#check_resource_refsObject



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/cfndsl/orchestration_template.rb', line 137

def check_resource_refs
  invalids = []
  @_resource_refs = {}
  if @Resources
    @Resources.each_key do |resource|
      @_resource_refs[resource.to_s] = @Resources[resource].build_references({})
    end
    @_resource_refs.each_key do |origin|
      @_resource_refs[origin].each_key do |ref|
        invalids.push "Invalid Reference: Resource #{origin} refers to #{ref}" unless valid_ref?(ref, origin)
      end
    end
  end
  invalids
end

#valid_ref?(ref, origin = nil) ⇒ Boolean

rubocop:disable Metrics/PerceivedComplexity

Returns:

  • (Boolean)


118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/cfndsl/orchestration_template.rb', line 118

def valid_ref?(ref, origin = nil)
  ref = ref.to_s
  origin = origin.to_s if origin

  return true if GLOBAL_REFS.key?(ref)

  return true if @Parameters && @Parameters.key?(ref)

  return !origin || !@_resource_refs || !@_resource_refs[ref] || !@_resource_refs[ref].key?(origin) if @Resources.key?(ref)

  false
end