forked from grizzthedj/smart_proxy_ipam
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRakefile
More file actions
169 lines (137 loc) · 5.42 KB
/
Copy pathRakefile
File metadata and controls
169 lines (137 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
require 'rake'
require 'rake/testtask'
desc 'Default: run unit tests.'
task default: :test
desc 'Test smart_proxy_ipam Plugin'
Rake::TestTask.new(:test) do |t|
t.libs << '.'
t.libs << 'lib'
t.libs << 'test'
t.test_files = FileList['test/**/*_test.rb']
t.verbose = true
end
desc 'Add files for new IPAM provider'
task :add_provider, [:provider_name] do |args|
provider_name = args[:provider_name].to_s.downcase
provider_dir = "lib/smart_proxy_ipam/#{provider_name}"
provider_example = "settings.d/externalipam_#{provider_name}.yml.example"
provider_client = "#{provider_dir}/#{provider_name}_client.rb"
provider_plugin = "#{provider_dir}/#{provider_name}_plugin.rb"
provider_stubs = %{require 'yaml'
require 'json'
require 'net/http'
require 'uri'
require 'sinatra'
require 'smart_proxy_ipam/ipam'
require 'smart_proxy_ipam/ipam_helper'
require 'smart_proxy_ipam/ipam_validator'
require 'smart_proxy_ipam/api_resource'
require 'smart_proxy_ipam/ip_cache'
module Proxy::#{provider_name.capitalize}
# Implementation class for External IPAM provider phpIPAM
class #{provider_name.capitalize}Client
include Proxy::Log
include Proxy::Ipam::IpamHelper
include Proxy::Ipam::IpamValidator
@ip_cache = nil
def initialize(conf)
@conf = conf
@api_base = "<API_BASE_URL>"
@token = "<API_TOKEN>"
@api_resource = Proxy::Ipam::ApiResource.new(api_base: @api_base, token: @token)
@ip_cache = Proxy::Ipam::IpCache.new(provider: '#{provider_name}')
end
def get_ipam_subnet(cidr, group_name = nil)
if group_name.nil?
get_ipam_subnet_by_cidr(cidr)
else
group = get_ipam_group(group_name)
get_ipam_subnet_by_group(cidr, group[:id])
end
end
def get_ipam_subnet_by_group(cidr, group_id)
# Should return nil if subnet not found, otherwise a hash with {:id, :subnet, :mask, :description}
end
def get_ipam_subnet_by_cidr(cidr)
# Should return nil if subnet not found, otherwise a hash with {:id, :subnet, :mask, :description}
end
def get_ipam_group(group_name)
# Should raise errors[:no_group] if group is not found, otherwise return a hash with {:id, :name, :description}
end
def get_ipam_groups
# Should return [] if no groups found, otherwise an array of group hashes with {:id, :name, :description}
end
def get_ipam_subnets(group_name)
# Should return nil if group not found or subnets not found, otherwise an array of subnet hashes with {:id, :subnet, :mask, :description}
end
def ip_exists?(ip, subnet_id, _group_name)
# Should return true or false
end
def add_ip_to_subnet(ip, params)
# Should return nil on success, otherwise hash with {:error}
end
def delete_ip_from_subnet(ip, params)
# Should return nil on success, otherwise hash with {:error}
end
def get_next_ip(mac, cidr, group_name)
# Should get next available ip from External IPAM, and should call cache_next_ip
# to handles the ip caching. Next IP should be returned in the "data" key of a hash
# ip = @api_resource.get("/provider/path/to/next_ip")
# next_ip = cache_next_ip(@ip_cache, ip, mac, cidr, subnet_id, group_name)
# { data: next_ip }
end
def groups_supported?
# Should return true or false, depending on whether groups are supported by the IPAM provider
end
def authenticated?
# Should return true or false, depending on whether the client is authenticated
end
end
end
}
provider_di = %{module Proxy::#{provider_name.capitalize}
class Plugin < ::Proxy::Provider
plugin :externalipam_#{provider_name}, Proxy::Ipam::VERSION
requires :externalipam, Proxy::Ipam::VERSION
validate :url, url: true
validate_presence :token
load_classes(proc do
require 'smart_proxy_ipam/#{provider_name}/#{provider_name}_client'
end)
load_dependency_injection_wirings(proc do |container_instance, settings|
container_instance.dependency :externalipam_client, -> { ::Proxy::#{provider_name.capitalize}::#{provider_name.capitalize}Client.new(settings) }
end)
end
end
}
provider_example_yml = %{---
:url: 'https://#{provider_name}.example.com'
:token: '#{provider_name}_api_token'
}
puts "\n==================================================="
puts "Creating files for new provider #{provider_name}"
puts "===================================================\n"
if provider_name.nil? || provider_name.empty?
raise 'ERROR: A provider name must be specified(bundle exec rake add_provider["providername"])\n\n'
end
has_special_chars = !provider_name.index(/[^[:alnum:]]/).nil?
has_numeric = provider_name.count('0-9').positive?
if has_special_chars || has_numeric
raise 'ERROR: Provider name cannot contain numbers or special characters'
end
if Dir.exist?(provider_dir)
raise "ERROR: Directory #{provider_dir} already exists!\n\n"
end
Dir.mkdir provider_dir
puts "Created: #{provider_dir}"
File.open(provider_client, 'w') { |f| f.write(provider_stubs) }
puts "Created: #{provider_client}"
File.open(provider_plugin, 'w') { |f| f.write(provider_di) }
puts "Created: #{provider_plugin}"
File.open(provider_example, 'w') { |f| f.write(provider_example_yml) }
puts "Created: #{provider_example}"
File.open('lib/smart_proxy_ipam.rb', 'a') do |f|
f.write("require 'smart_proxy_ipam/#{provider_name}/#{provider_name}_plugin'\n")
end
puts 'Updated: lib/smart_proxy_ipam.rb'
end