netdot-restclient 1.4 → 2.0.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.
@@ -1,10 +1,11 @@
1
+ # Netdot
1
2
  module Netdot
3
+ # RestClient
2
4
  class RestClient
3
- VERSION = "1.4"
5
+ VERSION = '2.0.0'
4
6
 
5
7
  def version
6
8
  VERSION
7
9
  end
8
-
9
10
  end
10
11
  end
@@ -4,25 +4,24 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'netdot/restclient/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "netdot-restclient"
7
+ spec.name = 'netdot-restclient'
8
8
  spec.version = Netdot::RestClient::VERSION
9
- spec.authors = ["Carlos Vicente"]
10
- spec.email = ["cvicente@gmail.com"]
11
- spec.summary = %q{RESTful API client for Netdot}
12
- spec.description = %q{Talk to Netdot via REST with Ruby}
13
- spec.homepage = ""
14
- spec.license = "Apache-2.0"
9
+ spec.authors = ['Carlos Vicente']
10
+ spec.email = ['cvicente@gmail.com']
11
+ spec.summary = 'RESTful API client for Netdot'
12
+ spec.description = 'Talk to Netdot via REST with Ruby'
13
+ spec.homepage = ''
14
+ spec.license = 'Apache-2.0'
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0")
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
19
+ spec.require_paths = ['lib']
20
20
 
21
- spec.add_development_dependency "bundler", "~> 1.6"
22
- spec.add_development_dependency "rake"
23
- spec.add_development_dependency "rspec", "~> 2.6"
24
-
25
- spec.add_dependency "httpclient"
26
- spec.add_dependency "xml-simple"
21
+ spec.add_development_dependency 'bundler', '~> 1.6'
22
+ spec.add_development_dependency 'rake'
23
+ spec.add_development_dependency 'rspec', '~> 2.6'
27
24
 
25
+ spec.add_dependency 'httpclient'
26
+ spec.add_dependency 'xml-simple'
28
27
  end
data/sample/sample.rb ADDED
@@ -0,0 +1,43 @@
1
+ #
2
+ # Example: Create, find, and delete Host and Ipblock resources.
3
+ #
4
+ require 'netdot'
5
+ require 'netaddr'
6
+
7
+ # Connect
8
+ netdot_restclient = Netdot::RestClient.new(server: 'http://localhost/netdot',
9
+ username: 'admin',
10
+ password: 'admin')
11
+
12
+ # Create a Netdot::Host object
13
+ netdot_host = Netdot::Host.new(connection: netdot_restclient)
14
+
15
+ # Create a Netdot::Ipblock object
16
+ netdot_ipblock = Netdot::Ipblock.new(connection: netdot_restclient)
17
+
18
+ # Ask Netdot to allocate a new CIDR block
19
+ cidr = netdot_ipblock.allocate('10.0.0.0/24', 24, 'example-net')
20
+
21
+ # Use NetAddr gem to allocate an address in the CIDR block
22
+ netaddr_cidr = NetAddr::CIDR.create(cidr)
23
+
24
+ # Use the first address
25
+ address = netaddr_cidr.nth(1)
26
+
27
+ # Create a new host (DNS A record)
28
+ netdot_host.create('example-host', address)
29
+
30
+ # Find the new host
31
+ _found = netdot_host.find_by_name('example-host')
32
+
33
+ # Delete the new Host
34
+ netdot_host.delete('example-host')
35
+
36
+ # Delete the new Ipblock, and all its children
37
+ ipblocks = netdot_ipblock.find_by_descr('example-net')
38
+ ipblocks.each do |ipblock|
39
+ address = ipblock[1]['address']
40
+ netdot_ipblock.delete(address, true)
41
+ end unless ipblocks.nil?
42
+
43
+ 0
data/spec/host_spec.rb CHANGED
@@ -1,46 +1,45 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Netdot::Host do
4
-
5
4
  before :all do
6
5
  @netdot = connect
7
- @host = Netdot::Host.new(:connection => @netdot) if @host.nil?
8
- subnet = Netdot::Subnet.new(:connection => @netdot)
9
- subnet_id = subnet.allocate('10.0.0.0/8', 24, 'test')
10
- @cidr = NetAddr::CIDR.create(subnet_id)
6
+ @host = Netdot::Host.new(connection: @netdot) if @host.nil?
7
+ pblock = Netdot::Ipblock.new(connection: @netdot)
8
+ cidr = pblock.allocate('10.0.0.0/8', 24, 'rspec-Ipblock-host')
9
+ @cidr = NetAddr::CIDR.create(cidr)
11
10
  end
