hudu 0.1.0 → 0.3.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ee8592c8b1e645dcf0b370b7d2661b821f0a411d7bd6675b942d5e2a64d1ec20
4
- data.tar.gz: 13ac2f1bcffab93943e0f3259c2c14f73c98a85cfb00952e181713501c290499
3
+ metadata.gz: 6de79a28de35dea4c3ac9ebd26823427f839192e44113fe67f9d5f5e845cade1
4
+ data.tar.gz: 1bada2b529ac16cc376b85f33b953a451a23f8e955dcc2068814171bcbf0509c
5
5
  SHA512:
6
- metadata.gz: 40187b263f4388eea09aed5d54713687afdf8aad57f69c23d2d6e538ef2af0f2449af3c41eb8f5c358576282527c0f09f2e88db205eb06056b9be5b74c276c57
7
- data.tar.gz: a20ac42a0c6aa74e62a2e8dc34a00f73762db7f6d43d62d6c3dde37dd3221181640050bfee855feea66b56c4a437052c3625b53334c6aabe816ae0363573ca28
6
+ metadata.gz: 59adb93ef27db882adc800a183ffbe1b7297e96a11014492ff61774395d0969a79603bd442c452f028fae38fc2b6a9ac591f66151276b94bc4d9b154bddda8f6
7
+ data.tar.gz: bdf2dfd500420081eb4b530f3621f90ae21f3204e0c817e7e33da8c1baae660425e2c844059693c5f5048d7ee1d16e26159987a7ff1d6562dbc716453e06ea97
data/CHANGELOG.md CHANGED
@@ -3,3 +3,6 @@
3
3
  ## [0.1.0] - 2024-02-20
4
4
  - Initial release
5
5
 
6
+ ## [0.2.0] - 2024-03-13
7
+ - update_company_asset added
8
+
data/Gemfile CHANGED
@@ -2,9 +2,10 @@
2
2
 
3
3
  source 'https://rubygems.org'
4
4
 
5
- # Specify your gem's dependencies in cloudally.gemspec
5
+ # Specify your gem's dependencies in hudu.gemspec
6
6
  gemspec
7
7
 
8
8
  gem 'rake', '~> 13.0'
9
9
  gem 'rubocop', '~> 1.7'
10
+ gem 'simplecov', require: false, group: :test
10
11
  gem 'wrapi'
data/README.md CHANGED
@@ -1,6 +1,9 @@
1
1
  # Hudu API
