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

GlobalRefs =
{
  '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, #FnEquals, #FnFindInMap, #FnFormat, #FnGetAZs, #FnGetAtt, #FnIf, #FnJoin, #FnNot, #FnOr, #FnSelect, #Ref

Methods included from RefCheck

#build_references, #ref_children

Constructor Details

#initializeOrchestrationTemplate

Returns a new instance of OrchestrationTemplate.



13
14
15
# File 'lib/cfndsl/orchestration_template.rb', line 13

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

Class Method Details

.create_typesObject

rubocop:disable Metrics/MethodLength, Metrics/AbcSize, Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity



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
113
114
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/cfndsl/orchestration_template.rb', line 86

def self.create_types
  names = {}
  nametypes = {}
  template_types['Resources'].each_pair do |name, type|
    # Subclass ResourceDefintion and generate property methods
    klass = Class.new(CfnDsl::ResourceDefinition)
    klassname = name.split('::').join('_')
    type_module.const_set(klassname, klass)
    type['Properties'].each_pair do |pname, ptype|
      if ptype.instance_of?(String)
        create_klass = type_module.const_get(ptype)

        klass.class_eval do
          CfnDsl.method_names(pname) do |method|
            define_method(method) do |*values, &block|
              values.push create_klass.new if values.empty?

              @Properties ||= {}
              @Properties[pname] = CfnDsl::PropertyDefinition.new(*values)
              @Properties[pname].value.instance_eval(&block) if block
              @Properties[pname].value
            end
          end
        end
      else
        # Array version
        klass.class_eval do
          CfnDsl.method_names(pname) do |method|
            define_method(method) do |*values, &block|
              values.push [] if values.empty?
              @Properties ||= {}
              @Properties[pname] ||= PropertyDefinition.new(*values)
              @Properties[pname].value.instance_eval(&block) if block
              @Properties[pname].value
            end
          end
        end

        sing_name = CfnDsl::Plurals.singularize(pname)
        create_klass = type_module.const_get(ptype[0])
        sing_names = sing_name == pname ? [ptype[0]] : [ptype[0], sing_name]

        klass.class_eval do
          sing_names.each do |sname|
            CfnDsl.method_names(sname) do |method|
              define_method(method) do |value = nil, &block|
                @Properties ||= {}
                @Properties[pname] ||= PropertyDefinition.new([])
                value = create_klass.new unless value
                @Properties[pname].value.push value
                value.instance_eval(&block) if block
                value
              end
            end
          end
        end
      end
    end
    parts = name.split('::')
    until parts.empty?
      break if parts[0] == 'Resource'
      abreve_name = parts.join('_')
      if names.key?(abreve_name)
        # this only happens if there is an ambiguity
        names[abreve_name] = nil
      else
        names[abreve_name] = type_module.const_get(klassname)
        nametypes[abreve_name] = name
      end
      parts.shift
    end
  end

  # Define property setter methods for each of the unambiguous type names
  names.each_pair do |typename, type|
    next unless type

    class_eval do
      CfnDsl.method_names(typename) do |method|
        define_method(method) do |name, *values, &block|
          name = name.to_s
          @Resources ||= {}
          resource = @Resources[name] ||= type.new(*values)
          resource.instance_eval(&block) if block
          resource.instance_variable_set('@Type', nametypes[typename])
          resource
        end
      end
    end
  end
end

Instance Method Details

#check_output_refsObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/cfndsl/orchestration_template.rb', line 67

def check_output_refs
  invalids = []

  output_refs = {}
  if @Outputs
    @Outputs.keys.each do |resource|
      output_refs[resource.to_s] = @Outputs[resource].build_references({})
    end
    output_refs.keys.each do |origin|
      output_refs[origin].keys.each 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



43
44
45
46
47
# File 'lib/cfndsl/orchestration_template.rb', line 43

def check_refs
  invalids = check_resource_refs + check_output_refs

  invalids.empty? ? nil : invalids
end

#check_resource_refsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cfndsl/orchestration_template.rb', line 49

def check_resource_refs
  invalids = []

  @_resource_refs = {}
  if @Resources
    @Resources.keys.each do |resource|
      @_resource_refs[resource.to_s] = @Resources[resource].build_references({})
    end
    @_resource_refs.keys.each do |origin|
      @_resource_refs[origin].keys.each 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)


27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/cfndsl/orchestration_template.rb', line 27

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

  return true if GlobalRefs.key?(ref)

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

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

  false
end