12
11
 
13
12
  context 'when creating a new Host instance' do
14
13
  it 'creates a new instance' do
15
- expect {
16
- host = Netdot::Host.new(:connection => @netdot)
17
- }.not_to raise_error
14
+ expect do
15
+ Netdot::Host.new(connection: @netdot)
16
+ end.not_to raise_error
18
17
  end
19
18
 
20
19
  it 'raises an exception for invalid arguments' do
21
- expect {
22
- host = Netdot::Host.new
23
- }.to raise_error(ArgumentError)
20
+ expect do
21
+ Netdot::Host.new
22
+ end.to raise_error(ArgumentError)
24
23
  end
25
24
  end
26
25
 
27
- context 'creating a new host' do
26
+ context 'when creating a new host' do
28
27
  it 'creates a new host allocation' do
29
- h = @host.create('test0-01-yyy', @cidr.nth(1))
28
+ h = @host.create('rspec-test0-01-yyy', @cidr.nth(1))
30
29
  expect(h.key? 'name').to be_truthy
31
- expect(h['name']).to match('test0-01-yyy')
30
+ expect(h['name']).to match('rspec-test0-01-yyy')
32
31
  end
33
32
 
34
33
  it 'updates an existing allocation' do
35
- h = @host.update('test0-01-zzz', @cidr.nth(1))
34
+ h = @host.update('rspec-test0-01-zzz', @cidr.nth(1))
36
35
  expect(h.key? 'name').to be_truthy
37
- expect(h['name']).to match('test0-01-zzz')
36
+ expect(h['name']).to match('rspec-test0-01-zzz')
38
37
  end
39
38
  end
40
39
 
41
- context 'finding existing hosts' do
40
+ context 'when finding existing hosts' do
42
41
  it 'finds a host by name' do
43
- h = @host.find_by_name('test0-01-zzz')
42
+ h = @host.find_by_name('rspec-test0-01-zzz')
44
43
  expect(h).to be_instance_of(Hash)
45
44
  end
46
45
 
@@ -50,11 +49,11 @@ describe Netdot::Host do
50
49
  end
51
50
  end
52
51
 
53
- context 'deleting a host' do
52
+ context 'when deleting a host' do
54
53
  it 'deletes a host' do
55
- expect {
56
- @host.delete('test-01-zzz')
57
- }.not_to raise_error
54
+ expect do
55
+ @host.delete('rspec-test-01-zzz')
56
+ end.not_to raise_error
58
57
  end
59
58
  end
60
59
  end
