cloud_formation 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7947c1d9043cf5c8121895f04ea632dee1db5d5f
4
- data.tar.gz: be5a73638e103ddafa4a018b5cfef1ff13a53572
3
+ metadata.gz: 5c28700bca1d3dad061e12b10d3587e60e2248f1
4
+ data.tar.gz: 2faf4c04795ecdcf492a00ebe4665aa9e51ef75a
5
5
  SHA512:
6
- metadata.gz: 7fd95397e280ef0232a3dab26df66ed336418af34a8ad2391332bad492c8bb4ad9f55375d265167a56d011a52e043ecf2433f0f558450629f0a3847c3ac784a6
7
- data.tar.gz: d57179e2befc0fc92e4118328ae88895d6a3e90581b75bfb635834e821ef04e3ee1e395668b9af9c30ce97616b186a9a7ed6ee80df92afce1fa49e5411f430d1
6
+ metadata.gz: 8b2174afbd62a8509242de4f1dd17c95e0fdf75e85dbb9a1d4f8990edeba4470bb2e2a65506b1cd39c252f4015e9119e86be15dabc9598aae99c6143571a4787
7
+ data.tar.gz: 1adad111f96331fb195abfe6685dfd2f4001a8dc50aff8458917b4a1b20f62f311b6c7115c635748ec9ee10b30f253911c86138cb541b7b99c165c8e276b38bc
@@ -1,47 +1,60 @@
1
1
  module CloudFormation
2
2
  class DynamoDB < Base
3
- attribute_list :name, :read_capacity_units, :write_capacity_units, :hash_key, :range_key
4
3
 
5
- def build_table &block
6
- yield self
7
- self
8
- end
4
+ attr_accessor :tables
9
5
 
10
- def serialize
11
- properties = {
12
- "Type" => "AWS::DynamoDB::Table",
13
- "Properties" => {
14
- "ProvisionedThroughput" => {
15
- "WriteCapacityUnits" => @write_capacity_units.to_s,
16
- "ReadCapacityUnits" => @read_capacity_units.to_s
17
- }
18
- }
19
- }
20
- properties["Properties"].merge! add_key_schemas
21
- properties
6
+ def initialize
7
+ @tables = []
22
8
  end
23
9
 
10
+ def build_table &block
11
+ table = Table.new
12
+ yield table
13
+ @tables << table
14
+ table
15
+ end
24
16
 
25
17
  private
26
18
 
27
- def key_type_mapping type
28
- case type
29
- when :string then "S"
30
- when :number then "N"
31
- when :binary then "B"
19
+ class Table < Base
20
+ attribute_list :name, :read_capacity_units, :write_capacity_units, :hash_key, :range_key
21
+
22
+ def serialize
23
+ properties = {
24
+ "Type" => "AWS::DynamoDB::Table",
25
+ "Properties" => {
26
+ "ProvisionedThroughput" => {
27
+ "WriteCapacityUnits" => @write_capacity_units.to_s,
28
+ "ReadCapacityUnits" => @read_capacity_units.to_s
29
+ }
30
+ }
31
+ }
32
+ properties["Properties"].merge! add_key_schemas
33
+ properties
32
34
  end
33
- end
34
35
 
35
- def add_key_schemas
36
- key_schema = { "KeySchema" => {}}
37
- key_schema["KeySchema"]["RangeKeyElement"] = add_key_schema_for(@range_key) if @range_key
38
- key_schema["KeySchema"]["HashKeyElement"] = add_key_schema_for(@hash_key) if @hash_key
39
- key_schema
40
- end
36
+ private
37
+
38
+ def key_type_mapping type
39
+ case type
40
+ when :string then "S"
41
+ when :number then "N"
42
+ when :binary then "B"
43
+ end
44
+ end
45
+
46
+ def add_key_schemas
47
+ key_schema = { "KeySchema" => {}}
48
+ key_schema["KeySchema"]["RangeKeyElement"] = add_key_schema_for(@range_key) if @range_key
49
+ key_schema["KeySchema"]["HashKeyElement"] = add_key_schema_for(@hash_key) if @hash_key
50
+ key_schema
51
+ end
52
+
53
+ def add_key_schema_for key
54
+ k = key.keys.first
55
+ { "AttributeName" => k.to_s, "AttributeType" => key_type_mapping(key[k]) }
56
+ end
41
57
 
42
- def add_key_schema_for key
43
- k = key.keys.first
44
- { "AttributeName" => k.to_s, "AttributeType" => key_type_mapping(key[k]) }
45
58
  end
