class Proxy::DHCP::EfficientIp::Api

Attributes

connection[R]

Public Class Methods

new(connection) click to toggle source
# File lib/smart_proxy_efficient_ip/api.rb, line 7
def initialize(connection)
  @connection = connection
end

Public Instance Methods

add_record(params) click to toggle source
# File lib/smart_proxy_efficient_ip/api.rb, line 74
def add_record(params)
  subnet = find_subnet(params['network'])

  connection.ip_address_add(
    site_name: subnet['site_name'],
    ip_addr: params['ip'],
    mac_addr: params['mac'],
    name: params['name']
  )
end
delete_record(record) click to toggle source
# File lib/smart_proxy_efficient_ip/api.rb, line 85
def delete_record(record)
  subnet = find_subnet(record.subnet.network)

  connection.ip_address_delete(
    hostaddr: record.ip,
    site_name: subnet['site_name'],
  )
end
find_free(network_address, start_ip, end_ip) click to toggle source
# File lib/smart_proxy_efficient_ip/api.rb, line 26
def find_free(network_address, start_ip, end_ip)
  subnet = find_subnet(network_address)

  result = connection.ip_address_find_free(
    subnet_id: subnet['subnet_id'],
    begin_addr: start_ip,
    end_addr: end_ip,
    max_find: 1
  )
  parse(result.body)&.first
end
find_record(ip_or_mac) click to toggle source
# File lib/smart_proxy_efficient_ip/api.rb, line 38
def find_record(ip_or_mac)
  result = connection.ip_address_list(
    where: "type='ip' and (hostaddr='#{ip_or_mac}' or mac_addr='#{ip_or_mac}')",
    limit: 1
  )
  parse(result.body)&.first
end
find_records(ip_or_mac) click to toggle source
# File lib/smart_proxy_efficient_ip/api.rb, line 46
def find_records(ip_or_mac)
  result = connection.ip_address_list(
    where: "type='ip' and (hostaddr='#{ip_or_mac}' or mac_addr='#{ip_or_mac}')",
  )
  parse(result.body)
end
find_subnet(network_address) click to toggle source
# File lib/smart_proxy_efficient_ip/api.rb, line 11
def find_subnet(network_address)
  result = connection.ip_subnet_list(
    where: "start_hostaddr='#{network_address}' and is_terminal='1'",
    limit: 1
  )
  parse(result.body)&.first
end
hosts(network_address) click to toggle source
# File lib/smart_proxy_efficient_ip/api.rb, line 53
def hosts(network_address)
  subnet = find_subnet(network_address)
  result = connection.ip_address_list(
    where: "subnet_id=#{subnet['subnet_id']} and dhcphost_id > 0"
  )
  parse(result.body)
end
leases(network_address) click to toggle source
# File lib/smart_proxy_efficient_ip/api.rb, line 61
def leases(network_address)
  subnet = find_subnet(network_address)
  lease_ids = parse(connection.ip_address_list(
    where: "subnet_id=#{subnet['subnet_id']} and dhcplease_id > 0"
  ).body).map { |r| r['dhcplease_id'] }

  result = connection.dhcp_lease_list(
    where: "dhcplease_id IN (#{lease_ids})"
  )

  parse(result.body)
end
subnets() click to toggle source
# File lib/smart_proxy_efficient_ip/api.rb, line 19
def subnets
  result = connection.ip_subnet_list(
    where: "is_terminal='1' and start_hostaddr!='0.0.0.0'"
  )
  parse(result.body)
end

Private Instance Methods

parse(response) click to toggle source
# File lib/smart_proxy_efficient_ip/api.rb, line 96
def parse(response)
  response.empty? ? nil : JSON.parse(response)
end