Skip to content

Instantly share code, notes, and snippets.

@glarizza
Last active August 29, 2015 14:08
Show Gist options
  • Save glarizza/43128437c0e54cc8ce87 to your computer and use it in GitHub Desktop.
Save glarizza/43128437c0e54cc8ce87 to your computer and use it in GitHub Desktop.
Read classification information
#!/opt/puppet/bin/ruby
require 'puppet'
require 'uri'
require 'json'
require 'net/http'
# Need to parse the Puppet config to get certificate info later
Puppet.parse_config
# Bail out if you don't provide a group name
if ARGV.size != 1
raise ArgumentError, "This script requires 1 argument: the group name for which to search"
else
groupname = ARGV[0]
end
# Setup the HTTP request and certificate information
server_name = 'localhost'
server_port = '4433'
uri = URI.parse("https://#{server_name}:#{server_port}/classifier-api/v1/groups")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.cert = OpenSSL::X509::Certificate.new(File.read(Puppet[:hostcert]))
http.key = OpenSSL::PKey::RSA.new(File.read(Puppet[:hostprivkey]))
http.ca_file = Puppet[:localcacert]
# Perform the request and parse the JSON object
request = http.get(uri.request_uri)
groups_array = JSON.parse(request.body)
# Search for the group's UID and bail out if it's not found
group_uid = nil
groups_array.each { |group| group_uid = group['id'] if group['name'] == groupname }
if group_uid.nil?
puts "Group was not found"
exit 1
end
# Do a request for that group's information and pretty-print the info
group_json_info = JSON.parse(http.get("#{uri.request_uri}/#{group_uid}").body)
puts JSON.pretty_generate(group_json_info)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment