Class: Netdot::Ipblock

Inherits:
Object
  • Object
show all
Defined in:
lib/netdot/ipblock.rb

Overview

Manage Ipblock objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = {}) ⇒ Ipblock

Constructor

Parameters:

  • argv (Hash) (defaults to: {})

    a customizable set of options

Options Hash (argv):

  • :connection (Hash) — default: REQUIRED

    a Netdot::RestClient object



9
10
11
12
13
14
15
# File 'lib/netdot/ipblock.rb', line 9

def initialize(argv = {})
  [:connection].each do |k|
    fail ArgumentError, "Missing required argument '#{k}'" unless argv[k]
  end

  argv.each { |k, v| instance_variable_set("@#{k}", v) }
end

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



5
6
7
# File 'lib/netdot/ipblock.rb', line 5

def connection
  @connection
end

Instance Method Details

#allocate(container, prefix = 24, description = nil) ⇒ String

Gets the next available Ipblock in the specified container.

Parameters:

  • container (String)

    address

  • prefix (Fixnum) (defaults to: 24)
  • description (String) (defaults to: nil)

    (optional)

Returns:

  • (String)

    new Ipblock id, or nil



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/netdot/ipblock.rb', line 22

def allocate(container, prefix = 24, description = nil)
  # Netdot currently only supports /24 prefixes
  fail ArgumentError, "Prefix size #{prefix} is not currently supported \
                      (must be 24)" unless prefix == 24

  # Search for container and get its ID
  cont_id = find_by_addr(container)

  # Get container's children blocks
  begin
    resp = @connection.get("Ipblock?parent=#{cont_id}")
  rescue => e
    # Not Found is ok, otherwise re-raise
    raise unless e.message =~ /404/
  end

  # store existing Ipblocks in hash (if any)
  ipblocks = {}
  if resp
    resp.values.each do |b|
      b.each do |_k, v|
        address = v['address']
        ipblocks[address] = 1
      end
    end
  end

  # Iterate over all possible Ipblocks
  # This assumes that Ipblocks are /24
  spref = container.split('/')[0]
  spref.gsub!(/(\d+\.\d+)\..*/, '\1')

  (1..255).each do |n|
    spref.dup
    saddr = spref + ".#{n}.0"
    if !ipblocks.empty? && ipblocks.key?(saddr)
      next # Ipblock exists
    end

    # Create Ipblock
    args = {
      'address' => saddr,
      'prefix' => prefix.to_s,
      'status' => 'Subnet'
    }
    args['description'] = description unless description.nil?
    resp = @connection.post('Ipblock', args)
    return resp['address'] + '/' + resp['prefix']
  end

  fail "Could not allocate Ipblock in #{container}"
end

#delete(ipblock, recursive = false) ⇒ Truth

Deletes an Ipblock (and optionally, all its children), for the specified address or CIDR.

Parameters:

  • ipblock (String)

    address or CIDR

Returns:

  • (Truth)


79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/netdot/ipblock.rb', line 79

def delete(ipblock, recursive = false)
  if recursive
    resp = @connection.get("host?subnet=#{ipblock}")
    unless resp.empty?
      resp['Ipblock'].keys.each do |id|
        @connection.delete("Ipblock/#{id}")
      end
    end
  end

  sid = find_by_addr(ipblock)
  @connection.delete("Ipblock/#{sid}")
end

#find_by_addr(cidr) ⇒ Ipblock

Gets the matching Ipblock id for the specified address (in CIDR format).

Parameters:

  • cidr (String)

Returns:



96
97
98
99
100
101
102
103
104
105
# File 'lib/netdot/ipblock.rb', line 96

def find_by_addr(cidr)
  (address, prefix) = cidr.split('/')
  prefix ||= '24'
  begin
    @connection.get("Ipblock?address=#{address}&prefix=#{prefix}")[
      'Ipblock'].keys[0]
  rescue => e
    raise unless e.message =~ /404/
  end
end

#find_by_descr(descr) ⇒ Array<Ipblock>

Gets an array of matching Ipblock ids for the specified description (name).

Parameters:

  • descr (String)

Returns:



111
112
113
114
115
# File 'lib/netdot/ipblock.rb', line 111

def find_by_descr(descr)
  @connection.get("Ipblock?description=#{descr}")['Ipblock']
rescue => e
  raise unless e.message =~ /404/
end