Class: Proxy::Phpipam::PhpipamClient

Inherits:
Object
  • Object
show all
Includes:
Ipam::IpamHelper, Ipam::IpamValidator, Log
Defined in:
lib/smart_proxy_ipam/phpipam/phpipam_client.rb

Overview

Implementation class for External IPAM provider phpIPAM

Constant Summary

Constants included from Ipam::IpamHelper

Ipam::IpamHelper::ERRORS, Ipam::IpamHelper::MAX_IP_RETRIES

Instance Method Summary collapse

Methods included from Ipam::IpamValidator

#validate_cidr!, #validate_ip!, #validate_ip_in_cidr!, #validate_mac!, #validate_required_params!

Methods included from Ipam::IpamHelper

#cache_next_ip, #find_new_ip, #get_request_group, #increment_ip, #provider, #usable_ip

Constructor Details

#initialize(conf) ⇒ PhpipamClient

Returns a new instance of PhpipamClient.



19
20
21
22
23
24
25
26
# File 'lib/smart_proxy_ipam/phpipam/phpipam_client.rb', line 19

def initialize(conf)
  @conf = conf
  @api_base = "#{@conf[:url]}/api/#{@conf[:user]}/"
  @token = authenticate
  @api_resource = Proxy::Ipam::ApiResource.new(api_base: @api_base, token: @token, auth_header: 'Token')
  @ip_cache = Proxy::Ipam::IpCache.instance
  @ip_cache.set_provider_name('phpipam')
end

Instance Method Details

#add_ip_to_subnet(ip, params) ⇒ Object



134
135
136
137
138
139
140
# File 'lib/smart_proxy_ipam/phpipam/phpipam_client.rb', line 134

def add_ip_to_subnet(ip, params)
  data = { subnetId: params[:subnet_id], ip: ip, description: 'Address auto added by Foreman' }
  subnet = @api_resource.post('addresses/', data.to_json)
  json_body = JSON.parse(subnet.body)
  return nil if json_body['code'] == 201
  { error: 'Unable to add IP to External IPAM' }
end

#authenticated?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/smart_proxy_ipam/phpipam/phpipam_client.rb', line 164

def authenticated?
  !@token.nil?
end

#delete_ip_from_subnet(ip, params) ⇒ Object



142
143
144
145
146
147
# File 'lib/smart_proxy_ipam/phpipam/phpipam_client.rb', line 142

def delete_ip_from_subnet(ip, params)
  subnet = @api_resource.delete("addresses/#{ip}/#{params[:subnet_id]}/")
  json_body = JSON.parse(subnet.body)
  return nil if json_body['success']
  { error: 'Unable to delete IP from External IPAM' }
end

#get_ipam_group(group_name) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/smart_proxy_ipam/phpipam/phpipam_client.rb', line 76

def get_ipam_group(group_name)
  return nil if group_name.nil?
  group = @api_resource.get("sections/#{group_name}/")
  json_body = JSON.parse(group.body)
  raise ERRORS[:no_group] if json_body['data'].nil?

  data = {
    id: json_body['data']['id'],
    name: json_body['data']['name'],
    description: json_body['data']['description']
  }

  return data if json_body['data']
end

#get_ipam_groupsObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/smart_proxy_ipam/phpipam/phpipam_client.rb', line 91

def get_ipam_groups
  groups = @api_resource.get('sections/')
  json_body = JSON.parse(groups.body)
  return [] if json_body['data'].nil?

  data = []
  json_body['data'].each do |group|
    data.push({
      id: group['id'],
      name: group['name'],
      description: group['description']
    })
  end

  return data if json_body['data']
end

#get_ipam_subnet(cidr, group_name = nil) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/smart_proxy_ipam/phpipam/phpipam_client.rb', line 28

def get_ipam_subnet(cidr, group_name = nil)
  if group_name.nil? || group_name.empty?
    get_ipam_subnet_by_cidr(cidr)
  else
    group = get_ipam_group(group_name)
    get_ipam_subnet_by_group(cidr, group[:id])
  end
end

#get_ipam_subnet_by_cidr(cidr) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/smart_proxy_ipam/phpipam/phpipam_client.rb', line 61

def get_ipam_subnet_by_cidr(cidr)
  subnet = @api_resource.get("subnets/cidr/#{cidr}")
  json_body = JSON.parse(subnet.body)
  return nil if json_body['data'].nil?

  data = {
    id: json_body['data'][0]['id'],
    subnet: json_body['data'][0]['subnet'],
    mask: json_body['data'][0]['mask'],
    description: json_body['data'][0]['description']
  }

  return data if json_body['data']
end

#get_ipam_subnet_by_group(cidr, group_id) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/smart_proxy_ipam/phpipam/phpipam_client.rb', line 37

def get_ipam_subnet_by_group(cidr, group_id)
  subnets = get_ipam_subnets(group_id)
  return nil if subnets.nil?
  subnet_id = nil

  subnets.each do |subnet|
    subnet_cidr = "#{subnet[:subnet]}/#{subnet[:mask]}"
    subnet_id = subnet[:id] if subnet_cidr == cidr
  end

  return nil if subnet_id.nil?
  response = @api_resource.get("subnets/#{subnet_id}/")
  json_body = JSON.parse(response.body)

  data = {
    id: json_body['data']['id'],
    subnet: json_body['data']['subnet'],
    mask: json_body['data']['mask'],
    description: json_body['data']['description']
  }

  return data if json_body['data']
end

#get_ipam_subnets(group_name) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/smart_proxy_ipam/phpipam/phpipam_client.rb', line 108

def get_ipam_subnets(group_name)
  group = get_ipam_group(group_name)
  raise ERRORS[:no_group] if group.nil?
  subnets = @api_resource.get("sections/#{group[:id]}/subnets/")
  json_body = JSON.parse(subnets.body)
  return nil if json_body['data'].nil?

  data = []
  json_body['data'].each do |subnet|
    data.push({
      id: subnet['id'],
      subnet: subnet['subnet'],
      mask: subnet['mask'],
      description: subnet['description']
    })
  end

  return data if json_body['data']
end

#get_next_ip(mac, cidr, group_name) ⇒ Object



149
150
151
152
153
154
155
156
157
158
# File 'lib/smart_proxy_ipam/phpipam/phpipam_client.rb', line 149

def get_next_ip(mac, cidr, group_name)
  subnet = get_ipam_subnet(cidr, group_name)
  raise ERRORS[:no_subnet] if subnet.nil?
  response = @api_resource.get("subnets/#{subnet[:id]}/first_free/")
  json_body = JSON.parse(response.body)
  return { error: json_body['message'] } if json_body['message']
  ip = json_body['data']
  next_ip = cache_next_ip(@ip_cache, ip, mac, cidr, subnet[:id], group_name)
  { data: next_ip }
end

#groups_supported?Boolean

Returns:

  • (Boolean)


160
161
162
# File 'lib/smart_proxy_ipam/phpipam/phpipam_client.rb', line 160

def groups_supported?
  true
end

#ip_exists?(ip, subnet_id, _group_name) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
131
132
# File 'lib/smart_proxy_ipam/phpipam/phpipam_client.rb', line 128

def ip_exists?(ip, subnet_id, _group_name)
  ip = @api_resource.get("subnets/#{subnet_id}/addresses/#{ip}/")
  json_body = JSON.parse(ip.body)
  json_body['success']
end