blob: b743a150695d71e7faaa5772790d28d22c0a0773 [file] [log] [blame]
Matteo Scandolo6288d5a2017-08-08 13:05:26 -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
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080017from django.contrib import admin
18
19from services.veg.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
29from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, TenantRootTenantInline, TenantRootPrivilegeInline
30from 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
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080037class VEGServiceAdmin(ReadOnlyAwareAdmin):
38 model = VEGService
39 verbose_name = "vEG Service"
40 verbose_name_plural = "vEG 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']}),
Andrea Campanella08c14ca2017-03-31 16:13:09 +020045 ("backend config", {'fields': ["url_filter_kind"],
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080046 'classes':['suit-tab suit-tab-backend']}),
47 ("vEG config", {'fields': ["dns_servers", "docker_image_name", "docker_insecure_registry"],
48 'classes':['suit-tab suit-tab-veg']}) ]
49 readonly_fields = ('backend_status_text', "service_specific_attribute")
50 inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline]
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080051
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 ('veg', 'vEG Config'),
59 ('administration', 'Administration'),
60 #('tools', 'Tools'),
61 ('slices','Slices'),
62 ('serviceattrs','Additional Attributes'),
63 ('serviceprivileges','Privileges') ,
64 )
65
66 suit_form_includes = (('vegadmin.html', 'top', 'administration'),
67 ) #('hpctools.html', 'top', 'tools') )
68
69 def get_queryset(self, request):
70 return VEGService.get_service_objects_by_user(request.user)
71
72class VEGTenantForm(forms.ModelForm):
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080073 last_ansible_hash = forms.CharField(required=False)
74 wan_container_ip = forms.CharField(required=False)
75 wan_container_mac = forms.CharField(required=False)
76
77 def __init__(self,*args,**kwargs):
78 super (VEGTenantForm,self ).__init__(*args,**kwargs)
79 self.fields['kind'].widget.attrs['readonly'] = True
Andrea Campanella08c14ca2017-03-31 16:13:09 +020080 self.fields['provider_service'].queryset = VEGService.objects.all()
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080081 if self.instance:
82 # fields for the attributes
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080083 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
88 self.fields['kind'].initial = VEG_KIND
89 self.fields['creator'].initial = get_request().user
Andrea Campanella08c14ca2017-03-31 16:13:09 +020090 if VEGService.objects.exists():
91 self.fields["provider_service"].initial = VEGService.objects.all()[0]
Andrea Campanellaedfdbca2017-02-01 17:33:47 -080092
93 def save(self, commit=True):
94 self.instance.creator = self.cleaned_data.get("creator")
95 self.instance.instance = self.cleaned_data.get("instance")
96 self.instance.last_ansible_hash = self.cleaned_data.get("last_ansible_hash")
97 return super(VEGTenantForm, self).save(commit=commit)
98
99 class Meta:
100 model = VEGTenant
101 fields = '__all__'
102
103class VEGTenantAdmin(ReadOnlyAwareAdmin):
104 list_display = ('backend_status_icon', 'id', 'subscriber_tenant' )
105 list_display_links = ('backend_status_icon', 'id')
Andrea Campanella08c14ca2017-03-31 16:13:09 +0200106 fieldsets = [ (None, {'fields': ['backend_status_text', 'kind', 'provider_service', 'subscriber_tenant', 'service_specific_id',
107 'wan_container_ip', 'wan_container_mac', 'creator', 'instance', 'last_ansible_hash'],
Andrea Campanellaedfdbca2017-02-01 17:33:47 -0800108 'classes':['suit-tab suit-tab-general']})]
Andrea Campanella08c14ca2017-03-31 16:13:09 +0200109 readonly_fields = ('backend_status_text', 'service_specific_attribute', 'wan_container_ip', 'wan_container_mac')
Andrea Campanellaedfdbca2017-02-01 17:33:47 -0800110 form = VEGTenantForm
111
112 suit_form_tabs = (('general','Details'),)
113
114 def get_queryset(self, request):
115 return VEGTenant.get_tenant_objects_by_user(request.user)
116
117
118admin.site.register(VEGService, VEGServiceAdmin)
119admin.site.register(VEGTenant, VEGTenantAdmin)
120