2
+ [![Version](https://img.shields.io/gem/v/hudu.svg)](https://rubygems.org/gems/hudu)
3
+ [![Maintainability](https://api.codeclimate.com/v1/badges/e02c3f2cd2f13261597d/maintainability)](https://codeclimate.com/github/jancotanis/hudu/maintainability)
4
+ [![Test Coverage](https://api.codeclimate.com/v1/badges/e02c3f2cd2f13261597d/test_coverage)](https://codeclimate.com/github/jancotanis/hudu/test_coverage)
2
5
 
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/
6
+ This is a wrapper for the Hudu rest API.
4
7
 
5
8
  Currently only the GET requests to get a list of hosts, host groups and problems are implemented.
6
9
 
@@ -75,19 +78,19 @@ end
75
78
  |:--|:--|
76
79
  |.api_info | /api/v1/api_info|
77
80
  |.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}|
81
+ |.companies, company(id) | /api/v1/companies/{id}|
82
+ |.articles, article(id) | /api/v1/articles/{id}|
83
+ |.asset_layouts, asset_layout(id) | /api/v1/asset_layouts/{id}|
84
+ |.assets, asset(id) | /api/v1/assets/{id}|
82
85
  |.asset_passwords | /api/v1/asset_passwords/{id}|
83
- |.folders, :folder(id) | /api/v1/folders/{id}|
84
- |.procedures, :procedure(id) | /api/v1/procedures/{id}|
86
+ |.folders, folder(id) | /api/v1/folders/{id}|
87
+ |.procedures, procedure(id) | /api/v1/procedures/{id}|
85
88
  |.expirations | /api/v1/expirations|
86
- |.websites, :website(id) | /api/v1/websites/{id}|
89
+ |.websites, website(id) | /api/v1/websites/{id}|
87
90
  |.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
+ |.company_articles(id) | /api/v1/companies/{id}/articles|
92
+ |.company_assets(id) | /api/v1/companies/{id}/assets|
93
+ |.company_asset(id,asset_id) | /api/v1/companies/{id}/assets/{asset_id}|
91
94
 
92
95
 
93
96
  ## Publishing
data/Rakefile CHANGED
@@ -1,12 +1,20 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'bundler/gem_tasks'
4
+ require 'dotenv'
4
5
  require 'rake/testtask'
5
6
 
7
+ Dotenv.load
8
+
9
+ system './bin/cc-test-reporter before-build'
10
+
6
11
  Rake::TestTask.new(:test) do |t|
7
12
  t.libs << 'test'
8
13
  t.libs << 'lib'
9
14
  t.test_files = FileList['test/**/*_test.rb']
15
+ at_exit do
16
+ system './bin/cc-test-reporter after-build'
17
+ end
10
18
  end
11
19
 
12
20
  require 'rubocop/rake_task'
Binary file
data/hudu.gemspec CHANGED
@@ -29,8 +29,9 @@ Gem::Specification.new do |s|
29
29
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
30
30
  s.platform = Gem::Platform::RUBY
31
31
  s.add_runtime_dependency 'faraday'
32
- s.add_runtime_dependency 'wrapi', ">= 0.2.0"
32
+ s.add_runtime_dependency 'wrapi', ">= 0.3.0"
33
33
  s.add_development_dependency 'dotenv'
34
34
  s.add_development_dependency 'minitest'
35
+ s.add_development_dependency 'simplecov'
35
36
  s.add_development_dependency 'rubocop'
36
37
  end
data/lib/hudu/api.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require "wrapi"
2
2
  require File.expand_path('authentication', __dir__)
3
3
  require File.expand_path('configuration', __dir__)
4
+ require File.expand_path('connection', __dir__)
4
5
 
5
6
  module Hudu
6
7
  # @private
@@ -27,6 +28,7 @@ module Hudu
27
28
 
28
29
  include Configuration
29
30
  include WrAPI::Connection
31
+ include Connection
30
32
  include WrAPI::Request
31
33
  include WrAPI::Authentication
32
34
  include Authentication
@@ -0,0 +1,32 @@
1
+ module Hudu
2
+
3
+ class AssetHelper
4
+ # Construct asset for updates, assuem it is an entity
5
+ def self.construct_asset asset
6
+ custom_asset = asset.attributes.slice( *%w(
7
+ id company_id asset_layout_id slug name
8
+ primary_serial primary_model primary_mail
9
+ primary_manufacturer )
10
+ )
11
+ custom_asset['custom_fields'] = self.custom_fields(asset.fields)
12
+ { asset: custom_asset }
13
+ end
14
+
15
+ # create new asset from layout
16
+ def self.create_asset name, asset_layout_id, fields
17
+ custom_asset = {
18
+ asset: {
19
+ name: name,
20
+ asset_layout_id: asset_layout_id,
21
+ custom_fields: self.custom_fields(fields)
22
+ }
23
+ }
24
+ end
25
+
26
+ private
27
+ def self.custom_fields(fields)
28
+ [fields.map{|field| [field.label.downcase.gsub(' ','_'),field.value]}.to_h]
29
+ end
30
+
31
+ end
32
+ end
data/lib/hudu/client.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require File.expand_path('api', __dir__)
2
+ require File.expand_path('asset_helper', __dir__)
2
3
 
3
4
  module Hudu
4
5
  # Wrapper for the Hudu REST API
@@ -17,7 +18,7 @@ module Hudu
17
18
  end
18
19
  # record by id
19
20
  self.send(:define_method, singular_method) do |id, params = {}|
20
- r = get(api_url("#{path}/" + id.to_s), params)
21
+ r = get(api_url("#{path}/#{id}"), params)
21
22
  r = hudu_data(r,singular_method)
22
23
  end
23
24
  else
@@ -26,6 +27,17 @@ module Hudu
26
27
  get(api_url(path), params)
27
28
  end
28
29
  end
30
+ # update
31
+ self.send(:define_method, "update_#{method}") do |id=nil,params = {}|
32
+ r = put(api_url("#{path}/#{id}"), params)
33
+ r = hudu_data(r,method)
34
+ end
35
+ # create
36
+ self.send(:define_method, "create_#{method}") do |id=nil,params = {}|
37
+ r = post(api_url("#{path}/#{id}"), params)
38
+ r = hudu_data(r,method)
39
+ end
40
+
29
41
  end
30
42
 
31
43
  public
@@ -47,16 +59,24 @@ module Hudu
47
59
  api_endpoint :expirations
48
60
  api_endpoint :websites, :website
49
61
  api_endpoint :relations
62
+ api_endpoint :magic_dashes, :magic_dash, 'magic_dash'
50
63
 
51
64
  def company_articles( company_id, params = {} )
52
- articles({company_id: company_id}.merge(parems))
65
+ articles({company_id: company_id}.merge(params))
53
66
  end
54
-
55
67
  def company_assets(id,params={})
56
68
  get_paged(api_url("companies/#{id}/assets"), params)
57
69
  end
58
- def company_asset(id,asset_id,params={})
59
- get(api_url("companies/#{id}/assets/#{asset_id}"), params)
70
+ def company_asset(company_id,asset_id,params={})
71
+ get(api_url("companies/#{company_id}/assets/#{asset_id}"), params)
72
+ end
73
+
74
+ def update_company_asset(asset)
75
+ hudu_data(put(api_url("companies/#{asset.company_id}/assets/#{asset.id}"), AssetHelper.construct_asset(asset),false),:asset)
76
+ end
77
+
78
+ def create_company_asset(company_id,asset_layout, fields)
79
+ hudu_data(post(api_url("companies/#{company_id}/assets"), AssetHelper.create_asset(asset_layout.name,asset_layout.id,fields),false),:asset)
60
80
  end
61
81
 
62
82
  # return api path
@@ -66,8 +86,8 @@ module Hudu
66
86
 
67
87
  # hudu returns data as {resource:{}} or {resource:[]}
68
88
  def hudu_data(result,resource)
69
- if result.is_a?(Hash) && result[resource.to_s]
70
- result[resource.to_s]
89
+ if result.is_a?(WrAPI::Request::Entity) && result.attributes[resource.to_s]
90
+ result.send resource.to_s
71
91
  else
72
92
  result
73
93
  end
@@ -0,0 +1,20 @@
1
+ require File.expand_path('rate_throttle_middleware', __dir__)
2
+
3
+ module Hudu
4
+ # Create connection including authorization parameters with default Accept format and User-Agent
5
+ # By default
6
+ # - Bearer authorization is access_token is not nil override with @setup_authorization
7
+ # - Headers setup for client-id and client-secret when client_id and client_secret are not nil @setup_headers
8
+ # @private
9
+ module Connection
10
+ private
11
+
12
+ def connection
13
+ unless @connection
14
+ @connection = super
15
+ @connection.use RateThrottleMiddleware, limit: 250, period: 60
16
+ end
17
+ @connection
18
+ end
19
+ end
20
+ end
@@ -15,7 +15,7 @@ module Hudu
15
15
  end
16
16
 
17
17
  def page_options
18
- { page: @page }
18
+ { page: @page, page_size: @page_size }
19
19
  end
20
20
 
21
21
  def next_page!(body)
@@ -31,12 +31,14 @@ module Hudu
31
31
 
32
32
  def self.data(body)
33
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
34
+ result = body
35
+ if result && result.respond_to?(:first)
36
+ k,v = body.first
37
+ if v.is_a?(Array) || v.is_a?(Hash)
38
+ result = v
39
+ end
39
40
  end
41
+ result
40
42
  end
41
43
  end
42
44
  end
@@ -0,0 +1,67 @@
1
+ require 'faraday'
2
+ require 'thread'
3
+
4
+ # A Faraday middleware for rate limiting requests.
5
+ #
6
+ # This middleware ensures that the number of requests made through a Faraday connection
7
+ # does not exceed a specified limit within a given time period.
8
+ #
9
+ # @example Add middleware to a Faraday connection
10
+ # connection = Faraday.new(url: 'https://api.example.com') do |faraday|
11
+ # faraday.use RateThrottleMiddleware, limit: 300, period: 60
12
+ # faraday.adapter Faraday.default_adapter
13
+ # end
14
+ #
15
+ # @see https://github.com/lostisland/faraday Faraday Documentation
16
+ #
17
+ class RateThrottleMiddleware < Faraday::Middleware
18
+ # Initializes the RateThrottleMiddleware.
19
+ #
20
+ # @param app [#call] The next middleware or the actual Faraday adapter.
21
+ # @param limit [Integer] The maximum number of requests allowed within the specified period. Default is 300.
22
+ # @param period [Integer] The time period in seconds over which the limit applies. Default is 60 seconds.
23
+ #
24
+ # @example
25
+ # middleware = RateThrottleMiddleware.new(app, limit: 300, period: 60)
26
+ #
27
+ def initialize(app, limit: 300, period: 60)
28
+ super(app)
29
+ @limit = limit
30
+ @period = period
31
+ @requests = []
32
+ @mutex = Mutex.new
33
+ @condition = ConditionVariable.new
34
+ end
35
+
36
+ def call(env)
37
+ throttle_request
38
+ @app.call(env)
39
+ end
40
+
41
+ private
42
+
43
+ def throttle_request
44
+ @mutex.synchronize do
45
+ now = Time.now.to_f
46
+ # Clear requests older than the rate limit period
47
+ while !@requests.empty? && @requests[0] < (now - @period)
48
+ @requests.pop
49
+ end
50
+
51
+ # Wait if the request limit is reached
52
+ while @requests.size >= @limit
53
+ sleep_time = @requests[0] + @period - now
54
+ puts "<#{sleep_time}>"
55
+ @condition.wait(@mutex, sleep_time) if sleep_time > 0
56
+ now = Time.now.to_f
57
+ while !@requests.empty? && @requests[0] < (now - @period)
58
+ @requests.pop
59
+ end
60
+ end
61
+
62
+ # Record the new request
63
+ @requests.push(Time.now.to_f)
64
+ @condition.broadcast
65
+ end
66
+ end
67
+ end
data/lib/hudu/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Hudu
4
- VERSION = '0.1.0'
4
+ VERSION = '0.3.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hudu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janco Tanis
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-02-20 00:00:00.000000000 Z
11
+ date: 2024-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 0.2.0
33
+ version: 0.3.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 0.2.0
40
+ version: 0.3.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: dotenv
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
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'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rubocop
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -92,14 +106,18 @@ files:
92
106
  - Gemfile
93
107
  - README.md
94
108
  - Rakefile
109
+ - bin/cc-test-reporter.exe
95
110
  - hudu.gemspec
96
111
  - lib/hudu.rb
97
112
  - lib/hudu/api.rb
113
+ - lib/hudu/asset_helper.rb
98
114
  - lib/hudu/authentication.rb
99
115
  - lib/hudu/client.rb
100
116
  - lib/hudu/configuration.rb
117
+ - lib/hudu/connection.rb
101
118
  - lib/hudu/error.rb
102
119
  - lib/hudu/pagination.rb
120
+ - lib/hudu/rate_throttle_middleware.rb
103
121
  - lib/hudu/version.rb
104
122
  homepage: https://rubygems.org/gems/hudu
105
123
  licenses:
@@ -122,7 +140,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
122
140
  - !ruby/object:Gem::Version
123
141
  version: '0'
124
142
  requirements: []
125
- rubygems_version: 3.2.12
143
+ rubygems_version: 3.1.6
126
144
  signing_key:
127
145
  specification_version: 4
128
146
  summary: A Ruby wrapper for the Hudu APIs (readonly)