cloud_formation 0.1.0

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4945e75d75aab59b2ecfe06ab6e1a5f8851e91a1
4
+ data.tar.gz: cb00df496843abb306400d73fd68d78e4e3beb17
5
+ SHA512:
6
+ metadata.gz: 6957921b9a415ee5bd3e4899c989995dd34ca2ea11341990a6a0913f49fde3fc247b605430fb0b541397c3c3c02fa13a654c9edb6f09110f43d7010ca5615a4a
7
+ data.tar.gz: ab2c7c94e6b0fdf72325e18471d2d8ded7d2b291e3fbfd262b87a893b3349d59f8894d430626c9cb9ac4567d44797cba137955890f3236ab4f5a521fccff5a47
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in cloud_formation.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 David White
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,103 @@
1
+ # CloudFormation
2
+
3
+ A Ruby gem to create AWS CloudFormation descriptions.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'cloud_formation'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install cloud_formation
18
+
19
+ ## Usage
20
+
21
+ ### Generate a template
22
+
23
+ ```ruby
24
+ template = CloudFormation::Template.new
25
+
26
+ template.build do |t|
27
+ t.name = "stack-name"
28
+ t.description = "A description of the stack"
29
+ end
30
+
31
+ template.render
32
+ ```
33
+
34
+ Running this will generate a nicely compacted:
35
+ ```json
36
+ {"AWSTemplateFormatVersion":"2010-09-09","Name":"stack-name","Description":"A description of the stack","Resources":{}}
37
+ ```
38
+
39
+ ### Generate a DynamoDB table
40
+
41
+ Generating a DynamoDB table is as simple as:
42
+
43
+ ```ruby
44
+ template = CloudFormation::Template.new
45
+
46
+ template.build do |t|
47
+ t.name = "stack-name"
48
+ t.description = "A description of the stack"
49
+ end
50
+
51
+ dynamo_db = CloudFormation::DynamoDB.new
52
+
53
+ table = dynamo_db.build_table do |t|
54
+ t.name = "commentsTable"
55
+ t.read_capacity_units = 10
56
+ t.write_capacity_units = 5
57
+ t.hash_key = { blog_post_id: :number }
58
+ t.range_key = { comment_id: :number }
59
+ end
60
+
61
+ template.add_resource table
62
+
63
+ puts template.render pretty: true
64
+ ```
65
+
66
+ Running with the `pretty true` options will generate an easier to read version:
67
+
68
+ ```json
69
+ {
70
+ "AWSTemplateFormatVersion": "2010-09-09",
71
+ "Name": "stack-name",
72
+ "Description": "A description of the stack",
73
+ "Resources": {
74
+ "commentsTable": {
75
+ "Type": "AWS::DynamoDB::Table",
76
+ "Properties": {
77
+ "ProvisionedThroughput": {
78
+ "WriteCapacityUnits": "5",
79
+ "ReadCapacityUnits": "10"
80
+ }
81
+ },
82
+ "KeySchema": {
83
+ "RangeKeyElement": {
84
+ "AttributeName": "comment_id",
85
+ "AttributeType": "N"
86
+ },
87
+ "HashKeyElement": {
88
+ "AttributeName": "blog_post_id",
89
+ "AttributeType": "N"
90
+ }
91
+ }
92
+ }
93
+ }
94
+ }
95
+ ```
96
+
97
+ ## Contributing
98
+
99
+ 1. Fork it ( http://github.com/<my-github-username>/cloud_formation/fork )
100
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
101
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
102
+ 4. Push to the branch (`git push origin my-new-feature`)
103
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << 'lib' << 'test'
6
+ t.test_files = FileList['test/*_test.rb']
7
+ t.verbose = true
8
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'cloud_formation/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "cloud_formation"
8
+ spec.version = CloudFormation::VERSION
9
+ spec.authors = ["David White"]
10
+ spec.email = ["david.white@spry-soft.com"]
11
+ spec.summary = %q{A Ruby gem to create AWS CloudFormation descriptions.}
12
+ spec.description = spec.summary
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,8 @@
1
+ require "cloud_formation/version"
2
+
3
+ module CloudFormation
4
+ autoload :Refinements, "cloud_formation/refinements"
5
+ autoload :Base, "cloud_formation/base"
6
+ autoload :Template, "cloud_formation/template"
7
+ autoload :DynamoDB, "cloud_formation/dynamo_db"
8
+ end
@@ -0,0 +1,21 @@
1
+ module CloudFormation
2
+ class Base
3
+ def self.attribute_list *attrs
4
+ attrs.each do |name|
5
+ define_method "#{name}=".to_sym do |value|
6
+ instance_variable_set "@#{name}", value
7
+ end
8
+ define_method name.to_sym do
9
+ instance_variable_get "@#{name}"
10
+ end
11
+ end
12
+ define_method :attributes do
13
+ attrs
14
+ end
15
+ end
16
+
17
+ def build &block
18
+ yield self
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,47 @@
1
+ module CloudFormation
2
+ class DynamoDB < Base
3
+ attribute_list :name, :read_capacity_units, :write_capacity_units, :hash_key, :range_key
4
+
5
+ def build_table &block
6
+ yield self
7
+ self
8
+ end
9
+
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.merge add_key_schemas
21
+ end
22
+
23
+
24
+ private
25
+
26
+ def key_type_mapping type
27
+ case type
28
+ when :string then "S"
29
+ when :number then "N"
30
+ when :binary then "B"
31
+ end
32
+ end
33
+
34
+ def add_key_schemas
35
+ key_schema = { "KeySchema" => {}}
36
+ key_schema["KeySchema"]["RangeKeyElement"] = add_key_schema_for(@range_key) if @range_key
37
+ key_schema["KeySchema"]["HashKeyElement"] = add_key_schema_for(@hash_key) if @hash_key
38
+ key_schema
39
+ end
40
+
41
+ def add_key_schema_for key
42
+ k = key.keys.first
43
+ { "AttributeName" => k.to_s, "AttributeType" => key_type_mapping(key[k]) }
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,14 @@
1
+
2
+ module CloudFormation
3
+
4
+ module Refinements
5
+ refine String do
6
+ def camel_case
7
+ return self if self !~ /_/ && self =~ /[A-Z]+.*/
8
+ split('_').map { |e| e.capitalize }.join
9
+ end
10
+ end
11
+ end
12
+ using Refinements
13
+
14
+ end
@@ -0,0 +1,35 @@
1
+ require 'json'
2
+
3
+ module CloudFormation
4
+ class Template < Base
5
+ using Refinements
6
+ AWS_TEMPLATE_FORMAT_VERSION = "2010-09-09"
7
+
8
+ attribute_list :name, :description, :resources
9
+
10
+ def initialize
11
+ @resources = {}
12
+ end
13
+
14
+ def add_resource resource
15
+ @resources[resource.name] = resource.serialize.tap { |a| a.delete("Name") }
16
+ end
17
+
18
+ def render pretty: false, indentation: 2
19
+ pretty ? JSON.pretty_generate(serialize, indent: " "*indentation) : serialize.to_json
20
+ end
21
+
22
+ private
23
+
24
+ def serialize
25
+ attributes.inject(default_hash) do |hash, value|
26
+ hash[value.to_s.camel_case] = self.send value
27
+ hash
28
+ end
29
+ end
30
+
31
+ def default_hash
32
+ { "AWSTemplateFormatVersion" => AWS_TEMPLATE_FORMAT_VERSION }
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,3 @@
1
+ module CloudFormation
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cloud_formation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - David White
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: A Ruby gem to create AWS CloudFormation descriptions.
42
+ email:
43
+ - david.white@spry-soft.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - cloud_formation.gemspec
54
+ - lib/cloud_formation.rb
55
+ - lib/cloud_formation/base.rb
56
+ - lib/cloud_formation/dynamo_db.rb
57
+ - lib/cloud_formation/refinements.rb
58
+ - lib/cloud_formation/template.rb
59
+ - lib/cloud_formation/version.rb
60
+ homepage: ''
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.2.0
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: A Ruby gem to create AWS CloudFormation descriptions.
84
+ test_files: []