diff --git a/lib/n_able_rails.rb b/lib/n_able_rails.rb index b3cee0e..43eff84 100644 --- a/lib/n_able_rails.rb +++ b/lib/n_able_rails.rb @@ -1,4 +1,4 @@ -require "n_able_rails/version" +require 'n_able_rails/version' require 'savon' module NAbleRails @@ -11,6 +11,12 @@ def self.initialize(sas_url, username, password) # create a client for the service @client = Savon.client(wsdl: "#{sas_url}/dms/services/ServerEI?wsdl") + # test connection to the api + version_info + rescue HTTPI::SSLError, Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, + EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, + Net::ProtocolError, Errno::ECONNREFUSED => error + return error end # List all available operations for the api. @@ -24,39 +30,78 @@ def self.version_info end def self.list_devices(customer_id) - @client.call(:device_list, message: { Username: @username, Password: @password, Settings: - { "@xsi:type" => "impl:ArrayOf_tns1_T_KeyPair", "@env:arrayType" => "impl:T_KeyPair[]", - Setting:{key: "customerID", value: customer_id} } + @client.call(:device_list, message: { Username: @username, + Password: @password, + Settings: + { '@xsi:type' => 'impl:ArrayOf_tns1_T_KeyPair', + '@env:arrayType' => 'impl:T_KeyPair[]', + Setting: { key: 'customerID', + value: customer_id } } }) + rescue Savon::SOAPFault => error + return 'Invalid username-password combination.' if error.to_hash[:fault][:detail].nil? + return "Customer with id #{customer_id} could not be found." end def self.get_device_info(device_id) - @client.call(:device_get, message: { Username: @username, Password: @password, Settings: - { Setting:{key: "deviceID", value: device_id} } - } ) + @client.call(:device_get, message: { Username: @username, + Password: @password, + Settings: { Setting: { key: 'deviceID', + value: device_id } } + }) + rescue Savon::SOAPFault => error + return "Device with id #{device_id} could not be found." if error.to_hash[:fault][:faultstring] == '5000 Query failed.' + return 'Invalid username-password combination.' end def self.list_device_property(device_id) - @client.call(:device_property_list, message: { Username: @username, Password: @password, DeviceIDs: { DeviceID: device_id}, DeviceNames:{}, FilterIDs:{}, FilterNames:{}, ReverseOrder: false }) + @client.call(:device_property_list, message: { Username: @username, + Password: @password, + DeviceIDs: { DeviceID: device_id }, + DeviceNames: {}, + FilterIDs: {}, + FilterNames: {}, + ReverseOrder: false }) + rescue Savon::SOAPFault => error + return "Device with id #{device_id} could not be found." if error.to_hash[:fault][:faultstring] == '5000 Query failed.' + return 'Invalid username-password combination.' end def self.list_customers - @client.call(:customer_list, message: { Username: @username, Password: @password, Settings: { } }) + @client.call(:customer_list, message: { Username: @username, + Password: @password, + Settings: {} }) + rescue Savon::SOAPFault => error + return "Device with id #{device_id} could not be found." if error.to_hash[:fault][:faultstring] == '5000 Query failed.' + return 'Invalid username-password combination.' end def self.device_asset_info_export2ById(device_id) - @client.call(:device_asset_info_export2, message: { Version: "0.0", Username: @username, Password: @password, Settings: - { Setting:{Key: 'TargetByDeviceID', Value: {Value: device_id} } } - } ) + @client.call(:device_asset_info_export2, message: { Version: '0.0', + Username: @username, + Password: @password, + Settings: { Setting: { Key: 'TargetByDeviceID', + Value: { Value: device_id } } } + }) + rescue Savon::SOAPFault + return 'Invalid username-password combination.' end - def self.device_asset_info_export2() - @client.call(:device_asset_info_export2, message: { Version: "0.0", Username: @username, Password: @password }) + def self.device_asset_info_export2 + @client.call(:device_asset_info_export2, message: { Version: '0.0', + Username: @username, + Password: @password }) + rescue Savon::SOAPFault + return 'Invalid username-password combination.' end def self.device_status(device_id) - @client.call(:device_get_status, message: { Username: @username, Password: @password, Settings: - { Setting:{key: "deviceID", value: device_id} } - } ) + @client.call(:device_get_status, message: { Username: @username, + Password: @password, + Settings: { Setting: { key: 'deviceID', + value: device_id } } + }) + rescue Savon::SOAPFault + return 'Invalid username-password combination.' end end diff --git a/lib/n_able_rails/version.rb b/lib/n_able_rails/version.rb index 487fc51..987e83a 100644 --- a/lib/n_able_rails/version.rb +++ b/lib/n_able_rails/version.rb @@ -1,3 +1,3 @@ module NAbleRails - VERSION = "0.1.0" + VERSION = '0.1.1'.freeze end diff --git a/n_able_rails.gemspec b/n_able_rails.gemspec index 08c7ecb..3811907 100644 --- a/n_able_rails.gemspec +++ b/n_able_rails.gemspec @@ -4,23 +4,25 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'n_able_rails/version' Gem::Specification.new do |spec| - spec.name = "n_able_rails" + spec.name = 'n_able_rails' spec.version = NAbleRails::VERSION - spec.authors = ["Alex Myers"] - spec.email = ["dev.alex.myers@gmail.com"] - spec.description = "An Api wrapper for N-Able from SolarWinds" + spec.authors = ['Alex Myers'] + spec.email = ['dev.alex.myers@gmail.com'] + spec.description = 'An Api wrapper for N-Able from SolarWinds' spec.summary = %q{Ruby Wrapper for N-Able API} - spec.homepage = "https://github.com/enspiresoftware/n_able_rails" - spec.license = "MIT" + spec.homepage = 'https://github.com/enspiresoftware/n_able_rails' + spec.license = 'MIT' spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } - spec.bindir = "exe" + spec.bindir = 'exe' spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } - spec.require_paths = ["lib"] + spec.require_paths = ['lib'] - spec.add_development_dependency "bundler", '~> 0' - spec.add_development_dependency "rake", "~> 10.0" + spec.add_development_dependency 'bundler', '~> 0' + spec.add_development_dependency 'rake', '~> 10.0' + spec.add_development_dependency 'minitest-rails' + spec.add_development_dependency 'minitest-emoji' spec.add_runtime_dependency 'savon', '~> 2.3' spec.add_runtime_dependency 'wasabi', '~> 3.2'