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, condition_values_json = nil) ⇒ Object

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



42
43
44
45
46
47
48
49
50
51
# File 'lib/cfn-model/parser/cfn_parser.rb', line 42

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

  apply_parameter_values(cfn_model, parameter_values_json)

  # 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

#parse_with_line_numbers(cloudformation_yml) ⇒ Object



53
54
55
56
57
58
59
# File 'lib/cfn-model/parser/cfn_parser.rb', line 53

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, condition_values_json = nil) ⇒ Object



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
87
88
89
90
91
92
93
94
95
96
# File 'lib/cfn-model/parser/cfn_parser.rb', line 61

def parse_without_parameters(cloudformation_yml, with_line_numbers=false, condition_values_json=nil)
  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

  process_conditions cfn_hash, cfn_model, condition_values_json

  process_mappings cfn_hash, cfn_model

  # 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
  transform_hash_into_globals cfn_hash, cfn_model



  cfn_model
end