Class: Netdot::Host

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

Overview

Manage Host objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv = {}) ⇒ Host

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/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

Instance Attribute Details

#connectionObject

Returns the value of attribute connection.



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

def connection
  @connection
end

Instance Method Details

#create(name, ip) ⇒ Object

Creates a DNS A record for the specified name and IP. Will also create PTR record if .arpa zone exists.

Parameters:

  • name (String)
  • ip (String)


48
49
50
51
52
# 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) ⇒ String

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.

Parameters:

  • name (String)
  • subnet (String)

Returns:

  • (String)

    IP address allocated



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# 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) ⇒ Object

Deletes the DNS A record for the specified name.

Parameters:

  • name (String)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# 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) ⇒ Object

Finds all RR and Ipblock records, given a flexible set of arguments. Handles NOT FOUND exceptions.

Parameters:

  • param (String)

    a generic parameter

  • value (String)

    a generic value



21
22
23
24
25
26
27
28
29
30
# 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) ⇒ Object

Finds all RR and Ipblock records associated with the specified IP.

Parameters:

  • ip (String)


40
41
42
# File 'lib/netdot/host.rb', line 40

def find_by_ip(ip)
  find(:address, ip)
end

#find_by_name(name) ⇒ Object

Finds all RR and Ipblock records associated with the specified name.

Parameters:

  • name (String)


34
35
36
# File 'lib/netdot/host.rb', line 34

def find_by_name(name)
  find(:name, name)
end

#update(name, ip) ⇒ Object

Updates the DNS A record for the sepcified name and IP. Will also create PTR record if .arpa zone exists.

Parameters:

  • name (String)
  • ip (String)


80
81
82
83
84
# 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