103 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			103 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
| require "httpclient"
 | |
| require "exceptions/devops_exception"
 | |
| require "exceptions/not_found"
 | |
| require "exceptions/invalid_query"
 | |
| require "devops-client/options/common_options"
 | |
| require "uri"
 | |
| require "json"
 | |
| require "devops-client/i18n"
 | |
| require "devops-client/handler/helpers/input_utils"
 | |
| require "devops-client/handler/helpers/colored_output_utils"
 | |
| require "devops-client/handler/helpers/http_utils"
 | |
| require "devops-client/handler/helpers/outputtable"
 | |
| require "devops-client/handler/helpers/resources_fetcher"
 | |
| require "devops-client/handler/helpers/resources_selector"
 | |
| require "devops-client/providers/providers"
 | |
| 
 | |
| class Handler
 | |
| 
 | |
|   include InputUtils
 | |
|   include ColoredOutputUtils
 | |
|   include HttpUtils
 | |
|   include Outputtable
 | |
| 
 | |
|   attr_reader :options
 | |
|   attr_writer :host
 | |
|   attr_accessor :auth
 | |
| 
 | |
|   def host
 | |
|     "http://#{@host}"
 | |
|   end
 | |
| 
 | |
|   #TODO: only basic auth now
 | |
|   def username
 | |
|     self.options[:username] || self.auth[:username]
 | |
|   end
 | |
| 
 | |
|   def password
 | |
|     self.options[:password] || self.auth[:password]
 | |
|   end
 | |
| 
 | |
|   def options= o
 | |
|     self.host = o.delete(:host) if o.has_key? :host
 | |
|     @options = o
 | |
|   end
 | |
| 
 | |
| protected
 | |
| 
 | |
|   def resources_selector
 | |
|     @resources_selector ||= Helpers::ResourcesSelector.new(fetcher)
 | |
|   end
 | |
| 
 | |
|   def fetcher
 | |
|     @fetcher ||= Helpers::ResourcesFetcher.new(host: @host, handler_object_options: @options, auth: @auth)
 | |
|   end
 | |
| 
 | |
|   def params_filter params
 | |
|     r = []
 | |
|     return params if params.kind_of?(String)
 | |
|     params.each do |k,v|
 | |
|       key = k.to_s
 | |
|       if v.kind_of?(Array)
 | |
|         v.each do |val|
 | |
|           r.push "#{key}[]=#{val}"
 | |
|         end
 | |
|       elsif v.kind_of?(Hash)
 | |
|         buf = {}
 | |
|         v.each do |k1,v1|
 | |
|           buf["#{key}[#{k1}]"] = v1
 | |
|         end
 | |
|         r = r + params_filter(buf)
 | |
|       else
 | |
|         r.push "#{key}=#{v}"
 | |
|       end
 | |
|     end
 | |
|     r
 | |
|   end
 | |
| 
 | |
|   def inspect_parameters names, *args
 | |
|     names.each_with_index do |name, i|
 | |
|       next if name.start_with? "[" and name.end_with? "]"
 | |
|       if args[i].nil? or args[i].empty?
 | |
|         return "\n" + I18n.t("handler.error.parameter.undefined", :name => name)
 | |
|       end
 | |
|     end
 | |
|     nil
 | |
|   end
 | |
| 
 | |
|   def check_status status
 | |
|     r = status.scan(/--\sStatus:\s([0-9]{1,5})\s--/i)[0]
 | |
|     if r.nil?
 | |
|       puts "WARN: status undefined"
 | |
|       -1
 | |
|     else
 | |
|       r[0].to_i
 | |
|     end
 | |
|   end
 | |
| 
 | |
|   def current_command
 | |
|     ARGV[1].to_sym if ARGV[1]
 | |
|   end
 | |
| 
 | |
| end
 | 
