| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | require "json" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-15 14:26:54 +03:00
										 |  |  | module Devops | 
					
						
							| 
									
										
										
										
											2015-07-17 20:22:29 +03:00
										 |  |  |   module API2_0 | 
					
						
							| 
									
										
										
										
											2014-12-22 14:22:04 +03:00
										 |  |  |     module Routes | 
					
						
							| 
									
										
										
										
											2014-12-15 14:26:54 +03:00
										 |  |  |       module KeyRoutes | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-15 14:26:54 +03:00
										 |  |  |         def self.registered(app) | 
					
						
							|  |  |  |           # Get list of available ssh keys | 
					
						
							|  |  |  |           # | 
					
						
							|  |  |  |           # * *Request* | 
					
						
							|  |  |  |           #   - method : GET | 
					
						
							|  |  |  |           #   - headers : | 
					
						
							|  |  |  |           #     - Accept: application/json | 
					
						
							|  |  |  |           # | 
					
						
							|  |  |  |           # * *Returns* : array of strings | 
					
						
							|  |  |  |           #   [ | 
					
						
							|  |  |  |           #     { | 
					
						
							|  |  |  |           #       "scope": "system", -> 'system' - key was added by server, 'user' - key was added by user | 
					
						
							|  |  |  |           #       "id": "devops" | 
					
						
							|  |  |  |           #     } | 
					
						
							|  |  |  |           #   ] | 
					
						
							| 
									
										
										
										
											2015-07-17 20:22:29 +03:00
										 |  |  |           app.get_with_headers "/keys", :headers => [:accept] do | 
					
						
							|  |  |  |             check_privileges("key", "r") | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |             json Devops::API2_0::Handler::Key.new(request).keys.map(&:to_hash) | 
					
						
							| 
									
										
										
										
											2015-07-17 20:22:29 +03:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-15 14:26:54 +03:00
										 |  |  |           # Create ssh key on devops server | 
					
						
							|  |  |  |           # | 
					
						
							|  |  |  |           # * *Request* | 
					
						
							|  |  |  |           #   - method : POST | 
					
						
							|  |  |  |           #   - headers : | 
					
						
							|  |  |  |           #     - Accept: application/json | 
					
						
							|  |  |  |           #     - Content-Type: application/json | 
					
						
							|  |  |  |           #   - body : | 
					
						
							|  |  |  |           #     { | 
					
						
							|  |  |  |           #       "file_name": "key file name", | 
					
						
							|  |  |  |           #       "key_name": "key name", | 
					
						
							|  |  |  |           #       "content": "key content" | 
					
						
							|  |  |  |           #     } | 
					
						
							|  |  |  |           # | 
					
						
							|  |  |  |           # * *Returns* : | 
					
						
							|  |  |  |           #   201 - Created | 
					
						
							| 
									
										
										
										
											2015-07-17 20:22:29 +03:00
										 |  |  |           app.post_with_headers "/key", :headers => [:accept, :content_type] do | 
					
						
							|  |  |  |             check_privileges("key", "w") | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |             Devops::API2_0::Handler::Key.new(request).create | 
					
						
							| 
									
										
										
										
											2015-07-17 20:22:29 +03:00
										 |  |  |             create_response("Created", nil, 201) | 
					
						
							|  |  |  |           end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-15 14:26:54 +03:00
										 |  |  |           # Delete ssh key from devops server | 
					
						
							|  |  |  |           # | 
					
						
							|  |  |  |           # * *Request* | 
					
						
							|  |  |  |           #   - method : DELETE | 
					
						
							|  |  |  |           #   - headers : | 
					
						
							|  |  |  |           #     - Accept: application/json | 
					
						
							|  |  |  |           # | 
					
						
							|  |  |  |           # * *Returns* : | 
					
						
							|  |  |  |           #   200 - Deleted | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |           app.delete_with_headers "/key/:key", :headers => [:accept] do |key| | 
					
						
							| 
									
										
										
										
											2015-07-17 20:22:29 +03:00
										 |  |  |             check_privileges("key", "w") | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |             r = Devops::API2_0::Handler::Key.new(request).delete key | 
					
						
							| 
									
										
										
										
											2015-07-27 18:27:52 +03:00
										 |  |  |             return [500, r["err"].inspect] if r["err"] | 
					
						
							| 
									
										
										
										
											2015-07-30 15:37:43 +03:00
										 |  |  |             create_response("Key '#{key}' removed") | 
					
						
							| 
									
										
										
										
											2015-07-17 20:22:29 +03:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2014-12-15 14:26:54 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |           puts "Key routes initialized" | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2014-05-08 15:34:26 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | end |