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.



111
112
113
# File 'lib/cfndsl/orchestration_template.rb', line 111

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

Class Method Details

.create_array_property_def(resource, pname, pclass) ⇒ Object



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

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

  sname = CfnDsl::Plurals.singularize pname

  unless 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 unless value
          @Properties[pname].value.push value
          value.instance_eval(&block) if block
          value
        end
      end
    end
  end
end

.create_property_def(resource, pname, pclass) ⇒ Object



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

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



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

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



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

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



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

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 parts.first == 'Resource' # Don't allow us to define Resource as different method
      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.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



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.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)


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

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