@@ -0,0 +1,108 @@
1
+ require 'spec_helper'
2
+
3
+ describe Netdot::Ipblock do
4
+ before :all do
5
+ @netdot = connect
6
+ @ipblock = Netdot::Ipblock.new(connection: @netdot) if @ipblock.nil?
7
+ @host = Netdot::Host.new(connection: @netdot) if @host.nil?
8
+ end
9
+
10
+ context 'when creating a new Ipblock instance' do
11
+ it 'creates a new instance' do
12
+ expect do
13
+ Ipblock = Netdot::Ipblock.new(connection: @netdot)
14
+ end.not_to raise_error
15
+ end
16
+
17
+ it 'raises an exception for invalid arguments' do
18
+ expect do
19
+ Ipblock = Netdot::Ipblock.new
20
+ end.to raise_error(ArgumentError)
21
+ end
22
+ end
23
+
24
+ context 'when creating a new Ipblock' do
25
+ it 'allocates a new Ipblock' do
26
+ cidr = @ipblock.allocate('10.0.0.0/8', 24, 'rspec-Ipblock-one')
27
+ expect(cidr).to match('10.0.*.0/24')
28
+ end
29
+
30
+ it 'raises an exception if the prefix is not /24' do
31
+ expect do
32
+ @ipblock.allocate('10.0.0.0/8', 26)
33
+ end.to raise_error(ArgumentError)
34
+ end
35
+ end
36
+
37
+ context 'when finding an Ipblock by description' do
38
+ it 'finds an Ipblock' do
39
+ expect(@ipblock.find_by_descr('rspec-Ipblock-one')).not_to be_nil
40
+ end
41
+
42
+ it 'raises an exception for an improperly formatted name' do
43
+ expect do
44
+ @ipblock.find_by_descr('rspec-Ipblock-one&Foo=bar')
45
+ end.to raise_error
46
+ end
47
+
48
+ it 'return nil for an empty string' do
49
+ expect(@ipblock.find_by_descr('')).to be_nil
50
+ end
51
+
52
+ it 'return nil if not found' do
53
+ expect(@ipblock.find_by_descr('rspec-Ipblock-bogus')).to be_nil
54
+ end
55
+ end
56
+
57
+ context 'when finding an Ipblock by CIDR' do
58
+ it 'finds an Ipblock' do
59
+ cidr = @ipblock.allocate('10.0.0.0/8', 24, 'rspec-Ipblock-two')
60
+ ipblock = @ipblock.find_by_addr(cidr)
61
+ expect(ipblock).not_to be_nil
62
+ end
63
+
64
+ it 'return nil if not found' do
65
+ expect(@ipblock.find_by_addr('1.2.3.4/32')).to be_nil
66
+ end
67
+ end
68
+
69
+ context 'when finding an Ipblock by address' do
70
+ it 'finds an Ipblock' do
71
+ cidr = @ipblock.allocate('10.0.0.0/8', 24, 'rspec-Ipblock-three')
72
+ (address, _prefix) = cidr.split('/')
73
+ ipblock = @ipblock.find_by_addr(address)
74
+ expect(ipblock).not_to be_nil
75
+ end
76
+
77
+ it 'return nil if not found' do
78
+ expect(@ipblock.find_by_addr('1.2.3.4')).to be_nil
79
+ end
80
+ end
81
+
82
+ context 'when deleting an Ipblock' do
83
+ it 'deletes an Ipblock' do
84
+ cidr = @ipblock.allocate('10.0.0.0/8', 24, 'rspec-Ipblock-four')
85
+ expect(@ipblock.delete(cidr)).to be_truthy
86
+ end
87
+
88
+ it 'deletes an Ipblock, and its children' do
89
+ cidr = @ipblock.allocate('10.0.0.0/8', 24, 'rspec-Ipblock-five')
90
+ netaddr_cidr = NetAddr::CIDR.create(cidr)
91
+ @host.create('rspec-test0-01-yyy', netaddr_cidr.nth(1))
92
+ expect(@ipblock.delete(cidr, true)).to be_truthy
93
+ end
94
+ end
95
+
96
+ context 'when cleaning up' do
97
+ %w( host one two three four five ).each do |s|
98
+ description = format('rspec-Ipblock-%s', s)
99
+ it "finds and deletes Ipblock '#{description}'" do
100
+ ipblocks = @ipblock.find_by_descr(description)
101
+ ipblocks.each do |ipblock|
102
+ address = ipblock[1]['address']
103
+ @ipblock.delete(address, true)
104
+ end unless ipblocks.nil?
105
+ end
106
+ end
107
+ end
108
+ end
@@ -1,32 +1,30 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe Netdot::RestClient do
4
-
5
4
  before :all do
6
5
  @netdot = connect
7
6
  end
8
7
 
9
8
  context 'when connecting' do
10
-
11
9
  it 'raises an exception for incomplete arguments' do
12
10
  args = {
13
11
  username: 'admin',
14
12
  server: 'http://localhost/netdot'
15
13
  }
16
- expect {
14
+ expect do
17
15
  Netdot::RestClient.new(args)
18
- }.to raise_error(ArgumentError)
16
+ end.to raise_error(ArgumentError)
19
17
  end
20
18
 
21
19
  it 'raises an exception for an invalid password' do
