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
# 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
  })
  import(properties) 
end

Class Method Details

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




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

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]
      resource[target] = CORL.template(config, resource[name]).render(input_data)
      
      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




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

def defaults(defaults, options = {})
  super(defaults, options)
  
  _set(:ready, false)
  return self 
end

#ensure_ready(options = {}) ⇒ Object


Resource operations



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

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

#groupObject


Property accessors / modifiers



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

def group
  return _get(:group)
end

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




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

def import(properties, options = {})
  super(properties, options)
  
  _set(:ready, false)
  return self  
end

#info(default = {}) ⇒ Object




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

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

#info=(info) ⇒ Object




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

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

#process(options = {}) ⇒ Object




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

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




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

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

#render(options = {}) ⇒ Object




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

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

#tag(tags, append = true) ⇒ Object




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

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




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

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

#title=(info) ⇒ Object




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

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

#translate(options = {}) ⇒ Object




168
169
170
171
172
173
174
175
176
177
# File 'lib/core/util/puppet/resource.rb', line 168

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