sparkle_formation 1.2.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -6,6 +6,9 @@ class SparkleFormation
6
6
  module SparkleAttribute
7
7
 
8
8
  autoload :Aws, 'sparkle_formation/sparkle_attribute/aws'
9
+ autoload :Azure, 'sparkle_formation/sparkle_attribute/azure'
10
+ autoload :Heat, 'sparkle_formation/sparkle_attribute/heat'
11
+ autoload :Rackspace, 'sparkle_formation/sparkle_attribute/rackspace'
9
12
 
10
13
  # Return current resource name
11
14
  #
@@ -88,26 +91,18 @@ class SparkleFormation
88
91
  SparkleFormation.nest(template, self, *args, &block)
89
92
  end
90
93
 
91
- # TODO: Deprecate or re-imagine
92
-
93
- # @return [TrueClass, FalseClass]
94
- def rhel?
95
- !!@platform[:rhel]
96
- end
97
-
98
- # @return [TrueClass, FalseClass]
99
- def debian?
100
- !!@platform[:debian]
101
- end
102
-
103
- # Set the destination platform
94
+ # Format the provided key. If symbol type is provided
95
+ # formatting is forced. Otherwise the default formatting
96
+ # is applied
104
97
  #
105
- # @param plat [String, Symbol] one of :rhel or :debian
106
- # @return [TrueClass]
107
- def _platform=(plat)
108
- @platform || __hashish
109
- @platform.clear
110
- @platform[plat.to_sym] = true
98
+ # @param key [String, Symbol] given key
99
+ # @return [String] formatted key
100
+ def __attribute_key(key)
101
+ if(key.is_a?(::Symbol) || key.is_a?(::String))
102
+ _process_key(key, key.is_a?(::Symbol) ? :force : nil)
103
+ else
104
+ key
105
+ end
111
106
  end
112
107
 
113
108
  end
@@ -29,8 +29,7 @@ class SparkleFormation
29
29
  # @note Symbol value will force key processing
30
30
  def _cf_ref(thing)
31
31
  __t_stringish(thing)
32
- thing = _process_key(thing, :force) if thing.is_a?(Symbol)
33
- {'Ref' => thing}
32
+ {'Ref' => __attribute_key(thing)}
34
33
  end
35
34
  alias_method :_ref, :_cf_ref
36
35
  alias_method :ref!, :_cf_ref
@@ -50,7 +49,7 @@ class SparkleFormation
50
49
  item
51
50
  end
52
51
  end
53
- thing = _process_key(thing, :force) if thing.is_a?(Symbol)
52
+ thing = __attribute_key(thing)
54
53
  if(key.is_a?(Symbol))
55
54
  key = ref!(key)
56
55
  end
@@ -65,7 +64,9 @@ class SparkleFormation
65
64
  # @param [Object] pass through arguments
66
65
  # @return [Hash]
67
66
  def _cf_attr(*args)
68
- __t_stringish(args.first)
67
+ r_name = args.first
68
+ args = args.slice(1, args.size)
69
+ __t_stringish(r_name)
69
70
  args = args.map do |thing|
70
71
  if(thing.is_a?(Symbol))
71
72
  _process_key(thing, :force)
@@ -73,7 +74,7 @@ class SparkleFormation
73
74
  thing
74
75
  end
75
76
  end
76
- {'Fn::GetAtt' => args}
77
+ {'Fn::GetAtt' => [__attribute_key(r_name), *args]}
77
78
  end
78
79
  alias_method :_cf_get_att, :_cf_attr
79
80
  alias_method :get_att!, :_cf_attr
@@ -124,7 +125,7 @@ class SparkleFormation
124
125
  # @return [Hash]
125
126
  def _condition(name)
126
127
  __t_stringish(name)
127
- {'Condition' => name.is_a?(Symbol) ? _process_key(name, :force) : name}
128
+ {'Condition' => __attribute_key(name)}
128
129
  end
129
130
  alias_method :condition!, :_condition
130
131
 
@@ -146,8 +147,7 @@ class SparkleFormation
146
147
  # @param false_value [Object]
147
148
  # @return [Hash]
148
149
  def _if(cond, true_value, false_value)
149
- cond = cond.is_a?(Symbol) ? _process_key(cond) : cond
150
- {'Fn::If' => _array(cond, true_value, false_value)}
150
+ {'Fn::If' => _array(__attribute_key(cond), true_value, false_value)}
151
151
  end
152
152
  alias_method :if!, :_if
153
153
 
@@ -269,7 +269,7 @@ class SparkleFormation
269
269
  # @param [Symbol, String, Array<Symbol, String>] resource names
270
270
  # @return [Array<String>]
271
271
  def _depends_on(*args)
272
- _set('DependsOn', [args].flatten.compact.map{|s| _process_key(s)})
272
+ _set('DependsOn', [args].flatten.compact.map{|s| __attribute_key(s)})
273
273
  end
274
274
  alias_method :depends_on!, :_depends_on
275
275
 
@@ -278,7 +278,7 @@ class SparkleFormation
278
278
  # @param stack_name [String, Symbol] logical resource name of stack
279
279
  # @apram output_name [String, Symbol] stack output name
280
280
  def _stack_output(stack_name, output_name)
281
- _cf_attr(_process_key(stack_name), "Outputs.#{_process_key(output_name)}")
281
+ _cf_attr(_process_key(stack_name), "Outputs.#{__attribute_key(output_name)}")
282
282
  end
283
283
  alias_method :stack_output!, :_stack_output
284
284
 
@@ -299,10 +299,10 @@ class SparkleFormation
299
299
  # @param hash [Hash] Key/value pair tags
300
300
  # @return [SparkleStruct]
301
301
  def _tags(hash)
302
+ __t_hashish(hash)
302
303
  _set('Tags',
303
304
  hash.map{ |k, v|
304
- key = k.is_a?(Symbol) ? _process_key(k, :force) : k
305
- {'Key' => key, 'Value' => v}
305
+ {'Key' => __attribute_key(k), 'Value' => v}
306
306
  }
307
307
  )
308
308
  end
