cloud_shaped 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b21a359b972abb22fad3f55b1be73a5bf6f77659
4
- data.tar.gz: 69029374c81f2afe26925332398a5c1bddbc0490
3
+ metadata.gz: 0fdc611b70fc3544aade1219d677b5048d4c53af
4
+ data.tar.gz: b0d8936622fdf2cf4b18767aa15c53c3c3a433c2
5
5
  SHA512:
6
- metadata.gz: ac6dbf62ac5e9c3b939fc920b9fd125792ea9e7e54be79496bc94886de35c0cbb292fcb16f57bd7820633aa8d63957c2644187caece0a4ef731a3ecad1e7e95a
7
- data.tar.gz: 2deb1051579c6ca6fa00809c72156c75ea48a2a3fa3bd27c385b7bc6b5a6472685a6138017599e2fcd712d731a8f7d3af669a7eac2881458daf3d2fdf0e3c63d
6
+ metadata.gz: b9b0676e9f7c504de7195197eadd4388eb9dc152a718c06b0b7e983086319148527abe29da03b9f4717b2c4783d9b5fb04d4952e6709e993e978ab46e52ac046
7
+ data.tar.gz: b9a67eb5d1a5c1c943fd0874d24822c26123b6a3d870a87420063fba10336e326a34df5a58ba63ca6c517651c332093e27a378c714d5b634d9d7b923ea69dc78
@@ -1,5 +1,6 @@
1
1
  require 'cloud_shaped/core_methods'
2
2
  require 'cloud_shaped/function_methods'
3
+ require 'cloud_shaped/interpolation'
3
4
  require 'cloud_shaped/sns_methods'
4
5
 
5
6
  module CloudShaped
@@ -7,6 +8,7 @@ module CloudShaped
7
8
 
8
9
  include CoreMethods
9
10
  include FunctionMethods
11
+ include Interpolation
10
12
  include SnsMethods
11
13
 
12
14
  extend self
