Scott Baker | 31acc65 | 2016-06-23 15:47:56 -0700 | [diff] [blame] | 1 | from django.contrib import admin |
| 2 | |
| 3 | from services.ceilometer.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, TenantAttrAsTabInline |
| 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 | class CeilometerServiceForm(forms.ModelForm): |
| 22 | ceilometer_pub_sub_url = forms.CharField(required=False, max_length=1024, help_text="REST URL of ceilometer PUB/SUB component in http://IP:port/ format") |
| 23 | |
| 24 | def __init__(self,*args,**kwargs): |
| 25 | super (CeilometerServiceForm,self ).__init__(*args,**kwargs) |
| 26 | if self.instance: |
| 27 | # fields for the attributes |
| 28 | self.fields['ceilometer_pub_sub_url'].initial = self.instance.ceilometer_pub_sub_url |
| 29 | |
| 30 | def save(self, commit=True): |
| 31 | self.instance.ceilometer_pub_sub_url = self.cleaned_data.get("ceilometer_pub_sub_url") |
| 32 | return super(CeilometerServiceForm, self).save(commit=commit) |
| 33 | |
| 34 | class Meta: |
| 35 | model = CeilometerService |
| 36 | |
| 37 | class CeilometerServiceAdmin(ReadOnlyAwareAdmin): |
| 38 | model = CeilometerService |
| 39 | verbose_name = "Ceilometer Service" |
| 40 | verbose_name_plural = "Ceilometer 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','ceilometer_pub_sub_url', "view_url","icon_url" ], 'classes':['suit-tab suit-tab-general']})] |
| 44 | readonly_fields = ('backend_status_text', ) |
| 45 | inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline] |
| 46 | form = CeilometerServiceForm |
| 47 | |
| 48 | extracontext_registered_admins = True |
| 49 | |
| 50 | user_readonly_fields = ["name", "enabled", "versionNumber", "description"] |
| 51 | |
| 52 | suit_form_tabs =(('general', 'Ceilometer Service Details'), |
| 53 | ('administration', 'Administration'), |
| 54 | ('slices','Slices'), |
| 55 | ('serviceattrs','Additional Attributes'), |
| 56 | ('serviceprivileges','Privileges'), |
| 57 | ) |
| 58 | |
| 59 | suit_form_includes = (('ceilometeradmin.html', 'top', 'administration'), |
| 60 | ) |
| 61 | |
| 62 | def queryset(self, request): |
| 63 | return CeilometerService.get_service_objects_by_user(request.user) |
| 64 | |
| 65 | class MonitoringChannelForm(forms.ModelForm): |
| 66 | creator = forms.ModelChoiceField(queryset=User.objects.all()) |
| 67 | |
| 68 | def __init__(self,*args,**kwargs): |
| 69 | super (MonitoringChannelForm,self ).__init__(*args,**kwargs) |
| 70 | self.fields['kind'].widget.attrs['readonly'] = True |
| 71 | self.fields['provider_service'].queryset = CeilometerService.get_service_objects().all() |
| 72 | if self.instance: |
| 73 | # fields for the attributes |
| 74 | self.fields['creator'].initial = self.instance.creator |
| 75 | if (not self.instance) or (not self.instance.pk): |
| 76 | # default fields for an 'add' form |
| 77 | self.fields['kind'].initial = CEILOMETER_KIND |
| 78 | self.fields['creator'].initial = get_request().user |
| 79 | if CeilometerService.get_service_objects().exists(): |
| 80 | self.fields["provider_service"].initial = CeilometerService.get_service_objects().all()[0] |
| 81 | |
| 82 | |
| 83 | def save(self, commit=True): |
| 84 | self.instance.creator = self.cleaned_data.get("creator") |
| 85 | return super(MonitoringChannelForm, self).save(commit=commit) |
| 86 | |
| 87 | class Meta: |
| 88 | model = MonitoringChannel |
| 89 | |
| 90 | class MonitoringChannelAdmin(ReadOnlyAwareAdmin): |
| 91 | list_display = ('backend_status_icon', 'id', ) |
| 92 | list_display_links = ('backend_status_icon', 'id') |
| 93 | fieldsets = [ (None, {'fields': ['backend_status_text', 'kind', 'provider_service', 'service_specific_attribute', |
| 94 | 'ceilometer_url', 'tenant_list_str', |
| 95 | 'instance', 'creator'], |
| 96 | 'classes':['suit-tab suit-tab-general']})] |
| 97 | readonly_fields = ('backend_status_text', 'instance', 'service_specific_attribute', 'ceilometer_url', 'tenant_list_str') |
| 98 | form = MonitoringChannelForm |
| 99 | |
| 100 | suit_form_tabs = (('general','Details'),) |
| 101 | actions=['delete_selected_objects'] |
| 102 | |
| 103 | def get_actions(self, request): |
| 104 | actions = super(MonitoringChannelAdmin, self).get_actions(request) |
| 105 | if 'delete_selected' in actions: |
| 106 | del actions['delete_selected'] |
| 107 | return actions |
| 108 | |
| 109 | def delete_selected_objects(self, request, queryset): |
| 110 | for obj in queryset: |
| 111 | obj.delete() |
| 112 | delete_selected_objects.short_description = "Delete Selected MonitoringChannel Objects" |
| 113 | |
| 114 | def queryset(self, request): |
| 115 | return MonitoringChannel.get_tenant_objects_by_user(request.user) |
| 116 | |
| 117 | class SFlowServiceForm(forms.ModelForm): |
| 118 | sflow_port = forms.IntegerField(required=False) |
| 119 | sflow_api_port = forms.IntegerField(required=False) |
| 120 | |
| 121 | def __init__(self,*args,**kwargs): |
| 122 | super (SFlowServiceForm,self ).__init__(*args,**kwargs) |
| 123 | if self.instance: |
| 124 | # fields for the attributes |
| 125 | self.fields['sflow_port'].initial = self.instance.sflow_port |
| 126 | self.fields['sflow_api_port'].initial = self.instance.sflow_api_port |
| 127 | if (not self.instance) or (not self.instance.pk): |
| 128 | # default fields for an 'add' form |
| 129 | self.fields['sflow_port'].initial = SFLOW_PORT |
| 130 | self.fields['sflow_api_port'].initial = SFLOW_API_PORT |
| 131 | |
| 132 | def save(self, commit=True): |
| 133 | self.instance.sflow_port = self.cleaned_data.get("sflow_port") |
| 134 | self.instance.sflow_api_port = self.cleaned_data.get("sflow_api_port") |
| 135 | return super(SFlowServiceForm, self).save(commit=commit) |
| 136 | |
| 137 | class Meta: |
| 138 | model = SFlowService |
| 139 | |
| 140 | class SFlowServiceAdmin(ReadOnlyAwareAdmin): |
| 141 | model = SFlowService |
| 142 | verbose_name = "SFlow Service" |
| 143 | verbose_name_plural = "SFlow Service" |
| 144 | list_display = ("backend_status_icon", "name", "enabled") |
| 145 | list_display_links = ('backend_status_icon', 'name', ) |
| 146 | fieldsets = [(None, {'fields': ['backend_status_text', 'name','enabled','versionNumber', 'description',"view_url","sflow_port","sflow_api_port","icon_url" ], 'classes':['suit-tab suit-tab-general']})] |
| 147 | readonly_fields = ('backend_status_text', ) |
| 148 | inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline] |
| 149 | form = SFlowServiceForm |
| 150 | |
| 151 | extracontext_registered_admins = True |
| 152 | |
| 153 | user_readonly_fields = ["name", "enabled", "versionNumber", "description"] |
| 154 | |
| 155 | suit_form_tabs =(('general', 'SFlow Service Details'), |
| 156 | ('administration', 'Administration'), |
| 157 | ('slices','Slices'), |
| 158 | ('serviceattrs','Additional Attributes'), |
| 159 | ('serviceprivileges','Privileges'), |
| 160 | ) |
| 161 | |
| 162 | suit_form_includes = (('sflowadmin.html', 'top', 'administration'), |
| 163 | ) |
| 164 | |
| 165 | def queryset(self, request): |
| 166 | return SFlowService.get_service_objects_by_user(request.user) |
| 167 | |
| 168 | class SFlowTenantForm(forms.ModelForm): |
| 169 | creator = forms.ModelChoiceField(queryset=User.objects.all()) |
| 170 | listening_endpoint = forms.CharField(max_length=1024, help_text="sFlow listening endpoint in udp://IP:port format") |
| 171 | |
| 172 | def __init__(self,*args,**kwargs): |
| 173 | super (SFlowTenantForm,self ).__init__(*args,**kwargs) |
| 174 | self.fields['kind'].widget.attrs['readonly'] = True |
| 175 | self.fields['provider_service'].queryset = SFlowService.get_service_objects().all() |
| 176 | if self.instance: |
| 177 | # fields for the attributes |
| 178 | self.fields['creator'].initial = self.instance.creator |
| 179 | self.fields['listening_endpoint'].initial = self.instance.listening_endpoint |
| 180 | if (not self.instance) or (not self.instance.pk): |
| 181 | # default fields for an 'add' form |
| 182 | self.fields['kind'].initial = SFLOW_KIND |
| 183 | self.fields['creator'].initial = get_request().user |
| 184 | if SFlowService.get_service_objects().exists(): |
| 185 | self.fields["provider_service"].initial = SFlowService.get_service_objects().all()[0] |
| 186 | |
| 187 | def save(self, commit=True): |
| 188 | self.instance.creator = self.cleaned_data.get("creator") |
| 189 | self.instance.listening_endpoint = self.cleaned_data.get("listening_endpoint") |
| 190 | return super(SFlowTenantForm, self).save(commit=commit) |
| 191 | |
| 192 | class Meta: |
| 193 | model = SFlowTenant |
| 194 | |
| 195 | class SFlowTenantAdmin(ReadOnlyAwareAdmin): |
| 196 | list_display = ('backend_status_icon', 'creator', 'listening_endpoint' ) |
| 197 | list_display_links = ('backend_status_icon', 'listening_endpoint') |
| 198 | fieldsets = [ (None, {'fields': ['backend_status_text', 'kind', 'provider_service', 'subscriber_service', 'service_specific_attribute', 'listening_endpoint', |
| 199 | 'creator'], |
| 200 | 'classes':['suit-tab suit-tab-general']})] |
| 201 | readonly_fields = ('backend_status_text', 'instance', 'service_specific_attribute') |
| 202 | inlines = [TenantAttrAsTabInline] |
| 203 | form = SFlowTenantForm |
| 204 | |
| 205 | suit_form_tabs = (('general','Details'), ('tenantattrs', 'Attributes')) |
| 206 | |
| 207 | def queryset(self, request): |
| 208 | return SFlowTenant.get_tenant_objects_by_user(request.user) |
| 209 | |
| 210 | admin.site.register(CeilometerService, CeilometerServiceAdmin) |
| 211 | admin.site.register(SFlowService, SFlowServiceAdmin) |
| 212 | admin.site.register(MonitoringChannel, MonitoringChannelAdmin) |
| 213 | admin.site.register(SFlowTenant, SFlowTenantAdmin) |
| 214 | |