swhd_api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4c8c21a8257146d6c2e916dbceefdd7aa7be8dc4
4
+ data.tar.gz: fc8a190a043edb5e6db5d4feb854b165ef583f34
5
+ SHA512:
6
+ metadata.gz: a35de960ca06c60fc80b8b8d9cca1fdd99dd3962c93c274ef2cbf698092e400cb737e7159c5946842fec428ff6464df014ad3f181765543b8661b4eef48e8d45
7
+ data.tar.gz: f41f6bfd8efec1c0747534db1aaacac10fe7ca84cf1e9305befe2187b087edc5a3c5aad5d594119276a9321c77571c9e3f1275383fe19ac5a328ec9355adfbb1
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.gem
11
+
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,15 @@
1
+ AllCops:
2
+ Include:
3
+ - '**/Rakefile'
4
+ - '**/config.ru'
5
+ Exclude:
6
+ - 'bin/**/*'
7
+ - 'db/**/*'
8
+ - 'config/**/*'
9
+ - 'script/**/*'
10
+ - !ruby/regexp /old_and_unused\.rb$/
11
+
12
+ Metrics/ClassLength:
13
+ Max: 200
14
+ Metrics/LineLength:
15
+ Max: 100
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.1
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in swhd_api.gemspec
4
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Marvin Frederickson
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,57 @@
1
+ # SolarWinds Helpdesk Api Helper
2
+
3
+ ## Installation
4
+
5
+ Add this line to your application's Gemfile:
6
+
7
+ ```ruby
8
+ gem 'swhd_api'
9
+ ```
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install swhd_api
18
+
19
+ ## Usage
20
+
21
+ ```ruby
22
+ require 'swhd_api'
23
+ m = SwhdApi::Manager.new(url, options)
24
+ m.connect(credentials)
25
+ m.request("Clients", :get).count
26
+ m.fetch("Clients").count
27
+ ```
28
+
29
+ The _url_ is the api service url such as `https://helpdesk/helpdesk/WebObjects/Helpdesk.woa/ra`
30
+ and _options_, if specified, is a hash that is passed to Typhoeus, and if you are using a self-signed
31
+ SSL certificate, it may need to contain `{ ssl_verifypeer: false, ssl_verifyhost: 0 }`. There's
32
+ also the `{ verbose: true }` option, if you are debugging.
33
+
34
+ The _credentials_ hash includes one of the following
35
+ `{ apikey: value }`
36
+ `{ techkey: value }`
37
+ `{ username: value, password: value }`
38
+ and is used to obtain a session for subsequent calls.
39
+
40
+ ** there are problems with their session api, so for now we have to pass the apikey: value in the credentials so they
41
+ get stored in the code and passed along instead of the sessionKey.
42
+
43
+ ## Development
44
+
45
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
46
+
47
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
48
+
49
+ ## Contributing
50
+
51
+ Bug reports and pull requests are welcome on GitHub at https://github.com/KPB-US/swhd_api.
52
+
53
+
54
+ ## License
55
+
56
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
57
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "swhd_api"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,102 @@
1
+ require 'swhd_api/version'
2
+ require 'swhd_api/exceptions'
3
+ require 'typhoeus'
4
+ require 'json'
5
+
6
+ module SwhdApi
7
+ # main api helper class
8
+ class Manager
9
+ attr_reader :session_id
10
+ attr_reader :url
11
+ attr_accessor :logger
12
+
13
+ def initialize(url = nil, options = {})
14
+ raise SwhdApi::MissingUrl if url.nil? || url.empty?
15
+ # TODO: strip trailing slash if any
16
+ @url = url
17
+ @options = options
18
+ end
19
+
20
+ def connect(credentials = {})
21
+ if credentials.key?(:apikey)
22
+ # HACK: remove once SolarWinds get the sessionKey working as documented
23
+ @session_id = credentials[:apikey]
24
+ return
25
+
26
+ elsif credentials.key?(:username) && credentials.key?(:password)
27
+ # use username and password
28
+ # @session_id = "2"
29
+ elsif credentials.key?(:techkey)
30
+ # use tech's key
31
+ # @session_id = "3"
32
+ else
33
+ raise MissingCredentials, ':apikey or :techkey or :username and :password'
34
+ end
35
+
36
+ results = request('Session', :get, credentials) # { "apiKey" => credentials[:apikey] })
37
+ @session_id = results['sessionKey']
38
+ end
39
+
40
+ def request(resource, method, params = {}, body = {})
41
+ raise NoSession if @session_id.nil? && resource != 'Session'
42
+ raise MissingResource if resource.nil? || resource.empty?
43
+
44
+ endpoint = "#{@url}/#{resource}"
45
+ # TODO: log a call to solarwinds and find out why no one can do this (other users on their forums)
46
+ # HACK: for now, use apikey instead of session key
47
+ params['apiKey'] = @session_id
48
+ payload = @options.dup
49
+ payload[:params] = params.reject { |_k, v| v.nil? }
50
+
51
+ response =
52
+ case method
53
+ when :post
54
+ payload[:body] = body.to_json
55
+ Typhoeus::Request.post(endpoint, payload)
56
+ when :get
57
+ Typhoeus::Request.get(endpoint, payload)
58
+ when :put
59
+ payload[:body] = body.to_json
60
+ Typhoeus::Request.put(endpoint, payload)
61
+ when :delete
62
+ Typhoeus::Request.delete(endpoint, payload)
63
+ end
64
+
65
+ if response.timed_out?
66
+ raise TimedOut, response.body
67
+ elsif response.code == 0
68
+ raise NoResponse, response.body
69
+ elsif response.return_code == :partial_file
70
+ raise PartialFile, response.body
71
+ elsif !response.success?
72
+ raise AuthenticationFailure, response.return_message if response.code == 401
73
+ raise RequestFailed, "Response Code: #{response.code}\n
74
+ Return Code: #{response.return_code} - #{response.return_message}\n
75
+ #{response.body}"
76
+ end
77
+
78
+ @logger.debug response.body unless @logger.nil?
79
+
80
+ JSON.parse(response.body)
81
+ end
82
+
83
+ # fetch all pages of results
84
+ def fetch(resource, params = {})
85
+ method = :get
86
+ page = 1
87
+
88
+ params[:page] = page
89
+ partial = request(resource, method, params)
90
+ while partial.is_a?(Array) && !partial.empty?
91
+ results ||= []
92
+ results += partial
93
+ page += 1
94
+ params[:page] = page
95
+ partial = request(resource, method, params)
96
+ end
97
+
98
+ results = partial unless partial.nil? || partial.empty?
99
+ results
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,11 @@
1
+ module SwhdApi
2
+ class AuthenticationFailure < StandardError; end
3
+ class MissingCredentials < ArgumentError; end
4
+ class MissingUrl < ArgumentError; end
5
+ class NoSession < StandardError; end
6
+ class MissingResource < ArgumentError; end
7
+ class TimedOut < StandardError; end
8
+ class PartialFile < StandardError; end
9
+ class NoResponse < StandardError; end
10
+ class RequestFailed < StandardError; end
11
+ end
@@ -0,0 +1,3 @@
1
+ module SwhdApi
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'swhd_api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "swhd_api"
8
+ spec.version = SwhdApi::VERSION
9
+ spec.authors = ["Marvin Frederickson"]
10
+ spec.email = ["mfrederickson@kpb.us"]
11
+
12
+ spec.summary = %q{A helper gem to access the solarwinds helpdesk api}
13
+ spec.description = %q{A helper gem to access the solarwinds helpdesk api}
14
+ spec.homepage = "http://github.com/KPB-US/swhd-api"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_dependency 'typhoeus', '~> 1.0'
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.11"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ end
metadata ADDED
@@ -0,0 +1,114 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: swhd_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marvin Frederickson
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-03-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: typhoeus
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: A helper gem to access the solarwinds helpdesk api
70
+ email:
71
+ - mfrederickson@kpb.us
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".rubocop.yml"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - lib/swhd_api.rb
87
+ - lib/swhd_api/exceptions.rb
88
+ - lib/swhd_api/version.rb
89
+ - swhd_api.gemspec
90
+ homepage: http://github.com/KPB-US/swhd-api
91
+ licenses:
92
+ - MIT
93
+ metadata: {}
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubyforge_project:
110
+ rubygems_version: 2.4.3
111
+ signing_key:
112
+ specification_version: 4
113
+ summary: A helper gem to access the solarwinds helpdesk api
114
+ test_files: []