Class: Bosh::Cpi::Cli
- Inherits:
-
Object
- Object
- Bosh::Cpi::Cli
- Defined in:
- lib/bosh/cpi/cli.rb
Constant Summary collapse
- KNOWN_RPC_METHODS =
%w( current_vm_id create_stemcell delete_stemcell create_vm delete_vm has_vm reboot_vm set_vm_metadata configure_networks create_disk has_disk delete_disk attach_disk detach_disk snapshot_disk delete_snapshot get_disks ping ).freeze
- RPC_METHOD_TO_RUBY_METHOD =
{ 'has_vm' => 'has_vm?', 'has_disk' => 'has_disk?', }.freeze
- INVALID_CALL_ERROR_TYPE =
'InvalidCall'.freeze
- UNKNOWN_ERROR_TYPE =
'Unknown'.freeze
Instance Method Summary collapse
-
#initialize(cpi, logs_string_io, result_io) ⇒ Cli
constructor
A new instance of Cli.
- #run(json) ⇒ Object
Constructor Details
#initialize(cpi, logs_string_io, result_io) ⇒ Cli
Returns a new instance of Cli.
33 34 35 36 37 |
# File 'lib/bosh/cpi/cli.rb', line 33 def initialize(cpi, logs_string_io, result_io) @cpi = cpi @logs_string_io = logs_string_io @result_io = result_io end |
Instance Method Details
#run(json) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/bosh/cpi/cli.rb', line 39 def run(json) begin request = JSON.load(json) rescue JSON::ParserError => e return error_response(INVALID_CALL_ERROR_TYPE, "Request cannot be deserialized, details: #{e.}", false, e.backtrace) end method = request['method'] unless method.is_a?(String) return error_response(INVALID_CALL_ERROR_TYPE, "Method must be a String, got: '#{method.inspect}'", false) end unless KNOWN_RPC_METHODS.include?(method) return error_response(INVALID_CALL_ERROR_TYPE, "Method is not known, got: '#{method}'", false) end arguments = request['arguments'] unless arguments.is_a?(Array) return error_response(INVALID_CALL_ERROR_TYPE, "Arguments must be an Array, got: '#{arguments.inspect}'", false) end context = request['context'] unless context.is_a?(Hash) && context['director_uuid'].is_a?(String) return error_response(INVALID_CALL_ERROR_TYPE, "Request should include context with director uuid, got: '#{context.inspect}'", false) end configure_director(context['director_uuid']) ruby_method = RPC_METHOD_TO_RUBY_METHOD[method] || method begin cpi = @cpi.call result = cpi.public_send(ruby_method, *arguments) rescue Bosh::Clouds::RetriableCloudError => e return error_response(error_name(e), e., e.ok_to_retry, e.backtrace) rescue Bosh::Clouds::CloudError, Bosh::Clouds::CpiError => e return error_response(error_name(e), e., false, e.backtrace) rescue ArgumentError => e return error_response(INVALID_CALL_ERROR_TYPE, "Arguments are not correct, details: '#{e.}'", false, e.backtrace) rescue Exception => e return error_response(UNKNOWN_ERROR_TYPE, e., false, e.backtrace) end result_response(result) end |