kaseya 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +16 -0
  3. data/.rspec +3 -0
  4. data/.travis.yml +7 -0
  5. data/CODE_OF_CONDUCT.md +74 -0
  6. data/Gemfile +6 -0
  7. data/LICENSE.txt +21 -0
  8. data/README.md +118 -0
  9. data/Rakefile +6 -0
  10. data/bin/console +16 -0
  11. data/bin/setup +8 -0
  12. data/kaseya.gemspec +34 -0
  13. data/lib/kaseya/bms/api.rb +29 -0
  14. data/lib/kaseya/bms/client/accounts.rb +56 -0
  15. data/lib/kaseya/bms/client/activities.rb +23 -0
  16. data/lib/kaseya/bms/client/admin.rb +15 -0
  17. data/lib/kaseya/bms/client/contacts.rb +27 -0
  18. data/lib/kaseya/bms/client/lists.rb +13 -0
  19. data/lib/kaseya/bms/client/locations.rb +23 -0
  20. data/lib/kaseya/bms/client/tickets.rb +7 -0
  21. data/lib/kaseya/bms/client.rb +39 -0
  22. data/lib/kaseya/bms/configuration.rb +37 -0
  23. data/lib/kaseya/bms/connection.rb +24 -0
  24. data/lib/kaseya/bms/exceptions.rb +15 -0
  25. data/lib/kaseya/bms/http_errors.rb +10 -0
  26. data/lib/kaseya/bms/version.rb +3 -0
  27. data/lib/kaseya/bms.rb +53 -0
  28. data/lib/kaseya/collection.rb +15 -0
  29. data/lib/kaseya/connection.rb +60 -0
  30. data/lib/kaseya/exceptions.rb +5 -0
  31. data/lib/kaseya/response.rb +22 -0
  32. data/lib/kaseya/result.rb +17 -0
  33. data/lib/kaseya/version.rb +3 -0
  34. data/lib/kaseya/vsa/api.rb +29 -0
  35. data/lib/kaseya/vsa/client/agents.rb +11 -0
  36. data/lib/kaseya/vsa/client/machine_groups.rb +7 -0
  37. data/lib/kaseya/vsa/client/organizations.rb +27 -0
  38. data/lib/kaseya/vsa/client.rb +23 -0
  39. data/lib/kaseya/vsa/configuration.rb +37 -0
  40. data/lib/kaseya/vsa/connection.rb +19 -0
  41. data/lib/kaseya/vsa/exceptions.rb +15 -0
  42. data/lib/kaseya/vsa/http_errors.rb +10 -0
  43. data/lib/kaseya/vsa/json_errors.rb +9 -0
  44. data/lib/kaseya/vsa.rb +42 -0
  45. data/lib/kaseya.rb +15 -0
  46. metadata +186 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4828eab96ff60e4d59b7b5b9df30a03f87b0a32e8515e22c4beb9f9993e276c7
