Skip to content

Commit

Permalink
stubbing cloudformation template generator
Browse files Browse the repository at this point in the history
  • Loading branch information
pghalliday committed Oct 28, 2015
1 parent 5242225 commit 83f828f
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 24 deletions.
26 changes: 16 additions & 10 deletions lib/formatron/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Configuration
def initialize(aws, directory)
@aws = aws
@directory = directory
@configs = {}
@formatronfiles = {}
end

Expand All @@ -15,42 +16,47 @@ def targets
end

def protected?(target)
_load_formatronfile target
_load target
@formatronfiles[target].bootstrap.protect
end

def name(target)
_load_formatronfile target
_load target
@formatronfiles[target].name
end

def kms_key(target)
_load_formatronfile target
_load target
@formatronfiles[target].bootstrap.kms_key
end

def bucket(target)
_load_formatronfile target
_load target
@formatronfiles[target].bucket
end

def config(target)
_load_formatronfile target
@config
_load target
@configs[target]
end

def _load_formatronfile(target)
@config = Config.target @directory, target
def cloud_formation_template(target)
_load target
@formatronfiles[target].cloud_formation_template
end

def _load(target)
@configs[target] ||= Config.target @directory, target
@formatronfiles[target] ||= Formatronfile.new(
@aws,
target,
@config,
@configs[target],
@directory
)
end

private(
:_load_formatronfile
:_load
)
end
end
30 changes: 19 additions & 11 deletions lib/formatron/configuration/formatronfile.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
require_relative 'formatronfile/dsl'
require_relative 'formatronfile/dsl/bootstrap'
require_relative 'formatronfile/bootstrap'
require_relative 'formatronfile/cloud_formation'

class Formatron
class Configuration
# Processes the Formatronfile in the context of the given target
class Formatronfile
attr_reader(
:bootstrap,
:cloud_formation_template,
:name,
:bucket
)
Expand All @@ -17,7 +19,9 @@ def initialize(aws, target, config, directory)
@target = target
@config = config
_initialize_dsl directory
_initialize_dsl_bootstrap
_initialize_bootstrap
_initialize_cloud_formation_template
end

def _initialize_dsl(directory)
Expand All @@ -30,16 +34,8 @@ def _initialize_dsl(directory)
@bucket = @dsl.bucket
end

def _initialize_bootstrap
dsl_bootstrap = _dsl_bootstrap
@bootstrap = Bootstrap.new(
dsl_bootstrap.protect,
dsl_bootstrap.kms_key
)
end

def _dsl_bootstrap
DSL::Bootstrap.new(
def _initialize_dsl_bootstrap
@dsl_bootstrap = DSL::Bootstrap.new(
@target,
@config,
@name,
Expand All @@ -48,10 +44,22 @@ def _dsl_bootstrap
)
end

def _initialize_bootstrap
@bootstrap = Bootstrap.new(
@dsl_bootstrap.protect,
@dsl_bootstrap.kms_key
)
end

def _initialize_cloud_formation_template
@cloud_formation_template = CloudFormation.bootstrap_template
end

private(
:_initialize_dsl,
:_initialize_dsl_bootstrap,
:_initialize_bootstrap,
:_dsl_bootstrap
:_initialize_cloud_formation_template
)
end
end
Expand Down
11 changes: 11 additions & 0 deletions lib/formatron/configuration/formatronfile/cloud_formation.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Formatron
class Configuration
class Formatronfile
# Generates the CloudFormation templates
module CloudFormation
def self.bootstrap_template
end
end
end
end
end
14 changes: 12 additions & 2 deletions lib/formatron/s3_cloud_formation_template.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
class Formatron
# manage the CloudFormation template stored on S3
module S3CloudFormationTemplate
def self.deploy(_aws, _formatronfile, _target)
def self.deploy(aws, configuration, target)
aws.upload(
configuration.kms_key(target),
configuration.bucket(target),
S3Path.path(configuration, target, 'cloud_formation_template.json'),
configuration.cloud_formation_template(target)
)
end

def self.destroy(_aws, _formatronfile, _target)
def self.destroy(aws, configuration, target)
aws.delete(
configuration.bucket(target),
S3Path.path(configuration, target, 'cloud_formation_template.json')
)
end
end
end
10 changes: 10 additions & 0 deletions spec/formatron/configuration/formatronfile/cloud_formation_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'spec_helper'
require 'formatron/configuration/formatronfile/cloud_formation'

describe Formatron::Configuration::Formatronfile::CloudFormation do
describe '::bootstrap_template' do
skip 'should return the CloudFormation template for ' \
'a bootstrap configuration' do
end
end
end
16 changes: 16 additions & 0 deletions spec/formatron/configuration/formatronfile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
protect = false
kms_key = 'kms_key'
bucket = 'bucket'
cloud_formation_template = 'cloud_formation_template'

before(:each) do
aws = instance_double('Formatron::AWS')
Expand Down Expand Up @@ -64,6 +65,13 @@
kms_key
) { @bootstrap }

