smart_proxy_ipam 0.0.3 → 0.0.4

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: 6837d0021bc1780d407c415305028b4df44d06384f3ff5f832bbdce27eac9553
4
- data.tar.gz: e6fd4ada7f80681809f337c78b543323da2d75eae3c647fc8a27bb3884ed3c02
3
+ metadata.gz: cf80d5537986cfb15d05ac927e2c0498a9cdcab10c98866dab18b4ead3a91131
4
+ data.tar.gz: 57d175cf91379394aae2f6ba477d0c89a84b4774363e9e391bc473fa9454bc5f
5
5
  SHA512:
6
- metadata.gz: '068c6ae6cb3765c73e462c4dfa7b9d6fe40b593f1fbc910a1e081ee12adadc4780d2dd47c8916c993678e8c0c5b83777338afa308d0f9b7751e24712476c45f5'
7
- data.tar.gz: d13ffd1797f101cb2a0c67d6c354794f8a43c16453c879cfedf07f1df77e219f3335aa7eb0e3ffb8196e4f1cddf7bc401560df4e0c44662e82cef383c1cfdf72
6
+ metadata.gz: 47f2404f8982398568a8d6d1984116658e7dd9eead7679f836c74bef9772bab678b084b82cdddfbfcc8d6b6b19adf3962510c5b566c0f7a1b80d53b9e9505da8
7
+ data.tar.gz: e3dc74e8020a7af4c7f660ef7204c03130088a58dd5fff23a0d18440061d3f602182f6a13f0a1d363429ad860499ce87ab33f71fa524a7f10f8006075f469e44
@@ -1,3 +1,2 @@
1
-
2
1
  require 'smart_proxy_ipam/version'
3
2
  require 'smart_proxy_ipam/ipam'
@@ -1,9 +1,9 @@
1
1
 
2
- module Proxy::ExternalIpam
2
+ module Proxy::Ipam
3
3
  class NotFound < RuntimeError; end
4
4
 
5
5
  class Plugin < ::Proxy::Plugin
6
- plugin 'external_ipam', Proxy::ExternalIpam::VERSION
6
+ plugin 'external_ipam', Proxy::Ipam::VERSION
7
7
 
8
8
  http_rackup_path File.expand_path('ipam_http_config.ru', File.expand_path('../', __FILE__))
9
9
  https_rackup_path File.expand_path('ipam_http_config.ru', File.expand_path('../', __FILE__))
@@ -1,11 +1,11 @@
1
1
 
2
- module Proxy::ExternalIpam
2
+ module Proxy::Ipam
3
3
  extend ::Proxy::Util
4
4
  extend ::Proxy::Log
5
5
 
6
6
  class << self
7
7
  def get_config
8
- Proxy::ExternalIpam::Plugin.settings.external_ipam
8
+ Proxy::Ipam::Plugin.settings.external_ipam
9
9
  end
10
10
  end
11
11
  end
@@ -13,26 +13,27 @@ module Proxy::Phpipam
13
13
 
14
14
  begin
15
15
  cidr = params[:cidr]
16
-
16
+
17
17
  if not cidr
18
18
  return {error: "A 'cidr' parameter for the subnet must be provided(e.g. 100.10.10.0/24)"}.to_json
19
19
  end
20
20
 
21
- response = PhpipamClient.get_subnet(cidr)
21
+ phpipam_client = PhpipamClient.new
22
+ response = phpipam_client.get_subnet(cidr)
22
23
 
23
24
  if response['message'] && response['message'].downcase == "no subnets found"
24
25
  return {error: "The specified subnet does not exist in phpIPAM."}.to_json
25
26
  end
26
27
 
27
28
  subnet_id = response['data'][0]['id']
28
- response = PhpipamClient.get_next_ip(subnet_id)
29
+ response = phpipam_client.get_next_ip(subnet_id)
29
30
 
30
31
  if response['message'] && response['message'].downcase == "no free addresses found"
31
32
  return {error: "There are no more free addresses in subnet #{cidr}"}.to_json
32
33
  end
33
34
 
