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