Class: Netdot::Subnet
- Inherits:
-
Object
- Object
- Netdot::Subnet
- Defined in:
- lib/netdot/subnet.rb
Instance Attribute Summary collapse
-
#connection ⇒ Object
Returns the value of attribute connection.
Instance Method Summary collapse
-
#allocate(container, prefix = 24, description = nil) ⇒ Object
Get next available subnet in given container.
-
#delete(subnet) ⇒ Object
Delete subnet and all its records.
-
#initialize(argv = {}) ⇒ Subnet
constructor
A new instance of Subnet.
Constructor Details
#initialize(argv = {}) ⇒ Subnet
Returns a new instance of Subnet.
5 6 7 8 9 10 11 |
# File 'lib/netdot/subnet.rb', line 5 def initialize(argv = {}) [:connection].each do |k| raise ArgumentError, "Missing required argument '#{k}'" unless argv[k] end argv.each { |k,v| instance_variable_set("@#{k}", v) } end |
Instance Attribute Details
#connection ⇒ Object
Returns the value of attribute connection.
3 4 5 |
# File 'lib/netdot/subnet.rb', line 3 def connection @connection end |
Instance Method Details
#allocate(container, prefix = 24, description = nil) ⇒ Object
Get next available subnet in given container
Arguments:
IP container block (string)
CIDR subnet size (optional integer)
description (optional string)
Returns:
New subnet ID when successful
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 |
# File 'lib/netdot/subnet.rb', line 22 def allocate(container, prefix=24, description=nil) # Netdot currently only supports /24 prefixes raise ArgumentError, "Prefix size #{prefix} is not currently supported (must be 24)" unless prefix==24 # Search for container and get its ID cont_id = get_ipblock_id(container) # Get container's children blocks begin resp = @connection.get("Ipblock?parent=#{cont_id}") rescue Exception => e # Not Found is ok, otherwise re-raise raise unless (e. =~ /404/) end # store existing subnets in hash (if any) subnets = Hash.new if ( resp ) resp.values.each do |b| b.each do |k,v| address = v['address'] subnets[address] = 1 end end end # Iterate over all possible subnets # This assumes that subnets are /24 spref = container.split('/')[0] spref.gsub!(/(\d+\.\d+)\..*/, '\1') (1..255).each do |n| saddr = spref.dup saddr = spref + ".#{n}.0" if !subnets.empty? && subnets.key?(saddr) next # subnet exists end # Create subnet 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 raise "Could not allocate subnet in #{container}" end |
#delete(subnet) ⇒ Object
Delete subnet and all its records
Arguments:
subnet address (CIDR)
Returns:
True if successful
79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/netdot/subnet.rb', line 79 def delete(subnet) resp = @connection.get("host?subnet=#{subnet}") if ! resp.empty? resp['Ipblock'].keys.each do |id| @connection.delete("Ipblock/#{id}") end end sid = get_ipblock_id(subnet) @connection.delete("Ipblock/#{sid}") end |