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.
- data/.rubocop-bbatsov-default.yml +720 -0
- data/.rubocop-bbatsov-disabled.yml +50 -0
- data/.rubocop-bbatsov-enabled.yml +1030 -0
- data/.rubocop.yml +720 -0
- data/README.md +2 -0
- data/Rakefile +7 -1
- data/lib/netdot.rb +5 -4
- data/lib/netdot/host.rb +32 -26
- data/lib/netdot/ipblock.rb +118 -0
- data/lib/netdot/restclient.rb +76 -106
- data/lib/netdot/restclient/version.rb +3 -2
- data/netdot-restclient.gemspec +13 -14
- data/sample/sample.rb +43 -0
- data/spec/host_spec.rb +21 -22
- data/spec/ipblock_spec.rb +108 -0
- data/spec/restclient_spec.rb +34 -46
- data/spec/spec_helper.rb +1 -1
- metadata +22 -42
- data/lib/netdot/subnet.rb +0 -107
- data/sample/host.rb +0 -36
- data/spec/subnet_spec.rb +0 -46
data/netdot-restclient.gemspec
CHANGED
@@ -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 =
|
7
|
+
spec.name = 'netdot-restclient'
|
8
8
|
spec.version = Netdot::RestClient::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
-
spec.summary =
|
12
|
-
spec.description =
|
13
|
-
spec.homepage =
|
14
|
-
spec.license =
|
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 = [
|
19
|
+
spec.require_paths = ['lib']
|
20
20
|
|
21
|
-
spec.add_development_dependency
|
22
|
-
spec.add_development_dependency
|
23
|
-
spec.add_development_dependency
|
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(:
|
8
|
-
|
9
|
-
|
10
|
-
@cidr = NetAddr::CIDR.create(
|
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
|
-
|
17
|
-
|
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
|
-
|
23
|
-
|
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
|
-
|
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
|
data/spec/restclient_spec.rb
CHANGED
@@ -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
|
-
|
16
|
+
end.to raise_error(ArgumentError)
|
19
17
|
end
|
20
18
|
|
21
19
|
it 'raises an exception for an invalid password' do
|
22
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
69
|
-
|
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
|
-
|
94
|
-
|
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
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
expect(resp['firstname']).to eq
|
104
|
-
expect(resp['lastname']).to eq
|
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
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
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
|
-
|
125
|
-
|
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
|
-
|