Class: Proxy::DHCP::EfficientIp::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_proxy_efficient_ip/api.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ Api

Returns a new instance of Api.



7
8
9
# File 'lib/smart_proxy_efficient_ip/api.rb', line 7

def initialize(connection)
  @connection = connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.



5
6
7
# File 'lib/smart_proxy_efficient_ip/api.rb', line 5

def connection
  @connection
end

Instance Method Details

#add_record(params) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# 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) ⇒ Object



85
86
87
88
89
90
91
92
# 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) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# 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) ⇒ Object



38
39
40
41
42
43
44
# 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) ⇒ Object



46
47
48
49
50
51
# 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) ⇒ Object



11
12
13
14
15
16
17
# 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) ⇒ Object



53
54
55
56
57
58
59
# 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) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# 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

#subnetsObject



19
20
21
22
23
24
# 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