Browse Source

Add admin actions to generate IP endpoints for VPN

Baptiste Jonglez 11 years ago
parent
commit
f5c7f1e808
2 changed files with 30 additions and 1 deletions
  1. 24 1
      coin/vpn/admin.py
  2. 6 0
      coin/vpn/models.py

+ 24 - 1
coin/vpn/admin.py

@@ -12,7 +12,8 @@ class VPNAdmin(admin.ModelAdmin):
                      'administrative_subscription__member__first_name',
                      'administrative_subscription__member__last_name',
                      'administrative_subscription__member__email')
-
+    actions = ("generate_endpoints", "generate_endpoints_v4",
+               "generate_endpoints_v6")
 
     def get_readonly_fields(self, request, obj=None):
         if obj:
@@ -20,4 +21,26 @@ class VPNAdmin(admin.ModelAdmin):
         else:
             return []
 
+    def generate_endpoints_generic(self, request, queryset, v4=True, v6=True):
+        count = 0
+        for vpn in queryset:
+            if vpn.generate_endpoints(v4, v6):
+                vpn.full_clean()
+                vpn.save()
+                count += 1
+        msg = "{} VPN subscription(s) updated.".format(count)
+        self.message_user(request, msg)
+
+    def generate_endpoints(self, request, queryset):
+        self.generate_endpoints_generic(request, queryset)
+    generate_endpoints.short_description = "Generate IPv4 and IPv6 endpoints"
+
+    def generate_endpoints_v4(self, request, queryset):
+        self.generate_endpoints_generic(request, queryset, v6=False)
+    generate_endpoints_v4.short_description = "Generate IPv4 endpoints"
+
+    def generate_endpoints_v6(self, request, queryset):
+        self.generate_endpoints_generic(request, queryset, v4=False)
+    generate_endpoints_v6.short_description = "Generate IPv6 endpoints"
+
 admin.site.register(VPNSubscription, VPNAdmin)

+ 6 - 0
coin/vpn/models.py

@@ -94,14 +94,18 @@ class VPNSubscription(CoinLdapSyncModel):
         is generated for this address family.  If there already is an
         endpoint, do nothing.
 
+        Returns True if an endpoint was generated.
+
         TODO: this should be factored for other technologies (DSL, etc)
 
         """
         subnets = self.administrative_subscription.ip_subnet.all()
+        updated = False
         if v4 and self.ipv4_endpoint is None:
             subnets_v4 = [s for s in subnets if s.inet.version == 4]
             if len(subnets_v4) > 0:
                 self.ipv4_endpoint = subnets_v4[0].inet.ip
+                updated = True
         if v6 and self.ipv6_endpoint is None:
             subnets_v6 = [s for s in subnets if s.inet.version == 6]
             if len(subnets_v6) > 0:
@@ -109,6 +113,8 @@ class VPNSubscription(CoinLdapSyncModel):
                 gen = subnets_v6[0].inet.iter_hosts()
                 gen.next()
                 self.ipv6_endpoint = gen.next()
+                updated = True
+        return updated
 
     def check_endpoints(self):
         """Check that the IP endpoints are included in one of the attributed IP