5 # Ward Vandewege <ward@clinicalfuture.com>
7 if RUBY_VERSION < '1.9.3' then
9 #{$0.gsub(/^\.\//,'')} requires Ruby version 1.9.3 or higher.
13 ENV['ORVOS_API_VERSION'] ||= 'v1'
15 if not ENV.include?('ORVOS_API_HOST') or not ENV.include?('ORVOS_API_TOKEN') then
17 ORVOS_API_HOST and ORVOS_API_TOKEN need to be defined as environment variables.
23 require 'google/api_client'
30 Please install all required gems:
41 original_verbosity = $VERBOSE
44 $VERBOSE = original_verbosity
49 # do this if you're testing with a dev server and you don't care about SSL certificate checks:
50 suppress_warnings { OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE }
52 class Google::APIClient
53 def discovery_document(api, version)
55 return @discovery_documents["#{api}:#{version}"] ||= (begin
56 response = self.execute!(
58 :uri => self.discovery_uri(api, version),
59 :authenticated => false
61 response.body.class == String ? JSON.parse(response.body) : response.body
66 client = Google::APIClient.new(:host => ENV['ORVOS_API_HOST'], :application_name => 'wh-cli', :application_version => '1.0')
67 orvos = client.discovered_api('orvos', ENV['ORVOS_API_VERSION'])
70 !!(s =~ /^(true|t|yes|y|1)$/i)
73 def parse_arguments(discovery_document)
74 sub_commands = Array.new()
75 sub_commands << '--help'
76 discovery_document["resources"].each do |k,v|
80 global_opts = Trollop::options do
81 banner "orvos cli client"
82 opt :dry_run, "Don't actually do anything", :short => "-n"
88 cmd = cmd.pluralize unless cmd.nil?
89 if sub_commands.include?(cmd) and cmd != '--help' then
91 # Now see if the method supplied exists
93 if discovery_document["resources"][cmd]["methods"].include?(method) then
94 # method exists. Collect arguments.
95 discovered_params = discovery_document["resources"][cmd]["methods"][method]["parameters"]
96 method_opts = Trollop::options do
97 discovered_params.each do |k,v|
99 opts[:type] = v["type"].to_sym if v.include?("type")
100 if [:datetime, :text, :object].index opts[:type]
101 opts[:type] = :string # else trollop bork
103 opts[:default] = v["default"] if v.include?("default")
104 opts[:default] = v["default"].to_i if opts[:type] == :integer
105 opts[:default] = to_boolean(v["default"]) if opts[:type] == :boolean
106 opts[:required] = true if v.include?("required") and v["required"]
108 description = ' ' + v["description"] if v.include?("description")
109 opt k.to_sym, description, opts
111 opt :json, "Return the raw json received from the API server", :short => "-j"
112 opt :jsonhuman, "Return the raw json received from the API server, formatted for human consumption", :short => "-h"
114 discovered_params.each do |k,v|
115 if v["type"] == "object" and method_opts.has_key? k
116 method_opts[k] = JSON.parse method_opts[k]
120 banner = "\nThis api command supports the following methods:\n\n"
121 discovery_document["resources"][cmd]["methods"].each do |k,v|
123 description = ' ' + v["description"] if v.include?("description")
124 banner += " #{sprintf("%20s",k)}#{description}\n"
130 if not method.nil? and method != '--help' then
131 Trollop::die "Unknown method #{method.to_s} for command #{cmd.to_s}"
139 banner = "\nThis Orvos cluster supports the following api commands:\n\n"
140 discovery_document["resources"].each do |k,v|
142 if discovery_document["schemas"].include?(k.singularize.capitalize) and
143 discovery_document["schemas"][k.singularize.capitalize].include?('description') then
144 description = ' ' + discovery_document["schemas"][k.singularize.capitalize]["description"]
146 banner += " #{sprintf("%30s",k.singularize)}#{description}\n"
152 if not cmd.nil? and cmd != '--help' then
153 Trollop::die "Unknown command #{cmd.inspect}"
159 return cmd, method, method_opts, ARGV
162 cmd, method, method_opts, remaining_opts = parse_arguments(orvos.discovery_document)
164 api_method = 'orvos.' + cmd + '.' + method
166 m_o = method_opts.reject {|key,value| key =~ /_given$|^json$|^jsonhuman$|^help$/ or value == nil }
168 result = client.execute :api_method => eval(api_method), :parameters => { :api_token => ENV['ORVOS_API_TOKEN'] }.merge(m_o), :authenticated => false
169 results = JSON.parse result.body
171 if results["errors"] then
172 abort "Error: #{results["errors"][0]}"
175 if method_opts[:json] then
177 elsif method_opts[:jsonhuman] then
178 puts results.pretty_inspect()
179 elsif results["items"] and results["kind"].match /list$/i
180 results['items'].each do |i| puts i['uuid'] end