Skip to content

Instantly share code, notes, and snippets.

@bgupta
Created June 6, 2011 06:58
Show Gist options
  • Save bgupta/1009838 to your computer and use it in GitHub Desktop.
Save bgupta/1009838 to your computer and use it in GitHub Desktop.
#!/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
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]
def get_collection(foreman_url,path)
response = RestClient.get foreman_url + path.to_s, :accept =>: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}"),
:accept =>:json
results = JSON.parse(response.to_str)
end
#p options
options.keys.each do |collection|
if options[collection]
if options[collection] == true
puts JSON.pretty_generate(get_collection(foreman_url,collection))
else
puts search_collection(foreman_url,collection,options[collection])
end
end
end
@bgupta
Copy link
Author

bgupta commented Jun 6, 2011

ubuntu@dmetest:~/bg-foreman$ ./foreman-cli2 --help
Usage: foreman-cli2 [options] ...
-v, --verbose Output more information
--hosts [filter] Retreive a list of hosts
--puppetclasses [filter] Retreive a list of puppetclasses
--fact_values [filter] Retreive a list of fact_values
--environments [filter] Retreive a list of environments
--hostgroups [filter] Retreive a list of hostgroups
--architectures Retreive a list of architectures
--auth_source_ldaps Retreive a list of auth_source_ldaps
--common_parameters Retreive a list of common_parameters
--config_templates Retreive a list of config_templates
--domains Retreive a list of domains
--hypervisors Retreive a list of hypervisors
--lookup_keys Retreive a list of lookup_keys
--media Retreive a list of media
--operatingsystems Retreive a list of operatingsystems
--ptables Retreive a list of ptables
--reports Retreive a list of reports
--smart_proxies Retreive a list of smart_proxies
--subnets Retreive a list of subnets
--usergroups Retreive a list of usergroups
--users Retreive a list of users
--dashboard Not implemented
--status Not implemented
-h, --help Display this screen

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment