Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature]: pagination for source http #2847

Merged
merged 22 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
## Added
- model for D-Link cisco like CLI (@mirackle-spb)
- model for Ruijie Networks RGOS devices (@spike77453)
- pagination for http source (@davama)

+ Added ability to send mail with the Docker container
+ Documentation to send mail with hooks
Expand Down
13 changes: 13 additions & 0 deletions docs/Sources.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,16 @@ source:
scheme: https
secure: false
```

HTTP source also supports pagination. Two settings must be enabled. (`pagination` as a bool and `pagination_key_name` as a string)
The `pagination_key_name` setting is the key name that an api returns to find the url of the next page.

**Disclaimer**: currently only tested with netbox as the source

```yaml
source:
default: http
http:
pagination: true
pagination_key_name: 'next'
```
43 changes: 36 additions & 7 deletions lib/oxidized/source/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ def setup

def load(node_want = nil)
nodes = []
data = JSON.parse(read_http(node_want))
data = string_navigate(data, @cfg.hosts_location) if @cfg.hosts_location?
data = read_http(node_want)
data.each do |node|
next if node.empty?

Expand All @@ -45,6 +44,14 @@ def load(node_want = nil)

private

def set_request(l_uri, l_headers, l_node_want)
req_uri = l_uri.request_uri
req_uri = "#{req_uri}/#{l_node_want}" if l_node_want
request = Net::HTTP::Get.new(req_uri, l_headers)
request.basic_auth(@cfg.user, @cfg.pass) if @cfg.user? && @cfg.pass?
request
end

def string_navigate(object, wants)
wants = wants.split(".").map do |want|
head, match, _tail = want.partition(/\[\d+\]/)
Expand All @@ -56,6 +63,29 @@ def string_navigate(object, wants)
object
end

def check_pagination(response, http, headers, node_want)
node_data = []
if @cfg.pagination?
raise Oxidized::OxidizedError, "if using pagination, 'pagination_key_name' setting must be set" unless @cfg.pagination_key_name?

next_key = @cfg.pagination_key_name
loop do
data = JSON.parse(response.body)
node_data += string_navigate(data, @cfg.hosts_location) if @cfg.hosts_location?
break if data[next_key].nil?

new_uri = URI.parse(data[next_key]) if data.has_key?(next_key)
request = set_request(new_uri, headers, node_want)
response = http.request(request)
end
# since new feature; dont break curent configs
else
data = JSON.parse(response.body)
node_data += string_navigate(data, @cfg.hosts_location) if @cfg.hosts_location?
end
node_data
end

def read_http(node_want)
uri = URI.parse(@cfg.url)
http = Net::HTTP.new(uri.host, uri.port)
Expand All @@ -71,11 +101,10 @@ def read_http(node_want)
headers[header] = value
end

req_uri = uri.request_uri
req_uri = "#{req_uri}/#{node_want}" if node_want
request = Net::HTTP::Get.new(req_uri, headers)
request.basic_auth(@cfg.user, @cfg.pass) if @cfg.user? && @cfg.pass?
http.request(request).body
request = set_request(uri, headers, node_want)
response = http.request(request)

check_pagination(response, http, headers, node_want)
end
end
end