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

.register_idsObject



6
7
8
# File 'lib/core/plugin/resource.rb', line 6

def self.register_ids
  [ :id ]
end

Instance Method Details

#dataObject




73
74
75
# File 'lib/core/plugin/resource.rb', line 73

def data
  hash(@data)
end

#data=(data) ⇒ Object



77
78
79
# File 'lib/core/plugin/resource.rb', line 77

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

#execute(operation) ⇒ Object


Operations



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/core/plugin/resource.rb', line 84

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

    execute_functions
    interpolate_parameters

    if respond_to?(method)
      success = send(method)
    end
  else
    success = false
  end
  success
end

#execute_functionsObject


Utilities



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/core/plugin/resource.rb', line 116

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




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

def id
  get(:id, '')
end

#init_tokensObject




23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/core/plugin/resource.rb', line 23

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)


44
45
46
# File 'lib/core/plugin/resource.rb', line 44

def initialized?(options = {})
  true
end

#interpolate_parametersObject




154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/core/plugin/resource.rb', line 154

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



13
14
15
16
17
18
19
# File 'lib/core/plugin/resource.rb', line 13

def normalize(reload)
  super

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

  yield if block_given?
end

#operation_deployObject




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

def operation_deploy
  yield if block_given?
end

#operation_destroyObject




109
110
111
# File 'lib/core/plugin/resource.rb', line 109

def operation_destroy
  yield if block_given?
end

#parametersObject



67
68
69
# File 'lib/core/plugin/resource.rb', line 67

def parameters
  hash(settings[:parameters])
end

#planObject


Property accessors / modifiers



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

def plan
  @plan
end

#settingsObject




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

def settings
  get_hash(:settings)
end