-
- def __init__(self, chain, target, src=None, dst=None, protocol=None,
- src_port=None, dst_port=None):
- r"Initialize object, see class documentation for details."
- self.chain = chain
- self.target = target
- self.src = src
- self.dst = dst
- self.protocol = protocol
- # TODO Validate that src_port and dst_port could be not None only
- # if the protocol is UDP or TCP
- self.src_port = src_port
- self.dst_port = dst_port
-
- def update(self, chain=None, target=None, src=None, dst=None, protocol=None,
- src_port=None, dst_port=None):
- r"update([chain[, ...]]) -> Update the values of a rule (see Rule doc)."
- if chain is not None: self.chain = chain
- if target is not None: self.target = target
- if src is not None: self.src = src
- if dst is not None: self.dst = dst
- if protocol is not None: self.protocol = protocol
- # TODO Validate that src_port and dst_port could be not None only
- # if the protocol is UDP or TCP
- if src_port is not None: self.src_port = src_port
- if dst_port is not None: self.dst_port = dst_port
-
- def __cmp__(self, other):
- r"Compares two Rule objects."
- if self.chain == other.chain \
- and self.target == other.target \
- and self.src == other.src \
- and self.dst == other.dst \
- and self.protocol == other.protocol \
- and self.src_port == other.src_port \
- and self.dst_port == other.dst_port:
- return 0
- return cmp(id(self), id(other))
-
- def as_tuple(self):
- r"Return a tuple representing the rule."
- return (self.chain, self.target, self.src, self.dst, self.protocol,
- self.src_port, self.dst_port)
-
-class RuleHandler(Handler):
- r"""RuleHandler(rules) -> RuleHandler instance :: Handle a list of rules.
+ chain = Field(UpOneOf(['INPUT', 'OUTPUT', 'FORWARD'], not_empty=True))
+ target = Field(UpOneOf(['ACCEPT', 'REJECT', 'DROP'], not_empty=True))
+ src = Field(CIDR(if_empty=None, if_missing=None))
+ dst = Field(CIDR(if_empty=None, if_missing=None))
+ protocol = Field(UpOneOf(['ICMP', 'UDP', 'TCP', 'ALL'], if_missing=None))
+ src_port = Field(Int(min=0, max=65535, if_empty=None, if_missing=None))
+ dst_port = Field(Int(min=0, max=65535, if_empty=None, if_missing=None))
+ def chained_validator(self, fields, state):
+ errors = dict()
+ if fields['protocol'] not in ('TCP', 'UDP'):
+ for name in ('src_port', 'dst_port'):
+ if fields[name] is not None:
+ errors[name] = u"Should be None if protocol " \
+ "(%(protocol)s) is not TCP or UDP" % fields
+ if errors:
+ raise Invalid(u"You can't specify any ports if the protocol "
+ u'is not TCP or UDP', fields, state, error_dict=errors)
+
+class RuleHandler(ListSubHandler):
+ r"""RuleHandler(parent) -> RuleHandler instance :: Handle a list of rules.