22
- args = {
20
+ args = {
23
21
  server: ENV['SERVER'] || 'http://localhost/netdot',
24
22
  username: ENV['USERNAME'] || 'admin',
25
23
  password: 'this-is-not-the-password'
26
24
  }
27
- expect {
25
+ expect do
28
26
  Netdot::RestClient.new(args)
29
- }.to raise_error
27
+ end.to raise_error
30
28
  end
31
29
 
32
30
  # The following two assume that local server has SSL
@@ -35,18 +33,18 @@ describe Netdot::RestClient do
35
33
  args = {
36
34
  server: ENV['SERVER'] || 'https://localhost/netdot',
37
35
  username: ENV['USERNAME'] || 'admin',
38
- password: ENV['PASSWORD'] || 'admin'
36
+ password: ENV['PASSWORD'] || 'admin'
39
37
  }
40
- expect {
38
+ expect do
41
39
  Netdot::RestClient.new(args)
42
- }.to raise_error
40
+ end.to raise_error
43
41
  end
44
42
 
45
43
  it 'does not raise an exception if SSL verification is disabled' do
46
44
  args = {
47
45
  server: ENV['SERVER'] || 'https://localhost/netdot',
48
46
  username: ENV['USERNAME'] || 'admin',
49
- password: ENV['PASSWORD'] || 'admin',
47
+ password: ENV['PASSWORD'] || 'admin',
50
48
  ssl_verify: false
51
49
  }
52
50
 
@@ -55,25 +53,23 @@ describe Netdot::RestClient do
55
53
  end
56
54
 
57
55
  it 'connects to the API' do
58
- n = connect
56
+ n = connect
59
57
  expect(n).to be_an_instance_of(Netdot::RestClient)
60
58
  end
61
-
62
59
  end
63
60
 
64
61
  context 'when getting' do
65
-
66
62
  it 'invalid resource raises exception' do
67
- expect {
68
- resp = @netdot.get('foobar')
69
- }.to raise_error
63
+ expect do
64
+ @netdot.get('foobar')
65
+ end.to raise_error
70
66
  end
71
67
 
72
68
  it 'valid resource as hash' do
73
69
  resp = @netdot.get('Entity/1')
74
70
  expect(resp).to be_an_instance_of(Hash)
75
71
  end
76
-
72
+
77
73
  it 'record by id' do
78
74
  resp = @netdot.get('Entity/1')
79
75
  expect(resp['name']).to eq('Unknown')
@@ -83,46 +79,41 @@ describe Netdot::RestClient do
83
79
  resp = @netdot.get('Entity?name=Unknown')
84
80
  expect(resp['Entity']['1']['name']).to eq('Unknown')
85
81
  end
86
-
87
82
  end
88
83
 
89
84
  context 'when posting' do
90
-
91
85
  it 'fails to update invalid record' do
92
- expect {
93
- resp = @netdot.post('Foobar/1', {'key' => 'value'} )
94
- }.to raise_error
86
+ expect do
87
+ @netdot.post('Foobar/1', 'key' => 'value')
88
+ end.to raise_error
95
89
  end
96
90
 
97
91
  it 'creates new record' do
98
- resp = @netdot.post('Person',
99
- { 'firstname' => 'Joe',
100
- 'lastname' => 'Plumber',
101
- 'username' => 'joetubes'
102
- })
103
- expect(resp['firstname']).to eq ('Joe')
104
- expect(resp['lastname']).to eq ('Plumber')
105
-
92
+ resp = @netdot.post('Person',
93
+ 'firstname' => 'Joe',
94
+ 'lastname' => 'Plumber',
95
+ 'username' => 'joetubes'
96
+ )
97
+ expect(resp['firstname']).to eq('Joe')
98
+ expect(resp['lastname']).to eq('Plumber')
106
99
  end
107
100
 
108
101
  it 'fails to create duplicate record' do
109
- expect {
110
- resp = @netdot.post('Person',
111
- { 'firstname' => 'Joe',
112
- 'lastname' => 'Plumber',
113
- 'username' => 'joetubes'
114
- })
115
- }.to raise_error
102
+ expect do
103
+ @netdot.post('Person',
104
+ 'firstname' => 'Joe',
105
+ 'lastname' => 'Plumber',
106
+ 'username' => 'joetubes'
107
+ )
108
+ end.to raise_error
116
109
  end
117
-
118
110
  end
119
111
 
120
112
  context 'when deleting' do
121
-
122
113
  it 'fails to delete invalid record' do
123
- expect {
124
- resp = @netdot.delete('FooBar/1234')
125
- }.to raise_error
114
+ expect do
115
+ @netdot.delete('FooBar/1234')
116
+ end.to raise_error
126
117
  end
127
118
 
128
119
  it 'deletes exiting record' do
@@ -131,8 +122,5 @@ describe Netdot::RestClient do
131
122
  resp = @netdot.delete("Person/#{person_id}")
132
123
  expect(resp).to be_truthy
133
124
  end
134
-
135
125
  end
136
-
137
126
  end
138
-