| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 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" | 
					
						
							| 
									
										
										
										
											2015-04-14 15:29:02 +03:00
										 |  |  | 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" | 
					
						
							| 
									
										
										
										
											2015-04-16 17:54:40 +03:00
										 |  |  | require "devops-client/providers/providers" | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | class Handler | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-01 11:57:36 +04:00
										 |  |  |   include InputUtils | 
					
						
							| 
									
										
										
										
											2015-04-14 15:29:02 +03:00
										 |  |  |   include ColoredOutputUtils | 
					
						
							|  |  |  |   include HttpUtils | 
					
						
							|  |  |  |   include Outputtable | 
					
						
							| 
									
										
										
										
											2014-07-01 11:57:36 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |   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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-14 15:29:02 +03:00
										 |  |  |   def resources_selector | 
					
						
							|  |  |  |     @resources_selector ||= Helpers::ResourcesSelector.new(fetcher) | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-14 15:29:02 +03:00
										 |  |  |   def fetcher | 
					
						
							|  |  |  |     @fetcher ||= Helpers::ResourcesFetcher.new(host: @host, handler_object_options: @options, auth: @auth) | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  |   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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 12:46:53 +03:00
										 |  |  |   def current_command | 
					
						
							|  |  |  |     ARGV[1].to_sym if ARGV[1] | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | end |