import network model admins from plstackapi-network branch
diff --git a/planetstack/core/admin.py b/planetstack/core/admin.py
index a1a21d6..73fd3db 100644
--- a/planetstack/core/admin.py
+++ b/planetstack/core/admin.py
@@ -39,12 +39,62 @@
exclude = ['enacted']
extra = 1
+class NetworkLookerUpper:
+ """ This is a callable that looks up a network name in a sliver and returns
+ the ip address for that network.
+ """
+
+ def __init__(self, name):
+ self.short_description = name
+ self.__name__ = name
+ self.network_name = name
+
+ def __call__(self, obj):
+ if obj is not None:
+ for nbs in obj.networksliver_set.all():
+ if (nbs.network.name == self.network_name):
+ return nbs.ip
+ return ""
+
+ def __str__(self):
+ return self.network_name
+
class SliverInline(PlStackTabularInline):
model = Sliver
fields = ['ip', 'instance_name', 'slice', 'numberCores', 'image', 'node', 'deploymentNetwork']
extra = 0
- #readonly_fields = ['ip', 'instance_name', 'image']
readonly_fields = ['ip', 'instance_name']
+
+
+ def _declared_fieldsets(self):
+ # Return None so django will call get_fieldsets and we can insert our
+ # dynamic fields
+ return None
+
+ def get_readonly_fields(self, request, obj=None):
+ readonly_fields = super(SliverInline, self).get_readonly_fields(request, obj)
+
+ # Lookup the networks that are bound to the slivers, and add those
+ # network names to the list of readonly fields.
+
+ for sliver in obj.slivers.all():
+ for nbs in sliver.networksliver_set.all():
+ if nbs.ip is not None:
+ network_name = nbs.network.name
+ if network_name not in [str(x) for x in readonly_fields]:
+ readonly_fields.append(NetworkLookerUpper(network_name))
+
+ return readonly_fields
+
+ def get_fieldsets(self, request, obj=None):
+ form = self.get_formset(request, obj).form
+ # fields = the read/write files + the read-only fields
+ fields = self.fields
+ for fieldName in self.get_readonly_fields(request,obj):
+ if not fieldName in fields:
+ fields.append(fieldName)
+
+ return [(None, {'fields': fields})]
class SiteInline(PlStackTabularInline):
@@ -121,6 +171,13 @@
return super(SliceMembershipInline, self).formfield_for_foreignkey(db_field, request, **kwargs)
+class SliceNetworkInline(admin.TabularInline):
+ # exclude = ['enacted']
+ model = Network.slices.through
+ extra = 0
+ verbose_name = "Network Connection"
+ verbose_name_plural = "Network Connections"
+
class SliceTagInline(PlStackTabularInline):
model = SliceTag
extra = 0
@@ -271,7 +328,7 @@
class SliceAdmin(PlanetStackBaseAdmin):
fields = ['name', 'site', 'serviceClass', 'description', 'slice_url']
list_display = ('name', 'site','serviceClass', 'slice_url')
- inlines = [SliverInline, SliceMembershipInline, TagInline, SliceTagInline]
+ inlines = [SliverInline, SliceMembershipInline, TagInline, SliceTagInline, SliceNetworkInline]
def formfield_for_foreignkey(self, db_field, request, **kwargs):
if db_field.name == 'site':
@@ -679,6 +736,46 @@
else:
return []
+class NetworkParameterTypeAdmin(admin.ModelAdmin):
+ exclude = ['enacted']
+ list_display = ("name", )
+
+class RouterAdmin(admin.ModelAdmin):
+ exclude = ['enacted']
+ list_display = ("name", )
+
+class RouterInline(admin.TabularInline):
+ # exclude = ['enacted']
+ model = Router.networks.through
+ extra = 0
+ verbose_name_plural = "Routers"
+ verbose_name = "Router"
+
+class NetworkParameterInline(generic.GenericTabularInline):
+ exclude = ['enacted']
+ model = NetworkParameter
+ extra = 1
+ verbose_name_plural = "Parameters"
+ verbose_name = "Parameter"
+
+class NetworkSliversInline(admin.TabularInline):
+ exclude = ['enacted']
+ readonly_fields = ("ip", )
+ model = NetworkSliver
+ extra = 0
+ verbose_name_plural = "Slivers"
+ verbose_name = "Sliver"
+
+class NetworkAdmin(admin.ModelAdmin):
+ exclude = ['enacted']
+ list_display = ("name", "subnet", "ports", "labels")
+ readonly_fields = ("subnet", )
+ inlines = [NetworkParameterInline, NetworkSliversInline, RouterInline]
+
+class NetworkTemplateAdmin(admin.ModelAdmin):
+ exclude = ['enacted']
+ list_display = ("name", "guaranteedBandwidth", "visibility")
+
# register a signal that caches the user's credentials when they log in
def cache_credentials(sender, user, request, **kwds):
auth = {'username': request.POST['username'],
@@ -708,6 +805,10 @@
admin.site.register(Project, ProjectAdmin)
admin.site.register(ServiceClass, ServiceClassAdmin)
admin.site.register(Reservation, ReservationAdmin)
+admin.site.register(Network, NetworkAdmin)
+admin.site.register(Router, RouterAdmin)
+admin.site.register(NetworkParameterType, NetworkParameterTypeAdmin)
+admin.site.register(NetworkTemplate, NetworkTemplateAdmin)
if showAll:
admin.site.register(Tag, TagAdmin)