Skip to content

Commit

Permalink
can now delete config from S3
Browse files Browse the repository at this point in the history
  • Loading branch information
pghalliday committed Oct 28, 2015
1 parent d567aa2 commit 5242225
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 20 deletions.
1 change: 1 addition & 0 deletions lib/formatron.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'formatron/aws'
require 'formatron/configuration'
require 'formatron/s3_path'
require 'formatron/s3_configuration'
require 'formatron/s3_cloud_formation_template'
require 'formatron/cloud_formation_stack'
Expand Down
7 changes: 7 additions & 0 deletions lib/formatron/aws.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ def upload(kms_key, bucket, key, content)
)
end

def delete(bucket, key)
@s3_client.delete_object(
bucket: bucket,
key: key
)
end

def _create_aws_credentials(credentials)
::Aws::Credentials.new(
credentials['access_key_id'],
Expand Down
19 changes: 13 additions & 6 deletions lib/formatron/configuration/formatronfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,27 @@ def _initialize_dsl(directory)
end

def _initialize_bootstrap
dsl_bootstrap = DSL::Bootstrap.new(
@target,
@config,
@dsl.bootstrap
)
dsl_bootstrap = _dsl_bootstrap
@bootstrap = Bootstrap.new(
dsl_bootstrap.protect,
dsl_bootstrap.kms_key
)
end

def _dsl_bootstrap
DSL::Bootstrap.new(
@target,
@config,
@name,
@bucket,
@dsl.bootstrap
)
end

private(
:_initialize_dsl,
:_initialize_bootstrap
:_initialize_bootstrap,
:_dsl_bootstrap
)
end
end
Expand Down
10 changes: 8 additions & 2 deletions lib/formatron/s3_configuration.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
require 'formatron/s3_path'

class Formatron
# manage the configuration stored on S3
module S3Configuration
def self.deploy(aws, configuration, target)
aws.upload(
configuration.kms_key(target),
configuration.bucket(target),
File.join(target, configuration.name(target), 'config.json'),
S3Path.path(configuration, target, 'config.json'),
configuration.config(target).to_json
)
end

def self.destroy(_aws, _formatronfile, _target)
def self.destroy(aws, configuration, target)
aws.delete(
configuration.bucket(target),
S3Path.path(configuration, target, 'config.json')
)
end
end
end
16 changes: 16 additions & 0 deletions lib/formatron/s3_path.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Formatron
# defines the S3 bucket keys for consistency
module S3Path
def self.path(configuration, target, sub_path)
File.join _base_path(configuration, target), sub_path
end

def self._base_path(configuration, target)
File.join target, configuration.name(target)
end

private_class_method(
:_base_path
)
end
end
16 changes: 16 additions & 0 deletions spec/formatron/aws_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,21 @@
)
end
end

describe '#delete' do
bucket = 'bucket'
key = 'key'

it 'should recursively delete the key from the bucket' do
expect(@s3_client).to receive(:delete_object).once.with(
bucket: bucket,
key: key
)
@aws.delete(
bucket,
key
)
end
end
end
end
2 changes: 2 additions & 0 deletions spec/formatron/configuration/formatronfile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
expect(dsl_bootstrap_class).to receive(:new).once.with(
target,
config,
name,
bucket,
bootstrap_block
) { dsl_bootstrap }
expect(dsl_bootstrap).to receive(:protect).once.with(
Expand Down
5 changes: 0 additions & 5 deletions spec/formatron/s3_cloud_formation_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,4 @@
skip 'should do something' do
end
end

describe '::destroy' do
skip 'should do something' do
end
end
end
34 changes: 28 additions & 6 deletions spec/formatron/s3_configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@
target = 'target'
kms_key = 'kms_key'
bucket = 'bucket'
name = 'name'
config = {
'param' => 'param'
}
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
it 'should upload the JSON configuration to S3' do
expect(@configuration).to receive(:name).once.with(
target
) { name }
expect(@s3_path).to receive(:path).once.with(
@configuration,
target,
'config.json'
) { key }
expect(@configuration).to receive(:kms_key).once.with(
target
) { kms_key }
Expand All @@ -35,7 +40,7 @@
expect(@aws).to receive(:upload).once.with(
kms_key,
bucket,
File.join(target, name, 'config.json'),
key,
config.to_json
)
Formatron::S3Configuration.deploy(
Expand All @@ -47,7 +52,24 @@
end

describe '::destroy' do
skip 'should do something' do
it 'should delete the JSON configuration from S3' do
expect(@s3_path).to receive(:path).once.with(
@configuration,
target,
'config.json'
) { key }
expect(@configuration).to receive(:bucket).once.with(
target
) { bucket }
expect(@aws).to receive(:delete).once.with(
bucket,
key
)
Formatron::S3Configuration.destroy(
@aws,
@configuration,
target
)
end
end
end
27 changes: 27 additions & 0 deletions spec/formatron/s3_path_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'spec_helper'
require 'formatron/aws'
require 'formatron/configuration'
require 'formatron/s3_path'

describe Formatron::S3Path do
target = 'target'
name = 'name'
sub_path = 'sub_path'

before(:each) do
@aws = instance_double 'Formatron::AWS'
@configuration = instance_double 'Formatron::Configuration'
end

describe '::path' do
it 'should create a standard path including the ' \
'configuration name and target' do
expect(@configuration).to receive(:name).once.with(
target
) { name }
expect(Formatron::S3Path.path(@configuration, target, sub_path)).to eql(
File.join(target, name, sub_path)
)
end
end
end
2 changes: 1 addition & 1 deletion spec/formatron_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
end
end

describe '#provision' do
describe '#destroy' do
before(:each) do
@s3_configuration = class_double(
'Formatron::S3Configuration'
Expand Down

0 comments on commit 5242225

Please sign in to comment.