Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions lib/cfhighlander.compiler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ class ComponentCompiler
:process_lambdas,
:lambda_mock_resolve

def initialize(component)
def initialize(component, qualified_name = nil)

@workdir = ENV['CFHIGHLANDER_WORKDIR']
@component = component
@sub_components = []
@component_name = component.highlander_dsl.name.downcase
@component_name = qualified_name || component.highlander_dsl.name.downcase
@cfndsl_compiled = false
@config_compiled = false
@cfn_template_paths = []
Expand All @@ -58,8 +58,17 @@ def initialize(component)
end

@component.highlander_dsl.subcomponents.each do |sub_component|
sub_component_compiler = Cfhighlander::Compiler::ComponentCompiler.new(sub_component.component_loaded)
sub_component_compiler.component_name = sub_component.name
# Inline sub-components share the parent's output directory and their
# resources get flattened into the parent template, so they don't need
# stable filenames. Qualify with parent name to prevent collisions when
# multiple sibling components define sub-components with the same name.
child_name = if sub_component.inlined
"#{@component_name}_#{sub_component.name}"
else
sub_component.name
end
Comment on lines +61 to +69
sub_component_compiler = Cfhighlander::Compiler::ComponentCompiler.new(sub_component.component_loaded, child_name)
sub_component_compiler.component_name = child_name
@sub_components << sub_component_compiler
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CfhighlanderTemplate do

Name 'child'

Component template: 'grandchild', name: 'gc', render: Inline, config: @config

end
3 changes: 3 additions & 0 deletions spec/data/inline_config_isolation/src/child/child.cfndsl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CloudFormation do

end
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CfhighlanderTemplate do

Name 'grandchild'

end
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
CloudFormation do

S3_Bucket(:ConfigBucket) do
BucketName bucket_prefix
end

end
8 changes: 8 additions & 0 deletions spec/data/inline_config_isolation/src/p.cfhighlander.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CfhighlanderTemplate do

Name 'p'

Component template: 'child', name: 'child_a', render: Inline, config: child_a
Component template: 'child', name: 'child_b', render: Inline, config: child_b

end
3 changes: 3 additions & 0 deletions spec/data/inline_config_isolation/src/p.cfndsl.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
CloudFormation do

end
5 changes: 5 additions & 0 deletions spec/data/inline_config_isolation/src/p.config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
child_a:
bucket_prefix: alpha

child_b:
bucket_prefix: beta
39 changes: 39 additions & 0 deletions spec/test_inline_config_isolation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require_relative '../bin/cfhighlander'
require_relative '../lib/util/cloudformation.util'
require 'rspec'
require 'yaml'
require 'fileutils'

RSpec.describe Cfhighlander::Compiler::ComponentCompiler, "inline config isolation" do

context "when two inline instances of the same template have different configs" do
it "each instance compiles with its own config" do

src_dir = "#{File.dirname(__FILE__)}/data/inline_config_isolation/src"
ENV['CFHIGHLANDER_WORKDIR'] = src_dir

factory = Cfhighlander::Factory::ComponentFactory.new
component = factory.loadComponentFromTemplate('p')
component.load
component.eval_cfndsl

compiler = Cfhighlander::Compiler::ComponentCompiler.new(component)
model_flat = compiler.compileCloudFormation

resources = model_flat['Resources']

child_a_bucket = resources['ConfigBucket']
child_b_bucket = resources['childbConfigBucket']

expect(child_a_bucket).not_to be_nil, "expected child_a's ConfigBucket resource"
expect(child_b_bucket).not_to be_nil, "expected child_b's childbConfigBucket resource"

child_a_name = child_a_bucket['Properties']['BucketName']
child_b_name = child_b_bucket['Properties']['BucketName']

expect(child_a_name).to eq('alpha'), "child_a should use its own config"
expect(child_b_name).to eq('beta'), "child_b should use its own config"
end
end

end
Loading