Commit c7fd2925 by Czémán Arnold

firewall: rework make_rule method in add_rule command

parent 3b75446c
......@@ -107,8 +107,9 @@ class Command(BaseCommand):
if port:
self.validate_port(port)
try:
rule = self.make_rule(port, proto, action,
dir, owner, firewall, fnet)
rule = self.make_rule(dport=port, proto=proto, action=action,
direction=dir, owner=owner,
firewall=firewall, foreign_network=fnet)
rule.save()
except Warning as e:
logger.warning(e)
......@@ -130,12 +131,11 @@ class Command(BaseCommand):
Rule.objects.bulk_create(rules)
def make_rule(self, port, proto, action, dir, owner, firewall, fnet):
def make_rule(self, **kwargs):
rule = Rule(direction=dir, dport=port, proto=proto, action=action,
firewall=firewall, foreign_network=fnet, owner=owner)
rule, created = Rule.objects.get_or_create(**kwargs)
if self.is_exist(port, proto, action, dir, owner, firewall, fnet):
if not created:
raise Warning(('Rule does exist: %s' %
unicode(rule)).encode('utf-8'))
......@@ -143,17 +143,6 @@ class Command(BaseCommand):
return rule
def is_exist(self, port, proto, action, dir, owner, firewall, fnet):
rules = Rule.objects.filter(direction=dir,
dport=port,
proto=proto,
action=action,
firewall=firewall,
foreign_network=fnet,
owner=owner)
return rules.exists()
def validate_port(self, port):
if port < 0 or port > 65535:
raise CommandError("Port '%i' not in range [0-65535]" % port)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment