smart_proxy_ipam 0.0.17 → 0.0.18

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d0624875b34dd0cd44fcaa5446e22b5a9f4c2d9ee6426445ba8605fd80ee9df6
4
- data.tar.gz: 9cecc6b2070e5fd02d1f42a7aa849b79a92d05dc18a660f2f5baf45af97bf368
3
+ metadata.gz: 58e0c15cc7ee1369b7a1c5b7fa7cfcae0c7c54d75c8fc6486f02c8672447e0f8
4
+ data.tar.gz: f7937ba79296964dbfb142c3655e8c0c2c6f97745c10c40fcb3b3eec2b2390d2
5
5
  SHA512:
6
- metadata.gz: 10ec0acd80412ff6cd523f99701cc1f57a61d67c6cf2e30b4c986c4cd61553443310ab32aa8f5116d8cbce8f5245c46c2a422f719f27d25cc5297550f5c465cf
7
- data.tar.gz: c9324940694102cfccbf3088091f2a2fb6a978261d3ab2bb3d65fba88c7cd6d678dfc67a3b24f785035db4edaebd0b12729c4bb2df54d4c811bb7a58a11a0091
6
+ metadata.gz: 68a637fbf28997b2fff0d961f1d8c6b5ffb6dcd8b32824bc25753a913429ba168e94261c3c9706ad1f2edeb786926c3626c91912c4b14976e34013f7e1bb419d
7
+ data.tar.gz: 291c8293ee1e81b577ac96e25fbcb497c6438c9a1d6c4411f7e91e073dfbf3d77847115cf0621a9c0c279f0d20bd259eed148e81375551c6fa6f5540c4fa6e9e
@@ -36,11 +36,12 @@ module Proxy::Phpipam
36
36
  phpipam_client = PhpipamClient.new
37
37
  subnet = JSON.parse(phpipam_client.get_subnet(cidr, section_name))
38
38
 
39
- return {:code => subnet['code'], :error => subnet['message']}.to_json if no_subnets_found(subnet)
39
+ return {:code => subnet['code'], :error => subnet['error']}.to_json if no_subnets_found?(subnet)
40
40
 
41
41
  ipaddr = phpipam_client.get_next_ip(subnet['data']['id'], mac, cidr, section_name)
42
-
43
- return {:code => subnet['code'], :error => ipaddr['message']}.to_json if no_free_ip_found(JSON.parse(ipaddr))
42
+ ipaddr_parsed = JSON.parse(ipaddr)
43
+
44
+ return {:code => 404, :error => ipaddr_parsed['error']}.to_json if no_free_ip_found?(ipaddr_parsed)
44
45
 
45
46
  ipaddr
46
47
  rescue Errno::ECONNREFUSED, Errno::ECONNRESET
@@ -105,7 +106,9 @@ module Proxy::Phpipam
105
106
 
106
107
  begin
107
108
  phpipam_client = PhpipamClient.new
108
- phpipam_client.get_sections
109
+ sections = phpipam_client.get_sections
110
+ return {:code => 200, :data => []}.to_json if no_sections_found?(JSON.parse(sections))
111
+ sections
109
112
  rescue Errno::ECONNREFUSED, Errno::ECONNRESET
110
113
  logger.debug(errors[:no_connection])
111
114
  raise
@@ -125,17 +128,18 @@ module Proxy::Phpipam
125
128
  # {"code":404,"error":"Not Found"}
126
129
  # Response if :error =>
127
130
  # {"error":"Unable to connect to phpIPAM server"}
128
- get '/groups/:group_name' do
131
+ get '/groups/:group' do
129
132
  content_type :json
130
133
 
131
134
  begin
132
- err = validate_required_params(["group_name"], params)
135
+ err = validate_required_params(["group"], params)
133
136
  return err if err.length > 0
134
137
 
135
138
  phpipam_client = PhpipamClient.new
136
- section = JSON.parse(phpipam_client.get_section(params[:group_name]))
139
+ section = JSON.parse(phpipam_client.get_section(params[:group]))
137
140
 
138
- return {:code => section['code'], :error => section['message']}.to_json if no_section_found(section)
141
+ return {:code => section['code'], :error => section['message']}.to_json if no_section_found?(section)
142
+ puts "SECTION: " + section.to_json
139
143
  section.to_json
140
144
  rescue Errno::ECONNREFUSED, Errno::ECONNRESET
141
145
  logger.debug(errors[:no_connection])
@@ -198,19 +202,19 @@ module Proxy::Phpipam
198
202
  # }
199
203
  # Response if :error =>
200
204
  # {"error":"Unable to connect to External IPAM server"}
201
- get '/groups/:group_name/subnets' do
205
+ get '/groups/:group/subnets' do
202
206
  content_type :json
203
207
 
204
208
  begin
205
- err = validate_required_params(["group_name"], params)
209
+ err = validate_required_params(["group"], params)
206
210
  return err if err.length > 0
207
211
 
208
212
  phpipam_client = PhpipamClient.new
209
- section = JSON.parse(phpipam_client.get_section(params[:group_name]))
213
+ section = JSON.parse(phpipam_client.get_section(params[:group]))
210
214
 
211
- return {:code => section['code'], :error => section['message']}.to_json if no_section_found(section)
215
+ return {:code => 404, :error => errors[:no_section]}.to_json if no_section_found?(section)
212
216
 
213
- phpipam_client.get_subnets(section['data']['id'].to_s)
217
+ phpipam_client.get_subnets(section['data']['id'].to_s, false)
214
218
  rescue Errno::ECONNREFUSED, Errno::ECONNRESET
215
219
  logger.debug(errors[:no_connection])
216
220
  raise
@@ -243,9 +247,15 @@ module Proxy::Phpipam
243
247
  phpipam_client = PhpipamClient.new
244
248
  subnet = JSON.parse(phpipam_client.get_subnet(cidr, section_name))
245
249
 
246
- return {:code => subnet['code'], :error => subnet['message']}.to_json if no_subnets_found(subnet)
250
+ return {:code => 404, :error => subnet['error']}.to_json if no_subnets_found?(subnet)
251
+
252
+ ip_exists = JSON.parse(phpipam_client.ip_exists(ip, subnet['data']['id']))
247
253
 
248
- phpipam_client.ip_exists(ip, subnet['data']['id'])
254
+ if ip_exists['data']
255
+ return {:code => 200, :exists => true}.to_json
256
+ else
257
+ return {:code => 404, :exists => false}.to_json
258
+ end
249
259
  rescue Errno::ECONNREFUSED, Errno::ECONNRESET
250
260
  logger.debug(errors[:no_connection])
251
261
  raise
@@ -280,9 +290,15 @@ module Proxy::Phpipam
280
290
  phpipam_client = PhpipamClient.new
281
291
  subnet = JSON.parse(phpipam_client.get_subnet(cidr, section_name))
282
292
 
283
- return {:code => subnet['code'], :error => subnet['message']}.to_json if no_subnets_found(subnet)
293
+ return {:code => 404, :error => subnet['error']}.to_json if no_subnets_found?(subnet)
284
294
 
285
- phpipam_client.add_ip_to_subnet(ip, subnet['data']['id'], 'Address auto added by Foreman')
295
+ add_ip = JSON.parse(phpipam_client.add_ip_to_subnet(ip, subnet['data']['id'], 'Address auto added by Foreman'))
296
+
297
+ if add_ip['message'] && add_ip['message'] == "Address created"
298
+ return {:code => 200}.to_json
299
+ else
300
+ return {:code => 500, :error => add_ip['message']}.to_json
301
+ end
286
302
  rescue Errno::ECONNREFUSED, Errno::ECONNRESET
287
303
  logger.debug(errors[:no_connection])
288
304
  raise
@@ -314,9 +330,15 @@ module Proxy::Phpipam
314
330
  phpipam_client = PhpipamClient.new
315
331
  subnet = JSON.parse(phpipam_client.get_subnet(cidr, section_name))
316
332
 
317
- return {:code => subnet['code'], :error => subnet['message']}.to_json if no_subnets_found(subnet)
333
+ return {:code => 404, :error => subnet['error']}.to_json if no_subnets_found?(subnet)
334
+
335
+ delete_ip = JSON.parse(phpipam_client.delete_ip_from_subnet(ip, subnet['data']['id']))
318
336
 
319
- phpipam_client.delete_ip_from_subnet(ip, subnet['data']['id'])
337
+ if delete_ip['message'] && delete_ip['message'] == "Address deleted"
338
+ return {:code => 200}.to_json
339
+ else
340
+ return {:code => 500, :error => delete_ip['message']}.to_json
341
+ end
320
342
  rescue Errno::ECONNREFUSED, Errno::ECONNRESET
321
343
  logger.debug(errors[:no_connection])
322
344
  raise
@@ -36,9 +36,9 @@ module Proxy::Phpipam
36
36
  end
37
37
  end
38
38
 
