Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 1 | from django.contrib import admin |
| 2 | |
| 3 | from services.vsg.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.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, TenantRootTenantInline, TenantRootPrivilegeInline |
| 14 | from core.middleware import get_request |
| 15 | |
| 16 | from functools import update_wrapper |
| 17 | from django.contrib.admin.views.main import ChangeList |
| 18 | from django.core.urlresolvers import reverse |
| 19 | from django.contrib.admin.utils import quote |
| 20 | |
| 21 | #----------------------------------------------------------------------------- |
| 22 | # vSG |
| 23 | #----------------------------------------------------------------------------- |
| 24 | |
| 25 | class 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 Baker | 8e66d66 | 2016-10-13 13:22:49 -0700 | [diff] [blame] | 34 | docker_image_name = forms.CharField(required=False) |
| 35 | docker_insecure_registry = forms.BooleanField(required=False) |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 36 | |
| 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 Baker | 8e66d66 | 2016-10-13 13:22:49 -0700 | [diff] [blame] | 48 | self.fields['docker_image_name'].initial = self.instance.docker_image_name |
| 49 | self.fields['docker_insecure_registry'].initial = self.instance.docker_insecure_registry |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 50 | |
| 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 Baker | 8e66d66 | 2016-10-13 13:22:49 -0700 | [diff] [blame] | 60 | 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 Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 62 | return super(VSGServiceForm, self).save(commit=commit) |
| 63 | |
| 64 | class Meta: |
| 65 | model = VSGService |
Zack Williams | d7ca08f | 2016-06-27 08:32:50 -0700 | [diff] [blame] | 66 | fields = '__all__' |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 67 | |
| 68 | class 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 Baker | 8e66d66 | 2016-10-13 13:22:49 -0700 | [diff] [blame] | 78 | ("vSG config", {'fields': ["dns_servers", "docker_image_name", "docker_insecure_registry"], |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 79 | '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 Williams | 0e6714d | 2016-06-27 13:11:08 -0700 | [diff] [blame] | 101 | def get_queryset(self, request): |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 102 | return VSGService.get_service_objects_by_user(request.user) |
| 103 | |
| 104 | class 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 Williams | d7ca08f | 2016-06-27 08:32:50 -0700 | [diff] [blame] | 139 | fields = '__all__' |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 140 | |
| 141 | class 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 Williams | 0e6714d | 2016-06-27 13:11:08 -0700 | [diff] [blame] | 152 | def get_queryset(self, request): |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 153 | return VSGTenant.get_tenant_objects_by_user(request.user) |
| 154 | |
| 155 | |
| 156 | admin.site.register(VSGService, VSGServiceAdmin) |
| 157 | admin.site.register(VSGTenant, VSGTenantAdmin) |
| 158 | |