from pymin.seqtools import Sequence
from pymin.dispatcher import Handler, handler, HandlerError
from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \
- TransactionalHandler, ParametersHandler
+ TransactionalHandler, ParametersHandler, \
+ DictSubHandler
-__ALL__ = ('DhcpHandler', 'Error', 'HostError', 'HostAlreadyExistsError',
- 'HostNotFoundError')
+__ALL__ = ('DhcpHandler', 'Error')
class Error(HandlerError):
r"""
"""
pass
-class HostError(Error, KeyError):
- r"""
- HostError(hostname) -> HostError instance
-
- This is the base exception for all host related errors.
- """
-
- def __init__(self, hostname):
- r"Initialize the object. See class documentation for more info."
- self.message = u'Host error: "%s"' % hostname
-
-class HostAlreadyExistsError(HostError):
- r"""
- HostAlreadyExistsError(hostname) -> HostAlreadyExistsError instance
-
- This exception is raised when trying to add a hostname that already exists.
- """
-
- def __init__(self, hostname):
- r"Initialize the object. See class documentation for more info."
- self.message = u'Host already exists: "%s"' % hostname
-
-class HostNotFoundError(HostError):
- r"""
- HostNotFoundError(hostname) -> HostNotFoundError instance
-
- This exception is raised when trying to operate on a hostname that doesn't
- exists.
- """
-
- def __init__(self, hostname):
- r"Initialize the object. See class documentation for more info."
- self.message = u'Host not found: "%s"' % hostname
-
-
class Host(Sequence):
r"""Host(name, ip, mac) -> Host instance :: Class representing a host.
r"Return a tuple representing the host."
return (self.name, self.ip, self.mac)
-class HostHandler(Handler):
- r"""HostHandler(hosts) -> HostHandler instance :: Handle a list of hosts.
+ def update(self, ip=None, mac=None):
+ if ip is not None:
+ self.ip = ip
+ if mac is not None:
+ self.mac = mac
+
+class HostHandler(DictSubHandler):
+ r"""HostHandler(parent) -> HostHandler instance :: Handle a list of hosts.
This class is a helper for DhcpHandler to do all the work related to hosts
administration.
-
- hosts - A dictionary with string keys (hostnames) and Host instances values.
"""
handler_help = u"Manage DHCP hosts"
- def __init__(self, parent):
- r"Initialize HostHandler object, see class documentation for details."
- self.parent = parent
-
- @handler(u'Add a new host')
- def add(self, name, ip, mac):
- r"add(name, ip, mac) -> None :: Add a host to the hosts list."
- if name in self.parent.hosts:
- raise HostAlreadyExistsError(name)
- self.parent.hosts[name] = Host(name, ip, mac)
-
- @handler(u'Update a host')
- def update(self, name, ip=None, mac=None):
- r"update(name[, ip[, mac]]) -> None :: Update a host of the hosts list."
- if not name in self.parent.hosts:
- raise HostNotFoundError(name)
- if ip is not None:
- self.parent.hosts[name].ip = ip
- if mac is not None:
- self.parent.hosts[name].mac = mac
-
- @handler(u'Delete a host')
- def delete(self, name):
- r"delete(name) -> None :: Delete a host of the hosts list."
- if not name in self.parent.hosts:
- raise HostNotFoundError(name)
- del self.parent.hosts[name]
-
- @handler(u'Get information about a host')
- def get(self, name):
- r"get(name) -> Host :: List all the information of a host."
- if not name in self.parent.hosts:
- raise HostNotFoundError(name)
- return self.parent.hosts[name]
-
- @handler(u'List hosts')
- def list(self):
- r"list() -> tuple :: List all the hostnames."
- return self.parent.hosts.keys()
-
- @handler(u'Get information about all hosts')
- def show(self):
- r"show() -> list of Hosts :: List all the complete hosts information."
- return self.parent.hosts.values()
-
+ _dict_subhandler_attr = 'hosts'
+ _dict_subhandler_class = Host
class DhcpHandler(Restorable, ConfigWriter, InitdHandler, TransactionalHandler,
ParametersHandler):
from pymin.seqtools import Sequence
from pymin.dispatcher import handler, HandlerError, Handler
from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \
- TransactionalHandler, ParametersHandler, call
+ TransactionalHandler, ParametersHandler, \
+ SubHandler, call
__ALL__ = ('DnsHandler', 'Error',
'ZoneError', 'ZoneNotFoundError', 'ZoneAlreadyExistsError',
def as_tuple(self):
return (self.name, self.ip)
-class HostHandler(Handler):
+class HostHandler(SubHandler):
handler_help = u"Manage DNS hosts"
- def __init__(self, parent):
- self.parent = parent
-
@handler(u'Adds a host to a zone')
def add(self, name, hostname, ip):
if not name in self.parent.zones:
def as_tuple(self):
return (self.mx, self.prio)
-class MailExchangeHandler(Handler):
+class MailExchangeHandler(SubHandler):
handler_help = u"Manage DNS mail exchangers (MX)"
- def __init__(self, parent):
- self.parent = parent
-
@handler(u'Adds a mail exchange to a zone')
def add(self, zonename, mx, prio):
if not zonename in self.parent.zones:
def as_tuple(self):
return (self.name)
-class NameServerHandler(Handler):
+class NameServerHandler(SubHandler):
handler_help = u"Manage DNS name servers (NS)"
- def __init__(self, parent):
- self.parent = parent
-
@handler(u'Adds a name server to a zone')
def add(self, zone, ns):
if not zone in self.parent.zones:
def as_tuple(self):
return (self.name, self.hosts, self.mxs, self.nss)
-class ZoneHandler(Handler):
+class ZoneHandler(SubHandler):
r"""ZoneHandler(parent.zones) -> ZoneHandler instance :: Handle a list of zones.
This class is a helper for DnsHandler to do all the work related to zone
handler_help = u"Manage DNS zones"
- def __init__(self, parent):
- self.parent = parent
-
@handler(u'Adds a zone')
def add(self, name):
if name in self.parent.zones:
self.parent.zones[name].mod = True
self.parent.zones[name].new = True
-
@handler(u'Deletes a zone')
def delete(self, name):
r"delete(name) -> None :: Delete a zone from the zone list."
from pymin.seqtools import Sequence
from pymin.dispatcher import Handler, handler, HandlerError
from pymin.services.util import Restorable, ConfigWriter, ServiceHandler, \
- TransactionalHandler
+ TransactionalHandler, SubHandler
__ALL__ = ('FirewallHandler', 'Error', 'RuleError', 'RuleAlreadyExistsError',
'RuleNotFoundError')
return (self.chain, self.target, self.src, self.dst, self.protocol,
self.src_port, self.dst_port)
-class RuleHandler(Handler):
+class RuleHandler(SubHandler):
r"""RuleHandler(rules) -> RuleHandler instance :: Handle a list of rules.
This class is a helper for FirewallHandler to do all the work related to rules
handler_help = u"Manage firewall rules"
- def __init__(self, parent):
- r"Initialize the object, see class documentation for details."
- self.parent = parent
-
@handler(u'Add a new rule')
def add(self, *args, **kwargs):
r"add(rule) -> None :: Add a rule to the rules list (see Rule doc)."
from pymin.seqtools import Sequence
from pymin.dispatcher import handler, HandlerError, Handler
from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \
- TransactionalHandler, call
+ TransactionalHandler, SubHandler, call
__ALL__ = ('IpHandler', 'Error','DeviceError', 'DeviceNotFoundError',
'RouteError', 'RouteNotFoundError', 'RouteAlreadyExistsError',
return 0
return cmp(id(self), id(other))
-class RouteHandler(Handler):
+class RouteHandler(SubHandler):
handler_help = u"Manage IP routes"
- def __init__(self, parent):
- self.parent = parent
-
@handler(u'Adds a route to a device')
def add(self, device, net_addr, prefix, gateway):
if not device in self.parent.devices:
def as_tuple(self):
return (self.ip, self.prefix, self.broadcast)
-class AddressHandler(Handler):
+class AddressHandler(SubHandler):
handler_help = u"Manage IP addresses"
- def __init__(self, parent):
- self.parent = parent
-
@handler(u'Adds an address to a device')
def add(self, device, ip, prefix, broadcast='+'):
if not device in self.parent.devices:
def as_tuple(self):
return (self.name, self.mac)
-class DeviceHandler(Handler):
+class DeviceHandler(SubHandler):
handler_help = u"Manage network devices"
from pymin.seqtools import Sequence
from pymin.dispatcher import Handler, handler, HandlerError
from pymin.services.util import Restorable, ConfigWriter, InitdHandler, \
- TransactionalHandler, ParametersHandler
+ TransactionalHandler, ParametersHandler, \
+ DictSubHandler
import crypt
-__ALL__ = ('ProxyHandler', 'Error', 'HostError', 'HostAlreadyExistsError',
- 'HostNotFoundError')
+__ALL__ = ('ProxyHandler', 'Error')
class Error(HandlerError):
r"""
"""
pass
-class HostError(Error, KeyError):
- r"""
- HostError(hostname) -> HostError instance
-
- This is the base exception for all host related errors.
- """
-
- def __init__(self, hostname):
- r"Initialize the object. See class documentation for more info."
- self.message = u'Host error: "%s"' % hostname
-
-class HostAlreadyExistsError(HostError):
- r"""
- HostAlreadyExistsError(hostname) -> HostAlreadyExistsError instance
-
- This exception is raised when trying to add a hostname that already exists.
- """
-
- def __init__(self, hostname):
- r"Initialize the object. See class documentation for more info."
- self.message = u'Host already exists: "%s"' % hostname
-
-class HostNotFoundError(HostError):
- r"""
- HostNotFoundError(hostname) -> HostNotFoundError instance
-
- This exception is raised when trying to operate on a hostname that doesn't
- exists.
- """
+class Host(Sequence):
+ def __init__(self,ip):
+ self.ip = ip
+ def as_tuple(self):
+ return (self.ip,)
- def __init__(self, hostname):
- r"Initialize the object. See class documentation for more info."
- self.message = u'Host not found: "%s"' % hostname
+# TODO convert to a SetSubHandler
+class HostHandler(DictSubHandler):
-class Host(Sequence):
+ handler_help = u"Manage proxy hosts"
- def __init__(self,ip):
- self.ip = ip
+ _dict_subhandler_attr = 'hosts'
+ _dict_subhandler_class = Host
+class User(Sequence):
+ def __init__(self, name, password):
+ self.name = name
+ self.password = crypt.crypt(password,'BA')
def as_tuple(self):
- return (self.ip)
+ return (self.name, self.password)
+ def update(self, password=None):
+ if password is not None:
+ self.password = crypt.crypt(password,'BA')
-class HostHandler(Handler):
+class UserHandler(DictSubHandler):
- handler_help = u"Manage proxy hosts"
+ handler_help = u"Manage proxy users"
- def __init__(self, parent):
- self.parent = parent
-
- @handler(u'Adds a host')
- def add(self, ip):
- if ip in self.parent.hosts:
- raise HostAlreadyExistsError(ip)
- self.parent.hosts[ip] = Host(ip)
-
- @handler(u'Deletes a host')
- def delete(self, ip):
- if not ip in self.parent.hosts:
- raise HostNotFoundError(ip)
- del self.parent.hosts[ip]
-
- @handler(u'Shows all hosts')
- def list(self):
- return self.parent.hosts.keys()
-
- @handler(u'Get information about all hosts')
- def show(self):
- return self.parent.hosts.items()
-
-
-class UserHandler(Handler):
-
- def __init__(self, parent):
- self.parent = parent
-
- @handler('Adds a user')
- def add(self, user, password):
- if user in self.parent.users:
- raise UserAlreadyExistsError(user)
- self.parent.users[user] = crypt.crypt(password,'BA')
-
- @handler('Deletes a user')
- def delete(self, user):
- if not user in self.parent.users:
- raise UserNotFound(user)
- del self.parent.users[user]
+ _dict_subhandler_attr = 'users'
+ _dict_subhandler_class = User
class ProxyHandler(Restorable, ConfigWriter, InitdHandler,
TransactionalHandler, ParametersHandler):