34
35
  {cidr: cidr, next_ip: response['data']}.to_json
35
- rescue Errno::ECONNREFUSED => e
36
+ rescue Errno::ECONNREFUSED
36
37
  return {error: "Unable to connect to phpIPAM server"}.to_json
37
38
  end
38
39
  end
@@ -43,9 +44,14 @@ module Proxy::Phpipam
43
44
  begin
44
45
  cidr = params[:cidr]
45
46
 
46
- subnet = PhpipamClient.get_subnet(cidr)
47
+ if not cidr
48
+ return {error: "A 'cidr' parameter for the subnet must be provided(e.g. 100.10.10.0/24)"}.to_json
49
+ end
50
+
51
+ phpipam_client = PhpipamClient.new
52
+ subnet = phpipam_client.get_subnet(cidr)
47
53
  subnet.to_json
48
- rescue Errno::ECONNREFUSED => e
54
+ rescue Errno::ECONNREFUSED
49
55
  return {error: "Unable to connect to phpIPAM server"}.to_json
50
56
  end
51
57
  end
@@ -54,9 +60,10 @@ module Proxy::Phpipam
54
60
  content_type :json
55
61
 
56
62
  begin
57
- sections = PhpipamClient.get_sections
63
+ phpipam_client = PhpipamClient.new
64
+ sections = phpipam_client.get_sections
58
65
  sections.to_json
59
- rescue Errno::ECONNREFUSED => e
66
+ rescue Errno::ECONNREFUSED
60
67
  return {error: "Unable to connect to phpIPAM server"}.to_json
61
68
  end
62
69
  end
@@ -71,9 +78,10 @@ module Proxy::Phpipam
71
78
  return {error: "A 'section_id' must be provided"}.to_json
72
79
  end
73
80
 
74
- subnets = PhpipamClient.get_subnets(section_id)
81
+ phpipam_client = PhpipamClient.new
82
+ subnets = phpipam_client.get_subnets(section_id)
75
83
  subnets.to_json
76
- rescue Errno::ECONNREFUSED => e
84
+ rescue Errno::ECONNREFUSED
77
85
  return {error: "Unable to connect to phpIPAM server"}.to_json
78
86
  end
79
87
  end
@@ -88,14 +96,15 @@ module Proxy::Phpipam
88
96
  return {error: "Missing 'cidr' parameter. A CIDR IPv4 address must be provided(e.g. 100.10.10.0/24)"}.to_json if not cidr
89
97
  return {error: "Missing 'ip' parameter. An IPv4 address must be provided(e.g. 100.10.10.22)"}.to_json if not ip
90
98
 
91
- subnet = PhpipamClient.get_subnet(cidr)
99
+ phpipam_client = PhpipamClient.new
100
+ subnet = phpipam_client.get_subnet(cidr)
92
101
 
93
102
  if subnet['message'] && subnet['message'].downcase == "no subnets found"
94
103
  return {error: "The specified subnet does not exist in phpIPAM."}.to_json
95
104
  end
96
105
 
97
106
  subnet_id = subnet['data'][0]['id']
98
- usage = PhpipamClient.get_subnet_usage(subnet_id)
107
+ usage = phpipam_client.get_subnet_usage(subnet_id)
99
108
 
100
109
  # We need to check subnet usage first in the case there are zero ips in the subnet. Checking
101
110
  # the ip existence on an empty subnet returns a malformed response from phpIPAM, containing
@@ -103,7 +112,7 @@ module Proxy::Phpipam
103
112
  if usage['data']['used'] == "0"
104
113
  return {ip: ip, exists: false}.to_json
105
114
  else
106
- response = PhpipamClient.ip_exists(ip, subnet_id)
115
+ response = phpipam_client.ip_exists(ip, subnet_id)
107
116
 
108
117
  if response && response['message'] && response['message'].downcase == 'no addresses found'
109
118
  return {ip: ip, exists: false}.to_json
@@ -111,7 +120,7 @@ module Proxy::Phpipam
111
120
  return {ip: ip, exists: true}.to_json
112
121
  end
