hudu 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ee8592c8b1e645dcf0b370b7d2661b821f0a411d7bd6675b942d5e2a64d1ec20
4
+ data.tar.gz: 13ac2f1bcffab93943e0f3259c2c14f73c98a85cfb00952e181713501c290499
5
+ SHA512:
6
+ metadata.gz: 40187b263f4388eea09aed5d54713687afdf8aad57f69c23d2d6e538ef2af0f2449af3c41eb8f5c358576282527c0f09f2e88db205eb06056b9be5b74c276c57
7
+ data.tar.gz: a20ac42a0c6aa74e62a2e8dc34a00f73762db7f6d43d62d6c3dde37dd3221181640050bfee855feea66b56c4a437052c3625b53334c6aabe816ae0363573ca28
data/.env.template ADDED
@@ -0,0 +1,3 @@
1
+ HUDU_API_HOST=<your veeam api host>
2
+ HUDU_API_KEY=<api-key>
3
+ HUDU_TEST_COMPANY_ID=<id>
data/.gitignore ADDED
@@ -0,0 +1,44 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+ /data/
13
+ *.log
14
+ *.txt
15
+ *.json
16
+ *.yml
17
+
18
+ # Used by dotenv library to load environment variables.
19
+ .env
20
+
21
+
22
+ ## Documentation cache and generated files:
23
+ /.yardoc/
24
+ /_yardoc/
25
+ /doc/
26
+ /rdoc/
27
+
28
+ ## Environment normalization:
29
+ /.bundle/
30
+ /vendor/bundle
31
+ /lib/bundler/man/
32
+
33
+ # for a library or gem, you might want to ignore these files since the code is
34
+ # intended to run in multiple environments; otherwise, check them in:
35
+ # Gemfile.lock
36
+ # .ruby-version
37
+ # .ruby-gemset
38
+
39
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
40
+ .rvmrc
41
+
42
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
43
+ # .rubocop-https?--*
44
+ Gemfile.lock
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2024-02-20
4
+ - Initial release
5
+
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in cloudally.gemspec
6
+ gemspec
7
+
8
+ gem 'rake', '~> 13.0'
9
+ gem 'rubocop', '~> 1.7'
10
+ gem 'wrapi'
data/README.md ADDED
@@ -0,0 +1,113 @@
1
+ # Hudu API
2
+
3
+ This is a wrapper for the Hudu rest API. You can see the API endpoints here https://www.zabbix.com/documentation/current/en/manual/api/reference/
4
+
5
+ Currently only the GET requests to get a list of hosts, host groups and problems are implemented.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'hudu'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install hudu
22
+
23
+ ## Usage
24
+
25
+ Before you start making the requests to API provide the endpoint and api key using the configuration wrapping.
26
+
27
+ ```ruby
28
+ require 'hudu'
29
+ require 'logger'
30
+
31
+ # use do block
32
+ Hudu.configure do |config|
33
+ config.endpoint = ENV['HUDU_API_HOST'].downcase
34
+ config.api_key = ENV['HUDU_API_KEY']
35
+ end
36
+
37
+ # or configure with options hash
38
+ client = Hudu.client({ logger: Logger.new(CLIENT_LOGGER) })
39
+ client.login
40
+
41
+ ```
42
+
43
+ ## Resources
44
+ ### Authentication
45
+ ```
46
+ # setup
47
+ #
48
+ client.login
49
+ ```
50
+ |Resource|API endpoint|Description|
51
+ |:--|:--|:--|
52
+ |.login| none |uses api_info to check if credentials are correct. Raises Hudu:AuthenticationError incase this fails|
53
+
54
+
55
+
56
+ ### Data resources
57
+ Endpoint for data related requests
58
+ ```ruby
59
+
60
+ # list all asset layouts/fields for a company
61
+ assets = client.company_assets(client.companies.first.id)
62
+ assets.each do |asset|
63
+ layout = client.asset_layout(asset.asset_layout_id)
64
+ puts "#{layout.name}: '#{asset.name}'"
65
+ puts "=================================================="
66
+ asset.fields.each do |field|
67
+ value = field.value || ''
68
+ # trim value to first 40 characters
69
+ puts " #{field.position.to_s.rjust(3)} #{field.label}: '#{value[0..40]}'"
70
+ end
71
+ end
72
+ ```
73
+
74
+ |Resource|API endpoint|
75
+ |:--|:--|
76
+ |.api_info | /api/v1/api_info|
77
+ |.activity_logs | /api/v1/activity_logs|
78
+ |.companies, :company(id) | /api/v1/companies/{id}|
79
+ |.articles, :article(id) | /api/v1/articles/{id}|
80
+ |.asset_layouts, :asset_layout(id) | /api/v1/asset_layouts/{id}|
81
+ |.assets, :asset(id) | /api/v1/assets/{id}|
82
+ |.asset_passwords | /api/v1/asset_passwords/{id}|
83
+ |.folders, :folder(id) | /api/v1/folders/{id}|
84
+ |.procedures, :procedure(id) | /api/v1/procedures/{id}|
85
+ |.expirations | /api/v1/expirations|
86
+ |.websites, :website(id) | /api/v1/websites/{id}|
87
+ |.relations | /api/v1/relations|
88
+ |.company_articles(id) |/api/v1/companies/{id}/articles|
89
+ |.company_assets(id) |/api/v1/companies/{id}/assets|
90
+ |.company_asset(id,asset_id) |/api/v1/companies/{id}/assets/{asset_id}|
91
+
92
+
93
+ ## Publishing
94
+
95
+ 1. Update version in [version.rb](lib/hudu/version.rb).
96
+ 2. Add release to [CHANGELOG.md](CHANGELOG.md)
97
+ 3. Commit.
98
+ 4. Test build.
99
+ ```
100
+ > rake build
101
+
102
+ ```
103
+ 5. Release
104
+ ```
105
+ > rake release
106
+
107
+ ## Contributing
108
+
109
+ Bug reports and pull requests are welcome on GitHub at https://github.com/jancotanis/hudu.
110
+
111
+ ## License
112
+
113
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ require 'rubocop/rake_task'
13
+ RuboCop::RakeTask.new
14
+ task default: %i[test rubocop]
data/hudu.gemspec ADDED
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/hudu/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'hudu'
7
+ s.version = Hudu::VERSION
8
+ s.authors = ['Janco Tanis']
9
+ s.email = 'gems@jancology.com'
10
+ s.license = 'MIT'
11
+
12
+ s.summary = 'A Ruby wrapper for the Hudu APIs (readonly)'
13
+ s.homepage = 'https://rubygems.org/gems/hudu'
14
+
15
+ s.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
16
+
17
+ s.metadata['homepage_uri'] = s.homepage
18
+ s.metadata['source_code_uri'] = 'https://github.com/jancotanis/hudu'
19
+
20
+ # Specify which files should be added to the gem when it is released.
21
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
22
+ s.files = Dir.chdir(File.expand_path(__dir__)) do
23
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
24
+ end
25
+ s.bindir = 'exe'
26
+ s.executables = s.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
27
+ s.require_paths = ['lib']
28
+
29
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
30
+ s.platform = Gem::Platform::RUBY
31
+ s.add_runtime_dependency 'faraday'
32
+ s.add_runtime_dependency 'wrapi', ">= 0.2.0"
33
+ s.add_development_dependency 'dotenv'
34
+ s.add_development_dependency 'minitest'
35
+ s.add_development_dependency 'rubocop'
36
+ end
data/lib/hudu/api.rb ADDED
@@ -0,0 +1,34 @@
1
+ require "wrapi"
2
+ require File.expand_path('authentication', __dir__)
3
+ require File.expand_path('configuration', __dir__)
4
+
5
+ module Hudu
6
+ # @private
7
+ class API
8
+
9
+ # @private
10
+ attr_accessor *Configuration::VALID_OPTIONS_KEYS
11
+
12
+ # Creates a new API and copies settings from singleton
13
+ def initialize(options = {})
14
+ options = Hudu.options.merge(options)
15
+ Configuration::VALID_OPTIONS_KEYS.each do |key|
16
+ send("#{key}=", options[key])
17
+ end
18
+ end
19
+
20
+ def config
21
+ conf = {}
22
+ Configuration::VALID_OPTIONS_KEYS.each do |key|
23
+ conf[key] = send key
24
+ end
25
+ conf
26
+ end
27
+
28
+ include Configuration
29
+ include WrAPI::Connection
30
+ include WrAPI::Request
31
+ include WrAPI::Authentication
32
+ include Authentication
33
+ end
34
+ end
@@ -0,0 +1,20 @@
1
+ require File.expand_path('error', __dir__)
2
+
3
+ module Hudu
4
+ # Deals with authentication flow and stores it within global configuration
5
+ module Authentication
6
+
7
+ #
8
+ # Authorize to the Hudu portal and return access_token
9
+ def login(options = {})
10
+ raise ArgumentError, "Accesstoken/api-key not set" unless api_key
11
+ connection_options.merge!({ headers: { "x-api-key": api_key }})
12
+ # only api key needed
13
+ # will do sanitty check if token if valid
14
+ get('/api/v1/api_info')
15
+ rescue Faraday::Error => e
16
+ raise AuthenticationError, e
17
+ end
18
+
19
+ end
20
+ end
@@ -0,0 +1,76 @@
1
+ require File.expand_path('api', __dir__)
2
+
3
+ module Hudu
4
+ # Wrapper for the Hudu REST API
5
+ #
6
+ # @see
7
+ class Client < API
8
+
9
+ private
10
+ def self.api_endpoint(method, singular_method = nil, path = method)
11
+ # generate all and by id
12
+ if singular_method
13
+ # all records
14
+ self.send(:define_method, method) do |params = {}|
15
+ r = get_paged(api_url(path), params)
16
+ r = hudu_data(r,method)
17
+ end
18
+ # record by id
19
+ self.send(:define_method, singular_method) do |id, params = {}|
20
+ r = get(api_url("#{path}/" + id.to_s), params)
21
+ r = hudu_data(r,singular_method)
22
+ end
23
+ else
24
+ # normal method, return result
25
+ self.send(:define_method, method) do |params = {}|
26
+ get(api_url(path), params)
27
+ end
28
+ end
29
+ end
30
+
31
+ public
32
+ api_endpoint :api_info
33
+
34
+ # Activity logs can be filtered on
35
+ # user_id, user_email
36
+ # resource_id, resource_type
37
+ # action_message
38
+ # start_date - Must be in ISO 8601 format
39
+ api_endpoint :activity_logs
40
+ api_endpoint :companies, :company
41
+ api_endpoint :articles, :article
42
+ api_endpoint :asset_layouts, :asset_layout
43
+ api_endpoint :assets, :asset
44
+ api_endpoint :asset_passwords, :asset_password
45
+ api_endpoint :folders, :folder
46
+ api_endpoint :procedures, :procedure
47
+ api_endpoint :expirations
48
+ api_endpoint :websites, :website
49
+ api_endpoint :relations
50
+
51
+ def company_articles( company_id, params = {} )
52
+ articles({company_id: company_id}.merge(parems))
53
+ end
54
+
55
+ def company_assets(id,params={})
56
+ get_paged(api_url("companies/#{id}/assets"), params)
57
+ end
58
+ def company_asset(id,asset_id,params={})
59
+ get(api_url("companies/#{id}/assets/#{asset_id}"), params)
60
+ end
61
+
62
+ # return api path
63
+ def api_url path
64
+ "/api/v1/#{path}"
65
+ end
66
+
67
+ # hudu returns data as {resource:{}} or {resource:[]}
68
+ def hudu_data(result,resource)
69
+ if result.is_a?(Hash) && result[resource.to_s]
70
+ result[resource.to_s]
71
+ else
72
+ result
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,43 @@
1
+ require 'wrapi'
2
+ require File.expand_path('version', __dir__)
3
+ require File.expand_path('pagination', __dir__)
4
+
5
+ module Hudu
6
+ # Defines constants and methods related to configuration
7
+ module Configuration
8
+ include WrAPI::Configuration
9
+
10
+ # An array of additional valid keys in the options hash when configuring a [Hudu::API]
11
+ VALID_OPTIONS_KEYS = (WrAPI::Configuration::VALID_OPTIONS_KEYS + [:api_key]).freeze
12
+
13
+ # @private
14
+ attr_accessor *VALID_OPTIONS_KEYS
15
+
16
+ DEFAULT_UA = "Ruby Hudu API wrapper #{Hudu::VERSION}".freeze
17
+ DEFAULT_PAGINATION = RequestPagination::PagingInfoPager
18
+ DEFAULT_PAGE_SIZE = 25
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
+ # Reset all configuration options to defaults
33
+ def reset
34
+ super
35
+ self.api_key = nil
36
+ self.endpoint = nil
37
+ self.user_agent = DEFAULT_UA
38
+ self.page_size = DEFAULT_PAGE_SIZE
39
+ self.pagination_class = DEFAULT_PAGINATION
40
+ end
41
+ end
42
+ end
43
+
data/lib/hudu/error.rb ADDED
@@ -0,0 +1,8 @@
1
+ module Hudu
2
+
3
+ # Generic error to be able to rescue all Hudu errors
4
+ class HuduError < StandardError; end
5
+
6
+ # Error when authentication fails
7
+ class AuthenticationError < HuduError; end
8
+ end
@@ -0,0 +1,43 @@
1
+ require 'uri'
2
+ require 'json'
3
+
4
+ module Hudu
5
+
6
+ # Defines HTTP request methods
7
+ # @see https://support.hudu.com/hc/en-us/articles/11422780787735-REST-API#pagination-0-5
8
+ module RequestPagination
9
+
10
+ class PagingInfoPager
11
+ attr_reader :offset, :limit, :total
12
+ def initialize(page_size)
13
+ @page = 1
14
+ @page_total = @page_size = page_size
15
+ end
16
+
17
+ def page_options
18
+ { page: @page }
19
+ end
20
+
21
+ def next_page!(body)
22
+ @page += 1
23
+ a = PagingInfoPager.data(body)
24
+ @page_total = a.is_a?(Array) ? a.count : 1
25
+ end
26
+
27
+ def more_pages?
28
+ # while full page we have next page
29
+ @page_total == @page_size
30
+ end
31
+
32
+ def self.data(body)
33
+ # assume hash {"resource":[...]}, get first key and return array data
34
+ k,v = body.first
35
+ if v.is_a?(Array) || v.is_a?(Hash)
36
+ v
37
+ else
38
+ body
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hudu
4
+ VERSION = '0.1.0'
5
+ end
data/lib/hudu.rb ADDED
@@ -0,0 +1,19 @@
1
+ require "wrapi"
2
+ require File.expand_path('hudu/configuration', __dir__)
3
+ require File.expand_path('hudu/client', __dir__)
4
+
5
+ module Hudu
6
+ extend Configuration
7
+ extend WrAPI::RespondTo
8
+
9
+ #
10
+ # @return [Hudu::Client]
11
+ def self.client(options = {})
12
+ Hudu::Client.new(options)
13
+ end
14
+
15
+ def self.reset
16
+ super
17
+
18
+ end
19
+ end
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hudu
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Janco Tanis
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-02-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: wrapi
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.2.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.2.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: dotenv
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
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: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email: gems@jancology.com
85
+ executables: []
86
+ extensions: []
87
+ extra_rdoc_files: []
88
+ files:
89
+ - ".env.template"
90
+ - ".gitignore"
91
+ - CHANGELOG.md
92
+ - Gemfile
93
+ - README.md
94
+ - Rakefile
95
+ - hudu.gemspec
96
+ - lib/hudu.rb
97
+ - lib/hudu/api.rb
98
+ - lib/hudu/authentication.rb
99
+ - lib/hudu/client.rb
100
+ - lib/hudu/configuration.rb
101
+ - lib/hudu/error.rb
102
+ - lib/hudu/pagination.rb
103
+ - lib/hudu/version.rb
104
+ homepage: https://rubygems.org/gems/hudu
105
+ licenses:
106
+ - MIT
107
+ metadata:
108
+ homepage_uri: https://rubygems.org/gems/hudu
109
+ source_code_uri: https://github.com/jancotanis/hudu
110
+ post_install_message:
111
+ rdoc_options: []
112
+ require_paths:
113
+ - lib
114
+ required_ruby_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ version: 2.4.0
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ requirements: []
125
+ rubygems_version: 3.2.12
126
+ signing_key:
127
+ specification_version: 4
128
+ summary: A Ruby wrapper for the Hudu APIs (readonly)
129
+ test_files: []