Class: CM::Plugin::Resource

Inherits:
Object
  • Object
show all
Defined in:
lib/core/plugin/resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.options(action) ⇒ Object



12
13
14
# File 'lib/core/plugin/resource.rb', line 12

def self.options(action)
  # Override if needed
end

.register_idsObject




8
9
10
# File 'lib/core/plugin/resource.rb', line 8

def self.register_ids
  [ :id ]
end

Instance Method Details

#action_settingsObject




69
70
71
# File 'lib/core/plugin/resource.rb', line 69

def action_settings
  plan.action_settings
end

#create_resource {|data| ... } ⇒ Object


Yields:



148
149
150
151
152
# File 'lib/core/plugin/resource.rb', line 148

def create_resource
  data = {}
  yield(data) if block_given?
  data
end

#dataObject




89
90
91
# File 'lib/core/plugin/resource.rb', line 89

def data
  hash(@data)
end

#data=(data) ⇒ Object



93
94
95
# File 'lib/core/plugin/resource.rb', line 93

def data=data
  @data = hash(data)
end

#delete_resource {|data| ... } ⇒ Object

Yields:



166
167
168
169
170
# File 'lib/core/plugin/resource.rb', line 166

def delete_resource
  data = {}
  yield(data) if block_given?
  data
end

#execute(operation) ⇒ Object


Operations



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/core/plugin/resource.rb', line 110

def execute(operation)
  if initialized?
    method = "operation_#{operation}"

    myself.status = code.success

    execute_functions
    interpolate_parameters

    if respond_to?(method)
      myself.data = send(method)
      set_tokens(myself.data)
    end
  end
  myself.status == code.success
end

#execute_functionsObject


Utilities



175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'lib/core/plugin/resource.rb', line 175

def execute_functions
  execute_value = lambda do |value|
    if value.is_a?(String) && match = value.match(/\<\<([^\>]+)\>\>/)
      match.captures.each do |function_id|
        function_components = function_id.split(':')
        function_provider = function_components.shift
        function_args = function_components

        function = CM.function({}, function_provider)
        rendered_output = function.execute(function_args)

        value.gsub!(/\<\<#{function_id}\>\>/, rendered_output)
      end
    end
    value
  end

  execute = lambda do |settings|
    settings.each do |name, value|
      if value.is_a?(Hash)
        execute.call(value)
      elsif value.is_a?(Array)
        final = []
        value.each do |item|
          final << execute_value.call(item)
        end
        settings[name] = final
      else
        settings[name] = execute_value.call(value)
      end
    end
  end

  execute.call(settings[:parameters])
end

#idObject




75
76
77
# File 'lib/core/plugin/resource.rb', line 75

def id
  get(:id, plugin_instance_name).to_sym
end

#init_tokensObject




29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/core/plugin/resource.rb', line 29

def init_tokens
  collect_tokens = lambda do |local_settings, token|
    local_settings.each do |name, value|
      setting_token = [ array(token), name ].flatten

      if value.is_a?(Hash)
        collect_tokens.call(value, setting_token)
      else
        token_base = setting_token.shift
        plan.set_token(token_base, setting_token, value)
      end
    end
  end

  # Generate parameter tokens
  collect_tokens.call(settings[:parameters], id)
end

#initialized?(options = {}) ⇒ Boolean


Checks

Returns:

  • (Boolean)


50
51
52
# File 'lib/core/plugin/resource.rb', line 50

def initialized?(options = {})
  true
end

#interpolate_parametersObject




213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/core/plugin/resource.rb', line 213

def interpolate_parameters
  interpolate_value = lambda do |base_config, name, value|
    interpolations = false
    plan.tokens.each do |token_name, token_value|
      if value.is_a?(String) && value.gsub!(/\{\{#{token_name}\}\}/, token_value.to_s)
        interpolations = true
      end
    end
    base_config[name] = value
    interpolations
  end

  interpolate = lambda do |settings|
    interpolations = false

    if settings.is_a?(Hash)
      settings.each do |name, value|
        if value.is_a?(Hash) || value.is_a?(Array)
          interpolations = true if interpolate.call(value)
        else
          interpolations = true if interpolate_value.call(settings, name, value)
        end
      end
    elsif settings.is_a?(Array)
      settings.each_with_index do |value, index|
        if value.is_a?(Hash) || value.is_a?(Array)
          interpolations = true if interpolate.call(value)
        else
          interpolations = true if interpolate_value.call(settings, index, value)
        end
      end
    end
    interpolations
  end

  tries = 0
  loop do
    tries += 1
    init_tokens
    break if tries >= 10 || !interpolate.call(settings[:parameters])
  end
end

#normalize(reload) ⇒ Object


Plugin interface



19
20
21
22
23
24
25
# File 'lib/core/plugin/resource.rb', line 19

def normalize(reload)
  super

  @plan = delete(:plan, nil) unless reload

  yield if block_given?
end

#operation_deployObject




129
130
131
132
133
134
135
136
137
# File 'lib/core/plugin/resource.rb', line 129

def operation_deploy
  info('cm.resource.info.run_internal', { :id => Nucleon.green(id), :op => 'deploy', :time => Nucleon.purple(Time.now.to_s) })
  if retrieve_resource
    data = update_resource
  else
    data = create_resource
  end
  data
end

#operation_destroyObject




141
142
143
144
# File 'lib/core/plugin/resource.rb', line 141

def operation_destroy
  info('cm.resource.info.run_internal', { :id => Nucleon.green(id), :op => 'destroy', :time => Nucleon.purple(Time.now.to_s) })
  delete_resource
end

#parametersObject



79
80
81
# File 'lib/core/plugin/resource.rb', line 79

def parameters
  hash(settings[:parameters])
end

#planObject


Property accessors / modifiers



57
58
59
# File 'lib/core/plugin/resource.rb', line 57

def plan
  @plan
end

#quitObject




99
100
101
# File 'lib/core/plugin/resource.rb', line 99

def quit
  @quit
end

#quit=(quit) ⇒ Object



103
104
105
# File 'lib/core/plugin/resource.rb', line 103

def quit=quit
  @quit = quit
end

#retrieve_resourceObject



154
155
156
157
158
# File 'lib/core/plugin/resource.rb', line 154

def retrieve_resource
  result = nil
  result = yield if block_given?
  result
end

#set_tokens(data) ⇒ Object




258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/core/plugin/resource.rb', line 258

def set_tokens(data)
  parse_tokens = lambda do |local_data, parent_keys = []|
    local_data.each do |key, value|
      location = [parent_keys, key].flatten

      if value.is_a?(Hash)
        parse_tokens.call(value, location)
      else
        plan.set_token(id, location, value)
      end
    end
  end

  parse_tokens.call(data)
end

#settingsObject




63
64
65
# File 'lib/core/plugin/resource.rb', line 63

def settings
  get_hash(:settings)
end

#timeoutObject



83
84
85
# File 'lib/core/plugin/resource.rb', line 83

def timeout
  settings[:timeout] ||= 1000
end

#update_resource {|data| ... } ⇒ Object

Yields:



160
161
162
163
164
# File 'lib/core/plugin/resource.rb', line 160

def update_resource
  data = {}
  yield(data) if block_given?
  data
end