Module: Coral::Resource
- Defined in:
- lib/coral_core/resource.rb
Constant Summary collapse
- @@resource_groups =
Resource groups
{}
Class Method Summary collapse
-
.add_members(group_name, resource_names) ⇒ Object
—.
-
.clear_groups ⇒ Object
—.
-
.groups ⇒ Object
—.
-
.namevar(type_name, resource_name) ⇒ Object
—.
-
.normalize(type_name, resources, options) ⇒ Object
—————————————————————————– Resource operations.
-
.render(resource, options = {}) ⇒ Object
—.
-
.translate(type_name, resources, options = {}) ⇒ Object
—.
-
.translate_resource_refs(type_name, resource_refs, options = {}) ⇒ Object
—.
-
.type_name(value) ⇒ Object
—————————————————————————– Utilities.
Class Method Details
.add_members(group_name, resource_names) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/coral_core/resource.rb', line 24 def self.add_members(group_name, resource_names) @@resource_groups[group_name] = [] unless @@resource_groups[group_name].is_a?(Array) unless resource_names.is_a?(Array) resource_names = [ resource_names ] end resource_names.each do |name| unless @@resource_groups[group_name].include?(name) @@resource_groups[group_name] << name end end end |
.clear_groups ⇒ Object
18 19 20 |
# File 'lib/coral_core/resource.rb', line 18 def self.clear_groups @@resource_groups = {} end |
.groups ⇒ Object
12 13 14 |
# File 'lib/coral_core/resource.rb', line 12 def self.groups return @@resource_groups end |
.namevar(type_name, resource_name) ⇒ Object
233 234 235 236 237 238 239 240 241 |
# File 'lib/coral_core/resource.rb', line 233 def self.namevar(type_name, resource_name) # Basically borrowed from Puppet (damn private methods!) resource = Puppet::Resource.new(type_name.sub(/^\@?\@/, ''), resource_name) if resource.builtin_type? and type = resource.resource_type and type.key_attributes.length == 1 return type.key_attributes.first.to_s else return 'name' end end |
.normalize(type_name, resources, options) ⇒ Object
Resource operations
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/coral_core/resource.rb', line 41 def self.normalize(type_name, resources, ) clear_groups config = Config.ensure() resources = Util::Data.value(resources) #dbg(resources, 'normalize -> init') unless Util::Data.empty?(resources) resources.keys.each do |name| #dbg(name, 'normalize -> name') if ! resources[name] || resources[name].empty? || ! resources[name].is_a?(Hash) resources.delete(name) else normalize = true namevar = namevar(type_name, name) if resources[name].has_key?(namevar) value = resources[name][namevar] if Util::Data.empty?(value) #dbg(value, "delete #{name}") resources.delete(name) normalize = false elsif value.is_a?(Array) value.each do |item| item_name = "#{name}_#{item}".gsub(/\-/, '_') new_resource = resources[name].clone new_resource[namevar] = item resources[item_name] = render(new_resource, config) add_members(name, item_name) end resources.delete(name) normalize = false end end #dbg(resources, 'normalize -> resources') if normalize resources[name] = render(resources[name], config) end end end end return resources end |
.render(resource, options = {}) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/coral_core/resource.rb', line 93 def self.render(resource, = {}) resource = Core.string_map(resource.clone) config = Config.ensure() resource.keys.each do |name| if match = name.match(/^(.+)_template$/) target = match.captures[0] #dbg(name, 'name') #dbg(target, 'target') config.set(:normalize_template, config.get("normalize_#{target}", true)) config.set(:interpolate_template, config.get("interpolate_#{target}", true)) resource[target] = Template.render(resource[name], resource[target], config) resource.delete(name) end end #dbg(resource, 'render exit') return resource end |
.translate(type_name, resources, options = {}) ⇒ Object
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 |
# File 'lib/coral_core/resource.rb', line 118 def self.translate(type_name, resources, = {}) config = Config.ensure() resources = Util::Data.value(resources) results = {} #dbg(resources, 'resources -> translate') prefix = config.get(:resource_prefix, '') name_map = {} resources.keys.each do |name| name_map[name] = true end config[:resource_names] = name_map resources.each do |name, data| #dbg(name, 'name') #dbg(data, 'data') resource = resources[name] resource['before'] = translate_resource_refs(type_name, data['before'], config) if data.has_key?('before') resource['notify'] = translate_resource_refs(type_name, data['notify'], config) if data.has_key?('notify') resource['require'] = translate_resource_refs(type_name, data['require'], config) if data.has_key?('require') resource['subscribe'] = translate_resource_refs(type_name, data['subscribe'], config) if data.has_key?('subscribe') unless prefix.empty? name = "#{prefix}_#{name}" end results[name] = resource end return results end |
.translate_resource_refs(type_name, resource_refs, options = {}) ⇒ Object
153 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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/coral_core/resource.rb', line 153 def self.translate_resource_refs(type_name, resource_refs, = {}) return :undef if Util::Data.undef?(resource_refs) config = Config.ensure() resource_names = config.get(:resource_names, {}) title_prefix = config.get(:title_prefix, '') title_pattern = config.get(:title_pattern, '^\s*([^\[\]]+)\s*$') title_group = config.get(:title_var_group, 1) title_flags = config.get(:title_flags, '') title_regexp = Regexp.new(title_pattern, title_flags.split('')) allow_single = config.get(:allow_single_return, true) type_name = type_name.sub(/^\@?\@/, '') values = [] case resource_refs when String if resource_refs.empty? return :undef else resource_refs = resource_refs.split(/\s*,\s*/) end when Puppet::Resource resource_refs = [ resource_refs ] end resource_refs.collect! do |value| if value.is_a?(Puppet::Resource) || ! value.match(title_regexp) value elsif resource_names.has_key?(value) if ! title_prefix.empty? "#{title_prefix}_#{value}" else value end elsif groups.has_key?(value) && ! groups[value].empty? results = [] groups[value].each do |resource_name| unless title_prefix.empty? resource_name = "#{title_prefix}_#{resource_name}" end results << resource_name end results else nil end end resource_refs.flatten.each do |ref| #dbg(ref, 'reference -> init') unless ref.nil? unless ref.is_a?(Puppet::Resource) ref = ref.match(title_regexp) ? Puppet::Resource.new(type_name, ref) : Puppet::Resource.new(ref) end #dbg(ref, 'reference -> final') values << ref unless ref.nil? end end return values[0] if allow_single && values.length == 1 return values end |
.type_name(value) ⇒ Object
Utilities
225 226 227 228 229 |
# File 'lib/coral_core/resource.rb', line 225 def self.type_name(value) # Basically borrowed from Puppet (damn private methods!) return :main if value == :main return "Class" if value == "" or value.nil? or value.to_s.downcase == "component" return value.to_s.split("::").collect { |s| s.capitalize }.join("::") end |