39
- def get_subnet_by_section(cidr, section_name)
39
+ def get_subnet_by_section(cidr, section_name, include_id = true)
40
40
  section = JSON.parse(get_section(section_name))
41
- subnets = JSON.parse(get_subnets(section['data']['id']))
41
+ subnets = JSON.parse(get_subnets(section['data']['id'], include_id))
42
42
  subnet_id = nil
43
43
 
44
44
  subnets['data'].each do |subnet|
@@ -49,14 +49,19 @@ module Proxy::Phpipam
49
49
  return {:code => 404, :error => "No subnet #{cidr} found in section #{URI.unescape(section_name)}"}.to_json if subnet_id.nil?
50
50
 
51
51
  response = get("subnets/#{subnet_id.to_s}/")
52
+ json_body = JSON.parse(response.body)
53
+ json_body['data'] = filter_hash(json_body['data'], [:id, :subnet, :mask, :description]) if json_body['data']
54
+ json_body = filter_hash(json_body, [:code, :data, :error, :message])
55
+ response.body = json_body.to_json
56
+ response.header['Content-Length'] = json_body.to_s.length
52
57
  response.body
53
58
  end
54
59
 
55
60
  def get_subnet_by_cidr(cidr)
56
61
  response = get("subnets/cidr/#{cidr.to_s}")
57
62
  json_body = JSON.parse(response.body)
58
- return response.body if no_subnets_found(json_body)
59
63
  json_body['data'] = filter_fields(json_body, [:id, :subnet, :description, :mask])[0]
64
+ json_body = filter_hash(json_body, [:code, :data, :error, :message])
60
65
  response.body = json_body.to_json
61
66
  response.header['Content-Length'] = json_body.to_s.length
62
67
  response.body
@@ -64,22 +69,31 @@ module Proxy::Phpipam
64
69
 
65
70
  def get_section(section_name)
66
71
  response = get("sections/#{section_name}/")
72
+ json_body = JSON.parse(response.body)
73
+ json_body['data'] = filter_hash(json_body['data'], [:id, :name, :description]) if json_body['data']
74
+ json_body = filter_hash(json_body, [:code, :data, :error, :message])
75
+ response.body = json_body.to_json
76
+ response.header['Content-Length'] = json_body.to_s.length
67
77
  response.body
68
78
  end
69
79
 
70
80
  def get_sections
71
81
  response = get('sections/')
72
82
  json_body = JSON.parse(response.body)
73
- json_body['data'] = filter_fields(json_body, [:id, :name, :description])
83
+ json_body['data'] = filter_fields(json_body, [:id, :name, :description]) if json_body['data']
84
+ json_body = filter_hash(json_body, [:code, :data, :error, :message])
74
85
  response.body = json_body.to_json
75
86
  response.header['Content-Length'] = json_body.to_s.length
76
87
  response.body
77
88
  end
78
89
 
79
- def get_subnets(section_id)
90
+ def get_subnets(section_id, include_id = true)
80
91
  response = get("sections/#{section_id}/subnets/")
92
+ fields = [:subnet, :mask, :description]
93
+ fields.push(:id) if include_id
81
94
  json_body = JSON.parse(response.body)
82
- json_body['data'] = filter_fields(json_body, [:id, :subnet, :mask, :sectionId, :description])
95
+ json_body['data'] = filter_fields(json_body, fields) if json_body['data']
96
+ json_body = filter_hash(json_body, [:code, :data, :error, :message])
83
97
  response.body = json_body.to_json
84
98
  response.header['Content-Length'] = json_body.to_s.length
85
99
  response.body
@@ -87,17 +101,30 @@ module Proxy::Phpipam
87
101
 
88
102
  def ip_exists(ip, subnet_id)
89
103
  response = get("subnets/#{subnet_id.to_s}/addresses/#{ip}/")
104
+ json_body = JSON.parse(response.body)
105
+ json_body['data'] = filter_fields(json_body, [:ip]) if json_body['data']
106
+ json_body = filter_hash(json_body, [:code, :data, :error, :message])
107
+ response.body = json_body.to_json
108
+ response.header['Content-Length'] = json_body.to_s.length
90
109
  response.body
91
110
  end
92
111
 
93
112
  def add_ip_to_subnet(ip, subnet_id, desc)
94
113
  data = {:subnetId => subnet_id, :ip => ip, :description => desc}
95
114
  response = post('addresses/', data)
115
+ json_body = JSON.parse(response.body)
116
+ json_body = filter_hash(json_body, [:code, :error, :message])
117
+ response.body = json_body.to_json
118
+ response.header['Content-Length'] = json_body.to_s.length
96
119
  response.body
