Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 1 | from django.contrib import admin |
| 2 | |
| 3 | from services.vrouter.models import * |
| 4 | from django import forms |
| 5 | from django.utils.safestring import mark_safe |
| 6 | from django.contrib.auth.admin import UserAdmin |
| 7 | from django.contrib.admin.widgets import FilteredSelectMultiple |
| 8 | from django.contrib.auth.forms import ReadOnlyPasswordHashField |
| 9 | from django.contrib.auth.signals import user_logged_in |
| 10 | from django.utils import timezone |
| 11 | from django.contrib.contenttypes import generic |
| 12 | from suit.widgets import LinkedSelect |
| 13 | from core.models import AddressPool |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 14 | from core.admin import ServiceAppAdmin, SliceInline, ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, AddressPoolInline |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 15 | from core.middleware import get_request |
| 16 | |
| 17 | from functools import update_wrapper |
| 18 | from django.contrib.admin.views.main import ChangeList |
| 19 | from django.core.urlresolvers import reverse |
| 20 | from django.contrib.admin.utils import quote |
| 21 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 22 | |
| 23 | class VRouterPortInline(XOSTabularInline): |
| 24 | model = VRouterPort |
| 25 | fields = ['openflow_id'] |
| 26 | suit_classes = 'suit-tab suit-tab-vrouter_ports' |
| 27 | extra = 0 |
| 28 | |
| 29 | |
| 30 | class VRouterInterfaceInline(XOSTabularInline): |
| 31 | model = VRouterInterface |
| 32 | fields = ['name', 'mac', 'vlan'] |
| 33 | suit_classes = 'suit-tab suit-tab-vrouter_interfaces' |
| 34 | extra = 0 |
| 35 | |
| 36 | |
| 37 | class VRouterIpInline(XOSTabularInline): |
| 38 | model = VRouterIp |
| 39 | fields = ['ip'] |
| 40 | suit_classes = 'suit-tab suit-tab-vrouter_ips' |
| 41 | extra = 0 |
| 42 | |
| 43 | |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 44 | class VRouterServiceForm(forms.ModelForm): |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 45 | def __init__(self, *args, **kwargs): |
| 46 | super(VRouterServiceForm, self).__init__(*args, **kwargs) |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 47 | |
| 48 | def save(self, commit=True): |
| 49 | return super(VRouterServiceForm, self).save(commit=commit) |
| 50 | |
| 51 | class Meta: |
| 52 | model = VRouterService |
Zack Williams | bde22ab | 2016-06-27 12:27:01 -0700 | [diff] [blame] | 53 | fields = '__all__' |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 54 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 55 | |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 56 | class VRouterServiceAdmin(ReadOnlyAwareAdmin): |
| 57 | model = VRouterService |
| 58 | verbose_name = "vRouter Service" |
| 59 | verbose_name_plural = "vRouter Service" |
| 60 | list_display = ("backend_status_icon", "name", "enabled") |
| 61 | list_display_links = ('backend_status_icon', 'name', ) |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 62 | fieldsets = [(None, { |
| 63 | 'fields': [ |
| 64 | 'backend_status_text', |
| 65 | 'name', |
| 66 | 'enabled', |
| 67 | 'versionNumber', |
| 68 | 'description', |
| 69 | 'view_url', |
| 70 | 'icon_url', |
| 71 | 'rest_hostname', |
| 72 | 'rest_port', |
| 73 | 'rest_user', |
| 74 | 'rest_pass', |
| 75 | ], |
| 76 | 'classes':['suit-tab suit-tab-general']})] |
| 77 | |
| 78 | # NOTE make rest_* params editable |
| 79 | readonly_fields = ( |
| 80 | 'backend_status_text', |
| 81 | 'rest_hostname', |
| 82 | 'rest_port', |
| 83 | 'rest_user', |
| 84 | 'rest_pass' |
| 85 | ) |
| 86 | inlines = [SliceInline, ServiceAttrAsTabInline, ServicePrivilegeInline, AddressPoolInline] |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 87 | form = VRouterServiceForm |
| 88 | |
| 89 | extracontext_registered_admins = True |
| 90 | |
| 91 | user_readonly_fields = ["name", "enabled", "versionNumber", "description"] |
| 92 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 93 | suit_form_tabs = ( |
| 94 | ('general', 'vRouter Service Details'), |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 95 | ('administration', 'Administration'), |
| 96 | ('addresspools', 'Addresses'), |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 97 | # ('tools', 'Tools'), |
| 98 | ('slices', 'Slices'), |
| 99 | ('serviceattrs', 'Additional Attributes'), |
| 100 | ('serviceprivileges', 'Privileges'), |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 101 | ) |
| 102 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 103 | suit_form_includes = ('vrouteradmin.html', 'top', 'administration') # ('hpctools.html', 'top', 'tools') ) |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 104 | |
Zack Williams | bde22ab | 2016-06-27 12:27:01 -0700 | [diff] [blame] | 105 | def get_queryset(self, request): |
Scott Baker | b03e0a7 | 2017-05-25 09:38:12 -0700 | [diff] [blame] | 106 | return VRouterService.select_by_user(request.user) |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 107 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 108 | |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 109 | class VRouterTenantForm(forms.ModelForm): |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 110 | gateway_ip = forms.CharField(required=False) |
| 111 | gateway_mac = forms.CharField(required=False) |
| 112 | cidr = forms.CharField(required=False) |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 113 | |
| 114 | def __init__(self,*args,**kwargs): |
| 115 | super (VRouterTenantForm,self ).__init__(*args,**kwargs) |
| 116 | self.fields['kind'].widget.attrs['readonly'] = True |
Scott Baker | b03e0a7 | 2017-05-25 09:38:12 -0700 | [diff] [blame] | 117 | self.fields['provider_service'].queryset = VRouterService.objects.all() |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 118 | if self.instance: |
| 119 | # fields for the attributes |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 120 | self.fields['gateway_ip'].initial = self.instance.gateway_ip |
| 121 | self.fields['gateway_mac'].initial = self.instance.gateway_mac |
| 122 | self.fields['cidr'].initial = self.instance.cidr |
| 123 | if (not self.instance) or (not self.instance.pk): |
| 124 | # default fields for an 'add' form |
| 125 | self.fields['kind'].initial = VROUTER_KIND |
Scott Baker | d94cbfc | 2017-03-13 11:40:46 -0700 | [diff] [blame] | 126 | if VRouterService.objects.exists(): |
| 127 | self.fields["provider_service"].initial = VRouterService.objects.first() |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 128 | |
| 129 | def save(self, commit=True): |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 130 | return super(VRouterTenantForm, self).save(commit=commit) |
| 131 | |
| 132 | class Meta: |
| 133 | model = VRouterTenant |
Zack Williams | edbf95f | 2016-06-27 08:31:44 -0700 | [diff] [blame] | 134 | fields = '__all__' |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 135 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 136 | |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 137 | class VRouterTenantAdmin(ReadOnlyAwareAdmin): |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 138 | list_display = ('backend_status_icon', 'id', 'subscriber_tenant', 'public_ip') |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 139 | list_display_links = ('backend_status_icon', 'id') |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 140 | fieldsets = [(None, { |
| 141 | 'fields': [ |
| 142 | 'backend_status_text', 'kind', 'provider_service', 'subscriber_tenant', 'subscriber_service', |
| 143 | 'address_pool', 'public_ip', 'public_mac', 'gateway_ip', 'gateway_mac', 'cidr'], |
| 144 | 'classes':['suit-tab suit-tab-general'] |
| 145 | })] |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 146 | readonly_fields = ('backend_status_text', 'service_specific_attribute', 'gateway_ip', 'gateway_mac', 'cidr') |
| 147 | form = VRouterTenantForm |
| 148 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 149 | suit_form_tabs = (('general', 'Details'),) |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 150 | |
Zack Williams | bde22ab | 2016-06-27 12:27:01 -0700 | [diff] [blame] | 151 | def get_queryset(self, request): |
Scott Baker | b03e0a7 | 2017-05-25 09:38:12 -0700 | [diff] [blame] | 152 | return VRouterTenant.select_by_user(request.user) |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 153 | |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 154 | |
| 155 | class VRouterDeviceAdmin(ReadOnlyAwareAdmin): |
| 156 | list_display = ('name', 'openflow_id', 'config_key', 'driver') |
| 157 | fieldsets = [(None, { |
| 158 | 'fields': ['name', 'openflow_id', 'vrouter_service', 'config_key', 'driver'], |
| 159 | 'classes':['suit-tab suit-tab-general'] |
| 160 | })] |
| 161 | inlines = [VRouterPortInline] |
| 162 | |
| 163 | suit_form_tabs = ( |
| 164 | ('general', 'Device Details'), |
| 165 | ('vrouter_ports', 'Ports'), |
| 166 | ) |
| 167 | |
| 168 | |
| 169 | class VRouterPortAdmin(ReadOnlyAwareAdmin): |
| 170 | list_display = ('name', 'openflow_id', 'vrouter_device') |
| 171 | fieldsets = [(None, { |
| 172 | 'fields': ['name', 'openflow_id', 'vrouter_service', 'vrouter_device'], |
| 173 | 'classes':['suit-tab suit-tab-general'] |
| 174 | })] |
| 175 | inlines = [VRouterInterfaceInline] |
| 176 | |
| 177 | suit_form_tabs = ( |
| 178 | ('general', 'Ports Details'), |
| 179 | ('vrouter_interfaces', 'Interfaces'), |
| 180 | ) |
| 181 | |
| 182 | |
| 183 | class VRouterInterfaceAdmin(ReadOnlyAwareAdmin): |
| 184 | list_display = ('name', 'mac', 'vlan') |
| 185 | fieldsets = [(None, { |
| 186 | 'fields': ['name', 'vrouter_port', 'mac', 'vlan'], |
| 187 | 'classes':['suit-tab suit-tab-general'] |
| 188 | })] |
| 189 | inlines = [VRouterIpInline] |
| 190 | |
| 191 | suit_form_tabs = ( |
| 192 | ('general', 'Interfaces Details'), |
| 193 | ('vrouter_ips', 'Ips'), |
| 194 | ) |
| 195 | |
| 196 | |
| 197 | class VRouterIpAdmin(ReadOnlyAwareAdmin): |
| 198 | list_display = ('name', 'ip', 'vrouter_interface') |
| 199 | fieldsets = [(None, {'fields': ['name', 'ip', 'vrouter_interface']})] |
| 200 | |
| 201 | |
| 202 | class VRouterAppAdmin(ReadOnlyAwareAdmin): |
| 203 | list_display = ('name', 'control_plane_connect_point', 'ospf_enabled') |
| 204 | fieldsets = [(None, {'fields': ['name', 'vrouter_service', 'control_plane_connect_point', 'ospf_enabled']})] |
| 205 | |
| 206 | |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 207 | admin.site.register(VRouterService, VRouterServiceAdmin) |
| 208 | admin.site.register(VRouterTenant, VRouterTenantAdmin) |
Matteo Scandolo | a4e6e9a | 2016-08-23 12:04:45 -0700 | [diff] [blame] | 209 | admin.site.register(VRouterDevice, VRouterDeviceAdmin) |
| 210 | admin.site.register(VRouterPort, VRouterPortAdmin) |
| 211 | admin.site.register(VRouterInterface, VRouterInterfaceAdmin) |
| 212 | admin.site.register(VRouterIp, VRouterIpAdmin) |
| 213 | admin.site.register(VRouterApp, VRouterAppAdmin) |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 214 | |