blob: 4d1de3a855a9c3bd2b7d2c78e7d56cde36b855cf [file] [log] [blame]
Matteo Scandolo6288d5a2017-08-08 13:05:26 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080015from django.contrib import admin
16
17from services.veg.models import *
18from django import forms
19from django.utils.safestring import mark_safe
20from django.contrib.auth.admin import UserAdmin
21from django.contrib.admin.widgets import FilteredSelectMultiple
22from django.contrib.auth.forms import ReadOnlyPasswordHashField
23from django.contrib.auth.signals import user_logged_in
24from django.utils import timezone
25from django.contrib.contenttypes import generic
26from suit.widgets import LinkedSelect
Andrea Campanella2a2df422017-08-30 16:59:17 +020027from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, SubscriberLinkInline, ProviderLinkInline, ProviderDependencyInline,SubscriberDependencyInline
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080028from core.middleware import get_request
29
30from functools import update_wrapper
31from django.contrib.admin.views.main import ChangeList
32from django.core.urlresolvers import reverse
33from django.contrib.admin.utils import quote
34
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080035class VEGServiceAdmin(ReadOnlyAwareAdmin):
36 model = VEGService
37 verbose_name = "vEG Service"
38 verbose_name_plural = "vEG Service"
39 list_display = ("backend_status_icon", "name", "enabled")
40 list_display_links = ('backend_status_icon', 'name', )
41 fieldsets = [(None, {'fields': ['backend_status_text', 'name','enabled','versionNumber', 'description', "view_url", "icon_url", "service_specific_attribute", "node_label"],
42 'classes':['suit-tab suit-tab-general']}),
Andrea Campanella2a2df422017-08-30 16:59:17 +020043 ("backend config", {'fields': [ "url_filter_kind" ],
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080044 'classes':['suit-tab suit-tab-backend']}),
45 ("vEG config", {'fields': ["dns_servers", "docker_image_name", "docker_insecure_registry"],
46 'classes':['suit-tab suit-tab-veg']}) ]
47 readonly_fields = ('backend_status_text', "service_specific_attribute")
Andrea Campanella2a2df422017-08-30 16:59:17 +020048 inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline, ProviderDependencyInline,SubscriberDependencyInline]
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080049
50 extracontext_registered_admins = True
51
52 user_readonly_fields = ["name", "enabled", "versionNumber", "description"]
53
54 suit_form_tabs =(('general', 'Service Details'),
55 ('backend', 'Backend Config'),
56 ('veg', 'vEG Config'),
57 ('administration', 'Administration'),
58 #('tools', 'Tools'),
59 ('slices','Slices'),
60 ('serviceattrs','Additional Attributes'),
Andrea Campanella2a2df422017-08-30 16:59:17 +020061 ('servicetenants', 'Dependencies'),
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080062 ('serviceprivileges','Privileges') ,
63 )
64
65 suit_form_includes = (('vegadmin.html', 'top', 'administration'),
66 ) #('hpctools.html', 'top', 'tools') )
67
68 def get_queryset(self, request):
Andrea Campanella2a2df422017-08-30 16:59:17 +020069 return VEGService.select_by_user(request.user)
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080070
71class VEGTenantForm(forms.ModelForm):
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080072 last_ansible_hash = forms.CharField(required=False)
73 wan_container_ip = forms.CharField(required=False)
74 wan_container_mac = forms.CharField(required=False)
75
76 def __init__(self,*args,**kwargs):
77 super (VEGTenantForm,self ).__init__(*args,**kwargs)
Andrea Campanella2a2df422017-08-30 16:59:17 +020078 self.fields['owner'].queryset = VEGService.objects.all()
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080079 if self.instance:
80 # fields for the attributes
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080081 self.fields['last_ansible_hash'].initial = self.instance.last_ansible_hash
82 self.fields['wan_container_ip'].initial = self.instance.wan_container_ip
83 self.fields['wan_container_mac'].initial = self.instance.wan_container_mac
84 if (not self.instance) or (not self.instance.pk):
85 # default fields for an 'add' form
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080086 self.fields['creator'].initial = get_request().user
Andrea Campanella08c14ca2017-03-31 16:13:09 +020087 if VEGService.objects.exists():
Andrea Campanella2a2df422017-08-30 16:59:17 +020088 self.fields["owner"].initial = VEGService.objects.all()[0]
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080089
90 def save(self, commit=True):
91 self.instance.creator = self.cleaned_data.get("creator")
92 self.instance.instance = self.cleaned_data.get("instance")
93 self.instance.last_ansible_hash = self.cleaned_data.get("last_ansible_hash")
94 return super(VEGTenantForm, self).save(commit=commit)
95
96 class Meta:
97 model = VEGTenant
98 fields = '__all__'
99
100class VEGTenantAdmin(ReadOnlyAwareAdmin):
Andrea Campanella2a2df422017-08-30 16:59:17 +0200101 list_display = ('backend_status_icon', 'id', )
Andrea Campanellaedfdbca2017-02-01 17:33:47 -0800102 list_display_links = ('backend_status_icon', 'id')
Andrea Campanella2a2df422017-08-30 16:59:17 +0200103 fieldsets = [ (None, {'fields': ['backend_status_text', 'owner', 'service_specific_id',
Andrea Campanella08c14ca2017-03-31 16:13:09 +0200104 'wan_container_ip', 'wan_container_mac', 'creator', 'instance', 'last_ansible_hash'],
Andrea Campanellaedfdbca2017-02-01 17:33:47 -0800105 'classes':['suit-tab suit-tab-general']})]
Andrea Campanella08c14ca2017-03-31 16:13:09 +0200106 readonly_fields = ('backend_status_text', 'service_specific_attribute', 'wan_container_ip', 'wan_container_mac')
Andrea Campanella2a2df422017-08-30 16:59:17 +0200107 inlines = (ProviderLinkInline, SubscriberLinkInline)
Andrea Campanellaedfdbca2017-02-01 17:33:47 -0800108 form = VEGTenantForm
109
Andrea Campanella2a2df422017-08-30 16:59:17 +0200110 suit_form_tabs = (('general','Details'),
111 ('servicelinks','Links'),)
Andrea Campanellaedfdbca2017-02-01 17:33:47 -0800112
113 def get_queryset(self, request):
Andrea Campanella2a2df422017-08-30 16:59:17 +0200114 return VEGTenant.select_by_user(request.user)
Andrea Campanellaedfdbca2017-02-01 17:33:47 -0800115
116
117admin.site.register(VEGService, VEGServiceAdmin)
118admin.site.register(VEGTenant, VEGTenantAdmin)
119