Class: CfnParser

Inherits:
Object
  • Object
show all
Defined in:
lib/cfn-model/parser/cfn_parser.rb

Overview

This class is the heart of the matter. It will take a CloudFormation template and return a CfnModel object to represent the underlying document in a way that is hopefully more convenient for (cfn-nag rule) developers to work with

Instance Method Summary collapse

Instance Method Details

#parse(cloudformation_yml, parameter_values_json = nil, with_line_numbers = false) ⇒ Object

Given raw json/yml CloudFormation template, returns a CfnModel object or raise ParserErrors if something is amiss with the format



39
40
41
42
43
44
45
# File 'lib/cfn-model/parser/cfn_parser.rb', line 39

def parse(cloudformation_yml, parameter_values_json=nil, with_line_numbers=false)
  cfn_model = parse_without_parameters(cloudformation_yml, with_line_numbers)

  apply_parameter_values(cfn_model, parameter_values_json)

  cfn_model
end

#parse_with_line_numbers(cloudformation_yml) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/cfn-model/parser/cfn_parser.rb', line 47

def parse_with_line_numbers(cloudformation_yml)
  handler = LineNumberHandler.new
  parser =  Psych::Parser.new(handler)
  handler.parser = parser
  parser.parse(cloudformation_yml)
  ToRubyWithLineNumbers.create.accept(handler.root).first
end

#parse_without_parameters(cloudformation_yml, with_line_numbers = false) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/cfn-model/parser/cfn_parser.rb', line 55

def parse_without_parameters(cloudformation_yml, with_line_numbers=false)
  pre_validate_model cloudformation_yml

  cfn_hash =
    if with_line_numbers
      parse_with_line_numbers(cloudformation_yml)
    else
      YAML.load cloudformation_yml
    end

  # Transform raw resources in template as performed by
  # transforms
  CfnModel::TransformRegistry.instance.perform_transforms cfn_hash

  validate_references cfn_hash

  cfn_model = CfnModel.new
  cfn_model.raw_model = cfn_hash

  # pass 1: wire properties into ModelElement objects
  if with_line_numbers
    transform_hash_into_model_elements_with_numbers cfn_hash, cfn_model
  else
    transform_hash_into_model_elements cfn_hash, cfn_model
  end
  transform_hash_into_parameters cfn_hash, cfn_model

  # pass 2: tie together separate resources only where necessary to make life easier for rule logic
  post_process_resource_model_elements cfn_model

  cfn_model
end