Module: Aws::Cfn::Compiler::Compile

Included in:
Base
Defined in:
lib/aws/cfn/compiler/mixins/compile.rb

Instance Method Summary collapse

Instance Method Details

#compile_specObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/aws/cfn/compiler/mixins/compile.rb', line 6

def compile_spec
  desc =  if @config[:description]
            @config[:description]
          elsif @spec and @spec['Description']
            @spec['Description']
          elsif @config[:template]
            File.basename(@config[:template]).gsub(%r/\..*$/, '')
          else
            'compiled template'
          end
  vers =  if @config[:formatversion]
            @config[:formatversion]
          else
            if @spec and @spec['AWSTemplateFormatVersion']
              @spec['AWSTemplateFormatVersion']
            else
              '2010-09-09'
            end
          end
  # [2014-06-29 Christo] IIRC it is important that the section names be strings instead of symbols ...
  # noinspection RubyStringKeysInHashInspection
  compiled =
  {
      'AWSTemplateFormatVersion' => vers,
      'Description'              => desc,
      'Mappings'                 => @items['Mappings'],
      'Parameters'               => @items['Parameters'],
      'Resources'                => @items['Resources'],
      'Outputs'                  => @items['Outputs'],
  }
end

#find_maps(hash) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/aws/cfn/compiler/mixins/compile.rb', line 88

def find_maps(hash)
  if hash.is_a? Hash
    tr = []
    hash.keys.collect do |key|
      if 'Fn::FindInMap' == key
        hash[key].first
      else
        find_maps(hash[key])
      end
    end.flatten.compact.uniq
  elsif hash.is_a? Array
    hash.collect{|a| find_maps(a)}.flatten.compact.uniq
  end
end

#find_refs(hash, type = 'Reference', parent = '') ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/aws/cfn/compiler/mixins/compile.rb', line 38

def find_refs(hash, type='Reference', parent='')
  h = {}
  newparent = parent
  if hash.is_a? Hash
    hash.keys.collect do |key|
      if %w{Mappings Parameters Resources Outputs}.include? key
        type = key#.gsub(/s$/, '')
        newparent = key
      elsif %w{Mappings Parameters Resources Outputs}.include? parent
        newparent = key
      end
      if %w{Ref}.include? key
        h = { hash[key] => [type,newparent] }
      elsif 'Fn::GetAtt' == key
        h = { hash[key].first => [type,newparent] }
        # elsif %w{SourceSecurityGroupName CacheSecurityGroupNames SecurityGroupNames}.include? key
        #   a = find_refs(hash[key],type,newparent)
        #   h = merge(h, a, *[type,newparent])
      else
        a = find_refs(hash[key],type,newparent)
        h = merge(h, a, *[type,newparent])
      end
    end.flatten.compact.uniq
  elsif hash.is_a? Array
    a = hash.map{|i| find_refs(i,type,newparent) }
    h = merge(h, a, type, *[type,newparent])
  end
  h
end

#map_resource_reference(rsrc) ⇒ Object



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
# File 'lib/aws/cfn/compiler/mixins/compile.rb', line 103

def map_resource_reference(rsrc)
  path = nil
  sub  = nil
  ref  = nil
  rel  = false
  # noinspection RubyParenthesesAroundConditionInspection
  if rsrc.match %r'^(\.\./.*?)::(.*)$'
    # Relative path stack reference
    path,sub,ref,rel  = map_resource_reference(File.basename(rsrc))
  elsif rsrc.match %r'^(~/.*?)$'
    # Relative to HOME
    path,sub,ref,rel  = map_resource_reference(File.expand_path(rsrc))
  elsif rsrc.match %r'^(\.\./[^:]*?)$'
    # Relative path
    path = File.dirname(rsrc)
    sub  = File.basename(path)
    path = File.dirname(path)
    ref  = File.basename(rsrc)
    rel  = true
  elsif rsrc.match %r'(^/.*?)::(.*)$'
    # Absolute path
    _,sub,ref,rel  = map_resource_reference(File.basename(rsrc))
    path = File.realpath(File.join(File.dirname(rsrc),_))
  elsif rsrc.match %r'(^/.*?[^:]*?)$'
    # Absolute path
    path = File.dirname(rsrc)
    sub  = File.basename(path)
    path = File.dirname(path)
    ref  = File.basename(rsrc)
  elsif (match = rsrc.match %r'^(.*?)::(.*)$')
    # Inherited stack reference
    ref = match[2]
    # noinspection RubyParenthesesAroundConditionInspection
    if (subm = match[1].match(%r'^(.+?)/(.+)$'))
      path = File.join(File.dirname(@config[:directory]),subm[1])
      sub = subm[2]
    else
      # sub = nil
      path = File.join(File.dirname(@config[:directory]),match[1])
    end
  else
    # Otherwise it is what it seems ;)
    ref  = rsrc
  end
  [path,sub,ref,rel]
end

#merge(h, a, *type) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/aws/cfn/compiler/mixins/compile.rb', line 68

def merge(h, a, *type)
  if a.is_a? Hash
    if a.size > 0
      h.merge! a
    end
  else
    a.flatten.compact.uniq.map { |i|
      if i.is_a? Hash
        if i.size > 0
          h.merge! i
          h
        end
      else
        h[i] = type
      end
    }
  end
  h
end