@@ -0,0 +1,161 @@
1
+ require 'sparkle_formation'
2
+
3
+ class SparkleFormation
4
+
5
+ # Provides template helper methods
6
+ module SparkleAttribute
7
+
8
+ # Azure specific helper implementations
9
+ module Azure
10
+
11
+ # Extract resources Hash from template dump and transform to
12
+ # Array type expected by the ARM API
13
+ #
14
+ # @param hash [Hash] template dump
15
+ # @return [Hash]
16
+ def self.resources_formatter(hash)
17
+ if(hash.key?('resources') && !hash['resources'].is_a?(Array))
18
+ resources = hash.delete('resources')
19
+ hash['resources'] = Array.new
20
+ resources.each do |r_name, r_contents|
21
+ hash['resources'].push(
22
+ r_contents.merge('name' => r_name)
23
+ )
24
+ end
25
+ end
26
+ hash
27
+ end
28
+
29
+ # Inject camel style on module inclusion
30
+ # Add custom dump functionality to properly set resources
31
+ def self.included(klass)
32
+ klass.const_set(:CAMEL_STYLE, :no_leading)
33
+
34
+ klass.class_eval do
35
+ def _azure_dump
36
+ result = _attribute_struct_dump
37
+ if(_parent.nil?)
38
+ result = ::SparkleFormation::SparkleAttribute::Azure.resources_formatter(result)
39
+ end
40
+ result
41
+ end
42
+ alias_method :_attribute_struct_dump, :_dump
43
+ alias_method :_dump, :_azure_dump
44
+ alias_method :dump!, :_azure_dump
45
+ end
46
+ end
47
+
48
+ # Valid azure builtin functions
49
+ AZURE_FUNCTIONS = [
50
+ 'add',
51
+ 'copyIndex',
52
+ 'div',
53
+ 'int',
54
+ 'length',
55
+ 'mod',
56
+ 'mul',
57
+ 'sub',
58
+ 'base64',
59
+ 'concat',
60
+ 'padLeft',
61
+ 'replace',
62
+ 'split',
63
+ 'string',
64
+ 'substring',
65
+ 'toLower',
66
+ 'toUpper',
67
+ 'trim',
68
+ 'uniqueString',
69
+ 'uri',
70
+ 'deployment',
71
+ 'parameters',
72
+ 'variables',
73
+ 'listKeys',
74
+ 'providers',
75
+ 'reference',
76
+ 'resourceGroup',
77
+ 'subscription'
78
+ ]
79
+
80
+ # Generate a builtin azure function
81
+ #
82
+ # @return [SparkleFormation::FunctionStruct]
83
+ def _fn_format(*args)
84
+ src = ::Kernel.__callee__.to_s
85
+ src = ::Bogo::Utility.camel(src.sub(/(^_|\!$)/, ''), false)
86
+ ::SparkleFormation::FunctionStruct.new(src, *args)
87
+ end
88
+
89
+ AZURE_FUNCTIONS.map do |f_name|
90
+ ::Bogo::Utility.snake(f_name)
91
+ end.each do |f_name|
92
+ alias_method "_#{f_name}".to_sym, :_fn_format
93
+ alias_method "#{f_name}!".to_sym, :_fn_format
94
+ end
95
+
96
+ # Customized resourceId generator that will perform automatic
97
+ # lookup on defined resources for building the function if Symbol
98
+ # type is provided
99
+ #
100
+ # @param args [Object]
101
+ # @return [FunctionStruct]
102
+ def _resource_id(*args)
103
+ if(args.size > 1)
104
+ ::SparkleFormation::FunctionStruct.new('resourceId', *args)
105
+ else
106
+ r_name = args.first
107
+ resource = _root.resources.set!(r_name)
108
+ if(resource.nil?)
109
+ ::Kernel.raise ::SparkleFormation::Error::NotFound::Resource.new(:name => r_name)
110
+ else
111
+ ::SparkleFormation::FunctionStruct.new(
112
+ 'resourceId',
113
+ resource.type,
114
+ resource.resource_name!
115
+ )
116
+ end
117
+ end
118
+ end
119
+ alias_method :resource_id!, :_resource_id
120
+
121
+ # Customized dependsOn generator. Will automatically build resource
122
+ # reference value using defined resources for Symbol type values. Sets
123
+ # directly into current context.
124
+ #
125
+ # @param args [Object]
126
+ # @return [Array<String>]
127
+ def _depends_on(*args)
128
+ args = args.map do |item|
129
+ case item
130
+ when ::Symbol
131
+ resource = _root.resources.set!(item)
132
+ if(resource.nil?)
133
+ ::Kernel.raise ::SparkleFormation::Error::NotFound::Resource.new(:name => item)
134
+ else
135
+ [resource.type, resource.resource_name!].join('/')
136
+ end
137
+ else
138
+ item
139
+ end
140
+ end
141
+ set!(:depends_on, args)
142
+ end
143
+ alias_method :depends_on!, :_depends_on
144
+
145
+ # Reference output value from nested stack
146
+ #
147
+ # @param stack_name [String, Symbol] logical resource name of stack
148
+ # @param output_name [String, Symbol] stack output name
149
+ # @return [Hash]
150
+ def _stack_output(stack_name, output_name)
151
+ stack_name = __attribute_key(stack_name)
152
+ output_name = __attribute_key(output_name)
153
+ o_root = _reference(stack_name)
154
+ o_root.outputs.set!(output_name).value
155
+ o_root
156
+ end
157
+ alias_method :stack_output!, :_stack_output
158
+
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,177 @@
1
+ require 'sparkle_formation'
2
+
3
+ class SparkleFormation
4
+
5
+ # Provides template helper methods
6
+ module SparkleAttribute
7
+
8
+ # Heat specific helper implementations
9
+ module Heat
10
+
11
+ # Set customized struct behavior
12
+ def self.included(klass)
13
+ klass.const_set(:CAMEL_KEYS, false)
14
+ end
15
+
16
+ # get_attr generator
17
+ #
18
+ # @param [Object] pass through arguments
19
+ # @return [Hash]
20
+ def _get_attr(*args)
21
+ __t_stringish(args.first)
22
+ args = args.map do |thing|
23
+ __attribute_key(thing)
24
+ end
25
+ {'get_attr' => args}
26
+ end
27
+ alias_method :_attr, :_get_attr
28
+ alias_method :attr!, :_get_attr
29
+
30
+ # list_join generator
31
+ #
32
+ # @param args [Object]
33
+ # @return [Hash]
34
+ def _list_join(*args)
35
+ options = args.detect{|i| i.is_a?(::Hash) && i[:options]} || {:options => {}}
36
+ args.delete(options)
37
+ unless(args.size == 1)
38
+ args = [args]
39
+ end
40
+ {'list_join' => [options[:options][:delimiter] || '', *args]}
41
+ end
42
+ alias_method :_join, :_list_join
43
+ alias_method :join!, :_list_join
44
+
45
+ # get_file generator
46
+ #
47
+ # @param loc [String]
48
+ # @return [Hash]
49
+ def _get_file(loc)
50
+ __t_string(loc)
51
+ {'get_file' => loc}
52
+ end
53
+ alias_method :_file, :_get_file
54
+ alias_method :file!, :_get_file
55
+
56
+ # get_param generator
57
+ #
58
+ # @param args [Object]
59
+ # @return [Hash]
60
+ def _get_param(*args)
61
+ __t_stringish(args.first)
62
+ args = args.map do |thing|
63
+ __attribute_key(thing)
64
+ end
65
+ {'get_param' => args.size == 1 ? args.first : args}
66
+ end
67
+ alias_method :_param, :_get_param
68
+ alias_method :param!, :_get_param
69
+
70
+ # get_resource generator
71
+ #
72
+ # @param r_name [String, Symbol]
73
+ # @return [Hash]
74
+ def _get_resource(r_name)
75
+ __t_stringish(r_name)
76
+ {'get_resource' => __attribute_key(r_name)}
77
+ end
78
+ alias_method :_resource, :_get_resource
79
+ alias_method :resource!, :_get_resource
80
+
81
+ def _digest(value, algorithm='sha512')
82
+ __t_string(algorithm)
83
+ {'digest' => [algorithm, value]}
84
+ end
85
+ alias_method :digest!, :_digest
86
+
87
+ # resource_facade generator
88
+ #
89
+ # @param type [String, Symbol]
90
+ # @return [Hash]
91
+ def _resource_facade(type)
92
+ __t_stringish(type)
93
+ {'resource_facade' => type}
94
+ end
95
+ alias_method :_facade, :_resource_facade
96
+ alias_method :facade!, :_resource_facade
97
+ alias_method :resource_facade!, :_resource_facade
98
+
99
+ # str_replace generator
100
+ #
101
+ # @param template [String]
102
+ # @param params [Hash]
103
+ # @return [Hash]
104
+ def _str_replace(template, params)
105
+ __t_stringish(template)
106
+ __t_hashish(params)
107
+ {'str_replace' => {'template' => template, 'params' => params}}
108
+ end
109
+ alias_method :_replace, :_str_replace
110
+ alias_method :replace!, :_str_replace
111
+
112
+ # str_split generator
113
+ #
114
+ # @param splitter [String]
115
+ # @param string [Object]
116
+ # @param idx [Numeric]
117
+ # @return [Hash]
118
+ def _str_split(splitter, string, idx=nil)
119
+ __t_string(splitter)
120
+ {'str_split' => [splitter, string, idx].compact}
121
+ end
122
+ alias_method :_split, :_str_split
123
+ alias_method :split!, :_str_split
124
+
125
+ # map_merge generator
126
+ #
127
+ # @param args [Object]
128
+ # @return [Hash]
129
+ def _map_merge(*args)
130
+ {'map_merge' => args}
131
+ end
132
+ alias_method :map_merge!, :_map_merge
133
+
134
+ # @return [Hash]
135
+ def _stack_id
136
+ _get_param('OS::stack_id')
137
+ end
138
+ alias_method :stack_id!, :_stack_id
139
+
140
+ # @return [Hash]
141
+ def _stack_name
142
+ _get_param('OS::stack_name')
143
+ end
144
+ alias_method :stack_name!, :_stack_name
145
+
146
+ # @return [Hash]
147
+ def _project_id
148
+ _get_param('OS::project_id')
149
+ end
150
+ alias_method :project_id!, :_project_id
151
+
152
+ # Resource dependency generator
153
+ #
154
+ # @param [Symbol, String, Array<Symbol, String>] resource names
155
+ # @return [Array<String>]
156
+ def _depends_on(*args)
157
+ _set('depends_on', [args].flatten.compact.map{|s| __attribute_key(s) })
158
+ end
159
+ alias_method :depends_on!, :_depends_on
160
+
161
+ # Reference output value from nested stack
162
+ #
163
+ # @param stack_name [String, Symbol] logical resource name of stack
164
+ # @param output_name [String, Symbol] stack output name
165
+ # @return [Hash]
166
+ def _stack_output(stack_name, output_name)
167
+ _attr(
168
+ __attribute_key(stack_name),
169
+ __attribute_key(output_name)
170
+ )
171
+ end
172
+ alias_method :stack_output!, :_stack_output
173
+
174
+ end
175
+
176
+ end
177
+ end