cfn-model 0.4.28 → 0.4.29

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: df1ac14db4c825a752953a999fca09e56eaaa3e7141146bea1892cfe0c1f350c
4
- data.tar.gz: 010c17ccd4d19b1999768d8b41b71dd98f6cca02713cf3065ac6cf208ee4d4f2
3
+ metadata.gz: 5c60ef54532b89cab5abaf2c3bf9381a7e9e0529f1ee9d1a516941e4ec9e9393
4
+ data.tar.gz: e77b5257bf99294ddc85ec54763e475be9a5b9ad4e4895e88cb552a7ea302a99
5
5
  SHA512:
6
- metadata.gz: 2530d80cc408f921c69ec5757c6b4c73a064a1d6e7c980cce9dd3a0441bdd501e82685ceff915c756ea7a4cd4fd52f712ddb7cd63a190686abc9b8613b63de05
7
- data.tar.gz: 8f053b02001d51fb7f4130e6b77d0e9cc7d3c36f35d8e3b9c3d55b6d9cc610a9ab02cdb8604c4385556819aa4e7581f2bf23cd912b910220b0f7c7bfd13d8dd6
6
+ metadata.gz: 1713962ad868efd3d92c97e5eff4a9e6300113afa35cf94f7ac5cd00cc7824c98ba8a9206616cd130a0e939d00e9ec44cbb422af2a0449674712c606b17383be
7
+ data.tar.gz: e9ba5783d5b37bec3edf86ec7f194c6bbf60bbcb310a87d14ce6e0b03b2b135f8abd565666b8a5ca8403659ca46b17cfb154643d71910ce7e8b04be8bc510e58
@@ -3,7 +3,7 @@
3
3
  require_relative 'references'
4
4
 
5
5
  class CfnModel
6
- attr_reader :resources, :parameters, :line_numbers, :conditions, :globals
6
+ attr_reader :resources, :parameters, :line_numbers, :conditions, :globals, :mappings
7
7
 
8
8
  ##
9
9
  # if you really want it, here it is - the raw Hash from YAML.load. you'll have to mess with structural nits of
@@ -16,6 +16,7 @@ class CfnModel
16
16
  @resources = {}
17
17
  @conditions = {}
18
18
  @globals = {}
19
+ @mappings = {}
19
20
  @raw_model = nil
20
21
  @line_numbers = {}
21
22
  end
@@ -38,6 +39,9 @@ class CfnModel
38
39
  @resources.each do |k, v|
39
40
  new_cfn_model.resources[k] = v
40
41
  end
42
+ @mappings.each do |k, v|
43
+ new_cfn_model.mappings[k] = v
44
+ end
41
45
  new_cfn_model.raw_model = @raw_model.dup unless @raw_model.nil?
42
46
  new_cfn_model
43
47
  end
@@ -10,24 +10,34 @@ require 'cfn-model/parser/parser_error'
10
10
  # clear
11
11
  module References
12
12
  def self.resolve_value(cfn_model, value)
13
+
13
14
  if value.is_a? Hash
14
15
  if value.has_key?('Ref')
15
- ref_id = value['Ref']
16
- if ref_id.is_a? String
17
- if cfn_model.parameters.has_key?(ref_id)
18
- return value if cfn_model.parameters[ref_id].synthesized_value.nil?
19
- return cfn_model.parameters[ref_id].synthesized_value
20
- else
21
- return value
22
- end
23
- else
24
- return value
25
- end
16
+ resolve_reference(cfn_model, value)
17
+ elsif value.has_key?('Fn::FindInMap')
18
+ resolve_map(cfn_model, value)
19
+ else
20
+ value
21
+ end
22
+ else
23
+ value
24
+ end
25
+ end
26
+
27
+ ##
28
+ # For a !Ref to another resource.... just returns the !REf
29
+ # For a !Ref to a Parameter, then try to synthesize the value
30
+ def self.resolve_reference(cfn_model, value)
31
+ ref_id = value['Ref']
32
+ if ref_id.is_a? String
33
+ if cfn_model.parameters.has_key?(ref_id)
34
+ return value if cfn_model.parameters[ref_id].synthesized_value.nil?
35
+ return cfn_model.parameters[ref_id].synthesized_value
26
36
  else
27
37
  return value
28
38
  end
29
39
  else
30
- return value
40
+ value
31
41
  end
32
42
  end
33
43
 
@@ -57,6 +67,33 @@ module References
57
67
  resolve_resource_id group_id, 'GroupId'
