dnsmanager/src/client/lib/dnsmanager-client.cr

106 lines
2.3 KiB
Crystal

require "../../requests/*"
class DNSManager::Client < IPC
property server_fd : Int32 = -1
def initialize
super()
fd = self.connect "dnsmanager"
if fd.nil?
raise "couldn't connect to 'auth' IPC service"
end
@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.
#
def login(token : String)
request = Request::Login.new token
send_now request
parse_message [ Response::Success ], read
end
# Adding a full zone.
def user_zone_add(zone : Storage::Zone)
request = Request::AddOrUpdateZone.new zone
send_now request
parse_message [ Response::Success ], read
end
# Removing a zone.
def user_zone_del(domain : String)
request = Request::DeleteZone.new domain
send_now request
parse_message [ Response::Success ], read
end
# Get a zone.
def user_zone_get(domain : String)
request = Request::GetZone.new domain
send_now request
parse_message [ Response::Zone ], read
end
# Add a RR.
def user_rr_add(domain : String, rr : Storage::Zone::ResourceRecord)
request = Request::AddRR.new domain, rr
send_now request
parse_message [ Response::Success ], read
end
# Update a RR.
def user_rr_update(domain : String, rr : Storage::Zone::ResourceRecord)
request = Request::UpdateRR.new domain, rr
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
#
# Admin stuff.
#
def admin_maintenance(key : String, subject : Request::Maintenance::Subject, value : Int32? = nil)
request = Request::Maintenance.new(key,subject)
if value
request.value = value
end
send_now request
parse_message [ Response::Success ], read
end
def send_now(msg : IPC::JSON)
m = IPCMessage::TypedMessage.new msg.type.to_u8, msg.to_json
write @server_fd, m
end
def send_now(type : Request::Type, payload)
m = IPCMessage::TypedMessage.new type.value.to_u8, payload
write @server_fd, m
end
def read
slice = self.read @server_fd
m = IPCMessage::TypedMessage.deserialize slice
m.not_nil!
end
end