46
59
 
47
60
  end
@@ -15,6 +15,10 @@ module CloudFormation
15
15
  @resources[resource.name] = resource.serialize.tap { |a| a.delete("Name") }
16
16
  end
17
17
 
18
+ def add_resources resources
19
+ resources.each { |resource| add_resource resource }
20
+ end
21
+
18
22
  def render pretty: false, indentation: 2
19
23
  pretty ? JSON.pretty_generate(serialize, indent: " "*indentation) : serialize.to_json
20
24
  end
@@ -1,3 +1,3 @@
1
1
  module CloudFormation
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -3,19 +3,25 @@ require 'test_helper'
3
3
  class DynamoDBTest < Minitest::Test
4
4
 
5
5
  def setup
6
- @dynamo_db = CloudFormation::DynamoDB.new
6
+ @table = build_dynamo_table_instance
7
+ end
8
+
9
+ def test_build_table_should_return_table_class
10
+ dynamo_db = CloudFormation::DynamoDB.new
11
+ table = build_dynamo_table_instance
12
+ assert_instance_of CloudFormation::DynamoDB::Table, table
13
+ end
14
+
15
+ def test_build_table_should_add_table_to_tables_array
16
+ dynamo_db = CloudFormation::DynamoDB.new
17
+ assert_empty dynamo_db.tables
7
18
 
8
- @dynamo_db.build_table do |t|
9
- t.name = "tableName"
10
- t.read_capacity_units = 10
11
- t.write_capacity_units = 5
12
- t.hash_key = { column_one_id: :number }
13
- t.range_key = { column_two_id: :number }
14
- end
19
+ build_dynamo_table_instance dynamo_db
20
+ assert_equal 1, dynamo_db.tables.length
15
21
  end
16
22
 
17
23
  def test_basic
18
- expected = @dynamo_db.serialize
24
+ expected = @table.serialize
19
25
  assert_equal load_fixture("dynamo_db/basic"), expected
20
26
  end
21
27
 
@@ -0,0 +1,39 @@
1
+ module FactoryHelpers
2
+
3
+ def build_template_instance opts = {}
4
+ options = {
5
+ name: "stack-name",
6
+ description: "A description of the stack"
7
+ }
8
+ options.merge! opts
9
+ template = CloudFormation::Template.new
10
+
11
+ template.build do |t|
12
+ t.name = options[:name]
13
+ t.description = options[:description]
14
+ end
15
+ template
16
+ end
17
+
18
+ def build_dynamo_table_instance db = nil, opts = {}
19
+ db = CloudFormation::DynamoDB.new if db === nil
20
+ options = {
21
+ name: "tableName",
22
+ read_capacity_units: 10,
23
+ write_capacity_units: 5,
24
+ hash_key: { column_one_id: :number },
25
+ range_key: { column_two_id: :number }
26
+
27
+ }
28
+ options.merge! opts
29
+
30
+ table = db.build_table do |t|
31
+ t.name = options[:name]
32
+ t.read_capacity_units = options[:read_capacity_units]
33
+ t.write_capacity_units = options[:write_capacity_units]
34
+ t.hash_key = options[:hash_key]
35
+ t.range_key = options[:range_key]
36
+ end
37
+ table
38
+ end
39
+ end
@@ -3,12 +3,7 @@ require 'test_helper'
3
3
  class TemplateTest < Minitest::Test
4
4
 
5
5
  def setup
6
- @template = CloudFormation::Template.new
7
-
8
- @template.build do |t|
9
- t.name = "stack-name"
10
- t.description = "A description of the stack"
11
- end
6
+ @template = build_template_instance
12
7
  end
13
8
 
14
9
  def test_default
@@ -21,4 +16,30 @@ class TemplateTest < Minitest::Test
21
16
  assert_equal load_fixture("template/default"), JSON.parse(rendered)
22
17
  end
23
18
 
19
+ def test_adding_single_resource
20
+ template = build_template_instance
21
+ table = build_dynamo_table_instance
22
+
23
+ template.add_resource table
24
+ rendered = template.render pretty: true
25
+
26
+ assert_equal load_fixture("template/adding_single_resource"), JSON.parse(rendered)
27
+ end
28
+
29
+
30
+ def test_adding_multiple_resources
31
+ template = build_template_instance
32
+ dynamo_db = CloudFormation::DynamoDB.new
33
+
34
+ build_dynamo_table_instance dynamo_db
35
+ build_dynamo_table_instance dynamo_db, name: "tableName2"
36
+
37
+
38
+ template.add_resources dynamo_db.tables
39
+
40
+ rendered = template.render pretty: true
41
+
42
+ assert_equal load_fixture("template/adding_multiple_resources"), JSON.parse(rendered)
43
+ end
44
+
24
45
  end
