Project

General

Profile

Download (3.85 KB) Statistics
| Branch: | Tag: | Revision:
038fa6cf Dmitri Dolguikh
class Proxy::DhcpApi < ::Sinatra::Base
helpers ::Proxy::Helpers
0c9a01b6 Dominic Cleal
authorize_with_trusted_hosts
0207401d Lukas Zapletal
authorize_with_ssl_client
b35ab467 Ohad Levy
use Rack::MethodOverride
038fa6cf Dmitri Dolguikh
before do
begin
raise "Smart Proxy is not configured to support DHCP" unless Proxy::DhcpPlugin.settings.enabled
case Proxy::DhcpPlugin.settings.dhcp_vendor.downcase
when "isc"
require 'dhcp/providers/server/isc'
25691fff Dmitri Dolguikh
unless Proxy::DhcpPlugin.settings.dhcp_config && Proxy::DhcpPlugin.settings.dhcp_leases \
&& File.exist?(Proxy::DhcpPlugin.settings.dhcp_config) && File.exist?(Proxy::DhcpPlugin.settings.dhcp_leases)
038fa6cf Dmitri Dolguikh
log_halt 400, "Unable to find the DHCP configuration or lease files"
end
77e0262c Lukas Zapletal
@server = Proxy::DHCP::ISC.new(:name => Proxy::DhcpPlugin.settings.dhcp_server,
25691fff Dmitri Dolguikh
:config => Proxy::DhcpPlugin.settings.dhcp_config,
:leases => Proxy::DhcpPlugin.settings.dhcp_leases)
038fa6cf Dmitri Dolguikh
when "native_ms"
require 'dhcp/providers/server/native_ms'
@server = Proxy::DHCP::NativeMS.new(:server => Proxy::DhcpPlugin.settings.dhcp_server ? Proxy::DhcpPlugin.settings.dhcp_server : "127.0.0.1")
when "virsh"
require 'dhcp/providers/server/virsh'
@server = Proxy::DHCP::Virsh.new(:virsh_network => Proxy::SETTINGS.virsh_network)
else
log_halt 400, "Unrecognized or missing DHCP vendor type: #{Proxy::DhcpPlugin.settings.dhcp_vendor.nil? ? "MISSING" : Proxy::DhcpPlugin.settings.dhcp_vendor}"
bba6b24b Ohad Levy
end
038fa6cf Dmitri Dolguikh
@subnets = @server.subnets
rescue => e
log_halt 400, e
bba6b24b Ohad Levy
end
46d028cd Ohad Levy
end

bba6b24b Ohad Levy
helpers do
def load_subnet
@subnet = @server.find_subnet(params[:network])
log_halt 404, "Subnet #{params[:network]} not found" unless @subnet
@subnet
end
46d028cd Ohad Levy
end
2b9164ca Ohad Levy
99339284 Dmitri Dolguikh
get "/?" do
bba6b24b Ohad Levy
begin
e21f675c Lukas Zapletal
if request.accept? 'application/json'
bba6b24b Ohad Levy
content_type :json
2b9164ca Ohad Levy
bba6b24b Ohad Levy
log_halt 404, "No subnets found on server @{name}" unless @subnets
@subnets.map{|s| {:network => s.network, :netmask => s.netmask }}.to_json
else
erb :"dhcp/index"
end
rescue => e
fcc0d38b Ohad Levy
log_halt 400, e
46d028cd Ohad Levy
end
end
2b9164ca Ohad Levy
038fa6cf Dmitri Dolguikh
get "/:network" do
bba6b24b Ohad Levy
begin
load_subnet
e21f675c Lukas Zapletal
if request.accept? 'application/json'
3929e459 Ohad Levy
content_type :json
{:reservations => @subnet.reservations, :leases => @subnet.leases}.to_json
else
erb :"dhcp/show"
end
bba6b24b Ohad Levy
rescue => e
fcc0d38b Ohad Levy
log_halt 400, e
bba6b24b Ohad Levy
end
46d028cd Ohad Levy
end
2b9164ca Ohad Levy
038fa6cf Dmitri Dolguikh
get "/:network/unused_ip" do
bba6b24b Ohad Levy
begin
content_type :json
a3ab6426 Ohad Levy
({:ip => load_subnet.unused_ip(:from => params[:from], :to => params[:to], :mac => params[:mac])}).to_json
bba6b24b Ohad Levy
rescue => e
fcc0d38b Ohad Levy
log_halt 400, e
bba6b24b Ohad Levy
end
46d028cd Ohad Levy
end
2b9164ca Ohad Levy
038fa6cf Dmitri Dolguikh
get "/:network/:record" do
bba6b24b Ohad Levy
begin
content_type :json
fa3178a0 Klaas Demter
record = @server.find_record(params[:record])
bba6b24b Ohad Levy
log_halt 404, "Record #{params[:network]}/#{params[:record]} not found" unless record
record.options.to_json
rescue => e
fcc0d38b Ohad Levy
log_halt 400, e
bba6b24b Ohad Levy
end
46d028cd Ohad Levy
end
2b9164ca Ohad Levy
bba6b24b Ohad Levy
# create a new record in a network
038fa6cf Dmitri Dolguikh
post "/:network" do
bba6b24b Ohad Levy
begin
50e38ad1 Ohad Levy
content_type :json
f2248acc Ohad Levy
@server.addRecord(params)
b1b32f74 Paul Kelly
rescue Proxy::DHCP::Collision => e
fcc0d38b Ohad Levy
log_halt 409, e
25691fff Dmitri Dolguikh
rescue Proxy::DHCP::AlreadyExists # rubocop:disable Lint/HandleExceptions
f0e9b8bf Ohad Levy
# no need to do anything
bba6b24b Ohad Levy
rescue => e
fcc0d38b Ohad Levy
log_halt 400, e
50e38ad1 Ohad Levy
end
bba6b24b Ohad Levy
end
50e38ad1 Ohad Levy
bba6b24b Ohad Levy
# delete a record from a network
038fa6cf Dmitri Dolguikh
delete "/:network/:record" do
bba6b24b Ohad Levy
begin
ad2b4651 Greg Sutcliffe
record = load_subnet.reservation_for(params[:record])
bba6b24b Ohad Levy
log_halt 404, "Record #{params[:network]}/#{params[:record]} not found" unless record
@server.delRecord @subnet, record
e21f675c Lukas Zapletal
if request.accept? 'application/json'
bba6b24b Ohad Levy
content_type :json
a551cc19 Paul Kelly
{}
bba6b24b Ohad Levy
else
redirect "/dhcp/#{params[:network]}"
end
67bfd9a0 David Swift
rescue Proxy::DHCP::InvalidRecord
log_halt 404, "Record #{params[:network]}/#{params[:record]} not found"
bba6b24b Ohad Levy
rescue Exception => e
fcc0d38b Ohad Levy
log_halt 400, e
bba6b24b Ohad Levy
end
2b9164ca Ohad Levy
end
end