Browse Source

Working logs for IP allocations

Alexandre Aubin 7 years ago
parent
commit
04b196f930
1 changed files with 29 additions and 7 deletions
  1. 29 7
      coin/configuration/models.py

+ 29 - 7
coin/configuration/models.py

@@ -1,6 +1,8 @@
 # -*- coding: utf-8 -*-
 from __future__ import unicode_literals
 
+import logging
+
 from django.db import models
 from polymorphic import PolymorphicModel
 from coin.offers.models import OfferSubscription
@@ -94,10 +96,19 @@ def offer_subscription_event(sender, **kwargs):
                                 ip_pool=offer_ip_pool.ip_pool)
             config.save()
 
-subnet_log = logging.getLogger("coin.subnets")
 
 @receiver(post_save, sender=IPSubnet)
+def subnet_event_save(sender, **kwargs):
+    kwargs["signal_type"] = "save"
+    subnet_event(sender, **kwargs)
+
+
 @receiver(post_delete, sender=IPSubnet)
+def subnet_event_delete(sender, **kwargs):
+    kwargs["signal_type"] = "delete"
+    subnet_event(sender, **kwargs)
+
+subnet_log = logging.getLogger("coin.subnets")
 def subnet_event(sender, **kwargs):
     """Fires when a subnet is created, modified or deleted.  We tell the
     configuration backend to do whatever it needs to do with it.
@@ -131,11 +142,22 @@ def subnet_event(sender, **kwargs):
         config = subnet.configuration
         if hasattr(config, 'subnet_event'):
             config.subnet_event()
-        subnet_log.info("zob")
-        subnet_log.info(sender)
-        subnet_log.info(conf)
-        subnet_log.info(conf.offersubscription)
-        subnet_log.info(conf.offersubscription.member)
-        subnet_log.info(subnet.inet)
+
+        offer = config.offersubscription.offer
+        member = config.offersubscription.member
+        ip = subnet.inet
+
+        if kwargs['signal_type'] == "save":
+            msg = "Allocating IP %s to member %s (%s - %s %s) for offer %s"
+        elif kwargs['signal_type'] == "delete":
+            msg = "Deallocating IP %s from member %s (%s - %s %s) (was offer %s)"
+        else:
+            # Does not happens
+            msg = ""
+
+        subnet_log.info(msg % (ip, str(member.pk),
+                        member.username, member.first_name, member.last_name,
+                        offer.name))
+
     except ObjectDoesNotExist:
         pass