Skip to content

Instantly share code, notes, and snippets.

@akram
Forked from ccaum/externalresources.rb
Created April 24, 2012 12:34
Show Gist options
  • Save akram/2479276 to your computer and use it in GitHub Desktop.
Save akram/2479276 to your computer and use it in GitHub Desktop.
Puppet class to turn parameters in to puppet resources
# Class: externalresources
#
# This module manages externalresources
#
# Parameters:
#
# Actions:
#
# Requires:
#
# Sample Usage:
#
# [Remember: No empty lines between comments and class definition]
hostclass :externalresources do
resourcesMap = scope.to_hash.reject { |key,value| !( key.is_a?(String) && value.is_a?(String) ) }
resourcesMap.keys.each do |resource|
resourceNameFirstChar = resourcesMap[resource][0]
if resourceNameFirstChar != nil && resourceNameFirstChar.chr == '{'
begin
#Convert the variable from a string to a hash
resourceHash = eval resourcesMap[resource]
rescue => e
notice "Couldn't convert variable #{resource} to hash: #{e}"
end
if resourceHash.has_key? :type
type = resourceHash[:type]
virtual = resourceHash.has_key? :virtual && resourceHash[:virtual] == 'true'
#Split out our :type parameter
resourceHash.reject! { |key,value| key == :type || key == :virtual }
#Create the virtual resource by sending it the type defined
begin
unless virtual
#Make our resource
send type.to_sym, resource, resourceHash
else
#Call our resource function and pass it to virtual
virtual send type.to_sym, resource, resourceHash
end
rescue => e
fail "Couldn't create resource #{resource}: #{e}"
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment