Skip to content

Instantly share code, notes, and snippets.

@bgupta
Created June 13, 2011 06:17
Show Gist options
  • Save bgupta/1022380 to your computer and use it in GitHub Desktop.
Save bgupta/1022380 to your computer and use it in GitHub Desktop.
auth is broken
#!/usr/bin/env ruby
# Copyright (C) 2011 Vijay Brian Gupta brian.gupta@brandorr.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
#
# v1.0 May 27, 2011 - Supports --hostlist
require "rubygems"
require "time"
require "openssl"
require "rest_client"
require "json"
require "socket"
require "open-uri"
require "optparse"
require "yaml"
foreman_url = "http://localhost:3000/"
collections_filterable = [
:hosts,
:puppetclasses,
:fact_values,
:environments,
:hostgroups,
]
collections = [
:architectures,
:auth_source_ldaps,
:common_parameters,
:config_templates,
:domains,
:hypervisors,
:lookup_keys,
:media,
:operatingsystems,
:ptables,
:reports,
:smart_proxies,
:subnets,
:usergroups,
:users
]
collections_not_implemented = [ :dashboard, :status ]
options = {}
OptionParser.new do|opts|
opts.banner = "Usage: " + File.basename($0) + " [options] ..."
options[:verbose] = false
opts.on( "-v", "--verbose", "Output more information" ) do
options[:verbose] = true
end
options[:username] = false
opts.on( '-u', '--user USER', "Foreman user") do|f|
options[:username] = f
end
options[:password] = false
opts.on( "-p", "--pass PASSWORD", "Foreman password" ) do|f|
options[:password] = f
end
collections_filterable.each do |collection|
options[collection] = false
opts.on("--#{collection}[filter]", "Retreive a list of #{collection}") do|f|
options[collection] = f || true
end
end
collections.each do |collection|
options[collection] = false
opts.on("--#{collection}", "Retreive a list of #{collection}") do
options[collection] = true
end
end
collections_not_implemented.each do |collection|
options[collection] = false
opts.on("--#{collection}", "Not implemented") do
options[collection] = true
end
end
# opts.on("-h", "--help", "Display this screen" ) do
# puts opts
# exit
# end
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
end.parse!
puts "Use -h, --help for usage." if options.values.uniq == [false]
if options[:username]
@@foreman_user = options[:username]
end
if options[:password]
@@foreman_pass = options[:password]
end
def get_collection(foreman_url,path)
response = RestClient.get foreman_url + path.to_s,
{:user => @@foreman_user,
:password => @@foreman_pass,
:headers => { :accept =>:json, :content_type => :json }}
results = JSON.parse(response.to_str)
end
def search_collection(foreman_url,path,search)
response = RestClient.get foreman_url +
path.to_s +
URI.escape("?search=#{search}"),
{:user => @@foreman_user,
:password => @@foreman_pass,
:headers => { :accept =>:json}}
results = JSON.parse(response.to_str)
end
#p options
options.keys.each do |collection|
if options[collection]
if options[collection] == true
# puts foreman_url + collection.to_s
puts JSON.pretty_generate(get_collection(foreman_url,collection))
else
#puts search_collection(foreman_url,collection,options[collection])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment