cfn-model 0.1.28 → 0.1.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8b793b08c7374b3d4b6278501a083db2e33e2a43c7d57127d5c536c3a0502743
4
- data.tar.gz: 74b42b3c781f10adb4d177d0498cb0e79ad8ce8c45db20561e9e10db21bb130c
3
+ metadata.gz: d727b122287e1f97d8c2f75323e7ab57553b8265cff424a06abd8127e4cc5893
4
+ data.tar.gz: ff0dd5f3e93115b884a4e217366688d960025b0ed54355a9a0a092d8e5de121b
5
5
  SHA512:
6
- metadata.gz: 39cebcc2c3d32eff01af5c361f32ef4f4f877aa2d4533131099e3cb0707e22582e8612b3ac85a4e8a96f28b3a469f325bf5df02530ee5f7bb9a3ae8d7ce9ab27
7
- data.tar.gz: 67c0fbbf7cb12f52d00556daeedf6982ec5cf5397fab89b4903dc501b47981b865b9941337ed9d2f2fef39d09ec49f9af9545f99311019216639f29c47dbd981
6
+ metadata.gz: 0c2a43026c14efb0de73770172ea6d1f89f3ee2c96b9f69ee19dd6ffa58b4e7f41d7b8854540d3c27c0e476bde42738b50a3a63d1adbdc60066edbbf368f3a55
7
+ data.tar.gz: b4e3e37b0b8176d28d67dde05b0860104aa19ec1828f753737047f8290efe320e1f46dbc7a257bec6a5db29375b500282b63af74478fe92f1e40cba947379b17
data/bin/cfn_parse CHANGED
@@ -4,5 +4,5 @@ require 'cfn-model'
4
4
  puts '======'
5
5
  puts ARGV[0]
6
6
  puts '======'
7
- cfn_model = CfnParser.new.parse IO.read(ARGV[0])
7
+ cfn_model = CfnParser.new.parse IO.read(ARGV[0]), nil, true
8
8
  puts cfn_model
@@ -1,7 +1,7 @@
1
1
  require_relative 'references'
2
2
 
3
3
  class CfnModel
4
- attr_reader :resources, :parameters
4
+ attr_reader :resources, :parameters, :line_numbers
5
5
 
6
6
  ##
7
7
  # if you really want it, here it is - the raw Hash from YAML.load. you'll have to mess with structural nits of
@@ -13,6 +13,7 @@ class CfnModel
13
13
  @parameters = {}
14
14
  @resources = {}
15
15
  @raw_model = nil
16
+ @line_numbers = {}
16
17
  end
17
18
 
18
19
  ##
@@ -78,4 +79,4 @@ class CfnModel
78
79
  def to_s
79
80
  @resources.to_s
80
81
  end
81
- end
82
+ end
@@ -0,0 +1,5 @@
1
+ # Psych's first step is to parse the Yaml into an AST of Node objects
2
+ # so we open the Node class and add a way to track the line.
3
+ class Psych::Nodes::Node
4
+ attr_accessor :line
5
+ end
@@ -1,8 +1,12 @@
1
1
  require 'yaml'
2
+ require 'psych'
2
3
  require 'json'
3
4
  require 'cfn-model/parser/transform_registry'
4
5
  require 'cfn-model/validator/cloudformation_validator'
5
6
  require 'cfn-model/validator/reference_validator'
7
+ require 'cfn-model/psych/handlers/line_number_handler'
8
+ require 'cfn-model/psych/visitors/to_ruby_with_line_numbers'
9
+ require 'cfn-model/monkey_patches/psych/nodes/node'
6
10
  require_relative 'parser_registry'
7
11
  require_relative 'parameter_substitution'
8
12
  require_relative 'parser_error'
@@ -32,18 +36,31 @@ class CfnParser
32
36
  ##
33
37
  # Given raw json/yml CloudFormation template, returns a CfnModel object
34
38
  # or raise ParserErrors if something is amiss with the format
35
- def parse(cloudformation_yml, parameter_values_json=nil)
36
- cfn_model = parse_without_parameters(cloudformation_yml)
39
+ def parse(cloudformation_yml, parameter_values_json=nil, with_line_numbers=false)
40
+ cfn_model = parse_without_parameters(cloudformation_yml, with_line_numbers)
37
41
 
38
42
  apply_parameter_values(cfn_model, parameter_values_json)
39
43
 
40
44
  cfn_model
41
45
  end
42
46
 
43
- def parse_without_parameters(cloudformation_yml)
47
+ def parse_with_line_numbers(cloudformation_yml)
48
+ handler = LineNumberHandler.new
49
+ parser = Psych::Parser.new(handler)
50
+ handler.parser = parser
51
+ parser.parse(cloudformation_yml)
52
+ ToRubyWithLineNumbers.create.accept(handler.root).first
53
+ end
54
+
55
+ def parse_without_parameters(cloudformation_yml, with_line_numbers=false)
44
56
  pre_validate_model cloudformation_yml
45
57
 
46
- cfn_hash = YAML.load cloudformation_yml
58
+ cfn_hash =
59
+ if with_line_numbers
60
+ parse_with_line_numbers(cloudformation_yml)
61
+ else
62
+ YAML.load cloudformation_yml
63
+ end
47
64
 
48
65
  # Transform raw resources in template as performed by
49
66
  # transforms
@@ -55,7 +72,11 @@ class CfnParser
55
72
  cfn_model.raw_model = cfn_hash
56
73
 
57
74
  # pass 1: wire properties into ModelElement objects
58
- transform_hash_into_model_elements cfn_hash, cfn_model
75
+ if with_line_numbers
76
+ transform_hash_into_model_elements_with_numbers cfn_hash, cfn_model
77
+ else
78
+ transform_hash_into_model_elements cfn_hash, cfn_model
79
+ end
59
80
  transform_hash_into_parameters cfn_hash, cfn_model
60
81
 
61
82
  # pass 2: tie together separate resources only where necessary to make life easier for rule logic
@@ -103,6 +124,23 @@ class CfnParser
103
124
  cfn_model
104
125
  end
105
126
 
127
+ def transform_hash_into_model_elements_with_numbers(cfn_hash, cfn_model)
128
+ cfn_hash['Resources'].each do |resource_name, resource|
129
+ resource_class = class_from_type_name resource['Type']['value']
130
+
131
+ resource_object = resource_class.new(cfn_model)
132
+ resource_object.logical_resource_id = resource_name
133
+ resource_object.resource_type = resource['Type']['value']
134
+ resource_object.metadata = resource['Metadata']
135
+
136
+ assign_fields_based_upon_properties resource_object, resource
137
+
138
+ cfn_model.resources[resource_name] = resource_object
139
+ cfn_model.line_numbers[resource_name] = resource['Type']['line']
140
+ end
141
+ cfn_model
142
+ end
143
+
106
144
  def transform_hash_into_parameters(cfn_hash, cfn_model)
107
145
  return cfn_model unless cfn_hash.has_key?('Parameters')
108
146
 
@@ -0,0 +1,18 @@
1
+ # We need to provide a handler that will add the line to the node
2
+ # as it is parsed. TreeBuilder is the "usual" handler, that
3
+ # creates the AST.
4
+ class LineNumberHandler < Psych::TreeBuilder
5
+
6
+ # The handler needs access to the parser in order to call mark
7
+ attr_accessor :parser
8
+
9
+ # We are only interested in scalars, so here we override
10
+ # the method so that it calls mark and adds the line info
11
+ # to the node.
12
+ def scalar value, anchor, tag, plain, quoted, style
13
+ mark = parser.mark
14
+ node = super
15
+ node.line = mark.line
16
+ node
17
+ end
18
+ end
@@ -0,0 +1,18 @@
1
+ class ToRubyWithLineNumbers < Psych::Visitors::ToRuby
2
+ def revive_hash hash, o
3
+ o.children.each_slice(2) { |k,v|
4
+ key = accept(k)
5
+ val = accept(v)
6
+ line = v.respond_to?(:line) ? v.line : v.start_line
7
+
8
+ # This is the important bit. If the value is a scalar,
9
+ # we replace it with the desired hash.
10
+ if v.is_a?(::Psych::Nodes::Scalar) && key == 'Type'
11
+ val = { "value" => val, "line" => line + 1} # line is 0 based, so + 1
12
+ end
13
+
14
+ hash[key] = val
15
+ }
16
+ hash
17
+ end
18
+ end
@@ -9,4 +9,4 @@ class CloudFormationValidator
9
9
 
10
10
  validator.validate(YAML.load(cloudformation_string))
11
11
  end
12
- end
12
+ end
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.1.28
4
+ version: 0.1.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: 2019-04-19 00:00:00.000000000 Z
11
+ date: 2019-05-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rubocop
@@ -70,6 +70,7 @@ files:
70
70
  - lib/cfn-model/model/security_group_ingress.rb
71
71
  - lib/cfn-model/model/statement.rb
72
72
  - lib/cfn-model/model/topic_policy.rb
73
+ - lib/cfn-model/monkey_patches/psych/nodes/node.rb
73
74
  - lib/cfn-model/parser/cfn_parser.rb
74
75
  - lib/cfn-model/parser/ec2_instance_parser.rb
75
76
  - lib/cfn-model/parser/ec2_network_interface_parser.rb
@@ -85,6 +86,8 @@ files:
85
86
  - lib/cfn-model/parser/security_group_parser.rb
86
87
  - lib/cfn-model/parser/transform_registry.rb
87
88
  - lib/cfn-model/parser/with_policy_document_parser.rb
89
+ - lib/cfn-model/psych/handlers/line_number_handler.rb
90
+ - lib/cfn-model/psych/visitors/to_ruby_with_line_numbers.rb
88
91
  - lib/cfn-model/schema/AWS_CloudFront_Distribution.yml
89
92
  - lib/cfn-model/schema/AWS_EC2_Instance.yml
90
93
  - lib/cfn-model/schema/AWS_EC2_NetworkInterface.yml