netdot-restclient 1.4 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- 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/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: netdot-restclient
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,11 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2015-05-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bundler
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirement: &79226940 !ruby/object:Gem::Requirement
|
17
17
|
none: false
|
18
18
|
requirements:
|
19
19
|
- - ~>
|
@@ -21,15 +21,10 @@ dependencies:
|
|
21
21
|
version: '1.6'
|
22
22
|
type: :development
|
23
23
|
prerelease: false
|
24
|
-
version_requirements:
|
25
|
-
none: false
|
26
|
-
requirements:
|
27
|
-
- - ~>
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
version: '1.6'
|
24
|
+
version_requirements: *79226940
|
30
25
|
- !ruby/object:Gem::Dependency
|
31
26
|
name: rake
|
32
|
-
requirement: !ruby/object:Gem::Requirement
|
27
|
+
requirement: &79224860 !ruby/object:Gem::Requirement
|
33
28
|
none: false
|
34
29
|
requirements:
|
35
30
|
- - ! '>='
|
@@ -37,15 +32,10 @@ dependencies:
|
|
37
32
|
version: '0'
|
38
33
|
type: :development
|
39
34
|
prerelease: false
|
40
|
-
version_requirements:
|
41
|
-
none: false
|
42
|
-
requirements:
|
43
|
-
- - ! '>='
|
44
|
-
- !ruby/object:Gem::Version
|
45
|
-
version: '0'
|
35
|
+
version_requirements: *79224860
|
46
36
|
- !ruby/object:Gem::Dependency
|
47
37
|
name: rspec
|
48
|
-
requirement: !ruby/object:Gem::Requirement
|
38
|
+
requirement: &79027910 !ruby/object:Gem::Requirement
|
49
39
|
none: false
|
50
40
|
requirements:
|
51
41
|
- - ~>
|
@@ -53,15 +43,10 @@ dependencies:
|
|
53
43
|
version: '2.6'
|
54
44
|
type: :development
|
55
45
|
prerelease: false
|
56
|
-
version_requirements:
|
57
|
-
none: false
|
58
|
-
requirements:
|
59
|
-
- - ~>
|
60
|
-
- !ruby/object:Gem::Version
|
61
|
-
version: '2.6'
|
46
|
+
version_requirements: *79027910
|
62
47
|
- !ruby/object:Gem::Dependency
|
63
48
|
name: httpclient
|
64
|
-
requirement: !ruby/object:Gem::Requirement
|
49
|
+
requirement: &79027320 !ruby/object:Gem::Requirement
|
65
50
|
none: false
|
66
51
|
requirements:
|
67
52
|
- - ! '>='
|
@@ -69,15 +54,10 @@ dependencies:
|
|
69
54
|
version: '0'
|
70
55
|
type: :runtime
|
71
56
|
prerelease: false
|
72
|
-
version_requirements:
|
73
|
-
none: false
|
74
|
-
requirements:
|
75
|
-
- - ! '>='
|
76
|
-
- !ruby/object:Gem::Version
|
77
|
-
version: '0'
|
57
|
+
version_requirements: *79027320
|
78
58
|
- !ruby/object:Gem::Dependency
|
79
59
|
name: xml-simple
|
80
|
-
requirement: !ruby/object:Gem::Requirement
|
60
|
+
requirement: &79026340 !ruby/object:Gem::Requirement
|
81
61
|
none: false
|
82
62
|
requirements:
|
83
63
|
- - ! '>='
|
@@ -85,12 +65,7 @@ dependencies:
|
|
85
65
|
version: '0'
|
86
66
|
type: :runtime
|
87
67
|
prerelease: false
|
88
|
-
version_requirements:
|
89
|
-
none: false
|
90
|
-
requirements:
|
91
|
-
- - ! '>='
|
92
|
-
- !ruby/object:Gem::Version
|
93
|
-
version: '0'
|
68
|
+
version_requirements: *79026340
|
94
69
|
description: Talk to Netdot via REST with Ruby
|
95
70
|
email:
|
96
71
|
- cvicente@gmail.com
|
@@ -99,21 +74,25 @@ extensions: []
|
|
99
74
|
extra_rdoc_files: []
|
100
75
|
files:
|
101
76
|
- .gitignore
|
77
|
+
- .rubocop-bbatsov-default.yml
|
78
|
+
- .rubocop-bbatsov-disabled.yml
|
79
|
+
- .rubocop-bbatsov-enabled.yml
|
80
|
+
- .rubocop.yml
|
102
81
|
- Gemfile
|
103
82
|
- LICENSE.txt
|
104
83
|
- README.md
|
105
84
|
- Rakefile
|
106
85
|
- lib/netdot.rb
|
107
86
|
- lib/netdot/host.rb
|
87
|
+
- lib/netdot/ipblock.rb
|
108
88
|
- lib/netdot/restclient.rb
|
109
89
|
- lib/netdot/restclient/version.rb
|
110
|
-
- lib/netdot/subnet.rb
|
111
90
|
- netdot-restclient.gemspec
|
112
|
-
- sample/
|
91
|
+
- sample/sample.rb
|
113
92
|
- spec/host_spec.rb
|
93
|
+
- spec/ipblock_spec.rb
|
114
94
|
- spec/restclient_spec.rb
|
115
95
|
- spec/spec_helper.rb
|
116
|
-
- spec/subnet_spec.rb
|
117
96
|
homepage: ''
|
118
97
|
licenses:
|
119
98
|
- Apache-2.0
|
@@ -135,12 +114,13 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
114
|
version: '0'
|
136
115
|
requirements: []
|
137
116
|
rubyforge_project:
|
138
|
-
rubygems_version: 1.8.
|
117
|
+
rubygems_version: 1.8.11
|
139
118
|
signing_key:
|
140
119
|
specification_version: 3
|
141
120
|
summary: RESTful API client for Netdot
|
142
121
|
test_files:
|
143
122
|
- spec/host_spec.rb
|
123
|
+
- spec/ipblock_spec.rb
|
144
124
|
- spec/restclient_spec.rb
|
145
125
|
- spec/spec_helper.rb
|
146
|
-
|
126
|
+
has_rdoc:
|
data/lib/netdot/subnet.rb
DELETED
@@ -1,107 +0,0 @@
|
|
1
|
-
module Netdot
|
2
|
-
class Subnet
|
3
|
-
attr_accessor :connection
|
4
|
-
|
5
|
-
def initialize(argv = {})
|
6
|
-
[:connection].each do |k|
|
7
|
-
raise ArgumentError, "Missing required argument '#{k}'" unless argv[k]
|
8
|
-
end
|
9
|
-
|
10
|
-
argv.each { |k,v| instance_variable_set("@#{k}", v) }
|
11
|
-
end
|
12
|
-
|
13
|
-
# Get next available subnet in given container
|
14
|
-
#
|
15
|
-
# Arguments:
|
16
|
-
# IP container block (string)
|
17
|
-
# CIDR subnet size (optional integer)
|
18
|
-
# description (optional string)
|
19
|
-
# Returns:
|
20
|
-
# New subnet ID when successful
|
21
|
-
#
|
22
|
-
def allocate(container, prefix=24, description=nil)
|
23
|
-
|
24
|
-
# Netdot currently only supports /24 prefixes
|
25
|
-
raise ArgumentError, "Prefix size #{prefix} is not currently supported (must be 24)" unless prefix==24
|
26
|
-
|
27
|
-
# Search for container and get its ID
|
28
|
-
cont_id = get_ipblock_id(container)
|
29
|
-
|
30
|
-
# Get container's children blocks
|
31
|
-
begin
|
32
|
-
resp = @connection.get("Ipblock?parent=#{cont_id}")
|
33
|
-
rescue Exception => e
|
34
|
-
# Not Found is ok, otherwise re-raise
|
35
|
-
raise unless (e.message =~ /404/)
|
36
|
-
end
|
37
|
-
|
38
|
-
# store existing subnets in hash (if any)
|
39
|
-
subnets = Hash.new
|
40
|
-
if ( resp )
|
41
|
-
resp.values.each do |b|
|
42
|
-
b.each do |k,v|
|
43
|
-
address = v['address']
|
44
|
-
subnets[address] = 1
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
# Iterate over all possible subnets
|
50
|
-
# This assumes that subnets are /24
|
51
|
-
spref = container.split('/')[0]
|
52
|
-
spref.gsub!(/(\d+\.\d+)\..*/, '\1')
|
53
|
-
|
54
|
-
(1..255).each do |n|
|
55
|
-
saddr = spref.dup
|
56
|
-
saddr = spref + ".#{n}.0"
|
57
|
-
if !subnets.empty? && subnets.key?(saddr)
|
58
|
-
next # subnet exists
|
59
|
-
end
|
60
|
-
|
61
|
-
# Create subnet
|
62
|
-
args = { 'address' => saddr, 'prefix' => prefix.to_s, 'status' => 'Subnet' }
|
63
|
-
args['description'] = description unless description.nil?
|
64
|
-
resp = @connection.post("Ipblock", args)
|
65
|
-
return resp['address'] + '/' + resp['prefix']
|
66
|
-
end
|
67
|
-
|
68
|
-
raise "Could not allocate subnet in #{container}"
|
69
|
-
end
|
70
|
-
|
71
|
-
######################################################################
|
72
|
-
# Delete subnet and all its records
|
73
|
-
#
|
74
|
-
# Arguments:
|
75
|
-
# subnet address (CIDR)
|
76
|
-
# Returns:
|
77
|
-
# True if successful
|
78
|
-
#
|
79
|
-
def delete(subnet)
|
80
|
-
resp = @connection.get("host?subnet=#{subnet}")
|
81
|
-
|
82
|
-
if ! resp.empty?
|
83
|
-
resp['Ipblock'].keys.each do |id|
|
84
|
-
@connection.delete("Ipblock/#{id}")
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
sid = get_ipblock_id(subnet)
|
89
|
-
@connection.delete("Ipblock/#{sid}")
|
90
|
-
end
|
91
|
-
|
92
|
-
private
|
93
|
-
######################################################################
|
94
|
-
# Get Ipblock ID given its address in CIDR format
|
95
|
-
#
|
96
|
-
# Arguments:
|
97
|
-
# subnet address (CIDR)
|
98
|
-
# Returns:
|
99
|
-
# ID (number)
|
100
|
-
#
|
101
|
-
def get_ipblock_id(cidr)
|
102
|
-
(address, prefix) = cidr.split('/')
|
103
|
-
resp = @connection.get("Ipblock?address=#{address}&prefix=#{prefix}")
|
104
|
-
resp['Ipblock'].keys[0]
|
105
|
-
end
|
106
|
-
end
|
107
|
-
end
|
data/sample/host.rb
DELETED
@@ -1,36 +0,0 @@
|
|
1
|
-
#
|
2
|
-
# Example: Use the special-purpose 'host' resource
|
3
|
-
# to allocate a new IP and assign DNS records
|
4
|
-
#
|
5
|
-
#
|
6
|
-
require 'netdot/restclient'
|
7
|
-
|
8
|
-
require 'pp'
|
9
|
-
|
10
|
-
netdot = Netdot::RestClient.new(
|
11
|
-
:server => 'http://localhost/netdot',
|
12
|
-
:username => 'admin',
|
13
|
-
:password => 'admin',
|
14
|
-
)
|
15
|
-
# Add
|
16
|
-
# This allocates the first available IP in the given subnet
|
17
|
-
# and creates A and PTR records, assuming that the subnet
|
18
|
-
# is associated with the appropriate forward and reverse zones
|
19
|
-
netdot.post('host', { 'name' => 'testhost1', 'subnet' => '192.168.1.0/24'})
|
20
|
-
|
21
|
-
# Find
|
22
|
-
resp = netdot.get('host?name=testhost1.defaultdomain')
|
23
|
-
|
24
|
-
pp resp
|
25
|
-
|
26
|
-
ipid = resp['Ipblock'].keys[0]
|
27
|
-
|
28
|
-
# Delete DNS records associated with new IP
|
29
|
-
netdot.delete("host?ipid=#{ipid}")
|
30
|
-
|
31
|
-
# Notice that the above does not delete the Ipblock object, but
|
32
|
-
# marks it as "available"
|
33
|
-
# If user has admin privileges and really wants to delete the
|
34
|
-
# Ipblock, then they can do:
|
35
|
-
|
36
|
-
netdot.delete("Ipblock/#{ipid}")
|
data/spec/subnet_spec.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Netdot::Subnet do
|
4
|
-
|
5
|
-
before :all do
|
6
|
-
@netdot = connect
|
7
|
-
@subnet = Netdot::Subnet.new(:connection => @netdot) if @subnet.nil?
|
8
|
-
end
|
9
|
-
|
10
|
-
context 'when creating a new Subnet instance' do
|
11
|
-
it 'creates a new instance' do
|
12
|
-
expect {
|
13
|
-
subnet = Netdot::Subnet.new(:connection => @netdot)
|
14
|
-
}.not_to raise_error
|
15
|
-
end
|
16
|
-
|
17
|
-
it 'raises an exception for invalid arguments' do
|
18
|
-
expect {
|
19
|
-
subnet = Netdot::Subnet.new
|
20
|
-
}.to raise_error(ArgumentError)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
context 'creating a new subnet' do
|
25
|
-
it 'allocates a new subnet' do
|
26
|
-
subnet_id = @subnet.allocate('10.0.0.0/8', 24, 'test')
|
27
|
-
expect(subnet_id).to match('10.0.*.0/24')
|
28
|
-
end
|
29
|
-
|
30
|
-
it 'throws an exception if the prefix is not /24' do
|
31
|
-
expect {
|
32
|
-
subnet_id = @subnet.allocate('10.0.0.0/8', 26)
|
33
|
-
}.to raise_error(ArgumentError)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
context 'deleting a subnet' do
|
38
|
-
let(:subnet_id) do
|
39
|
-
@subnet.allocate('10.0.0.0/8', 24)
|
40
|
-
end
|
41
|
-
|
42
|
-
it 'deletes a subnet' do
|
43
|
-
expect(@subnet.delete(subnet_id)).to be_truthy
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|