@@ -0,0 +1,45 @@
1
+ {
2
+ "AWSTemplateFormatVersion": "2010-09-09",
3
+ "Name": "stack-name",
4
+ "Description": "A description of the stack",
5
+ "Resources": {
6
+ "tableName": {
7
+ "Type": "AWS::DynamoDB::Table",
8
+ "Properties": {
9
+ "ProvisionedThroughput": {
10
+ "WriteCapacityUnits": "5",
11
+ "ReadCapacityUnits": "10"
12
+ },
13
+ "KeySchema": {
14
+ "RangeKeyElement": {
15
+ "AttributeName": "column_two_id",
16
+ "AttributeType": "N"
17
+ },
18
+ "HashKeyElement": {
19
+ "AttributeName": "column_one_id",
20
+ "AttributeType": "N"
21
+ }
22
+ }
23
+ }
24
+ },
25
+ "tableName2": {
26
+ "Type": "AWS::DynamoDB::Table",
27
+ "Properties": {
28
+ "ProvisionedThroughput": {
29
+ "WriteCapacityUnits": "5",
30
+ "ReadCapacityUnits": "10"
31
+ },
32
+ "KeySchema": {
33
+ "RangeKeyElement": {
34
+ "AttributeName": "column_two_id",
35
+ "AttributeType": "N"
36
+ },
37
+ "HashKeyElement": {
38
+ "AttributeName": "column_one_id",
39
+ "AttributeType": "N"
40
+ }
41
+ }
42
+ }
43
+ }
44
+ }
45
+ }
@@ -0,0 +1,26 @@
1
+ {
2
+ "AWSTemplateFormatVersion": "2010-09-09",
3
+ "Name": "stack-name",
4
+ "Description": "A description of the stack",
5
+ "Resources": {
6
+ "tableName": {
7
+ "Type": "AWS::DynamoDB::Table",
8
+ "Properties": {
9
+ "ProvisionedThroughput": {
10
+ "WriteCapacityUnits": "5",
11
+ "ReadCapacityUnits": "10"
12
+ },
13
+ "KeySchema": {
14
+ "RangeKeyElement": {
15
+ "AttributeName": "column_two_id",
16
+ "AttributeType": "N"
17
+ },
18
+ "HashKeyElement": {
19
+ "AttributeName": "column_one_id",
20
+ "AttributeType": "N"
21
+ }
22
+ }
23
+ }
24
+ }
25
+ }
26
+ }
data/test/test_helper.rb CHANGED
@@ -3,6 +3,10 @@ require 'minitest/pride'
3
3
  require 'cloud_formation'
4
4
  require 'json'
5
5
 
6
+ require 'factories/factory_helpers'
7
+
8
+ include FactoryHelpers
9
+
6
10
  def load_fixture path
7
11
  JSON.parse File.read("test/templates/#{path}.json")
8
12
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloud_formation
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David White
@@ -58,8 +58,11 @@ files:
58
58
  - lib/cloud_formation/template.rb
59
59
  - lib/cloud_formation/version.rb
60
60
  - test/dynamo_db_test.rb
61
+ - test/factories/factory_helpers.rb
61
62
  - test/template_test.rb
62
63
  - test/templates/dynamo_db/basic.json
64
+ - test/templates/template/adding_multiple_resources.json
65
+ - test/templates/template/adding_single_resource.json
63
66
  - test/templates/template/default.json
64
67
  - test/templates/template/pretty.json
65
68
  - test/test_helper.rb
@@ -89,8 +92,11 @@ specification_version: 4
89
92
  summary: A Ruby gem to create AWS CloudFormation descriptions.
90
93
  test_files:
91
94
  - test/dynamo_db_test.rb
95
+ - test/factories/factory_helpers.rb
92
96
  - test/template_test.rb
93
97
  - test/templates/dynamo_db/basic.json
98
+ - test/templates/template/adding_multiple_resources.json
99
+ - test/templates/template/adding_single_resource.json
94
100
  - test/templates/template/default.json
95
101
  - test/templates/template/pretty.json
96
102
  - test/test_helper.rb