@cloud_formation = class_double(
'Formatron::Configuration::Formatronfile::CloudFormation'
).as_stubbed_const
expect(@cloud_formation).to receive(:bootstrap_template).once.with(
no_args
) { cloud_formation_template }

@formatronfile = Formatron::Configuration::Formatronfile.new(
aws,
target,
Expand All @@ -89,4 +97,12 @@
expect(@formatronfile.bucket).to eql bucket
end
end

describe '#cloud_formation_template' do
it 'should return the CloudFormation template for the configuration' do
expect(
@formatronfile.cloud_formation_template
).to eql cloud_formation_template
end
end
end
15 changes: 15 additions & 0 deletions spec/formatron/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
target_config = {
param: 'param'
}
cloud_formation_template = 'cloud_formation_template'
protect = true
name = 'name'
kms_key = 'kms_key'
Expand All @@ -29,6 +30,9 @@
allow(@formatronfile_class).to receive(:new) { @formatronfile }
allow(@formatronfile).to receive(:name) { name }
allow(@formatronfile).to receive(:bucket) { bucket }
allow(@formatronfile).to receive(
:cloud_formation_template
) { cloud_formation_template }

@bootstrap = instance_double(
'Formatron::Configuration::Formatronfile::Bootstrap'
Expand Down Expand Up @@ -92,4 +96,15 @@
expect(@configuration.config(targets[0])).to eql target_config
end
end

describe '#cloud_formation_template' do
it 'should return the CloudFormation template to be uploaded to S3' do
expect(@configuration.cloud_formation_template(targets[0])).to eql(
cloud_formation_template
)
expect(@formatronfile).to have_received(
:cloud_formation_template
).once.with no_args
end
end
end
63 changes: 62 additions & 1 deletion spec/formatron/s3_cloud_formation_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,69 @@
require 'formatron/s3_cloud_formation_template'

describe Formatron::S3CloudFormationTemplate do
target = 'target'
kms_key = 'kms_key'
bucket = 'bucket'
cloud_formation_template = 'cloud_formation_template'
key = 'key'

before(:each) do
@aws = instance_double 'Formatron::AWS'
@configuration = instance_double 'Formatron::Configuration'
@s3_path = class_double(
'Formatron::S3Path'
).as_stubbed_const
end

describe '::deploy' do
skip 'should do something' do
it 'should upload the CloudFormation template to S3' do
expect(@s3_path).to receive(:path).once.with(
@configuration,
target,
'cloud_formation_template.json'
) { key }
expect(@configuration).to receive(:kms_key).once.with(
target
) { kms_key }
expect(@configuration).to receive(:bucket).once.with(
target
) { bucket }
expect(@configuration).to receive(:cloud_formation_template).once.with(
target
) { cloud_formation_template }
expect(@aws).to receive(:upload).once.with(
kms_key,
bucket,
key,
cloud_formation_template
)
Formatron::S3CloudFormationTemplate.deploy(
@aws,
@configuration,
target
)
end
end

describe '::destroy' do
it 'should delete the CloudFormation template from S3' do
expect(@s3_path).to receive(:path).once.with(
@configuration,
target,
'cloud_formation_template.json'
) { key }
expect(@configuration).to receive(:bucket).once.with(
target
) { bucket }
expect(@aws).to receive(:delete).once.with(
bucket,
key
)
Formatron::S3CloudFormationTemplate.destroy(
@aws,
@configuration,
target
)
end
end
end

0 comments on commit 83f828f

Please sign in to comment.