97
120
  end
98
121
 
99
122
  def delete_ip_from_subnet(ip, subnet_id)
100
123
  response = delete("addresses/#{ip}/#{subnet_id.to_s}/")
124
+ json_body = JSON.parse(response.body)
125
+ json_body = filter_hash(json_body, [:code, :error, :message])
126
+ response.body = json_body.to_json
127
+ response.header['Content-Length'] = json_body.to_s.length
101
128
  response.body
102
129
  end
103
130
 
@@ -130,6 +157,8 @@ module Proxy::Phpipam
130
157
  json_body['data'] = next_ip
131
158
  end
132
159
 
160
+ json_body = {:code => json_body['code'], :data => json_body['data']}
161
+
133
162
  response.body = json_body.to_json
134
163
  response.header['Content-Length'] = json_body.to_s.length
135
164
  response.body
@@ -223,7 +252,7 @@ module Proxy::Phpipam
223
252
  verify_ip = JSON.parse(ip_exists(new_ip, subnet_id))
224
253
 
225
254
  # If new IP doesn't exist in IPAM and not in the cache
226
- if ip_not_found_in_ipam(verify_ip) && !ip_exists_in_cache(new_ip, cidr, mac, section_name)
255
+ if ip_not_found_in_ipam?(verify_ip) && !ip_exists_in_cache(new_ip, cidr, mac, section_name)
227
256
  found_ip = new_ip.to_s
228
257
  add_ip_to_cache(found_ip, mac, cidr, section_name)
229
258
  break
@@ -9,19 +9,23 @@ module PhpipamHelper
9
9
  err.length == 0 ? [] : {:code => 400, :error => err}.to_json
10
10
  end
11
11
 
12
- def no_subnets_found(subnet)
13
- subnet['message'] && subnet['message'].downcase == "no subnets found"
12
+ def no_subnets_found?(subnet)
13
+ subnet['error'] && subnet['error'].downcase == "no subnets found"
14
14
  end
15
15
 
16
- def no_section_found(section)
16
+ def no_section_found?(section)
17
17
  section['message'] && section['message'].downcase == "not found"
18
18
  end
19
19
 
20
- def no_free_ip_found(ip)
21
- !ip.kind_of?(Array) && ip['message'] && ip['message'].downcase == "no free addresses found"
20
+ def no_sections_found?(sections)
21
+ sections['message'] && sections['message'].downcase == "no sections available"
22
22
  end
23
23
 
24
- def ip_not_found_in_ipam(ip)
24
+ def no_free_ip_found?(ip)
25
+ ip['error'] && ip['error'].downcase == "no free addresses found"
26
+ end
27
+
28
+ def ip_not_found_in_ipam?(ip)
25
29
  ip && ip['message'] && ip['message'].downcase == 'no addresses found'
26
30
  end
27
31
 
@@ -36,13 +40,23 @@ module PhpipamHelper
36
40
  data
37
41
  end
38
42
 
43
+ # Returns a hash with only the fields given in the fields param
44
+ def filter_hash(hash, fields)
45
+ new_hash = {}
46
+ fields.each do |field|
47
+ new_hash[field.to_sym] = hash[field.to_s] if hash[field.to_s]
48
+ end
49
+ new_hash
50
+ end
51
+
39
52
  def errors
40
53
  {
41
54
  :cidr => "A 'cidr' parameter for the subnet must be provided(e.g. IPv4: 100.10.10.0/24, IPv6: 2001:db8:abcd:12::/124)",
42
55
  :mac => "A 'mac' address must be provided(e.g. 00:0a:95:9d:68:10)",
43
56
  :ip => "Missing 'ip' parameter. An IPv4 or IPv6 address must be provided(e.g. IPv4: 100.10.10.22, IPv6: 2001:db8:abcd:12::3)",
44
57
  :section_name => "A 'section_name' must be provided",
45
- :no_connection => "Unable to connect to External IPAM server"
58
+ :no_connection => "Unable to connect to External IPAM server",
59
+ :no_section => "Group not found in External IPAM"
46
60
  }
47
61
  end
48
62
  end
@@ -1,6 +1,6 @@
1
1
 
2
2
  module Proxy
3
3
  module Ipam
4
- VERSION = '0.0.17'
4
+ VERSION = '0.0.18'
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_proxy_ipam
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.17
4
+ version: 0.0.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Smith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-17 00:00:00.000000000 Z
11
+ date: 2020-02-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Smart proxy plugin for IPAM integration with various IPAM providers
14
14
  email: chrisjsmith001@gmail.com