113
122
  end
114
- rescue Errno::ECONNREFUSED => e
123
+ rescue Errno::ECONNREFUSED
115
124
  return {error: "Unable to connect to phpIPAM server"}.to_json
116
125
  end
117
126
  end
@@ -126,7 +135,8 @@ module Proxy::Phpipam
126
135
  return {error: "Missing 'cidr' parameter. A CIDR IPv4 address must be provided(e.g. 100.10.10.0/24)"}.to_json if not cidr
127
136
  return {error: "Missing 'ip' parameter. An IPv4 address must be provided(e.g. 100.10.10.22)"}.to_json if not ip
128
137
 
129
- response = PhpipamClient.get_subnet(cidr)
138
+ phpipam_client = PhpipamClient.new
139
+ response = phpipam_client.get_subnet(cidr)
130
140
 
131
141
  if response['message'] && response['message'].downcase == "no subnets found"
132
142
  return {error: "The specified subnet does not exist in phpIPAM."}.to_json
@@ -134,10 +144,10 @@ module Proxy::Phpipam
134
144
 
135
145
  subnet_id = response['data'][0]['id']
136
146
 
137
- PhpipamClient.add_ip_to_subnet(ip, subnet_id, 'Address auto added by Foreman')
147
+ phpipam_client.add_ip_to_subnet(ip, subnet_id, 'Address auto added by Foreman')
138
148
 
139
149
  {message: "IP #{ip} added to subnet #{cidr} successfully."}.to_json
140
- rescue Errno::ECONNREFUSED => e
150
+ rescue Errno::ECONNREFUSED
141
151
  return {error: "Unable to connect to phpIPAM server"}.to_json
142
152
  end
143
153
  end
@@ -152,7 +162,8 @@ module Proxy::Phpipam
152
162
  return {error: "Missing 'cidr' parameter. A CIDR IPv4 address must be provided(e.g. 100.10.10.0/24)"}.to_json if not cidr
153
163
  return {error: "Missing 'ip' parameter. An IPv4 address must be provided(e.g. 100.10.10.22)"}.to_json if not ip
154
164
 
155
- response = PhpipamClient.get_subnet(cidr)
165
+ phpipam_client = PhpipamClient.new
166
+ response = phpipam_client.get_subnet(cidr)
156
167
 
157
168
  if response['message'] && response['message'].downcase == "no subnets found"
158
169
  return {error: "The specified subnet does not exist in phpIPAM."}.to_json
@@ -160,10 +171,10 @@ module Proxy::Phpipam
160
171
 
161
172
  subnet_id = response['data'][0]['id']
162
173
 
163
- PhpipamClient.delete_ip_from_subnet(ip, subnet_id)
174
+ phpipam_client.delete_ip_from_subnet(ip, subnet_id)
164
175
 
165
176
  {message: "IP #{ip} deleted from subnet #{cidr} successfully."}.to_json
166
- rescue Errno::ECONNREFUSED => e
177
+ rescue Errno::ECONNREFUSED
167
178
  return {error: "Unable to connect to phpIPAM server"}.to_json
168
179
  end
169
180
  end
@@ -7,50 +7,52 @@ require 'smart_proxy_ipam/ipam_main'
7
7
 
8
8
  module Proxy::Phpipam
9
9
  class PhpipamClient
10
- conf = Proxy::ExternalIpam.get_config[:phpipam]
11
- @phpipam_config = {url: conf[:url], user: conf[:user], password: conf[:password]}
12
- @api_base = conf[:url] + '/api/' + conf[:user] + '/'
13
- @token = nil
10
+ def initialize
11
+ conf = Proxy::Ipam.get_config[:phpipam]
12
+ @phpipam_config = {url: conf[:url], user: conf[:user], password: conf[:password]}
13
+ @api_base = conf[:url] + '/api/' + conf[:user] + '/'
14
+ @token = nil
15
+ end
14
16
 
15
- def self.get_subnet(cidr)
17
+ def get_subnet(cidr)
16
18
  url = 'subnets/cidr/' + cidr.to_s + '/'
