Skip to content

Commit

Permalink
BAM: Add element types on violations to difference parameters from re…
Browse files Browse the repository at this point in the history
…sources (#96)

* BAM: Added line numbers and logical resource id to Parameters

* Add specs

* BAM: Add element type functionality to difference parameters from resources

* BAM: Add basic test for element_types
  • Loading branch information
benniemosher committed Oct 26, 2021
1 parent 683d538 commit 3ca8460
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/cfn-model/model/cfn_model.rb
Expand Up @@ -3,7 +3,7 @@
require_relative 'references'

class CfnModel
attr_reader :resources, :parameters, :line_numbers, :conditions, :globals, :mappings
attr_reader :resources, :parameters, :line_numbers, :conditions, :globals, :mappings, :element_types

##
# if you really want it, here it is - the raw Hash from YAML.load. you'll have to mess with structural nits of
Expand All @@ -19,6 +19,7 @@ def initialize
@mappings = {}
@raw_model = nil
@line_numbers = {}
@element_types = {}
end

##
Expand Down
3 changes: 3 additions & 0 deletions lib/cfn-model/parser/cfn_parser.rb
Expand Up @@ -156,6 +156,7 @@ def transform_hash_into_model_elements(cfn_hash, cfn_model)
assign_fields_based_upon_properties resource_object, resource, cfn_model

cfn_model.resources[resource_name] = resource_object
cfn_model.element_types[resource_name] = "resource"
end
cfn_model
end
Expand All @@ -173,6 +174,7 @@ def transform_hash_into_model_elements_with_numbers(cfn_hash, cfn_model)

cfn_model.resources[resource_name] = resource_object
cfn_model.line_numbers[resource_name] = resource['Type']['line']
cfn_model.element_types[resource_name] = "resource"
end
cfn_model
end
Expand All @@ -193,6 +195,7 @@ def transform_hash_into_parameters(cfn_hash, cfn_model)

cfn_model.parameters[parameter_name] = parameter
cfn_model.line_numbers[parameter_name] = parameter_hash['Type']['line']
cfn_model.element_types[parameter_name] = "parameter"
end
cfn_model
end
Expand Down
23 changes: 23 additions & 0 deletions spec/parser/cfn_parser_element_types_spec.rb
@@ -0,0 +1,23 @@
require 'spec_helper'
require 'cfn-model/parser/cfn_parser'

describe CfnParser do
before :each do
@cfn_parser = CfnParser.new
end

context 'element types enabled' do
it 'returns model with element_types for each resource' do
cloudformation_template_yml = IO.read(yaml_test_templates('iam_user/iam_user_with_literal_username_and_addition').first)
actual_cfn_model = @cfn_parser.parse cloudformation_template_yml, nil, true
expected_element_types = {
"AccessKey" => "parameter",
"iamUserWithAddition" => "resource",
"groupA" => "resource",
"addition1" => "resource",
"addition2" => "resource"
}
expect(actual_cfn_model.element_types).to eq expected_element_types
end
end
end

0 comments on commit 3ca8460

Please sign in to comment.