class Netdot::Host

Manage Host objects.

Attributes

connection[RW]

Public Class Methods

new(argv = {}) click to toggle source

Constructor @option argv [Hash] :connection (REQUIRED) a Netdot::RestClient object

# File lib/netdot/host.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

Public Instance Methods

create(name, ip) click to toggle source

Creates a DNS A record for the specified name and IP. Will also create PTR record if .arpa zone exists. @param name [String] @param ip [String]

# File lib/netdot/host.rb, line 48
def create(name, ip)
  Netdot.logger.debug("Creating new DNS records with name:#{name}" \
  " and ip:#{ip}")
  @connection.post('host', 'name' => name, 'address' => ip)
end
create_next(name, subnet) click to toggle source

Creates a DNS A record for the specified name, using the next available IP address in the given subnet. Will also create PTR record if .arpa zone exists. @param name [String] @param subnet [String] @return [String] IP address allocated

# File lib/netdot/host.rb, line 60
def create_next(name, subnet)
  Netdot.logger.debug("Creating new DNS records with name:#{name}" \
  " and in subnet:#{subnet}")
  host = @connection.post('host', 'name' => name, 'subnet' => subnet)
  r = find_by_name(host['name'])
  # The issue here is that the response only gives us the RR
  # which can have both IPv4 and IPv6 addresses, so we need to
  # try to pick the right one to return
  r['Ipblock'].keys.each do |id|
    if (r['Ipblock'][id]['parent'] == subnet)
      return r['Ipblock'][id]['address']
    end
  end
  fail 'Failed to find the allocated address'
end
delete(name) click to toggle source

Deletes the DNS A record for the specified name. @param name [String]

# File lib/netdot/host.rb, line 88
def delete(name)
  host = find_by_name(name)
  return unless host

  # remove any associated IP addresses
  Netdot.logger.debug("Removing IP records for #{name}")
  host['Ipblock'].keys.each do |id|
    begin
      @connection.delete("host?ipid=#{id}")
    rescue => e
      # Not Found is ok, otherwise re-raise
      raise unless e.message =~ /404/
    end
  end
end
find(param, value) click to toggle source

Finds all RR and Ipblock records, given a flexible set of arguments. Handles NOT FOUND exceptions. @param param [String] a generic parameter @param value [String] a generic value

# File lib/netdot/host.rb, line 21
def find(param, value)
  begin
    host = @connection.get("/host?#{param}=#{value}")
  rescue => e
    # Not Found is ok, otherwise re-raise
    raise unless e.message =~ /404/
  end
  # Return what we got
  host
end
find_by_ip(ip) click to toggle source

Finds all RR and Ipblock records associated with the specified IP. @param ip [String]

# File lib/netdot/host.rb, line 40
def find_by_ip(ip)
  find(:address, ip)
end
find_by_name(name) click to toggle source

Finds all RR and Ipblock records associated with the specified name. @param name [String]

# File lib/netdot/host.rb, line 34
def find_by_name(name)
  find(:name, name)
end
update(name, ip) click to toggle source

Updates the DNS A record for the sepcified name and IP. Will also create PTR record if .arpa zone exists. @param name [String] @param ip [String]

# File lib/netdot/host.rb, line 80
def update(name, ip)
  Netdot.logger.debug("Updating DNS records with name:#{name} and ip:#{ip}")
  delete(name)
  create(name, ip)
end