cfndsl 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/bin/cfndsl +4 -0
  2. data/lib/cfndsl.rb +255 -0
  3. metadata +47 -0
data/bin/cfndsl ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+ require 'cfndsl'
3
+ load ARGV[0]
4
+
data/lib/cfndsl.rb ADDED
@@ -0,0 +1,255 @@
1
+ require 'json';
2
+
3
+ class Module
4
+ private
5
+ def attr_setter(*symbols)
6
+ symbols.each do |symbol|
7
+ class_eval do
8
+ define_method(symbol) do |value|
9
+ instance_variable_set( "@#{symbol}", value)
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ @@plurals = {
16
+ :Metadata => :Metadata,
17
+ :Property => :Properties
18
+ }
19
+
20
+ def content_object(*symbols)
21
+ symbols.each do |symbol|
22
+ plural = @@plurals[symbol] || "#{symbol}s"
23
+ class_eval %Q/
24
+ def #{symbol} (name,*values,&block)
25
+ @#{plural} ||= {}
26
+ @#{plural}[name] ||= CfnDsl::#{symbol}Definition.new(*values)
27
+ @#{plural}[name].instance_eval &block if block_given?
28
+ end /
29
+ end
30
+ end
31
+ end
32
+
33
+
34
+ module RefCheck
35
+ def references(refs)
36
+ raise "Circular reference" if @visited
37
+
38
+ @visited = true
39
+
40
+ self.get_references(refs) if self.respond_to?(:get_references)
41
+
42
+ self.ref_children.each do |elem|
43
+ elem.references(refs) if elem.respond_to?(:references)
44
+ end
45
+ @visited = nil
46
+ end
47
+
48
+ def ref_children
49
+ return []
50
+ end
51
+
52
+ end
53
+
54
+ class Array
55
+ include RefCheck
56
+ def ref_children
57
+ return self
58
+ end
59
+ end
60
+
61
+ class Hash
62
+ include RefCheck
63
+ def ref_children
64
+ return self.values
65
+ end
66
+ end
67
+
68
+ module CfnDsl
69
+ module Functions
70
+
71
+ def Ref(value)
72
+ RefDefinition.new(value)
73
+ end
74
+
75
+ def FnBase64( value )
76
+ Fn.new("Base64", value);
77
+ end
78
+
79
+ def FnFindInMap( map, key, value)
80
+ Fn.new("FindInMap", [map,key,value] )
81
+ end
82
+
83
+ def FnGetAtt(logicalResource, attribute)
84
+ Fn.new( "GetAtt", [logicalResource, attribute] )
85
+ end
86
+
87
+ def FnGetAZs(region)
88
+ Fn.new("GetAZs", region)
89
+ end
90
+
91
+ def FnJoin(string, array)
92
+ Fn.new("Join", [ string, array] )
93
+ end
94
+ end
95
+
96
+
97
+ class JSONable
98
+ include Functions
99
+ extend Functions
100
+ include RefCheck
101
+
102
+ def to_json(*a)
103
+ hash = {}
104
+ self.instance_variables.each do |var|
105
+ name = var[1..-1]
106
+ hash[name] = self.instance_variable_get var
107
+ end
108
+ hash.to_json(*a)
109
+ end
110
+
111
+ def ref_children
112
+ return self.instance_variables.map { |var| self.instance_variable_get var }
113
+ end
114
+
115
+ end
116
+
117
+ class Fn < JSONable
118
+ def initialize( function, argument )
119
+ @function = function
120
+ @argument = argument
121
+ end
122
+
123
+ def to_json(*a)
124
+ hash = {}
125
+ hash["Fn::#{@function}"] = @argument
126
+ hash.to_json(*a)
127
+ end
128
+
129
+ def get_references( refs )
130
+ refs[ @argument[0] ] = 1 if @function=="GetAtt"
131
+ end
132
+
133
+ def ref_children
134
+ return [@argument]
135
+ end
136
+
137
+ end
138
+
139
+
140
+ class RefDefinition < JSONable
141
+ def initialize( value )
142
+ @Ref = value
143
+ end
144
+
145
+ def get_references( refs )
146
+ refs[ @Ref ] = 1
147
+ end
148
+ end
149
+
150
+
151
+ class PropertyDefinition < JSONable
152
+ def initialize(value)
153
+ @value = value;
154
+ end
155
+
156
+ def to_json(*a)
157
+ @value.to_json(*a)
158
+ end
159
+ end
160
+
161
+ class MappingDefinition < JSONable
162
+ def initialize(value)
163
+ @value = value
164
+ end
165
+
166
+ def to_json(*a)
167
+ @value.to_json(*a)
168
+ end
169
+ end
170
+
171
+ class ResourceDefinition < JSONable
172
+ attr_setter :Type, :DependsOn, :DeletionPolicy
173
+ content_object :Property, :Metadata
174
+
175
+ def get_references( refs )
176
+ if @DependsOn then
177
+ if( @DependsOn.respond_to?(:each) ) then
178
+ @DependsOn.each do |dep|
179
+ refs[ dep ] = 1
180
+ end
181
+ end
182
+
183
+ if( @DependsOn.instance_of?(String) ) then
184
+ refs[ @DependsOn ] = 1
185
+ end
186
+ end
187
+ end
188
+ end
189
+
190
+ class MetadataDefinition < JSONable
191
+ end
192
+
193
+
194
+ class ParameterDefinition < JSONable
195
+ attr_setter :Type, :Default, :NoEcho, :AllowedValues, :AllowedPattern, :MaxLength, :MinLength, :MaxValue, :MinValue, :Description, :ConstraintDescription
196
+ def initialize
197
+ @Type = :String
198
+ end
199
+
200
+ def String
201
+ @Type = :String
202
+ end
203
+
204
+ def Number
205
+ @Type = :Number
206
+ end
207
+
208
+ def CommaDelimitedList
209
+ @Type = :CommaDelimitedList
210
+ end
211
+
212
+ def to_hash()
213
+ h = {}
214
+ h[:Type] = @Type;
215
+ h[:Default] = @Default if @Default
216
+ end
217
+ end
218
+
219
+ class OutputDefinition < JSONable
220
+ attr_setter :Value, :Description
221
+
222
+ def initialize( value=nil)
223
+ @Value = value if value
224
+ end
225
+ end
226
+
227
+ class CloudFormationTemplate < JSONable
228
+ attr_setter :AWSTemplateFormatVersion, :Description
229
+ content_object :Parameter, :Output, :Resource, :Mapping
230
+
231
+ def initialize
232
+ @AWSTemplateFormatVersion = "2010-09-09"
233
+ end
234
+
235
+ def generateOutput()
236
+ puts self.to_json # uncomment for pretty printing # {:space => ' ', :indent => ' ', :object_nl => "\n", :array_nl => "\n" }
237
+ end
238
+
239
+ def dsl(&block)
240
+ self.instance_eval &block
241
+ end
242
+
243
+ end
244
+
245
+
246
+ end
247
+
248
+ def CloudFormation(&block)
249
+ x = CfnDsl::CloudFormationTemplate.new
250
+ x.dsl(&block)
251
+ x.generateOutput
252
+
253
+ end
254
+
255
+
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cfndsl
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Chris Howe
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-12-12 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: DSL for creating AWS Cloudformation templates
15
+ email: chris@howeville.com
16
+ executables:
17
+ - cfndsl
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/cfndsl.rb
22
+ - bin/cfndsl
23
+ homepage: https://github.com/howech/cfndsl
24
+ licenses: []
25
+ post_install_message:
26
+ rdoc_options: []
27
+ require_paths:
28
+ - lib
29
+ required_ruby_version: !ruby/object:Gem::Requirement
30
+ none: false
31
+ requirements:
32
+ - - ! '>='
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ required_rubygems_version: !ruby/object:Gem::Requirement
36
+ none: false
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements: []
42
+ rubyforge_project:
43
+ rubygems_version: 1.8.23
44
+ signing_key:
45
+ specification_version: 3
46
+ summary: AWS Cloudformation DSL
47
+ test_files: []