Skip to content

Commit 82b770f

Browse files
author
Billy Reisinger
committed
Properly implement Resource#get. This closes Granicus#33
1 parent 3162d25 commit 82b770f

File tree

4 files changed

+54
-21
lines changed

4 files changed

+54
-21
lines changed

README.md

+21-7
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,37 @@ An instance of the `Infoblox::Connection` class is necessary:
2525
connection = Infoblox::Connection.new(username: '', password: '', host: '')
2626

2727
## Reading
28-
Each resource class implements `all` and `find`. You can use the `_max_results` and `_return_fields` parameters for both of these methods. See the Infoblox WAPI documentation on how to use these parameters.
28+
Each resource class implements `.all`, `.find`, and `#get`.
2929

30+
### `.find`
31+
Use find when you don't know what you are looking for or you don't have a `_ref` saved (see `get` below). Use `_max_results` to limit / expand the number of returned results.
32+
33+
# You can find hosts that match a regular expression:
34+
Infoblox::Host.find(connection, {"name~" => "demo[0-9]{1,}-web.domain"})
35+
# => [...]
36+
37+
### `.all`
38+
Show all results (note that this is limited by `_max_results`). Use this cautiously when you have a large potential set of results.
39+
3040
# Find all networks. Note that this is limited to 1000 objects, as per the
3141
# Infoblox WAPI documentation.
3242
Infoblox::Network.all(connection)
3343
# => [...]
34-
44+
3545
# If you want more than 1,000 records, use `_max_results`:
3646
Infoblox::Network.all(connection, _max_results: 7890)
3747

38-
# You can find hosts that match a regular expression:
39-
Infoblox::Host.find(connection, {"name~" => "demo[0-9]{1,}-web.domain"})
40-
# => [...]
41-
4248
The usage of search parameters is well-documented in the Infoblox WAPI documentation, and this client supports them fully.
4349

50+
### `#get`
51+
Use this when you have saved a reference (`_ref`) and want to load it later.
52+
53+
host = Infoblox::Host.new(:connection => c, :_ref => ref).get
54+
puts host.name
55+
# => foo.bar
56+
4457
## Searching
45-
You can also search across the Infoblox cluster using the `Infoblox::Search` resource. The response will contain any number of `Infoblox::Resource` subclass instances.
58+
You can also search across all Infoblox resource types using the `Infoblox::Search` resource. The response will contain any number of `Infoblox::Resource` subclass instances.
4659

4760
result = Infoblox::Search.find(connection, "search_string~" => "webserver-")
4861
# => [#<Infoblox::Host>, #<Infoblox::Ptr>, ...]
@@ -183,6 +196,7 @@ To run the tests:
183196

184197
To run the integration tests (you will be prompted for your Infoblox credentials):
185198

199+
INTEGRATION=true bundle
186200
INTEGRATION=true rspec
187201

188202
Please note that the integration tests do not work in Ruby 1.8.7, but the unit tests function normally.

lib/infoblox/resource.rb

+19-13
Original file line numberDiff line numberDiff line change
@@ -125,17 +125,7 @@ def self.resource_map
125125
end
126126

127127
def initialize(attrs={})
128-
attrs.each do |k,v|
129-
# Some things have specialized writers,
130-
# like Host
131-
if respond_to?("#{k}=")
132-
send("#{k}=", v)
133-
134-
# Some things don't have writers (i.e. remote_attr_reader fields)
135-
else
136-
instance_variable_set("@#{k}", v)
137-
end
138-
end
128+
load_attributes(attrs)
139129
end
140130

141131
def post
@@ -148,8 +138,10 @@ def delete
148138
connection.delete(resource_uri).status == 200
149139
end
150140

151-
def get(params={})
152-
connection.get(resource_uri, params)
141+
def get(params=self.class.default_params)
142+
response = connection.get(resource_uri, params).body
143+
load_attributes(JSON.parse(response))
144+
self
153145
end
154146

155147
def put
@@ -179,6 +171,20 @@ def remote_attribute_hash(write=false, post=false)
179171
def unquote(str)
180172
str.gsub(/\A['"]+|['"]+\Z/, "")
181173
end
174+
175+
def load_attributes(attrs)
176+
attrs.each do |k,v|
177+
# Some things have specialized writers,
178+
# like Host
179+
if respond_to?("#{k}=")
180+
send("#{k}=", v)
181+
182+
# Some things don't have writers (i.e. remote_attr_reader fields)
183+
else
184+
instance_variable_set("@#{k}", v)
185+
end
186+
end
187+
end
182188
end
183189

184190
end

lib/infoblox/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module Infoblox
2-
VERSION = "0.4.1"
2+
VERSION = "0.5.0"
33
end

spec/resource_spec.rb

+13
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,19 @@ class FooResource < Infoblox::Resource
9090
expect(f.sect).to eq(:larry)
9191
end
9292

93+
it 'should load attributes on get' do
94+
conn = double
95+
uri = Infoblox.base_path + "a:ref:that:is:fake"
96+
json = {:name => "john", :junction => "hi", :extattrs => {"foo" => 3}}.to_json
97+
response = FooResponse.new(json)
98+
expect(conn).to receive(:get).with(uri, FooResource.default_params).and_return(response)
99+
f = FooResource.new(:connection => conn, :_ref => "a:ref:that:is:fake")
100+
f.get
101+
expect(f.name).to eq("john")
102+
expect(f.junction).to eq("hi")
103+
expect(f.extattrs).to eq({"foo" => 3})
104+
end
105+
93106
it 'should map wapi objects to classes' do
94107
@expected = {}
95108
ObjectSpace.each_object(Class) do |p|

0 commit comments

Comments
 (0)