4
+ data.tar.gz: e2204f67564dc0b6259c32a3d116f5b95349601a8c18a3cae48e3b5a19e8133f
5
+ SHA512:
6
+ metadata.gz: 452497382f2165e56c18d365c8aa1e8c7f1112670f6234bc2aee8ddb05912c01449b715d99ba20d4d0d833fc59b292d6c670a2f50ba316f8a002fc404ff287e4
7
+ data.tar.gz: c2d2625d8f3c8ee7c4485296017e29941f60d950bd69cc7d617c43ecdc70d9f103b0b92a5417099ea0f62ed7b103d96895bae9208df214f3647c24535df37891
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ # Debugging history
14
+ .byebug_history
15
+
16
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.4.3
7
+ before_install: gem install bundler -v 1.16.4
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at cbisnett@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in kaseya.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Chris Bisnett
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.
data/README.md ADDED
@@ -0,0 +1,118 @@
1
+ # Kaseya
2
+
3
+ The Kaseya Ruby library provides convenient access to the Kaseya [BMS](https://www.kaseya.com/products/bms/) and [VSA](https://www.kaseya.com/products/vsa/) API's. The library is structured such that it can be used to manage multiple Kaseya tenants.
4
+
5
+ *Note: Not all API endpoints are implemented and more continue to be added as time permits.*
6
+
7
+ ## Documentation
8
+
9
+ API documentation:
10
+ - BMS: https://bms.kaseya.com/api
11
+ - VSA: http://help.kaseya.com/webhelp/EN/restapi/9040000/index.asp
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'kaseya'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install kaseya
28
+
29
+ ## Usage
30
+
31
+ To manage a BMS tenant, create a client by providing the required authentication parameters:
32
+
33
+ ```ruby
34
+ bms = Kaseya::BMS.authenticate("bms.kaseya.com", "username", "password", "Company Name")
35
+
36
+ # get all accounts in the CRM
37
+ bms.accounts.all()
38
+ ```
39
+
40
+ API methods are organized in the client based on the record type. For example, methods for creating/modifying/deleting contacts can be accessed through the `contacts` namespace:
41
+
42
+ ```ruby
43
+ # get all contacts
44
+ bms.contacts.all()
45
+
46
+ # add a contact
47
+ bms.contacts.new(
48
+ account_id: 2528,
49
+ location_id: 3169,
50
+ first_name: "Jane",
51
+ last_name: "Doe",
52
+ default_email_type_id: 1,
53
+ default_email_address: "foo@example.com",
54
+ default_phone_type_id: 1,
55
+ default_phone_number: "5551234567")
56
+ ```
57
+
58
+ ### Parameters
59
+
60
+ In order to support an idiomatic Ruby experience, symbol parameters will be converted from snake-case to camel-case before being submitted to the API.
61
+
62
+ ```ruby
63
+ bms.locations.new(
64
+ account_id: 1,
65
+ location_name: "California Office",
66
+ addresses: [{
67
+ address_1: "One Infinite Loop",
68
+ city: "Cupertino",
69
+ state: "CA",
70
+ zip_code: "95014"
71
+ }])
72
+ ```
73
+
74
+ is equivalent to:
75
+
76
+ ```ruby
77
+ bms.locations.new(
78
+ "AccountId" => 1,
79
+ "LocationName" => "California Office",
80
+ "Addresses" => [{
81
+ "Address1" => "One Infinite Loop",
82
+ "City" => "Cupertino",
83
+ "State" => "CA",
84
+ "ZipCode" => "95014"
85
+ }])
86
+ ```
87
+
88
+ ### Filtering, Sorting, Paging
89
+
90
+ Both the BMS and VSA API's use Odata expressions to specify filtering, sorting, and paging parameters. Where supported, these parameters can be passed as options using `skip`, `top`, `orderby`, and `filter` and the library will prepend the `$` before adding them to the query.
91
+
92
+ To query the second page of accounts where each page has 50 results:
93
+
94
+ ```ruby
95
+ bms.accounts(top: 50, skip: 50)
96
+ ```
97
+
98
+ Consult the documentation for details on which filtering expressions are supported and which properties support sorting.
99
+
100
+ ## Development
101
+
102
+ 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.
103
+
104
+ Run all tests:
105
+
106
+ bundle exec rake spec
107
+
108
+ ## Contributing
109
+
110
+ Bug reports and pull requests are welcome on GitHub at https://github.com/huntresslabs/kaseya-ruby. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
111
+
112
+ ## License
113
+
114
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
115
+
116
+ ## Code of Conduct
117
+
118
+ Everyone interacting in the Kaseya project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/huntresslabs/kaseya-ruby/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -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
data/bin/console ADDED
@@ -0,0 +1,16 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "kaseya"
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 "byebug"
14
+
15
+ require "irb"
16
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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
data/kaseya.gemspec ADDED
@@ -0,0 +1,34 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "kaseya/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "kaseya"
8
+ spec.version = Kaseya::VERSION
9
+ spec.authors = ["Chris Bisnett"]
10
+ spec.email = ["cbisnett@gmail.com"]
11
+
12
+ spec.summary = "Ruby client for the Kaseya BMS and VSA APIs"
13
+ spec.description = "Ruby client for the Kaseya BMS and VSA APIs"
14
+ spec.homepage = "https://github.com/huntresslabs/kaseya-ruby"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.16"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec", "~> 3.0"
29
+ spec.add_development_dependency "byebug"
30
+
31
+ spec.add_dependency "faraday", "~> 0.15.0"
32
+ spec.add_dependency "faraday_middleware", "~> 0.13.1"
33
+ spec.add_dependency "activesupport", "~> 5.2.3"
34
+ end
@@ -0,0 +1,29 @@
1
+ module Kaseya::BMS
2
+ class Api
3
+ attr_reader :connection
4
+
5
+ def initialize(connection)
6
+ @connection = connection
7
+ end
8
+
9
+ def get_many(path, params = {})
10
+ Kaseya::Collection.new connection.get(path, params)
11
+ end
12
+
13
+ def get_one(path, params = {})
14
+ Kaseya::Response.new connection.get(path, params)
15
+ end
16
+
17
+ def post(path, params = {})
18
+ Kaseya::Result.new connection.post(path, params)
19
+ end
20
+
21
+ def put(path, params = {})
22
+ Kaseya::Result.new connection.put(path, params)
23
+ end
24
+
25
+ def delete(path)
26
+ connection.delete(path)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,56 @@
1
+ module Kaseya::BMS
2
+ class Client::Accounts < Api
3
+ def all(params = {})
4
+ get_many "crm/accounts", params
5
+ end
6
+
7
+ def find(id)
8
+ get_one "crm/accounts/#{id}"
9
+ end
10
+
11
+ def create(params = {})
12
+ post "crm/accounts", params
13
+ end
14
+
15
+ def update(id, params = {})
16
+ put "crm/accounts/#{id}", params
17
+ end
18
+
19
+ def delete(id)
20
+ delete "crm/accounts/#{id}"
21
+ end
22
+
23
+ def locations(id, params = {})
24
+ get_many "crm/accounts/#{id}/locations", params
25
+ end
26
+
27
+ def main_location(id)
28
+ response = get_many("crm/accounts/#{id}/locations", top: 1, filter: "IsMain eq true")
29
+ response.first
30
+ end
31
+
32
+ def main_location_id(id)
33
+ (main_location(id) || {})["Id"]
34
+ end
35
+
36
+ def notes(id, params = {})
37
+ get_many "crm/accounts/#{id}/notes", params
38
+ end
39
+
40
+ def activities(id, params = {})
41
+ get_many "crm/accounts/#{id}/activity", params
42
+ end
43
+
44
+ def linked(id, params = {})
45
+ get_many "crm/accounts/#{id}/linkedaccounts", params
46
+ end
47
+
48
+ def types(params = {})
49
+ get_many "crm/accounts/types", params
50
+ end
51
+
52
+ def new_type(params = {})
53
+ post "crm/accounts/types", params
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,23 @@
1
+ module Kaseya::BMS
2
+ class Client::Activities < Api
3
+ def all(params = {})
4
+ get_many "crm/activity", params
5
+ end
6
+
7
+ def find(id)
8
+ get_one "crm/activity/#{id }"
9
+ end
10
+
11
+ def new(params = {})
12
+ post "crm/activity", params
13
+ end
14
+
15
+ def update(id, params = {})
16
+ put "crm/activity/#{id}", params
17
+ end
18
+
19
+ def notes(id, params = {})
20
+ get_many "crm/activity/#{id}/notes", params
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ module Kaseya::BMS
2
+ class Client::Admin < Api
3
+ def statuses(params = {})
4
+ get_many "system/statuses", params
5
+ end
6
+
7
+ def priorities(params = {})
8
+ get_many "system/priorities", params
9
+ end
10
+
11
+ def queues(params = {})
12
+ get_many "system/queues", params
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,27 @@
1
+ module Kaseya::BMS
2
+ class Client::Contacts < Api
3
+ def all(params = {})
4
+ get_many "crm/contacts", params
5
+ end
6
+
7
+ def find(id)
8
+ get_one "crm/contacts/#{id}"
9
+ end
10
+
11
+ def new(params = {})
12
+ post "crm/contacts", params
13
+ end
14
+
15
+ def update(id, params = {})
16
+ put "crm/contacts/#{id}", params
17
+ end
18
+
19
+ def delete(id)
20
+ delete "crm/contacts/#{id}"
21
+ end
22
+
23
+ def notes(id, params = {})
24
+ get_many "crm/contacts/#{id}/notes", params
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,13 @@
1
+ module Kaseya::BMS
2
+ class Client
3
+ class Lists < Api
4
+ def find(id, params = {})
5
+ get_many "system/lists/#{id}", params
6
+ end
7
+
8
+ def ticket_types(params = {})
9
+ find(LIST_TYPES[:ticket_type], params)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,23 @@
1
+ module Kaseya::BMS
2
+ class Client::Locations < Api
3
+ def all(params = {})
4
+ get_many "crm/locations", params
5
+ end
6
+
7
+ def find(id)
8
+ get_one "crm/locations/#{id}"
9
+ end
10
+
11
+ def new(params = {})
12
+ post "crm/locations", params
13
+ end
14
+
15
+ def update(id, params = {})
16
+ put "crm/locations/#{id}", params
17
+ end
18
+
19
+ def delete(id)
20
+ delete "crm/locations/#{id}"
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,7 @@
1
+ module Kaseya::BMS
2
+ class Client::Tickets < Api
3
+ def new(params = {})
4
+ post "servicedesk/tickets", params
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,39 @@
1
+ module Kaseya::BMS
2
+ class Client
3
+ Dir[File.expand_path('../client/*.rb', __FILE__)].each { |f| require f }
4
+
5
+ attr_reader :connection
6
+
7
+ def initialize(connection)
8
+ @connection = connection
9
+ end
10
+
11
+ def accounts
12
+ @accounts ||= Client::Accounts.new(@connection)
13
+ end
14
+
15
+ def activities
16
+ @activities ||= Client::Activities.new(@connection)
17
+ end
18
+
19
+ def admin
20
+ @admin ||= Client::Admin.new(@connection)
21
+ end
22
+
23
+ def contacts
24
+ @contacts ||= Client::Contacts.new(@connection)
25
+ end
26
+
27
+ def lists
28
+ @lists ||= Client::Lists.new(@connection)
29
+ end
30
+
31
+ def locations
32
+ @locations ||= Client::Locations.new(@connection)
33
+ end
34
+
35
+ def tickets
36
+ @tickets ||= Client::Tickets.new(@connection)
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,37 @@
1
+ module Kaseya::BMS
2
+ module Configuration
3
+ VALID_OPTIONS_KEYS = [
4
+ :debug,
5
+ :user_agent,
6
+ ].freeze
7
+
8
+ DEFAULT_DEBUG = false
9
+
10
+ DEFAULT_USER_AGENT = "Kaseya BMS Ruby Gem #{Kaseya::VERSION}".freeze
11
+
12
+ # Define accessor methods for configuration keys
13
+ attr_accessor *VALID_OPTIONS_KEYS
14
+
15
+ # Convenience method to allow configuration options to be set in a block
16
+ def configure
17
+ yield self
18
+ end
19
+
20
+ # When this module is extended, set all configuration options to their default values
21
+ def self.extended(base)
22
+ base.reset
23
+ end
24
+
25
+ # Create a hash of options and their values
26
+ def options
27
+ VALID_OPTIONS_KEYS.inject({}) do |option, key|
28
+ option.merge!(key => send(key))
29
+ end
30
+ end
31
+
32
+ def reset
33
+ self.debug = DEFAULT_DEBUG
34
+ self.user_agent = DEFAULT_USER_AGENT
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,24 @@
1
+ require "faraday"
2
+
3
+ require_relative "http_errors"
4
+
5
+ module Kaseya::BMS
6
+ class Connection < Kaseya::Connection
7
+ ERROR_MIDDLEWARE = [Kaseya::BMS::HttpErrors]
8
+
9
+ def initialize(host, token, expires)
10
+ super(host, token)
11
+ @expires = expires
12
+ end
13
+
14
+ def connection_options
15
+ {
16
+ headers: {
17
+ user_agent: Kaseya::BMS.user_agent,
18
+ },
19
+ url: "https://#{@host}/api",
20
+ ssl: { verify: !Kaseya::BMS.debug },
21
+ }
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,15 @@
1
+ module Kaseya::BMS
2
+ class BMSError < Kaseya::ApiError
3
+ attr_reader :response, :error, :description
4
+
5
+ def initialize(response)
6
+ @response = response
7
+ if @response&.body
8
+ @error = @response.body["error"]
9
+ @description = @response.body["error_description"]
10
+ end
11
+
12
+ super("#{@error || 'Error'}: #{@description || 'Unknown API error'}")
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,10 @@
1
+ module Kaseya::BMS
2
+ class HttpErrors < Faraday::Response::Middleware
3
+ def on_complete(env)
4
+ case env[:status].to_i
5
+ when 400..600
6
+ raise BMSError, env.response
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,3 @@
1
+ module Kaseya::BMS
2
+ VERSION = '0.1.0'.freeze
3
+ end
data/lib/kaseya/bms.rb ADDED
@@ -0,0 +1,53 @@
1
+ module Kaseya
2
+ module BMS
3
+ require_relative "bms/api"
4
+ require_relative "bms/client"
5
+ require_relative "bms/configuration"
6
+ require_relative "bms/connection"
7
+ require_relative "bms/exceptions"
8
+ require_relative "bms/http_errors"
9
+ require_relative "bms/version"
10
+
11
+ extend Configuration
12
+
13
+ TICKET_SOURCES = {
14
+ client_portal: 0,
15
+ phone: 1,
16
+ in_person: 2,
17
+ on_site: 3,
18
+ email: 4,
19
+ monitoring_system: 5,
20
+ voice_mail: 6,
21
+ verbal: 7,
22
+ other: 8,
23
+ recurring: 9,
24
+ }
25
+
26
+ LIST_TYPES = {
27
+ ticket_type: 1,
28
+ }
29
+
30
+ def self.authenticate(host, username, password, tenant)
31
+ params = {
32
+ grant_type: "password",
33
+ username: username,
34
+ password: password,
35
+ tenant: tenant
36
+ }
37
+
38
+ conn = Faraday.new(url: "https://#{host}/api", ssl: { verify: !debug }) do |faraday|
39
+ faraday.use HttpErrors
40
+
41
+ faraday.request :url_encoded
42
+ faraday.response :json
43
+ faraday.adapter Faraday.default_adapter
44
+ end
45
+
46
+ response = conn.post('token', params)
47
+ connection = Connection.new(host, response.body["access_token"], response.body["expires_in"])
48
+ Client.new(connection)
49
+ rescue Faraday::ConnectionFailed => e
50
+ raise Kaseya::ConnectionFailed, e.message
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,15 @@
1
+ module Kaseya
2
+ class Collection
3
+ attr_reader :total, :code, :status, :error, :result
4
+
5
+ delegate_missing_to :@results
6
+
7
+ def initialize(response)
8
+ @total = response["TotalRecords"]
9
+ @code = response["ResponseCode"]
10
+ @status = response["Status"]
11
+ @error = response["Error"]
12
+ @results = (response["Result"] || []).map { |r| Result.new r }
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,60 @@
1
+ require "faraday"
2
+ require "faraday_middleware"
3
+
4
+ module Kaseya
5
+ class Connection
6
+ ODATA_PARAMS = [:skip, :top, :orderby, :filter]
7
+ ERROR_MIDDLEWARE = []
8
+
9
+ def initialize(host, token)
10
+ @host = host
11
+ @token = token
12
+
13
+ @connection = Faraday::Connection.new(connection_options) do |conn|
14
+ ERROR_MIDDLEWARE.each do |klass|
15
+ conn.use klass
16
+ end
17
+
18
+ conn.authorization :Bearer, @token
19
+ conn.request :json
20
+ conn.response :json
21
+ conn.adapter Faraday.default_adapter
22
+ end
23
+ end
24
+
25
+ def get(path, params = {})
26
+ response = @connection.get(path, format_params(params))
27
+ response.body
28
+ end
29
+
30
+ def post(path, params = {})
31
+ response = @connection.post(path, format_params(params))
32
+ response.body
33
+ end
34
+
35
+ def put(path, params = {})
36
+ response = @connection.put(path, format_params(params))
37
+ response.body
38
+ end
39
+
40
+ def delete(path)
41
+ response = @connection.delete(path)
42
+ end
43
+
44
+ private
45
+
46
+ def connection_options
47
+ {}
48
+ end
49
+
50
+ def format_params(params)
51
+ params.transform_keys do |key|
52
+ case
53
+ when ODATA_PARAMS.include?(key.to_sym) then "$#{key}"
54
+ when key.is_a?(Symbol) then key.to_s.camelize
55
+ else key
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,5 @@
1
+ module Kaseya
2
+ class ConnectionFailed < StandardError; end
3
+
4
+ class ApiError < StandardError; end
5
+ end
@@ -0,0 +1,22 @@
1
+ module Kaseya
2
+ class Response
3
+ attr_reader :code, :status, :error, :result
4
+
5
+ delegate_missing_to :@result
6
+
7
+ def initialize(response)
8
+ @code = response["ResponseCode"]
9
+ @status = response["Status"]
10
+ @error = response["Error"]
11
+ @result = Result.new(response["Result"])
12
+ end
13
+
14
+ def error?
15
+ @error != "None"
16
+ end
17
+
18
+ def success?
19
+ !error?
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ module Kaseya
2
+ class Result
3
+ attr_reader :raw
4
+
5
+ delegate_missing_to :@raw
6
+
7
+ def initialize(result)
8
+ @raw = result || {}
9
+ end
10
+
11
+ def [](key)
12
+ # Convert snake-case symbols to camel-case strings
13
+ key = key.to_s.camelize if key.is_a?(Symbol)
14
+ @raw[key]
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Kaseya
2
+ VERSION = "0.2.0"
3
+ end
@@ -0,0 +1,29 @@
1
+ module Kaseya::VSA
2
+ class Api
3
+ attr_reader :connection
4
+
5
+ def initialize(connection)
6
+ @connection = connection
7
+ end
8
+
9
+ def get_many(path, params = {})
10
+ Kaseya::Collection.new connection.get(path, params)
11
+ end
12
+
13
+ def get_one(path, params = {})
14
+ Kaseya::Response.new connection.get(path, params)
15
+ end
16
+
17
+ def post(path, params = {})
18
+ Kaseya::Result.new connection.post(path, params)
19
+ end
20
+
21
+ def put(path, params = {})
22
+ Kaseya::Result.new connection.put(path, params)
23
+ end
24
+
25
+ def delete(path)
26
+ connection.delete(path)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,11 @@
1
+ module Kaseya::VSA
2
+ class Client::Agents < Api
3
+ def all(params = {})
4
+ get_many "assetmgmt/agents", params
5
+ end
6
+
7
+ def find(id)
8
+ get_one "assetmgmt/agents/#{id}"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,7 @@
1
+ module Kaseya::VSA
2
+ class Client::MachineGroups < Api
3
+ def all(params = {})
4
+ get_many "system/machinegroups", params
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,27 @@
1
+ module Kaseya::VSA
2
+ class Client::Organizations < Api
3
+ def all(params = {})
4
+ get_many "system/orgs", params
5
+ end
6
+
7
+ def find(id)
8
+ get_one "system/orgs/#{id}"
9
+ end
10
+
11
+ def machine_groups(id, params = {})
12
+ get_many "system/orgs/#{id}/machine_groups", params
13
+ end
14
+
15
+ def create(params = {})
16
+ post "system/orgs", params
17
+ end
18
+
19
+ def update(id, params = {})
20
+ put "system/orgs/#{id}", params
21
+ end
22
+
23
+ def delete(id)
24
+ delete "system/orgs/#{id}"
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,23 @@
1
+ module Kaseya::VSA
2
+ class Client
3
+ Dir[File.expand_path('../client/*.rb', __FILE__)].each { |f| require f }
4
+
5
+ attr_reader :connection
6
+
7
+ def initialize(connection)
8
+ @connection = connection
9
+ end
10
+
11
+ def agents
12
+ @agents ||= Client::Agents.new(@connection)
13
+ end
14
+
15
+ def machine_groups
16
+ @machine_groups ||= Client::MachineGroups.new(@connection)
17
+ end
18
+
19
+ def organizations
20
+ @organizations ||= Client::Organizations.new(@connection)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,37 @@
1
+ module Kaseya::VSA
2
+ module Configuration
3
+ VALID_OPTIONS_KEYS = [
4
+ :debug,
5
+ :user_agent,
6
+ ].freeze
7
+
8
+ DEFAULT_DEBUG = false
9
+
10
+ DEFAULT_USER_AGENT = "Kaseya VSA Ruby Gem #{Kaseya::VERSION}".freeze
11
+
12
+ # Define accessor methods for configuration keys
13
+ attr_accessor *VALID_OPTIONS_KEYS
14
+
15
+ # Convenience method to allow configuration options to be set in a block
16
+ def configure
17
+ yield self
18
+ end
19
+
20
+ # When this module is extended, set all configuration options to their default values
21
+ def self.extended(base)
22
+ base.reset
23
+ end
24
+
25
+ # Create a hash of options and their values
26
+ def options
27
+ VALID_OPTIONS_KEYS.inject({}) do |option, key|
28
+ option.merge!(key => send(key))
29
+ end
30
+ end
31
+
32
+ def reset
33
+ self.debug = DEFAULT_DEBUG
34
+ self.user_agent = DEFAULT_USER_AGENT
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,19 @@
1
+ require "faraday"
2
+
3
+ require_relative "http_errors"
4
+
5
+ module Kaseya::VSA
6
+ class Connection < Kaseya::Connection
7
+ ERROR_MIDDLEWARE = [Kaseya::VSA::HttpErrors]
8
+
9
+ def connection_options
10
+ {
11
+ headers: {
12
+ user_agent: Kaseya::VSA.user_agent
13
+ },
14
+ url: "https://#{@host}/api/v1.0",
15
+ ssl: { verify: !Kaseya::VSA.debug },
16
+ }
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ module Kaseya::VSA
2
+ class VSAError < Kaseya::ApiError
3
+ attr_reader :response, :code, :error
4
+
5
+ def initialize(response)
6
+ @response = response
7
+ if @response&.body
8
+ @code = @response.body["ResponseCode"]
9
+ @error = @response.body["Error"]
10
+ end
11
+
12
+ super("Error (#{@code}): #{@error || 'Unknown API error'}")
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,10 @@
1
+ module Kaseya::VSA
2
+ class HttpErrors < Faraday::Response::Middleware
3
+ def on_complete(env)
4
+ case env[:status].to_i
5
+ when 400..600
6
+ raise VSAError, env.response
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,9 @@
1
+ module Kaseya::VSA
2
+ class JsonErrors < Faraday::Response::Middleware
3
+ def on_complete(env)
4
+ unless env.response.body["ResponseCode"] == 0
5
+ raise VSAError, env.response
6
+ end
7
+ end
8
+ end
9
+ end
data/lib/kaseya/vsa.rb ADDED
@@ -0,0 +1,42 @@
1
+ require "digest"
2
+
3
+ module Kaseya
4
+ module VSA
5
+ require_relative "vsa/api"
6
+ require_relative "vsa/client"
7
+ require_relative "vsa/configuration"
8
+ require_relative "vsa/connection"
9
+ require_relative "vsa/exceptions"
10
+ require_relative "vsa/http_errors"
11
+ require_relative "vsa/json_errors"
12
+
13
+ extend Configuration
14
+
15
+ def self.authenticate(host, username, password)
16
+ nonce = SecureRandom.random_number(2**16).to_s
17
+ raw2 = Digest::SHA256.hexdigest(password)
18
+ pass2 = Digest::SHA256.hexdigest(Digest::SHA256.hexdigest(password + username) + nonce)
19
+ raw1 = Digest::SHA1.hexdigest(password)
20
+ pass1 = Digest::SHA1.hexdigest(Digest::SHA1.hexdigest(password + username) + nonce)
21
+
22
+ auth_string = "user=#{username},pass2=#{pass2},pass1=#{pass1},rpass2=#{raw2},rpass1=#{raw1},rand2=#{nonce}"
23
+ auth = Base64.strict_encode64(auth_string)
24
+
25
+ conn = Faraday.new(url: "https://#{host}/api/v1.0", ssl: { verify: !debug }) do |faraday|
26
+ faraday.use JsonErrors
27
+ faraday.use HttpErrors
28
+
29
+ faraday.authorization :Basic, auth
30
+ faraday.request :url_encoded
31
+ faraday.response :json
32
+ faraday.adapter Faraday.default_adapter
33
+ end
34
+
35
+ response = conn.get("auth")
36
+ connection = Connection.new(host, response.body.dig("Result", "ApiToken"))
37
+ Client.new(connection)
38
+ rescue Faraday::ConnectionFailed => e
39
+ raise Kaseya::ConnectionFailed, e.message
40
+ end
41
+ end
42
+ end
data/lib/kaseya.rb ADDED
@@ -0,0 +1,15 @@
1
+ require "kaseya/version"
2
+
3
+ require "active_support"
4
+ require "active_support/core_ext/hash"
5
+
6
+ module Kaseya
7
+ require "kaseya/collection"
8
+ require "kaseya/connection"
9
+ require "kaseya/exceptions"
10
+ require "kaseya/response"
11
+ require "kaseya/result"
12
+
13
+ require "kaseya/bms"
14
+ require "kaseya/vsa"
15
+ end
metadata ADDED
@@ -0,0 +1,186 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kaseya
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Chris Bisnett
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-03-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.15.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.15.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: faraday_middleware
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 0.13.1
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 0.13.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: activesupport
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 5.2.3
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 5.2.3
111
+ description: Ruby client for the Kaseya BMS and VSA APIs
112
+ email:
113
+ - cbisnett@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - CODE_OF_CONDUCT.md
122
+ - Gemfile
123
+ - LICENSE.txt
124
+ - README.md
125
+ - Rakefile
126
+ - bin/console
127
+ - bin/setup
128
+ - kaseya.gemspec
129
+ - lib/kaseya.rb
130
+ - lib/kaseya/bms.rb
131
+ - lib/kaseya/bms/api.rb
132
+ - lib/kaseya/bms/client.rb
133
+ - lib/kaseya/bms/client/accounts.rb
134
+ - lib/kaseya/bms/client/activities.rb
135
+ - lib/kaseya/bms/client/admin.rb
136
+ - lib/kaseya/bms/client/contacts.rb
137
+ - lib/kaseya/bms/client/lists.rb
138
+ - lib/kaseya/bms/client/locations.rb
139
+ - lib/kaseya/bms/client/tickets.rb
140
+ - lib/kaseya/bms/configuration.rb
141
+ - lib/kaseya/bms/connection.rb
142
+ - lib/kaseya/bms/exceptions.rb
143
+ - lib/kaseya/bms/http_errors.rb
144
+ - lib/kaseya/bms/version.rb
145
+ - lib/kaseya/collection.rb
146
+ - lib/kaseya/connection.rb
147
+ - lib/kaseya/exceptions.rb
148
+ - lib/kaseya/response.rb
149
+ - lib/kaseya/result.rb
150
+ - lib/kaseya/version.rb
151
+ - lib/kaseya/vsa.rb
152
+ - lib/kaseya/vsa/api.rb
153
+ - lib/kaseya/vsa/client.rb
154
+ - lib/kaseya/vsa/client/agents.rb
155
+ - lib/kaseya/vsa/client/machine_groups.rb
156
+ - lib/kaseya/vsa/client/organizations.rb
157
+ - lib/kaseya/vsa/configuration.rb
158
+ - lib/kaseya/vsa/connection.rb
159
+ - lib/kaseya/vsa/exceptions.rb
160
+ - lib/kaseya/vsa/http_errors.rb
161
+ - lib/kaseya/vsa/json_errors.rb
162
+ homepage: https://github.com/huntresslabs/kaseya-ruby
163
+ licenses:
164
+ - MIT
165
+ metadata: {}
166
+ post_install_message:
167
+ rdoc_options: []
168
+ require_paths:
169
+ - lib
170
+ required_ruby_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ required_rubygems_version: !ruby/object:Gem::Requirement
176
+ requirements:
177
+ - - ">="
178
+ - !ruby/object:Gem::Version
179
+ version: '0'
180
+ requirements: []
181
+ rubyforge_project:
182
+ rubygems_version: 2.7.6.2
183
+ signing_key:
184
+ specification_version: 4
185
+ summary: Ruby client for the Kaseya BMS and VSA APIs
186
+ test_files: []