Created
November 2, 2015 18:09
-
-
Save smbambling/85cad0fe10240f1dcfa7 to your computer and use it in GitHub Desktop.
This file contains 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
require "net/http" | |
require "net/https" | |
require "uri" | |
require "json" | |
require "socket" | |
require "puppet" | |
module Puppet::Parser::Functions | |
newfunction(:fetch_environments, :type => :rvalue) do |args| | |
me = Socket.gethostname | |
cert = File.read("/etc/puppetlabs/puppet/ssl/certs/#{me}.pem") | |
key = File.read("/etc/puppetlabs/puppet/ssl/private_keys/#{me}.pem") | |
ca_cert = "/etc/puppetlabs/puppet/ssl/certs/ca.pem" | |
url = "https://#{me}:8140/puppet/v3/environments" | |
encoded_url = URI.encode(url) | |
uri = URI.parse(encoded_url) | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
http.cert = OpenSSL::X509::Certificate.new(cert) | |
http.key = OpenSSL::PKey::RSA.new(key) | |
http.ca_file = ca_cert | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
request = Net::HTTP::Get.new(uri.request_uri, initheader = {'Accept' =>'*/*'}) | |
response = http.request(request) | |
data = response.body | |
data = JSON.parse(data) | |
environments = Array.new | |
data['environments'].each_key { |key| | |
environments << key | |
} | |
environments.join(",") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment