infoblox 0.0.11 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -24,30 +24,31 @@ querying, adding, and removing host names from dns records:
24
24
 
25
25
 
26
26
  ## Ruby client usage
27
- Before you do anything, create a new client. This adds a connection to the
28
- base Resource class.
27
+ The Client class has a few helper methods to make certain operations easier. Creating a host record:
29
28
 
30
29
  client = Infoblox::Client.new(:username => "foo",
31
30
  :password => "bar",
32
31
  :host => "https://foo-bar-dns.example.com")
33
32
 
34
- The client class has a few helper methods to make certain operations easier. Creating a host record:
35
33
 
36
34
  client.create_host("build-machine.internal", "10.10.3.3")
37
35
  client.delete_host("build-machine.internal")
38
36
  client.find_host_by_name("build-")
39
37
 
40
- One can also use the resource classes directly to run arbitrary query logic:
38
+ One can also use the resource classes directly to run arbitrary query logic. An instance
39
+ of the Infoblox::Connection class is necessary:
41
40
 
42
- Infoblox::Network.find({"_return_fields" => "extensible_attributes"})
41
+ connection = Infoblox::Connection.new(:username => '', :password => '', :host => '')
42
+
43
+ Infoblox::Network.find(connection, {"_return_fields" => "extensible_attributes"})
43
44
  # => [...]
44
45
 
45
- Infoblox::Host.find({"name~" => "demo[0-9]{1,}-web.domain"})
46
+ Infoblox::Host.find(connection, {"name~" => "demo[0-9]{1,}-web.domain"})
46
47
  # => [...]
47
48
 
48
49
  The Resource sub-classes also support `get`, `post`, `put`, and `delete`. For example, creating a network is pretty straightforward:
49
50
 
50
- network = Infoblox::Network.new
51
+ network = Infoblox::Network.new(:connection => connection)
51
52
  network.network = "10.20.30.0/24"
52
53
  network.extensible_attributes = {"VLAN" => "my_vlan"}
53
54
  network.auto_create_reversezone = true
@@ -5,18 +5,18 @@ module Infoblox
5
5
  # :password
6
6
  # :host (host if infoblox appliance, including protocol)
7
7
  def initialize(options={})
8
- Resource.connection = Connection.new(options)
8
+ self.connection = Connection.new(options)
9
9
  end
10
10
 
11
11
  def all_hosts
12
- Host.all
12
+ Host.all(connection)
13
13
  end
14
14
 
15
15
  ##
16
16
  # Find all host records by wildcard name.
17
17
  #
18
18
  def find_host_by_name(name)
19
- Host.find(:"name~" => name)
19
+ Host.find(connection, :"name~" => name)
20
20
  end
21
21
 
22
22
  ##
@@ -24,20 +24,24 @@ module Infoblox
24
24
  # ex: find_ips('10.10.2') => [#<Infoblox::Ipv4addr>...]
25
25
  #
26
26
  def find_ips(ip_fragment)
27
- Ipv4addr.find(:"ipv4addr~" => ip_fragment)
27
+ Ipv4addr.find(connection, :"ipv4addr~" => ip_fragment)
28
28
  end
29
29
 
30
30
  ##
31
+ # Create a host record for the given IP address
31
32
  # hostname : string
32
33
  # ip : ipv4 string, i.e. "10.40.20.3"
33
34
  def create_host(hostname, ip, dns=true)
34
- host = Host.new(:name => hostname, :configure_for_dns => true)
35
+ host = Host.new(:name => hostname, :configure_for_dns => true, :connection => connection)
35
36
  host.add_ipv4addr(ip)
36
37
  host.create
37
38
  end
38
39
 
40
+ ##
41
+ # Remove all host entries for a given host
42
+ #
39
43
  def delete_host(hostname)
40
- if !(hosts = Host.find(:name => hostname)).empty?
44
+ if !(hosts = Host.find(connection, :name => hostname)).empty?
41
45
  hosts.map(&:delete)
42
46
  hosts
43
47
  else
@@ -1,6 +1,6 @@
1
1
  module Infoblox
2
2
  class Resource
3
- attr_accessor :_ref
3
+ attr_accessor :_ref, :connection
4
4
 
5
5
  def self.wapi_object(obj=nil)
6
6
  obj.nil? ? @wapi_object : @wapi_object = obj
@@ -61,9 +61,9 @@ module Infoblox
61
61
  ##
62
62
  # Return an array of all records for this resource.
63
63
  #
64
- def self.all
64
+ def self.all(connection)
65
65
  JSON.parse(connection.get(resource_uri, default_params).body).map do |item|
66
- new(item)
66
+ new(item.merge({:connection => connection}))
67
67
  end
68
68
  end
69
69
 
@@ -76,21 +76,13 @@ module Infoblox
76
76
  # Example: filter resources by name.
77
77
  # {"name~" => "foo.*bar"}
78
78
  #
79
- def self.find(params)
79
+ def self.find(connection, params)
80
80
  params = default_params.merge(params)
81
81
  JSON.parse(connection.get(resource_uri, params).body).map do |item|
82
- new(item)
82
+ new(item.merge({:connection => connection}))
83
83
  end
84
84
  end
85
85
 
86
- def self.connection
87
- @@connection
88
- end
89
-
90
- def self.connection=(con)
91
- @@connection = con
92
- end
93
-
94
86
  def self.resource_uri
95
87
  BASE_PATH + self.wapi_object
96
88
  end
@@ -102,7 +94,7 @@ module Infoblox
102
94
  end
103
95
 
104
96
  def post
105
- self._ref = connection.post(resource_uri, remote_attribute_hash(write = true, post = true)).body
97
+ self._ref = unquote(connection.post(resource_uri, remote_attribute_hash(write = true, post = true)).body)
106
98
  true
107
99
  end
108
100
  alias_method :create, :post
@@ -116,7 +108,8 @@ module Infoblox
116
108
  end
117
109
 
118
110
  def put
119
- connection.put(resource_uri, remote_attribute_hash(write = true))
111
+ self._ref = unquote(connection.put(resource_uri, remote_attribute_hash(write = true)).body)
112
+ true
120
113
  end
121
114
 
122
115
  def resource_uri
@@ -138,10 +131,9 @@ module Infoblox
138
131
  end
139
132
 
140
133
  private
141
-
142
- def connection
143
- self.class.connection
134
+ def unquote(str)
135
+ str.gsub(/\A['"]+|['"]+\Z/, "")
144
136
  end
145
-
146
137
  end
138
+
147
139
  end
@@ -1,3 +1,3 @@
1
1
  module Infoblox
2
- VERSION = "0.0.11"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -17,13 +17,13 @@ describe Infoblox::Host, "#add_ipv4addr" do
17
17
  :ipv4addrs => [{:ipv4addr => "10.10.10.10"}],
18
18
  :name => "test-server.test.ing",
19
19
  :configure_for_dns => nil,
20
- :extensible_attributes => nil}).and_return(HostResponse.new("hey"))
20
+ :extensible_attributes => nil}).and_return(HostResponse.new("\"hey\""))
21
21
 
22
- Infoblox::Host.connection = conn
23
- h = Infoblox::Host.new
22
+ h = Infoblox::Host.new(:connection => conn)
24
23
  h.add_ipv4addr("10.10.10.10")
25
24
  h.name = "test-server.test.ing"
26
25
  h.post
26
+ h._ref.should eq("hey")
27
27
  end
28
28
  end
29
29
 
@@ -32,30 +32,28 @@ describe Infoblox::Resource, "#add_ipv4addr" do
32
32
  conn = double
33
33
  uri = Infoblox::BASE_PATH + "foo:animal"
34
34
  allow(conn).to receive(:get).with(uri, {:_return_fields => "name,junction"}).and_return(FooResponse.new("[]"))
35
- FooResource.connection = conn
36
- FooResource.all.should eq([])
35
+ FooResource.all(conn).should eq([])
37
36
  end
38
37
 
39
38
  it "should put with the right attributes" do
40
39
  conn = double
41
40
  uri = Infoblox::BASE_PATH + "abcd"
42
- allow(conn).to receive(:put).with(uri, {:name => "jerry", :junction => "32", :do_it => false}).and_return(FooResponse.new("[]"))
43
- FooResource.connection = conn
44
- f = FooResource.new
41
+ allow(conn).to receive(:put).with(uri, {:name => "jerry", :junction => "32", :do_it => false}).and_return(FooResponse.new("\"foobar\""))
42
+ f = FooResource.new(:connection => conn)
45
43
  f._ref = "abcd"
46
44
  f.do_it = false
47
45
  f.junction = "32"
48
46
  f.name = "jerry"
49
47
  f.sect = :fulburns
50
48
  f.put
49
+ f._ref.should eq("foobar")
51
50
  end
52
51
 
53
52
  it "should post with the right attributes" do
54
53
  conn = double
55
54
  uri = Infoblox::BASE_PATH + "foo:animal"
56
- allow(conn).to receive(:post).with(uri, {:name => "jerry", :junction => "32", :do_it => false, :sect => :fulburns}).and_return(FooResponse.new("abcdefg"))
57
- FooResource.connection = conn
58
- f = FooResource.new
55
+ allow(conn).to receive(:post).with(uri, {:name => "jerry", :junction => "32", :do_it => false, :sect => :fulburns}).and_return(FooResponse.new("\"abcdefg\""))
56
+ f = FooResource.new(:connection => conn)
59
57
  f.do_it = false
60
58
  f.junction = "32"
61
59
  f.name = "jerry"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: infoblox
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-29 00:00:00.000000000 Z
12
+ date: 2013-08-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: faraday