@@ -0,0 +1,42 @@
1
+ require 'cloud_shaped/core_methods'
2
+ require 'cloud_shaped/function_methods'
3
+
4
+ module CloudShaped
5
+
6
+ module Interpolation
7
+
8
+ include CoreMethods
9
+ include FunctionMethods
10
+
11
+ DEFAULT_DELIMITERS =['{{','}}']
12
+
13
+ # Interpolates a String, inserting calls to "Ref" and "Fn::GetAtt".
14
+ #
15
+ # @param string [String] input string
16
+ # @param delimiters [Array] opening and closing delimter
17
+ #
18
+ def interpolate(string, delimiters = DEFAULT_DELIMITERS)
19
+ fragments = string.lines.map do |line|
20
+ interpolate_line(line, delimiters)
21
+ end.flatten
22
+ fn_join('', fragments)
23
+ end
24
+
25
+ private
26
+
27
+ def interpolate_line(line, delimiters)
28
+ open, close = delimiters
29
+ tokens = line.split(/(#{Regexp.quote(open)}[\w:.]+#{Regexp.quote(close)})/)
30
+ tokens.reject!(&:empty?)
31
+ tokens.map do |token|
32
+ if token =~ /^#{Regexp.quote(open)}([\w:]+)(?:\.(\w+))?#{Regexp.quote(close)}$/
33
+ ref($1, $2)
34
+ else
35
+ token
36
+ end
37
+ end
38
+ end
39
+
40
+ end
41
+
42
+ end
@@ -1,27 +1,28 @@
1
1
  require 'cloud_shaped/core_methods'
2
2
 
3
3
  module CloudShaped
4
-
4
+ # Methods to create sns topics
5
5
  module SnsMethods
6
-
7
6
  include CoreMethods
8
7
 
9
- def sns_topic(target)
10
- resource "AWS::SNS::Topic", "Subscription" => [
11
- { "Protocol" => sns_protocol(target), "Endpoint" => target }
8
+ def sns_topic(endpoint)
9
+ proto, target = sns_proto_target(endpoint)
10
+ resource 'AWS::SNS::Topic', 'Subscription' => [
11
+ { 'Protocol' => proto, 'Endpoint' => target }
12
12
  ]
13
13
  end
14
14
 
15
15
  private
16
16
 
17
- def sns_protocol(target)
17
+ def sns_proto_target(target)
18
18
  case target
19
19
  when /^(https?):/
20
- $1.upcase
21
- else "email"
20
+ [Regexp.last_match[1].upcase, target]
21
+ when /^(mailto):(.*)/
22
+ ['email', Regexp.last_match[2]]
23
+ else ['email', target]
22
24
  end
23
25
  end
24
-
25
26
  end
26
-
27
27
  end
28
+
@@ -8,6 +8,7 @@ module CloudShaped
8
8
 
9
9
  def initialize(settings = {})
10
10
  @parameters = {}
11
+ @mappings = {}
11
12
  @resources = {}
12
13
  @outputs = {}
13
14
  end
@@ -15,12 +16,13 @@ module CloudShaped
15
16
  # @return [Hash] a CloudFormation template as Ruby data
16
17
  #
17
18
  def template
18
- {
19
- "AWSTemplateFormatVersion" => '2010-09-09',
20
- "Parameters" => parameters,
21
- "Resources" => resources,
22
- "Outputs" => outputs
23
- }
19
+ {}.tap do |template|
20
+ template["AWSTemplateFormatVersion"] = '2010-09-09'
21
+ template["Parameters"] = parameters unless parameters.empty?
22
+ template["Mappings"] = mappings unless mappings.empty?
23
+ template["Resources"] = resources
24
+ template["Outputs"] = outputs unless outputs.empty?
25
+ end
24
26
  end
25
27
 
26
28
  include CloudShaped::DSL
@@ -40,6 +42,19 @@ module CloudShaped
40
42
  parameters[name] = parameter(options)
41
43
  end
42
44
 
45
+ # Declares a Mappping.
46
+ #
47
+ # @param name [String] the mapping name
48
+ # @param mapping [Hash] the mapping
49
+ #
50
+ # @example
51
+ # def_mapping "regionMap", "us-east-1" => { "32" => "ami-6411e20d" }
52
+ #
53
+ def def_mapping(name, mapping = {})
54
+ yield mapping if block_given?
55
+ mappings[name] = mapping
56
+ end
57
+
43
58
  # Declares a Resource.
44
59
  #
45
60
  # @param name [String] the resource name
@@ -70,6 +85,7 @@ module CloudShaped
70
85
  protected
71
86
 
72
87
  attr_reader :parameters
88
+ attr_reader :mappings
73
89
  attr_reader :resources
74
90
  attr_reader :outputs
75
91
 
@@ -1,3 +1,3 @@
1
1
  module CloudShaped
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -0,0 +1,128 @@
1
+ require 'spec_helper'
2
+
3
+ require 'cloud_shaped/interpolation'
4
+
5
+ describe CloudShaped::Interpolation do
6
+
7
+ include described_class
8
+
9
+ describe "#interpolate" do
10
+
11
+ let(:output) { interpolate(input) }
12
+
13
+ context "with a resource or parameter name" do
14
+
15
+ let(:input) { "prefix-{{myResource}}-suffix" }
16
+
17
+ it "generates a Ref" do
18
+ expect(output).to eq(
19
+ {
20
+ "Fn::Join" => [
21
+ "", [
22
+ "prefix-",
23
+ { "Ref" => "myResource" },
24
+ "-suffix"
25
+ ]
26
+ ]
27
+ }
28
+ )
29
+ end
30
+
31
+ end
32
+
33
+ context "with a built-in CloudFormation resource" do
34
+
35
+ let(:input) { "{{AWS::StackName}}" }
36
+
37
+ it "generates a Ref" do
38
+ expect(output).to eq(
39
+ {
40
+ "Fn::Join" => [
41
+ "", [
42
+ { "Ref" => "AWS::StackName" }
43
+ ]
44
+ ]
45
+ }
46
+ )
47
+ end
48
+
49
+ end
50
+
51
+ context "with a built-in CloudFormation resource surrounded by other stuff" do
52
+
53
+ let(:input) { "prefix-{{AWS::StackName}}-suffix" }
54
+
55
+ it "generates a Ref" do
56
+ expect(output).to eq(
57
+ {
58
+ "Fn::Join" => [
59
+ "", [
60
+ "prefix-",
61
+ { "Ref" => "AWS::StackName" },
62
+ "-suffix"
63
+ ]
64
+ ]
65
+ }
66
+ )
67
+ end
68
+
69
+ end
70
+
71
+ context "with a resource name and attribute" do
72
+
73
+ let(:input) { "prefix-{{loadBalancer.cname}}-suffix" }
74
+
75
+ it "generates an Fn::GetAtt" do
76
+ expect(output).to eq(
77
+ {
78
+ "Fn::Join" => [
79
+ "", [
80
+ "prefix-",
81
+ { "Fn::GetAtt" => ["loadBalancer", "cname"] },
82
+ "-suffix"
83
+ ]
84
+ ]
85
+ }
86
+ )
87
+ end
88
+
89
+ end
90
+
91
+ it "supports alternate delimiters" do
92
+ double_square_brackets = ["[[", "]]"]
93
+ expect(interpolate("[[foo]]", double_square_brackets)).to eq(
94
+ {
95
+ "Fn::Join" => [
96
+ "", [
97
+ { "Ref" => "foo" }
98
+ ]
99
+ ]
100
+ }
101
+ )
102
+ end
103
+
104
+ context "with a mix of stuff" do
105
+
106
+ let(:input) { "#!/bin/foo\n\nprefix-{{myResource}}-suffix\n" }
107
+
108
+ it "all just works" do
109
+ expect(output).to eq(
110
+ {
111
+ "Fn::Join" => [
112
+ "", [
113
+ "#!/bin/foo\n",
114
+ "\n",
115
+ "prefix-",
116
+ { "Ref" => "myResource" },
117
+ "-suffix\n"
118
+ ]
119
+ ]
120
+ }
121
+ )
122
+ end
123
+
124
+ end
125
+
126
+ end
127
+
128
+ end
@@ -22,6 +22,18 @@ describe CloudShaped::SnsMethods do
22
22
  )
23
23
  end
24
24
 
25
+ it "generates an email resource after stripping mailto:" do
26
+ output = sns_topic("mailto:root@example.com")
27
+ expect(output).to eq(
28
+ "Type" => "AWS::SNS::Topic",
29
+ "Properties" => {
30
+ "Subscription" => [
31
+ { "Protocol" => "email", "Endpoint" => "root@example.com" }
32
+ ]
33
+ }
34
+ )
35
+ end
36
+
25
37
  end
26
38
 
27
39
  context "with an HTTP URL" do
@@ -58,3 +70,4 @@ describe CloudShaped::SnsMethods do
58
70
 
59
71
  end
60
72
  end
73
+
@@ -14,9 +14,7 @@ describe CloudShaped::TemplateBuilder do
14
14
 
15
15
  expect(template).to eq(
16
16
  "AWSTemplateFormatVersion" => '2010-09-09',
17
- "Parameters" => {},
18
- "Resources" => {},
19
- "Outputs" => {}
17
+ "Resources" => {}
20
18
  )
21
19
 
22
20
  end
@@ -117,4 +115,49 @@ describe CloudShaped::TemplateBuilder do
117
115
 
118
116
  end
119
117
 
118
+ describe "#def_mapping" do
119
+
120
+ context "with a Hash" do
121
+
122
+ let(:region_map) do
123
+ {
124
+ "us-east-1" => { "32" => "ami-6411e20d"},
125
+ "us-west-1" => { "32" => "ami-c9c7978c"},
126
+ "eu-west-1" => { "32" => "ami-37c2f643"},
127
+ "ap-southeast-1" => { "32" => "ami-66f28c34"},
128
+ "ap-northeast-1" => { "32" => "ami-9c03a89d"}
129
+ }
130
+ end
131
+
132
+ before do
133
+ template_builder.def_mapping("RegionMap", region_map)
134
+ end
135
+
136
+ it "defines a Mapping" do
137
+ expect(template["Mappings"]).to eq("RegionMap" => region_map)
138
+ end
139
+
140
+ end
141
+
142
+ context "with a block" do
143
+
144
+ before do
145
+ template_builder.def_mapping("RegionMap") do |map|
146
+ map["ap-southeast-2"] = "foo"
147
+ map["us-east-1"] = "bar"
148
+ end
149
+ end
150
+
151
+ it "defines a Mapping" do
152
+ expected_mapping = {
153
+ "ap-southeast-2" => "foo",
154
+ "us-east-1" => "bar"
155
+ }
156
+ expect(template["Mappings"]).to eq("RegionMap" => expected_mapping)
157
+ end
158
+
159
+ end
160
+
161
+ end
162
+
120
163
  end
metadata CHANGED
@@ -1,55 +1,55 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cloud_shaped
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-03 00:00:00.000000000 Z
11
+ date: 2014-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ~>
17
+ - - "~>"
18
18
  - !ruby/object:Gem::Version
19
19
  version: '1.6'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ~>
24
+ - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: '1.6'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  description: CloudShaped makes it easier to generate CloudFormation templates, using
@@ -60,8 +60,8 @@ executables: []
60
60
  extensions: []
61
61
  extra_rdoc_files: []
62
62
  files:
63
- - .gitignore
64
- - .rspec
63
+ - ".gitignore"
64
+ - ".rspec"
65
65
  - Gemfile
66
66
  - LICENSE.txt
67
67
  - README.md
@@ -74,12 +74,14 @@ files:
74
74
  - lib/cloud_shaped/core_methods.rb
75
75
  - lib/cloud_shaped/dsl.rb
76
76
  - lib/cloud_shaped/function_methods.rb
77
+ - lib/cloud_shaped/interpolation.rb
77
78
  - lib/cloud_shaped/sns_methods.rb
78
79
  - lib/cloud_shaped/template_builder.rb
79
80
  - lib/cloud_shaped/version.rb
80
81
  - spec/cloud_shaped/camelate_spec.rb
81
82
  - spec/cloud_shaped/core_methods_spec.rb
82
83
  - spec/cloud_shaped/function_methods_spec.rb
84
+ - spec/cloud_shaped/interpolation_spec.rb
83
85
  - spec/cloud_shaped/sns_methods_spec.rb
84
86
  - spec/cloud_shaped/template_builder_spec.rb
85
87
  - spec/cloud_shaped_spec.rb
@@ -94,17 +96,17 @@ require_paths:
94
96
  - lib
95
97
  required_ruby_version: !ruby/object:Gem::Requirement
96
98
  requirements:
97
- - - '>='
99
+ - - ">="
98
100
  - !ruby/object:Gem::Version
99
101
  version: '0'
100
102
  required_rubygems_version: !ruby/object:Gem::Requirement
101
103
  requirements:
102
- - - '>='
104
+ - - ">="
103
105
  - !ruby/object:Gem::Version
104
106
  version: '0'
105
107
  requirements: []
106
108
  rubyforge_project:
107
- rubygems_version: 2.3.0
109
+ rubygems_version: 2.2.2
108
110
  signing_key:
109
111
  specification_version: 4
110
112
  summary: DSL for AWS CloudFormation templates.
@@ -112,6 +114,7 @@ test_files:
112
114
  - spec/cloud_shaped/camelate_spec.rb
113
115
  - spec/cloud_shaped/core_methods_spec.rb
114
116
  - spec/cloud_shaped/function_methods_spec.rb
117
+ - spec/cloud_shaped/interpolation_spec.rb
115
118
  - spec/cloud_shaped/sns_methods_spec.rb
116
119
  - spec/cloud_shaped/template_builder_spec.rb
117
120
  - spec/cloud_shaped_spec.rb