Class: CORL::Util::Puppet::Resource

Inherits:
Core
  • Object
show all
Includes:
Mixin::SubConfig
Defined in:
lib/core/util/puppet/resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(group, info, title, properties = {}) ⇒ Resource


Constructor / Destructor



12
13
14
15
16
17
18
19
20
21
# File 'lib/core/util/puppet/resource.rb', line 12

def initialize(group, info, title, properties = {})
  super({
    :group => group,
    :title => string(title),
    :info  => symbol_map(hash(info)),
    :ready => false
  }, {}, true, true, false)

  import(properties)
end

Class Method Details

.render(resource_info, options = {}) ⇒ Object




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
151
152
153
154
155
156
157
158
# File 'lib/core/util/puppet/resource.rb', line 125

def self.render(resource_info, options = {})
  resource = string_map(resource_info)
  config   = Config.ensure(options)

  resource.keys.each do |name|
    if match = name.to_s.match(/^(.+)_template$/)
      target = match.captures[0]

      config.set(:normalize_template, config.get("normalize_#{target}", true))
      config.set(:interpolate_template, config.get("interpolate_#{target}", true))

      input_data       = resource[target]
      plugin           = CORL.template(config.import({ :new => true }), resource[name])
      resource[target] = plugin.render(input_data)
      CORL.remove_plugin(plugin)

      if config.get(:debug, false)
        CORL.ui.info("\n", { :prefix => false })
        CORL.ui_group("#{resource[name]} template", :cyan) do |ui|
          ui.info("-----------------------------------------------------")

          source_dump  = Console.blue(Data.to_json(input_data, true))
          value_render = Console.green(resource[target])

          ui.info("Data:\n#{source_dump}")
          ui.info("Rendered:\n#{value_render}")
          ui.info("\n", { :prefix => false })
        end
      end
      resource.delete(name)
    end
  end
  return resource
end

Instance Method Details

#defaults(defaults, options = {}) ⇒ Object




62
63
64
65
66
67
# File 'lib/core/util/puppet/resource.rb', line 62

def defaults(defaults, options = {})
  super(defaults, options)

  _set(:ready, false)
  return self
end

#ensure_ready(options = {}) ⇒ Object


Resource operations



81
82
83
84
85
# File 'lib/core/util/puppet/resource.rb', line 81

def ensure_ready(options = {})
  unless ready
    process(options)
  end
end

#groupObject


Property accessors / modifiers



26
27
28
# File 'lib/core/util/puppet/resource.rb', line 26

def group
  return _get(:group)
end

#import(properties, options = {}) ⇒ Object




71
72
73
74
75
76
# File 'lib/core/util/puppet/resource.rb', line 71

def import(properties, options = {})
  super(properties, options)

  _set(:ready, false)
  return self
end

#info(default = {}) ⇒ Object




32
33
34
# File 'lib/core/util/puppet/resource.rb', line 32

def info(default = {})
  return hash(_get(:info, default))
end

#info=(info) ⇒ Object




38
39
40
# File 'lib/core/util/puppet/resource.rb', line 38

def info=info
  _set(:info, hash(info))
end

#process(options = {}) ⇒ Object




89
90
91
92
93
94
95
96
97
98
# File 'lib/core/util/puppet/resource.rb', line 89

def process(options = {})
  config = Config.ensure(options)

  tag(config[:tag])
  render(config)
  translate(config)

  _set(:ready, true) # Ready for resource creation
  return self
end

#ready(default = false) ⇒ Object




56
57
58
# File 'lib/core/util/puppet/resource.rb', line 56

def ready(default = false)
  return test(_get(:ready, default))
end

#render(options = {}) ⇒ Object




162
163
164
165
166
167
# File 'lib/core/util/puppet/resource.rb', line 162

def render(options = {})
  resource = self.class.render(export, options)
  clear
  import(Config.ensure(resource).export, options)
  return self
end

#tag(tags, append = true) ⇒ Object




102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/core/util/puppet/resource.rb', line 102

def tag(tags, append = true)
  unless Data.empty?(tags)
    if tags.is_a?(String)
      tags = tags.to_s.split(/\s*,\s*/)
    else
      tags = tags.flatten
    end
    resource_tags = get(:tag)

    if ! resource_tags || ! append
      set(:tag, tags)
    else
      tags.each do |tag|
        resource_tags << tag unless resource_tags.include?(tag)
      end
      set(:tag, resource_tags)
    end
  end
  return self
end

#title(default = '') ⇒ Object




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

def title(default = '')
  return string(_get(:title, default))
end

#title=(info) ⇒ Object




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

def title=info
  _set(:title, string(info))
end

#translate(options = {}) ⇒ Object




171
172
173
174
175
176
177
178
179
180
# File 'lib/core/util/puppet/resource.rb', line 171

def translate(options = {})
  config = Config.ensure(options)

  set(:before, translate_resource_refs(get(:before), config)) if get(:before)
  set(:notify, translate_resource_refs(get(:notify), config)) if get(:notify)
  set(:require, translate_resource_refs(get(:require), config)) if get(:require)
  set(:subscribe, translate_resource_refs(get(:subscribe), config)) if get(:subscribe)

  return self
end