17
- self.get(url)
19
+ get(url)
18
20
  end
19
21
 
20
- def self.get_next_ip(subnet_id)
22
+ def get_next_ip(subnet_id)
21
23
  url = 'subnets/' + subnet_id.to_s + '/first_free/'
22
- self.get(url)
24
+ get(url)
23
25
  end
24
26
 
25
- def self.add_ip_to_subnet(ip, subnet_id, desc)
27
+ def add_ip_to_subnet(ip, subnet_id, desc)
26
28
  data = {'subnetId': subnet_id, 'ip': ip, 'description': desc}
27
- self.post('addresses/', data)
29
+ post('addresses/', data)
28
30
  end
29
31
 
30
- def self.get_sections
31
- self.get('sections/')['data']
32
+ def get_sections
33
+ get('sections/')['data']
32
34
  end
33
35
 
34
- def self.get_subnets(section_id)
35
- self.get('sections/' + section_id.to_s + '/subnets/')
36
+ def get_subnets(section_id)
37
+ get('sections/' + section_id.to_s + '/subnets/')
36
38
  end
37
39
 
38
- def self.ip_exists(ip, subnet_id)
39
- self.get('subnets/' + subnet_id.to_s + '/addresses/' + ip + '/')
40
+ def ip_exists(ip, subnet_id)
41
+ get('subnets/' + subnet_id.to_s + '/addresses/' + ip + '/')
40
42
  end
41
43
 
42
- def self.get_subnet_usage(subnet_id)
43
- self.get('subnets/' + subnet_id.to_s + '/usage/')
44
+ def get_subnet_usage(subnet_id)
45
+ get('subnets/' + subnet_id.to_s + '/usage/')
44
46
  end
45
47
 
46
- def self.delete_ip_from_subnet(ip, subnet_id)
47
- self.delete('addresses/' + ip + '/' + subnet_id.to_s + '/')
48
+ def delete_ip_from_subnet(ip, subnet_id)
49
+ delete('addresses/' + ip + '/' + subnet_id.to_s + '/')
48
50
  end
49
51
 
50
52
  private
51
53
 
52
- def self.get(path, body=nil)
53
- self.authenticate
54
+ def get(path, body=nil)
55
+ authenticate
54
56
  uri = URI(@api_base + path)
55
57
  uri.query = URI.encode_www_form(body) if body
56
58
  request = Net::HTTP::Get.new(uri)
@@ -63,8 +65,8 @@ module Proxy::Phpipam
63
65
  JSON.parse(response.body)
64
66
  end
65
67
 
66
- def self.delete(path, body=nil)
67
- self.authenticate
68
+ def delete(path, body=nil)
69
+ authenticate
68
70
  uri = URI(@api_base + path)
69
71
  uri.query = URI.encode_www_form(body) if body
70
72
  request = Net::HTTP::Delete.new(uri)
@@ -77,8 +79,8 @@ module Proxy::Phpipam
77
79
  JSON.parse(response.body)
78
80
  end
79
81
 
80
- def self.post(path, body=nil)
81
- self.authenticate
82
+ def post(path, body=nil)
83
+ authenticate
82
84
  uri = URI(@api_base + path)
83
85
  uri.query = URI.encode_www_form(body) if body
84
86
  request = Net::HTTP::Post.new(uri)
@@ -91,7 +93,7 @@ module Proxy::Phpipam
91
93
  JSON.parse(response.body)
92
94
  end
93
95
 
94
- def self.authenticate
96
+ def authenticate
95
97
  auth_uri = URI(@api_base + '/user/')
96
98
  request = Net::HTTP::Post.new(auth_uri)
97
99
  request.basic_auth @phpipam_config[:user], @phpipam_config[:password]
@@ -1,6 +1,6 @@
1
1
 
2
2
  module Proxy
3
- module ExternalIpam
4
- VERSION = '0.0.3'
3
+ module Ipam
4
+ VERSION = '0.0.4'
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.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Smith
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-06-11 00:00:00.000000000 Z
11
+ date: 2019-06-12 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