New/del domain + client API changes.

This commit is contained in:
Philippe Pittoli 2023-06-27 13:00:26 +02:00
parent f0fdf75251
commit a4ee0acf0e
5 changed files with 126 additions and 61 deletions

View File

@ -23,12 +23,15 @@ VERBOSITY ?= 4
run-client-verbosity:
@$(LD_P) ./bin/dnsmanager-client admin maintenance verbosity $(VERBOSITY) $(LOGIN_OPT)
run-client-domain-add:
@$(LD_P) ./bin/dnsmanager-client user domain add $(DOMAIN) $(LOGIN_OPT)
run-client-domain-del:
@$(LD_P) ./bin/dnsmanager-client user domain del $(DOMAIN) $(LOGIN_OPT)
run-client-zone-add:
@$(LD_P) ./bin/dnsmanager-client user zone add $(DOMAIN).json $(LOGIN_OPT)
run-client-zone-del:
@$(LD_P) ./bin/dnsmanager-client user zone del $(DOMAIN) $(LOGIN_OPT)
RRID ?= 1
NAME ?=
TTL ?= 3600

View File

@ -12,16 +12,6 @@ class DNSManager::Client < IPC
@server_fd = fd
end
# TODO: parse_message should raise exception if response not anticipated
def parse_message(expected_messages, message)
em = Array(IPC::JSON.class).new
expected_messages.each do |e|
em << e
end
em << Response::Error
em.parse_ipc_json message
end
#
# Simple users.
#
@ -32,16 +22,38 @@ class DNSManager::Client < IPC
parse_message [ Response::Success ], read
end
# Adding a full zone.
def user_zone_add(zone : Storage::Zone)
request = Request::AddOrUpdateZone.new zone
#
# Domain operations
#
# Add a domain.
def user_domain_add(domain : String)
request = Request::NewDomain.new domain
send_now request
parse_message [ Response::Success ], read
end
# Removing a zone.
def user_zone_del(domain : String)
request = Request::DeleteZone.new domain
# Remove a domain.
def user_domain_del(domain : String)
request = Request::DeleteDomain.new domain
send_now request
parse_message [ Response::Success ], read
end
# Get user domain list.
def user_domain_list()
request = Request::UserDomains.new
send_now request
parse_message [ Response::DomainList ], read
end
#
# Zone operations
#
# Add a full zone.
def user_zone_add(zone : Storage::Zone)
request = Request::AddOrUpdateZone.new zone
send_now request
parse_message [ Response::Success ], read
end
@ -53,6 +65,10 @@ class DNSManager::Client < IPC
parse_message [ Response::Zone ], read
end
#
# Resource Record operations
#
# Add a RR.
def user_rr_add(domain : String, rr : Storage::Zone::ResourceRecord)
request = Request::AddRR.new domain, rr
@ -74,15 +90,8 @@ class DNSManager::Client < IPC
parse_message [ Response::Success ], read
end
# Get user domain list.
def user_domain_list()
request = Request::UserDomains.new
send_now request
parse_message [ Response::DomainList ], read
end
#
# Admin stuff.
# Admin stuff
#
def admin_maintenance(subject : Request::Maintenance::Subject, value : Int32? = nil)
@ -94,6 +103,10 @@ class DNSManager::Client < IPC
parse_message [ Response::Success ], read
end
#
# Utils
#
def send_now(msg : IPC::JSON)
m = IPCMessage::TypedMessage.new msg.type.to_u8, msg.to_json
write @server_fd, m
@ -109,4 +122,14 @@ class DNSManager::Client < IPC
m = IPCMessage::TypedMessage.deserialize slice
m.not_nil!
end
# TODO: parse_message should raise exception if response not anticipated
def parse_message(expected_messages, message)
em = Array(IPC::JSON.class).new
expected_messages.each do |e|
em << e
end
em << Response::Error
em.parse_ipc_json message
end
end

View File

@ -35,17 +35,19 @@ class Actions
# Maintenance.
@the_call["admin-maintenance"] = ->admin_maintenance
# Domain operations.
@the_call["user-domain-add"] = ->user_domain_add
@the_call["user-domain-del"] = ->user_domain_del
@the_call["user-domain-list"] = ->user_domain_list
# Zone operations.
@the_call["user-zone-add"] = ->user_zone_add
@the_call["user-zone-del"] = ->user_zone_del
@the_call["user-zone-get"] = ->user_zone_get
# Zone RR operations.
@the_call["user-rr-add-a"] = ->user_rr_add_a
@the_call["user-rr-update-a"] = ->user_rr_update_a
@the_call["user-rr-del"] = ->user_rr_del
@the_call["user-domain-list"] = ->user_domain_list
end
def admin_maintenance
@ -75,6 +77,30 @@ class Actions
end
end
def user_domain_add
domains = Context.args.not_nil!
domains.each do |domain|
begin
pp! domain
pp! @dnsmanagerd.user_domain_add domain
rescue e
puts "error for user_domain_add: #{e.message}"
end
end
end
def user_domain_del
domains = Context.args.not_nil!
domains.each do |domain|
begin
pp! domain
pp! @dnsmanagerd.user_domain_del domain
rescue e
puts "error for user_domain_del: #{e.message}"
end
end
end
def user_zone_add
zones = read_zones
zones.each do |zone|
@ -87,18 +113,6 @@ class Actions
end
end
def user_zone_del
domains = Context.args.not_nil!
domains.each do |domain|
begin
pp! domain
pp! @dnsmanagerd.user_zone_del domain
rescue e
puts "error for user_zone_del: #{e.message}"
end
end
end
def user_zone_get
domains = Context.args.not_nil!
domains.each do |domain|

View File

@ -103,6 +103,31 @@ def parsing_cli(authd_config : Baguette::Configuration::Auth)
# User section.
parser.on "user", "Simple user operations." do
parser.banner = "Operations as a simple user."
# Domain.
parser.on("domain", "Domain operations.") do
parser.on("add", "Add a new domain.") do
Baguette::Log.info "add domain."
Context.command = "user-domain-add"
parser.banner = "COMMAND: user domain add domain [domain...]"
unrecognized_args_to_context_args.call parser, nil, 1
end
parser.on("del", "Delete a domain.") do
Baguette::Log.info "del domain."
Context.command = "user-domain-del"
parser.banner = "COMMAND: user domain del domain [domain...]"
unrecognized_args_to_context_args.call parser, nil, 1
end
parser.on("list", "List all domains.") do
Baguette::Log.info "list domains."
Context.command = "user-domain-list"
parser.banner = "COMMAND: user domain list"
unrecognized_args_to_context_args.call parser, 0, nil
end
end
# Zone.
parser.on("zone", "Zone operations.") do
parser.on("add", "Add new zone.") do
@ -112,9 +137,9 @@ def parsing_cli(authd_config : Baguette::Configuration::Auth)
unrecognized_args_to_context_args.call parser, nil, 1
end
parser.on("del", "Delete a zone.") do
parser.on("del", "Delete a zone (alias to domain del).") do
Baguette::Log.info "del zone."
Context.command = "user-zone-del"
Context.command = "user-domain-del"
parser.banner = "COMMAND: user zone del domain [domain...]"
unrecognized_args_to_context_args.call parser, nil, 1
end
@ -126,8 +151,8 @@ def parsing_cli(authd_config : Baguette::Configuration::Auth)
unrecognized_args_to_context_args.call parser, nil, 1
end
parser.on("list", "List all domains.") do
Baguette::Log.info "List domains."
parser.on("list", "List all domains (alias to domain list).") do
Baguette::Log.info "list domains."
Context.command = "user-domain-list"
parser.banner = "COMMAND: user zone list"
unrecognized_args_to_context_args.call parser, 0, nil

View File

@ -17,7 +17,21 @@ class DNSManager::Request
end
DNSManager.requests << NewDomain
IPC::JSON.message AddOrUpdateZone, 10 do
IPC::JSON.message DeleteDomain, 10 do
property domain : String
def initialize(@domain)
end
def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) : IPC::JSON
user = dnsmanagerd.get_logged_user event
return Response::ErrorUserNotLogged.new unless user
dnsmanagerd.storage.delete_domain user.uid, @domain
end
end
DNSManager.requests << DeleteDomain
IPC::JSON.message AddOrUpdateZone, 11 do
property zone : DNSManager::Storage::Zone
def initialize(@zone)
@ -31,20 +45,6 @@ class DNSManager::Request
end
DNSManager.requests << AddOrUpdateZone
IPC::JSON.message DeleteZone, 11 do
property domain : String
def initialize(@domain)
end
def handle(dnsmanagerd : DNSManager::Service, event : IPC::Event) : IPC::JSON
user = dnsmanagerd.get_logged_user event
return Response::ErrorUserNotLogged.new unless user
dnsmanagerd.storage.delete_domain user.uid, @domain
end
end
DNSManager.requests << DeleteZone
IPC::JSON.message GetZone, 12 do
property domain : String