Last active
August 29, 2015 13:55
-
-
Save stephenrjohnson/8736273 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Puppet::Parser::Functions | |
newfunction(:type, :type => :rvalue, :doc => "Return the type requested when given the certname") do |arguments| | |
if (arguments.size != 2) then | |
raise(Puppet::ParseError, "type(): Wrong number of arguments "+ | |
"given #{arguments.size} for 2 argument required") | |
end | |
certname = arguments[0] | |
type = arguments[1] | |
rx = /([a-z]{2}[a-z0-9][a-z]?)[0-9]{2}\.([a-z])([a-z])\.([a-z]{3}[a-z0-9][0-9])/ | |
if match = rx.match(certname) | |
if type == "environment" | |
case match[2] | |
when "p"; env = "production" | |
when "s"; env = "staging" | |
when "t"; env = "test" | |
else env = "unknown" | |
end | |
return env | |
elsif type == "location" | |
return match[4] | |
elsif type == "network" | |
case match[3] | |
when "a"; zone = "web" | |
when "d"; zone = "database" | |
when "m"; zone = "management" | |
else zone = "unknown" | |
end | |
elsif type == 'function' | |
return match[1] | |
else | |
fail("Invalid type requested") | |
end | |
else | |
fail("Invalid fqdn unable to run rx on #{certname}") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment