]> git.llucax.com Git - software/pymin.git/blob - services/qos/host.py
Add validation to vpn service (refs #20)
[software/pymin.git] / services / qos / host.py
1 # vim: set encoding=utf-8 et sw=4 sts=4 :
2
3 from pymin.seqtools import Sequence
4 from pymin.dispatcher import handler
5 from pymin.service.util import SubHandler, ContainerNotFoundError, \
6                                ItemAlreadyExistsError
7
8 __all__ = ('HostHandler',)
9
10
11 class Host(Sequence):
12
13     def __init__(self, ip):
14         self.ip = ip
15
16     def as_tuple(self):
17         return (self.ip)
18
19     def __cmp__(self, other):
20         if self.ip == other.ip:
21             return 0
22         return cmp(id(self), id(other))
23
24
25 class HostHandler(SubHandler):
26
27     def __init__(self, parent):
28         self.parent = parent
29
30     @handler('Adds a host to a class : add <device> <class id> <ip>')
31     def add(self, dev, cid, ip):
32         if not dev in self.parent.devices:
33             raise ContainerNotFoundError(dev)
34
35         if not cid in self.parent.devices[dev].classes:
36             raise ContainerNotFoundError(cid)
37
38         try:
39             self.parent.devices[dev].classes[cid].hosts[ip] = Host(ip)
40         except ValueError:
41             raise ItemAlreadyExistsError(h  + ' -> ' + dev)
42
43     @handler(u'Lists hosts : list <dev> <class id>')
44     def list(self, dev, cid):
45         try:
46             k = self.parent.devices[dev].classes[cid].hosts.keys()
47         except KeyError:
48             k = dict()
49         return k
50