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): ...@@ -107,8 +107,9 @@ class Command(BaseCommand):
if port: if port:
self.validate_port(port) self.validate_port(port)
try: try:
rule = self.make_rule(port, proto, action, rule = self.make_rule(dport=port, proto=proto, action=action,
dir, owner, firewall, fnet) direction=dir, owner=owner,
firewall=firewall, foreign_network=fnet)
rule.save() rule.save()
except Warning as e: except Warning as e:
logger.warning(e) logger.warning(e)
...@@ -130,12 +131,11 @@ class Command(BaseCommand): ...@@ -130,12 +131,11 @@ class Command(BaseCommand):
Rule.objects.bulk_create(rules) 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, rule, created = Rule.objects.get_or_create(**kwargs)
firewall=firewall, foreign_network=fnet, owner=owner)
if self.is_exist(port, proto, action, dir, owner, firewall, fnet): if not created:
raise Warning(('Rule does exist: %s' % raise Warning(('Rule does exist: %s' %
unicode(rule)).encode('utf-8')) unicode(rule)).encode('utf-8'))
...@@ -143,17 +143,6 @@ class Command(BaseCommand): ...@@ -143,17 +143,6 @@ class Command(BaseCommand):
return rule 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): def validate_port(self, port):
if port < 0 or port > 65535: if port < 0 or port > 65535:
raise CommandError("Port '%i' not in range [0-65535]" % port) 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