blob: b87ffdc7a3b244990d46047b9694551ef2a8e3de [file] [log] [blame]
Scott Baker761e1062016-06-20 17:18:17 -07001from django.contrib import admin
2
3from services.vsg.models import *
4from django import forms
5from django.utils.safestring import mark_safe
6from django.contrib.auth.admin import UserAdmin
7from django.contrib.admin.widgets import FilteredSelectMultiple
8from django.contrib.auth.forms import ReadOnlyPasswordHashField
9from django.contrib.auth.signals import user_logged_in
10from django.utils import timezone
11from django.contrib.contenttypes import generic
12from suit.widgets import LinkedSelect
13from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, TenantRootTenantInline, TenantRootPrivilegeInline
14from core.middleware import get_request
15
16from functools import update_wrapper
17from django.contrib.admin.views.main import ChangeList
18from django.core.urlresolvers import reverse
19from django.contrib.admin.utils import quote
20
21#-----------------------------------------------------------------------------
22# vSG
23#-----------------------------------------------------------------------------
24
25class VSGServiceForm(forms.ModelForm):
26 bbs_api_hostname = forms.CharField(required=False)
27 bbs_api_port = forms.IntegerField(required=False)
28 bbs_server = forms.CharField(required=False)
29 backend_network_label = forms.CharField(required=False)
30 bbs_slice = forms.ModelChoiceField(queryset=Slice.objects.all(), required=False)
31 dns_servers = forms.CharField(required=False)
32 url_filter_kind = forms.ChoiceField(choices=VSGService.URL_FILTER_KIND_CHOICES, required=False)
33 node_label = forms.CharField(required=False)
Scott Baker8e66d662016-10-13 13:22:49 -070034 docker_image_name = forms.CharField(required=False)
35 docker_insecure_registry = forms.BooleanField(required=False)
Scott Baker761e1062016-06-20 17:18:17 -070036
37 def __init__(self,*args,**kwargs):
38 super (VSGServiceForm,self ).__init__(*args,**kwargs)
39 if self.instance:
40 self.fields['bbs_api_hostname'].initial = self.instance.bbs_api_hostname
41 self.fields['bbs_api_port'].initial = self.instance.bbs_api_port
42 self.fields['bbs_server'].initial = self.instance.bbs_server
43 self.fields['backend_network_label'].initial = self.instance.backend_network_label
44 self.fields['bbs_slice'].initial = self.instance.bbs_slice
45 self.fields['dns_servers'].initial = self.instance.dns_servers
46 self.fields['url_filter_kind']. initial = self.instance.url_filter_kind
47 self.fields['node_label'].initial = self.instance.node_label
Scott Baker8e66d662016-10-13 13:22:49 -070048 self.fields['docker_image_name'].initial = self.instance.docker_image_name
49 self.fields['docker_insecure_registry'].initial = self.instance.docker_insecure_registry
Scott Baker761e1062016-06-20 17:18:17 -070050
51 def save(self, commit=True):
52 self.instance.bbs_api_hostname = self.cleaned_data.get("bbs_api_hostname")
53 self.instance.bbs_api_port = self.cleaned_data.get("bbs_api_port")
54 self.instance.bbs_server = self.cleaned_data.get("bbs_server")
55 self.instance.backend_network_label = self.cleaned_data.get("backend_network_label")
56 self.instance.bbs_slice = self.cleaned_data.get("bbs_slice")
57 self.instance.dns_servers = self.cleaned_data.get("dns_servers")
58 self.instance.url_filter_kind = self.cleaned_data.get("url_filter_kind")
59 self.instance.node_label = self.cleaned_data.get("node_label")
Scott Baker8e66d662016-10-13 13:22:49 -070060 self.instance.docker_image_name = self.cleaned_data.get("docker_image_name")
61 self.instance.docker_insecure_registry = self.cleaned_data.get("docker_insecure_registry")
Scott Baker761e1062016-06-20 17:18:17 -070062 return super(VSGServiceForm, self).save(commit=commit)
63
64 class Meta:
65 model = VSGService
Zack Williamsd7ca08f2016-06-27 08:32:50 -070066 fields = '__all__'
Scott Baker761e1062016-06-20 17:18:17 -070067
68class VSGServiceAdmin(ReadOnlyAwareAdmin):
69 model = VSGService
70 verbose_name = "vSG Service"
71 verbose_name_plural = "vSG Service"
72 list_display = ("backend_status_icon", "name", "enabled")
73 list_display_links = ('backend_status_icon', 'name', )
74 fieldsets = [(None, {'fields': ['backend_status_text', 'name','enabled','versionNumber', 'description', "view_url", "icon_url", "service_specific_attribute", "node_label"],
75 'classes':['suit-tab suit-tab-general']}),
76 ("backend config", {'fields': [ "backend_network_label", "url_filter_kind", "bbs_api_hostname", "bbs_api_port", "bbs_server", "bbs_slice"],
77 'classes':['suit-tab suit-tab-backend']}),
Scott Baker8e66d662016-10-13 13:22:49 -070078 ("vSG config", {'fields': ["dns_servers", "docker_image_name", "docker_insecure_registry"],
Scott Baker761e1062016-06-20 17:18:17 -070079 'classes':['suit-tab suit-tab-vsg']}) ]
80 readonly_fields = ('backend_status_text', "service_specific_attribute")
81 inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline]
82 form = VSGServiceForm
83
84 extracontext_registered_admins = True
85
86 user_readonly_fields = ["name", "enabled", "versionNumber", "description"]
87
88 suit_form_tabs =(('general', 'Service Details'),
89 ('backend', 'Backend Config'),
90 ('vsg', 'vSG Config'),
91 ('administration', 'Administration'),
92 #('tools', 'Tools'),
93 ('slices','Slices'),
94 ('serviceattrs','Additional Attributes'),
95 ('serviceprivileges','Privileges') ,
96 )
97
98 suit_form_includes = (('vcpeadmin.html', 'top', 'administration'),
99 ) #('hpctools.html', 'top', 'tools') )
100
Zack Williams0e6714d2016-06-27 13:11:08 -0700101 def get_queryset(self, request):
Scott Baker761e1062016-06-20 17:18:17 -0700102 return VSGService.get_service_objects_by_user(request.user)
103
104class VSGTenantForm(forms.ModelForm):
105 bbs_account = forms.CharField(required=False)
106 creator = forms.ModelChoiceField(queryset=User.objects.all())
107 instance = forms.ModelChoiceField(queryset=Instance.objects.all(),required=False)
108 last_ansible_hash = forms.CharField(required=False)
109 wan_container_ip = forms.CharField(required=False)
110 wan_container_mac = forms.CharField(required=False)
111
112 def __init__(self,*args,**kwargs):
113 super (VSGTenantForm,self ).__init__(*args,**kwargs)
114 self.fields['kind'].widget.attrs['readonly'] = True
115 self.fields['provider_service'].queryset = VSGService.get_service_objects().all()
116 if self.instance:
117 # fields for the attributes
118 self.fields['bbs_account'].initial = self.instance.bbs_account
119 self.fields['creator'].initial = self.instance.creator
120 self.fields['instance'].initial = self.instance.instance
121 self.fields['last_ansible_hash'].initial = self.instance.last_ansible_hash
122 self.fields['wan_container_ip'].initial = self.instance.wan_container_ip
123 self.fields['wan_container_mac'].initial = self.instance.wan_container_mac
124 if (not self.instance) or (not self.instance.pk):
125 # default fields for an 'add' form
126 self.fields['kind'].initial = VCPE_KIND
127 self.fields['creator'].initial = get_request().user
128 if VSGService.get_service_objects().exists():
129 self.fields["provider_service"].initial = VSGService.get_service_objects().all()[0]
130
131 def save(self, commit=True):
132 self.instance.creator = self.cleaned_data.get("creator")
133 self.instance.instance = self.cleaned_data.get("instance")
134 self.instance.last_ansible_hash = self.cleaned_data.get("last_ansible_hash")
135 return super(VSGTenantForm, self).save(commit=commit)
136
137 class Meta:
138 model = VSGTenant
Zack Williamsd7ca08f2016-06-27 08:32:50 -0700139 fields = '__all__'
Scott Baker761e1062016-06-20 17:18:17 -0700140
141class VSGTenantAdmin(ReadOnlyAwareAdmin):
142 list_display = ('backend_status_icon', 'id', 'subscriber_tenant' )
143 list_display_links = ('backend_status_icon', 'id')
144 fieldsets = [ (None, {'fields': ['backend_status_text', 'kind', 'provider_service', 'subscriber_tenant', 'service_specific_id', # 'service_specific_attribute',
145 'wan_container_ip', 'wan_container_mac', 'bbs_account', 'creator', 'instance', 'last_ansible_hash'],
146 'classes':['suit-tab suit-tab-general']})]
147 readonly_fields = ('backend_status_text', 'service_specific_attribute', 'bbs_account', 'wan_container_ip', 'wan_container_mac')
148 form = VSGTenantForm
149
150 suit_form_tabs = (('general','Details'),)
151
Zack Williams0e6714d2016-06-27 13:11:08 -0700152 def get_queryset(self, request):
Scott Baker761e1062016-06-20 17:18:17 -0700153 return VSGTenant.get_tenant_objects_by_user(request.user)
154
155
156admin.site.register(VSGService, VSGServiceAdmin)
157admin.site.register(VSGTenant, VSGTenantAdmin)
158