Skip to content

Commit

Permalink
allow for client methods to return multiple values, when multiple var…
Browse files Browse the repository at this point in the history
…binds are returned (happens when one does a GET request with multiple OIDs, for example)

The API is still not the best (one has to pass multiple hash arguments),
but it's getting there.
  • Loading branch information
HoneyryderChuck committed Feb 10, 2018
1 parent 41638aa commit 9032f8b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lib/netsnmp/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,24 @@ def close
#
# @see {NETSNMP::Varbind#new}
#
def get(oid_opts)
request = @session.build_pdu(:get, oid_opts)
def get(*oid_opts)
request = @session.build_pdu(:get, *oid_opts)
response = handle_retries { @session.send(request) }
yield response if block_given?
response.varbinds.first.value
values = response.varbinds.map(&:value)
values.size > 1 ? values : values.first
end

# Performs an SNMP GETNEXT Request
#
# @see {NETSNMP::Varbind#new}
#
def get_next(oid_opts)
request = @session.build_pdu(:getnext, oid_opts)
def get_next(*oid_opts)
request = @session.build_pdu(:getnext, *oid_opts)
response = handle_retries { @session.send(request) }
yield response if block_given?
varbind = response.varbinds.first
[varbind.oid, varbind.value]
values = response.varbinds.map { |v| [v.oid, v.value] }
values.size > 1 ? values : values.first
end

# Perform a SNMP Walk (issues multiple subsequent GENEXT requests within the subtree rooted on an OID)
Expand Down Expand Up @@ -124,11 +125,12 @@ def walk(oid:)
#
# @see {NETSNMP::Varbind#new}
#
def set(oid_opts)
request = @session.build_pdu(:set, oid_opts)
def set(*oid_opts)
request = @session.build_pdu(:set, *oid_opts)
response = handle_retries { @session.send(request) }
yield response if block_given?
response.varbinds.map(&:value)
values = response.varbinds.map(&:value)
values.size > 1 ? values : values.first
end

private
Expand Down
8 changes: 8 additions & 0 deletions spec/support/request_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
it "fetches the varbinds for a given oid" do
expect(value).to eq(get_result)
end
context "with multiple oids" do
let(:value) { subject.get({ oid: get_oid }, oid: next_oid) }
it "returns the values for both" do
expect(value).to be_a(Array)
expect(value).to include(/#{get_result}/)
expect(value).to include(/#{next_result}/)
end
end
end

describe "#get_next" do
Expand Down

0 comments on commit 9032f8b

Please sign in to comment.