blob: 169d58a05143b489527870914a001c181739f4b8 [file] [log] [blame]
Matteo Scandoloaca86652017-08-08 13:05:27 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Scott Baker761e1062016-06-20 17:18:17 -070017from django.contrib import admin
18
19from services.vsg.models import *
20from django import forms
21from django.utils.safestring import mark_safe
22from django.contrib.auth.admin import UserAdmin
23from django.contrib.admin.widgets import FilteredSelectMultiple
24from django.contrib.auth.forms import ReadOnlyPasswordHashField
25from django.contrib.auth.signals import user_logged_in
26from django.utils import timezone
27from django.contrib.contenttypes import generic
28from suit.widgets import LinkedSelect
Scott Baker80238f82017-07-18 16:01:10 -070029from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, SubscriberLinkInline, ProviderLinkInline, ProviderDependencyInline,SubscriberDependencyInline
Scott Baker761e1062016-06-20 17:18:17 -070030from core.middleware import get_request
31
32from functools import update_wrapper
33from django.contrib.admin.views.main import ChangeList
34from django.core.urlresolvers import reverse
35from django.contrib.admin.utils import quote
36
Scott Baker761e1062016-06-20 17:18:17 -070037class VSGServiceAdmin(ReadOnlyAwareAdmin):
38 model = VSGService
39 verbose_name = "vSG Service"
40 verbose_name_plural = "vSG Service"
41 list_display = ("backend_status_icon", "name", "enabled")
42 list_display_links = ('backend_status_icon', 'name', )
43 fieldsets = [(None, {'fields': ['backend_status_text', 'name','enabled','versionNumber', 'description', "view_url", "icon_url", "service_specific_attribute", "node_label"],
44 'classes':['suit-tab suit-tab-general']}),
Scott Bakercc9b98d2017-03-20 09:42:37 -070045 ("backend config", {'fields': [ "url_filter_kind" ],
Scott Baker761e1062016-06-20 17:18:17 -070046 'classes':['suit-tab suit-tab-backend']}),
Scott Baker8e66d662016-10-13 13:22:49 -070047 ("vSG config", {'fields': ["dns_servers", "docker_image_name", "docker_insecure_registry"],
Scott Baker761e1062016-06-20 17:18:17 -070048 'classes':['suit-tab suit-tab-vsg']}) ]
49 readonly_fields = ('backend_status_text', "service_specific_attribute")
Scott Baker80238f82017-07-18 16:01:10 -070050 inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline, ProviderDependencyInline,SubscriberDependencyInline]
Scott Baker761e1062016-06-20 17:18:17 -070051
52 extracontext_registered_admins = True
53
54 user_readonly_fields = ["name", "enabled", "versionNumber", "description"]
55
56 suit_form_tabs =(('general', 'Service Details'),
57 ('backend', 'Backend Config'),
58 ('vsg', 'vSG Config'),
59 ('administration', 'Administration'),
60 #('tools', 'Tools'),
61 ('slices','Slices'),
62 ('serviceattrs','Additional Attributes'),
Scott Baker80238f82017-07-18 16:01:10 -070063 ('servicetenants', 'Dependencies'),
Scott Baker761e1062016-06-20 17:18:17 -070064 ('serviceprivileges','Privileges') ,
65 )
66
67 suit_form_includes = (('vcpeadmin.html', 'top', 'administration'),
68 ) #('hpctools.html', 'top', 'tools') )
69
Zack Williams0e6714d2016-06-27 13:11:08 -070070 def get_queryset(self, request):
Scott Baker248ed752017-05-24 16:40:28 -070071 return VSGService.select_by_user(request.user)
Scott Baker761e1062016-06-20 17:18:17 -070072
73class VSGTenantForm(forms.ModelForm):
Scott Baker761e1062016-06-20 17:18:17 -070074 last_ansible_hash = forms.CharField(required=False)
75 wan_container_ip = forms.CharField(required=False)
76 wan_container_mac = forms.CharField(required=False)
77
78 def __init__(self,*args,**kwargs):
79 super (VSGTenantForm,self ).__init__(*args,**kwargs)
Scott Baker80238f82017-07-18 16:01:10 -070080 self.fields['owner'].queryset = VSGService.objects.all()
Scott Baker761e1062016-06-20 17:18:17 -070081 if self.instance:
82 # fields for the attributes
Scott Baker761e1062016-06-20 17:18:17 -070083 self.fields['last_ansible_hash'].initial = self.instance.last_ansible_hash
84 self.fields['wan_container_ip'].initial = self.instance.wan_container_ip
85 self.fields['wan_container_mac'].initial = self.instance.wan_container_mac
86 if (not self.instance) or (not self.instance.pk):
87 # default fields for an 'add' form
Scott Baker761e1062016-06-20 17:18:17 -070088 self.fields['creator'].initial = get_request().user
Scott Bakercc9b98d2017-03-20 09:42:37 -070089 if VSGService.objects.exists():
Scott Baker80238f82017-07-18 16:01:10 -070090 self.fields["owner"].initial = VSGService.objects.all()[0]
Scott Baker761e1062016-06-20 17:18:17 -070091
92 def save(self, commit=True):
93 self.instance.creator = self.cleaned_data.get("creator")
94 self.instance.instance = self.cleaned_data.get("instance")
95 self.instance.last_ansible_hash = self.cleaned_data.get("last_ansible_hash")
96 return super(VSGTenantForm, self).save(commit=commit)
97
98 class Meta:
99 model = VSGTenant
Zack Williamsd7ca08f2016-06-27 08:32:50 -0700100 fields = '__all__'
Scott Baker761e1062016-06-20 17:18:17 -0700101
102class VSGTenantAdmin(ReadOnlyAwareAdmin):
Scott Baker80238f82017-07-18 16:01:10 -0700103 list_display = ('backend_status_icon', 'id', )
Scott Baker761e1062016-06-20 17:18:17 -0700104 list_display_links = ('backend_status_icon', 'id')
Scott Baker80238f82017-07-18 16:01:10 -0700105 fieldsets = [ (None, {'fields': ['backend_status_text', 'owner', 'service_specific_id',
Scott Bakercc9b98d2017-03-20 09:42:37 -0700106 'wan_container_ip', 'wan_container_mac', 'creator', 'instance', 'last_ansible_hash'],
Scott Baker761e1062016-06-20 17:18:17 -0700107 'classes':['suit-tab suit-tab-general']})]
Scott Bakercc9b98d2017-03-20 09:42:37 -0700108 readonly_fields = ('backend_status_text', 'service_specific_attribute', 'wan_container_ip', 'wan_container_mac')
Scott Baker80238f82017-07-18 16:01:10 -0700109 inlines = (ProviderLinkInline, SubscriberLinkInline)
Scott Baker761e1062016-06-20 17:18:17 -0700110 form = VSGTenantForm
111
Scott Baker80238f82017-07-18 16:01:10 -0700112 suit_form_tabs = (('general','Details'),
113 ('servicelinks','Links'),)
Scott Baker761e1062016-06-20 17:18:17 -0700114
Zack Williams0e6714d2016-06-27 13:11:08 -0700115 def get_queryset(self, request):
Scott Baker248ed752017-05-24 16:40:28 -0700116 return VSGTenant.select_by_user(request.user)
Scott Baker761e1062016-06-20 17:18:17 -0700117
118
119admin.site.register(VSGService, VSGServiceAdmin)
120admin.site.register(VSGTenant, VSGTenantAdmin)
121