58
68
  end
59
69
 
70
+ ##
71
+ # Try to compute the FindInMap against a real Mapping
72
+ #
73
+ # If anything doesn't match up - either a syntax error,
74
+ # a missing Mapping, or some other kind Cfn evaluation
75
+ # we don't understand, just return the call to FindInMap
76
+ def self.resolve_map(cfn_model, find_in_map)
77
+ map_name = find_in_map['Fn::FindInMap'][0]
78
+ top_level_key = find_in_map['Fn::FindInMap'][1]
79
+ second_level_key = find_in_map['Fn::FindInMap'][2]
80
+
81
+ map = cfn_model.mappings[map_name]
82
+
83
+ return find_in_map if map.nil?
84
+
85
+ top_level_resolved = resolve_value(cfn_model, top_level_key)
86
+
87
+ return find_in_map if !map.has_key?(top_level_resolved)
88
+
89
+ top_level = map[top_level_resolved]
90
+ second_level = top_level[resolve_value(cfn_model, second_level_key)]
91
+
92
+ return find_in_map if second_level.nil?
93
+
94
+ second_level
95
+ end
96
+
60
97
  private
61
98
 
62
99
  def self.logical_resource_id_from_get_att(attribute_spec, attr_to_retrieve=nil)
@@ -76,6 +76,8 @@ class CfnParser
76
76
 
77
77
  process_conditions cfn_hash, cfn_model, condition_values_json
78
78
 
79
+ process_mappings cfn_hash, cfn_model
80
+
79
81
  # pass 1: wire properties into ModelElement objects
80
82
  if with_line_numbers
81
83
  transform_hash_into_model_elements_with_numbers cfn_hash, cfn_model
@@ -93,6 +95,14 @@ class CfnParser
93
95
 
94
96
  private
95
97
 
98
+ def process_mappings(cfn_hash, cfn_model)
99
+ if cfn_hash.key?('Mappings')
100
+ cfn_hash['Mappings'].each do |mapping_key, mapping_value|
101
+ cfn_model.mappings[mapping_key] = mapping_value
102
+ end
103
+ end
104
+ end
105
+
96
106
  def process_conditions(cfn_hash, cfn_model, condition_values_json)
97
107
  if cfn_hash.key?('Conditions')
98
108
  if condition_values_json.nil?
@@ -33,6 +33,32 @@ class ParameterSubstitution
33
33
  end
34
34
  end
35
35
 
36
+ def apply_pseudo_parameter_values(cfn_model, parameter_values)
37
+ # leave out 'AWS::NoValue'? not sure - we explicitly check it in some places...
38
+ # might make sense to substitute here?
39
+ pseudo_function_defaults = {
40
+ 'AWS::URLSuffix' => 'amazonaws.com',
41
+ 'AWS::Partition' => 'aws',
42
+ 'AWS::NotificationARNs' => '',
43
+ 'AWS::AccountId' => '111111111111',
44
+ 'AWS::Region' => 'us-east-1',
45
+ 'AWS::StackId' => 'arn:aws:cloudformation:us-east-1:111111111111:stack/stackname/51af3dc0-da77-11e4-872e-1234567db123',
46
+ 'AWS::StackName' => 'stackname'
47
+ }
48
+ pseudo_function_defaults.each do |function_name, default_value|
49
+ parameter = Parameter.new
50
+ parameter.id = function_name
51
+ parameter.type = 'String'
52
+ cfn_model.parameters[function_name] = parameter
53
+
54
+ if parameter_values[PARAMETERS].has_key?(function_name)
55
+ parameter.synthesized_value = parameter_values[PARAMETERS][function_name]
56
+ else
57
+ parameter.synthesized_value = default_value
58
+ end
59
+ end
60
+ end
61
+
36
62
  def apply_parameter_values_impl(cfn_model, parameter_values)
37
63
  parameter_values[PARAMETERS].each do |parameter_name, parameter_value|
38
64
  if cfn_model.parameters.has_key?(parameter_name)
@@ -50,6 +76,7 @@ class ParameterSubstitution
50
76
  parameter.synthesized_value = parameter.default.to_s
51
77
  end
52
78
  end
79
+ apply_pseudo_parameter_values(cfn_model, parameter_values)
53
80
  end
54
81
 
55
82
  def is_aws_format?(parameter_values)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cfn-model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.28
4
+ version: 0.4.29
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Kascic
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-04-08 00:00:00.000000000 Z
11
+ date: 2020-05-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop