| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  | require_relative "request_parser" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module Devops | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |   module API3 | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |     module Parser | 
					
						
							|  |  |  |       class ServerParser < RequestParser | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def servers | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           q = {} | 
					
						
							|  |  |  |           if @params.key?("reserved") | 
					
						
							|  |  |  |             q[:reserved] = true if @params["reserved"] == "true" | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |           if @params.key?("provider") | 
					
						
							|  |  |  |             q[:provider] = @params["provider"] | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |           q | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def server | 
					
						
							| 
									
										
										
										
											2015-09-14 14:12:51 +03:00
										 |  |  |           @params[:key] || @params["key"] | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |         # TODO: skip_rollback flag | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |         def create | 
					
						
							| 
									
										
										
										
											2015-08-03 18:01:50 +03:00
										 |  |  |           @body ||= create_object_from_json_body | 
					
						
							|  |  |  |           project_name = check_string(@body["project"], "Parameter 'project' must be a not empty string") | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           env_name = check_string(@body["environment"], "Parameter 'environment' must be a not empty string") | 
					
						
							|  |  |  |           category = check_string(@body["category"], "Parameter 'category' must be a not empty string") | 
					
						
							| 
									
										
										
										
											2015-08-03 18:01:50 +03:00
										 |  |  |           server_name = check_string(@body["name"], "Parameter 'name' should be null or not empty string", true) | 
					
						
							|  |  |  |           without_bootstrap = @body["without_bootstrap"] | 
					
						
							|  |  |  |           force = @body["force"] | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |           raise InvalidRecord.new("Parameter 'without_bootstrap' should be a null or true") unless without_bootstrap.nil? or without_bootstrap == true | 
					
						
							|  |  |  |           raise InvalidRecord.new("Parameter 'force' should be a null or true") unless force.nil? or force == true | 
					
						
							| 
									
										
										
										
											2015-08-03 18:01:50 +03:00
										 |  |  |           groups = check_array(@body["groups"], "Parameter 'groups' should be null or not empty array of string", String, true) | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           key_name = check_string(@body["ssh_key"], "Parameter 'key' should be null or not empty string", true) | 
					
						
							| 
									
										
										
										
											2015-08-04 12:36:10 +03:00
										 |  |  |           rl = check_array(@body["run_list"], "Parameter 'run_list' should be a not empty array of string", String, true, true) | 
					
						
							|  |  |  |           Validators::Helpers::RunList.new(rl).validate! unless rl.nil? | 
					
						
							| 
									
										
										
										
											2015-08-03 18:01:50 +03:00
										 |  |  |           @body | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def bootstrap | 
					
						
							| 
									
										
										
										
											2015-08-03 18:01:50 +03:00
										 |  |  |           @body ||= create_object_from_json_body(Hash, true) | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           check_string(@body["cm_name"], "Parameter 'cm_name' should be a not empty string", true) | 
					
						
							| 
									
										
										
										
											2015-08-03 18:01:50 +03:00
										 |  |  |           rl = check_array(@body["run_list"], "Parameter 'run_list' should be a not empty array of string", String, true) | 
					
						
							| 
									
										
										
										
											2015-08-04 12:36:10 +03:00
										 |  |  |           Validators::Helpers::RunList.new(rl).validate! unless rl.nil? | 
					
						
							| 
									
										
										
										
											2015-08-03 18:01:50 +03:00
										 |  |  |           t = check_string(@body["bootstrap_template"], "Parameter 'bootstrap_template' should be a not empty string", true) | 
					
						
							|  |  |  |           @body | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |         def deploy | 
					
						
							|  |  |  |           r = create_object_from_json_body | 
					
						
							|  |  |  |           tags = check_array(r["tags"], "Parameter 'tags' should be an array of strings", String, true) | 
					
						
							|  |  |  |           build_number = check_string(r["build_number"], "Parameter 'build_number' should be a not empty string", true) | 
					
						
							|  |  |  |           rl = check_array(r["run_list"], "Parameter 'run_list' should be an array of string", String, true) | 
					
						
							|  |  |  |           Validators::Helpers::RunList.new(rl).validate! unless rl.nil? | 
					
						
							|  |  |  |           r | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def delete_list | 
					
						
							|  |  |  |           body = create_object_from_json_body | 
					
						
							|  |  |  |           check_array(body["servers_ids"], "Parameter 'servers_ids' must be not empty array of string", String) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |         def add_server | 
					
						
							| 
									
										
										
										
											2015-08-03 18:01:50 +03:00
										 |  |  |           @body ||= create_object_from_json_body | 
					
						
							|  |  |  |           project = check_string(@body["project"], "Parameter 'project' must be a not empty string") | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           env = check_string(@body["environment"], "Parameter 'environment' must be a not empty string") | 
					
						
							|  |  |  |           category = check_string(@body["category"], "Parameter 'category' must be a not empty string") | 
					
						
							| 
									
										
										
										
											2015-08-03 18:01:50 +03:00
										 |  |  |           remote_user = check_string(@body["remote_user"], "Parameter 'remote_user' must be a not empty string") | 
					
						
							|  |  |  |           private_ip = check_string(@body["private_ip"], "Parameter 'private_ip' must be a not empty string") | 
					
						
							|  |  |  |           public_ip = check_string(@body["public_ip"], "Parameter 'public_ip' should be a not empty string", true) | 
					
						
							| 
									
										
										
										
											2015-08-04 12:36:10 +03:00
										 |  |  |           rl = check_array(@body["run_list"], "Parameter 'run_list' should be a not empty array of string", String, true, true) | 
					
						
							|  |  |  |           Validators::Helpers::RunList.new(rl).validate! unless rl.nil? | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           name = check_string(@body["name"], "Parameter 'name' should be a not empty string") | 
					
						
							| 
									
										
										
										
											2016-02-16 10:01:43 +03:00
										 |  |  |           server_attrs = { | 
					
						
							|  |  |  |             project: project, | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |             environment: env, | 
					
						
							|  |  |  |             category: category, | 
					
						
							|  |  |  |             name: name, | 
					
						
							| 
									
										
										
										
											2016-02-16 10:01:43 +03:00
										 |  |  |             remote_user: remote_user, | 
					
						
							|  |  |  |             private_ip: private_ip, | 
					
						
							|  |  |  |             public_ip: public_ip, | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |             run_list: rl, | 
					
						
							|  |  |  |             name: name | 
					
						
							| 
									
										
										
										
											2016-02-16 10:01:43 +03:00
										 |  |  |           } | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def add_and_bootstrap_servers | 
					
						
							|  |  |  |           @body ||= create_object_from_json_body | 
					
						
							|  |  |  |           project = check_string(@body["project"], "Parameter 'project' must be a not empty string") | 
					
						
							|  |  |  |           deploy_env = check_string(@body["deploy_env"], "Parameter 'deploy_env' must be a not empty string") | 
					
						
							|  |  |  |           key = check_string(@body["key"], "Parameter 'key' must be a not empty string") | 
					
						
							|  |  |  |           remote_user = check_string(@body["remote_user"], "Parameter 'remote_user' must be a not empty string") | 
					
						
							|  |  |  |           rl = check_array(@body["run_list"], "Parameter 'run_list' should be a not empty array of string", String, true, true) | 
					
						
							|  |  |  |           check_string(@body["bootstrap_template"], "Parameter 'bootstrap_template' should be a not empty string", true) | 
					
						
							|  |  |  |           Validators::Helpers::RunList.new(rl).validate! unless rl.nil? | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |           ips_with_names = parse_list_of_ips_with_names(@body['ips_with_names']) | 
					
						
							|  |  |  |           servers_attrs = ips_with_names.map do |ip, chef_node_name| | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |               project: project, | 
					
						
							|  |  |  |               deploy_env: deploy_env, | 
					
						
							|  |  |  |               key: key, | 
					
						
							|  |  |  |               remote_user: remote_user, | 
					
						
							|  |  |  |               private_ip: ip, | 
					
						
							|  |  |  |               chef_node_name: chef_node_name, | 
					
						
							|  |  |  |               run_list: rl | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2018-04-04 22:44:39 +03:00
										 |  |  |           servers_attrs[:public_ip] = @body['public_ip'] if @body['public_ip'] | 
					
						
							| 
									
										
										
										
											2016-02-16 10:01:43 +03:00
										 |  |  |           [@body, servers_attrs] | 
					
						
							| 
									
										
										
										
											2015-08-03 18:01:50 +03:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def tags | 
					
						
							|  |  |  |           @body ||= create_object_from_json_body | 
					
						
							| 
									
										
										
										
											2015-08-04 12:36:10 +03:00
										 |  |  |           check_param(@body["tags"], Hash, "Parameter 'tags' should be a hash", false, true) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def run_list | 
					
						
							|  |  |  |           @body ||= create_object_from_json_body | 
					
						
							|  |  |  |           rl = check_array(@body["run_list"], "Parameter 'run_list' should be a not empty array of string", String, false, true) | 
					
						
							|  |  |  |           Validators::Helpers::RunList.new(rl).validate! | 
					
						
							|  |  |  |           rl | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-12 16:18:17 +03:00
										 |  |  |         def delete_list | 
					
						
							|  |  |  |           @body ||= create_object_from_json_body | 
					
						
							|  |  |  |           check_array(@body["servers_ids"], "Parameter 'servers_ids' should be a not empty array of string", String, false) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-16 10:01:43 +03:00
										 |  |  |         private | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def parse_list_of_ips_with_names(text) | 
					
						
							|  |  |  |           hash = {} | 
					
						
							|  |  |  |           text.each_line do |line| | 
					
						
							|  |  |  |             ip, name = line.split(':').map(&:strip) | 
					
						
							|  |  |  |             hash[ip] = name | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |           hash | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |