blob: dfb4e0d9e163c90f9c5e638584a009a049b9d886 [file] [log] [blame]
Siobhan Tully4bc09f22013-04-10 21:15:21 -04001from plstackapi.core.models import Site
Tony Mackf1c12242013-04-09 16:08:43 -04002from plstackapi.core.models import *
Tony Mack02755d42013-05-02 00:00:10 -04003from plstackapi.openstack.manager import OpenStackManager
Tony Macke59a7c82013-04-27 11:08:10 -04004
Tony Mack7130ac32013-03-22 21:58:00 -04005from django.contrib import admin
Siobhan Tully53437282013-04-26 19:30:27 -04006from django.contrib.auth.models import Group
Siobhan Tully4bc09f22013-04-10 21:15:21 -04007from django import forms
Tony Mackd90cdbf2013-04-16 22:48:40 -04008from django.utils.safestring import mark_safe
Tony Mack7130ac32013-03-22 21:58:00 -04009from django.contrib.auth.admin import UserAdmin
Siobhan Tully4bc09f22013-04-10 21:15:21 -040010from django.contrib.admin.widgets import FilteredSelectMultiple
Siobhan Tully53437282013-04-26 19:30:27 -040011from django.contrib.auth.forms import ReadOnlyPasswordHashField
Tony Mack31c2b8f2013-04-26 20:01:42 -040012from django.contrib.auth.signals import user_logged_in
Tony Mack7130ac32013-03-22 21:58:00 -040013
Siobhan Tully4bc09f22013-04-10 21:15:21 -040014
15class ReadonlyTabularInline(admin.TabularInline):
16 can_delete = False
17 extra = 0
18 editable_fields = []
19
20 def get_readonly_fields(self, request, obj=None):
21 fields = []
22 for field in self.model._meta.get_all_field_names():
23 if (not field == 'id'):
24 if (field not in self.editable_fields):
25 fields.append(field)
26 return fields
27
28 def has_add_permission(self, request):
29 return False
30
31class SliverInline(admin.TabularInline):
32 model = Sliver
Siobhan Tullyd1cd5df2013-05-02 06:02:36 -040033 fields = ['ip', 'name', 'slice', 'numberCores', 'image', 'key', 'node', 'deploymentNetwork']
Siobhan Tully4bc09f22013-04-10 21:15:21 -040034 extra = 0
35
36class SiteInline(admin.TabularInline):
37 model = Site
38 extra = 0
39
Tony Mack00d361f2013-04-28 10:28:42 -040040class SliceInline(admin.TabularInline):
41 model = Slice
42 extra = 0
43
44class UserInline(admin.TabularInline):
45 model = PLUser
46 extra = 0
47
48class RoleInline(admin.TabularInline):
49 model = Role
50 extra = 0
51
Siobhan Tully4bc09f22013-04-10 21:15:21 -040052class NodeInline(admin.TabularInline):
53 model = Node
54 extra = 0
55
Tony Mack5e71a662013-05-03 23:30:41 -040056class PlainTextWidget(forms.HiddenInput):
57 input_type = 'hidden'
58
59 def render(self, name, value, attrs=None):
60 if value is None:
61 value = ''
62 return mark_safe(value + super(PlainTextWidget, self).render(name, value, attrs))
Tony Mack9bcbe4f2013-04-29 08:13:27 -040063
Siobhan Tully4bc09f22013-04-10 21:15:21 -040064class PlanetStackBaseAdmin(admin.ModelAdmin):
65 save_on_top = False
66
Tony Mackfdd4d802013-04-27 13:02:33 -040067class OSModelAdmin(PlanetStackBaseAdmin):
Tony Mackd685bfa2013-05-02 10:09:51 -040068 """Attach client connection to openstack on delete() and save()"""
Tony Mack79748612013-05-01 14:52:03 -040069
Tony Mackfdd4d802013-04-27 13:02:33 -040070 def save_model(self, request, obj, form, change):
Tony Mack38e247c2013-05-05 11:48:14 -040071 if request.user.site:
72 auth = request.session.get('auth', {})
73 auth['tenant'] = request.user.site.login_base
74 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mackfdd4d802013-04-27 13:02:33 -040075 obj.save()
76
77 def delete_model(self, request, obj):
Tony Mack38e247c2013-05-05 11:48:14 -040078 if request.user.site:
79 auth = request.session.get('auth', {})
80 auth['tenant'] = request.user.site.login_base
81 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mackd685bfa2013-05-02 10:09:51 -040082 obj.delete()
Tony Mack9bcbe4f2013-04-29 08:13:27 -040083
Tony Mackd685bfa2013-05-02 10:09:51 -040084class RoleAdmin(OSModelAdmin):
Tony Mack9bcbe4f2013-04-29 08:13:27 -040085 fieldsets = [
86 ('Role', {'fields': ['role_type']})
87 ]
88 list_display = ('role_type',)
Tony Mackfdd4d802013-04-27 13:02:33 -040089
Tony Mack02755d42013-05-02 00:00:10 -040090
Siobhan Tully4bc09f22013-04-10 21:15:21 -040091class DeploymentNetworkAdminForm(forms.ModelForm):
92 sites = forms.ModelMultipleChoiceField(
93 queryset=Site.objects.all(),
94 required=False,
95 widget=FilteredSelectMultiple(
96 verbose_name=('Sites'), is_stacked=False
97 )
98 )
99 class Meta:
100 model = DeploymentNetwork
101
102 def __init__(self, *args, **kwargs):
103 super(DeploymentNetworkAdminForm, self).__init__(*args, **kwargs)
104
105 if self.instance and self.instance.pk:
106 self.fields['sites'].initial = self.instance.sites.all()
107
108 def save(self, commit=True):
109 deploymentNetwork = super(DeploymentNetworkAdminForm, self).save(commit=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400110 if commit:
111 deploymentNetwork.save()
112
113 if deploymentNetwork.pk:
114 deploymentNetwork.sites = self.cleaned_data['sites']
115 self.save_m2m()
116
117 return deploymentNetwork
118
119class DeploymentNetworkAdmin(PlanetStackBaseAdmin):
120 form = DeploymentNetworkAdminForm
121 inlines = [NodeInline,]
122
Tony Mack5cd13202013-05-01 21:48:38 -0400123 def get_formsets(self, request, obj=None):
124 for inline in self.get_inline_instances(request, obj):
125 # hide MyInline in the add view
126 if obj is None:
127 continue
128 # give inline object access to driver and caller
Tony Macked163d72013-05-02 20:05:42 -0400129 auth = request.session.get('auth', {})
130 auth['tenant'] = request.user.site.login_base
131 inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack5cd13202013-05-01 21:48:38 -0400132 yield inline.get_formset(request, obj)
133
Tony Mackfdd4d802013-04-27 13:02:33 -0400134class SiteAdmin(OSModelAdmin):
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400135 fieldsets = [
136 (None, {'fields': ['name', 'site_url', 'enabled', 'is_public', 'login_base']}),
137 ('Location', {'fields': ['latitude', 'longitude']}),
138 ('Deployment Networks', {'fields': ['deployments']})
139 ]
140 list_display = ('name', 'login_base','site_url', 'enabled')
141 filter_horizontal = ('deployments',)
142 inlines = [NodeInline,]
143 search_fields = ['name']
144
Tony Mack5cd13202013-05-01 21:48:38 -0400145 def get_formsets(self, request, obj=None):
146 for inline in self.get_inline_instances(request, obj):
147 # hide MyInline in the add view
148 if obj is None:
149 continue
150 # give inline object access to driver and caller
Tony Mack60722062013-05-02 10:57:04 -0400151 auth = request.session.get('auth', {})
152 auth['tenant'] = request.user.site.login_base
153 inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack5cd13202013-05-01 21:48:38 -0400154 yield inline.get_formset(request, obj)
155
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400156class SitePrivilegeAdmin(PlanetStackBaseAdmin):
Tony Mack00d361f2013-04-28 10:28:42 -0400157 fieldsets = [
158 (None, {'fields': ['user', 'site', 'role']})
159 ]
160 list_display = ('user', 'site', 'role')
161
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400162 def save_model(self, request, obj, form, change):
163 # update openstack connection to use this site/tenant
Tony Mack93048c22013-05-02 11:20:26 -0400164 auth = request.session.get('auth', {})
165 auth['tenant'] = obj.site.login_base
166 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400167 obj.save()
168
169 def delete_model(self, request, obj):
170 # update openstack connection to use this site/tenant
Tony Mack93048c22013-05-02 11:20:26 -0400171 auth = request.session.get('auth', {})
172 auth['tenant'] = obj.site.login_base
173 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400174 obj.delete()
175
Tony Mackfdd4d802013-04-27 13:02:33 -0400176class KeyAdmin(OSModelAdmin):
Tony Mack759b57a2013-04-14 21:03:31 -0400177 fieldsets = [
178 ('Key', {'fields': ['name', 'key', 'type', 'blacklisted', 'user']})
179 ]
180 list_display = ['name', 'key', 'type', 'blacklisted', 'user']
Tony Mack8484bdb2013-04-14 20:26:03 -0400181
Tony Mack956104d2013-04-27 12:36:19 -0400182 def get_queryset(self, request):
183 # get keys user is allowed to see
184 qs = super(KeyAdmin, self).get_queryset(request)
185 if request.user.is_superuser:
186 return qs
Tony Mackfdd4d802013-04-27 13:02:33 -0400187 # users can only see their own keys
Tony Mack956104d2013-04-27 12:36:19 -0400188 return qs.filter(user=request.user)
189
Tony Macke59a7c82013-04-27 11:08:10 -0400190
Tony Mackfdd4d802013-04-27 13:02:33 -0400191class SliceAdmin(OSModelAdmin):
Siobhan Tullyf7914062013-05-02 05:50:30 -0400192 fields = ['name', 'site', 'serviceClass', 'instantiation', 'description', 'slice_url']
193 list_display = ('name', 'site','serviceClass', 'slice_url', 'instantiation')
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400194 inlines = [SliverInline]
195
Tony Mack79748612013-05-01 14:52:03 -0400196 def get_formsets(self, request, obj=None):
197 for inline in self.get_inline_instances(request, obj):
198 # hide MyInline in the add view
199 if obj is None:
200 continue
201 # give inline object access to driver and caller
Tony Mack93048c22013-05-02 11:20:26 -0400202 auth = request.session.get('auth', {})
203 auth['tenant'] = obj.name # meed to connect using slice's tenant
204 inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack79748612013-05-01 14:52:03 -0400205 yield inline.get_formset(request, obj)
206
Tony Mackfdd4d802013-04-27 13:02:33 -0400207 def get_queryset(self, request):
208 qs = super(SliceAdmin, self).get_queryset(request)
209 if request.user.is_superuser:
210 return qs
211 # users can only see slices at their site
212 return qs.filter(site=request.user.site)
213
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400214class SliceMembershipAdmin(PlanetStackBaseAdmin):
Tony Mack00d361f2013-04-28 10:28:42 -0400215 fieldsets = [
216 (None, {'fields': ['user', 'slice', 'role']})
217 ]
218 list_display = ('user', 'slice', 'role')
Tony Mack00d361f2013-04-28 10:28:42 -0400219
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400220 def save_model(self, request, obj, form, change):
Tony Mack93048c22013-05-02 11:20:26 -0400221 # update openstack connection to use this site/tenant
222 auth = request.session.get('auth', {})
223 auth['tenant'] = obj.slice.name
224 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400225 obj.save()
226
227 def delete_model(self, request, obj):
Tony Mack93048c22013-05-02 11:20:26 -0400228 # update openstack connection to use this site/tenant
229 auth = request.session.get('auth', {})
230 auth['tenant'] = obj.slice.name
231 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400232 obj.delete()
233
Tony Mack93048c22013-05-02 11:20:26 -0400234
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400235class SubnetAdmin(PlanetStackBaseAdmin):
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400236 fields = ['cidr', 'ip_version', 'start', 'end', 'slice']
Tony Mackfdd4d802013-04-27 13:02:33 -0400237 list_display = ('slice','cidr', 'start', 'end', 'ip_version')
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400238
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400239 def save_model(self, request, obj, form, change):
Tony Mack951dab42013-05-02 19:51:45 -0400240 # update openstack connection to use this site/tenant
241 auth = request.session.get('auth', {})
242 auth['tenant'] = obj.slice.name
243 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400244 obj.save()
245
246 def delete_model(self, request, obj):
Tony Mack951dab42013-05-02 19:51:45 -0400247 # update openstack connection to use this site/tenant
248 auth = request.session.get('auth', {})
249 auth['tenant'] = obj.slice.name
250 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400251 obj.delete()
252
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400253class ImageAdmin(admin.ModelAdmin):
254 fields = ['image_id', 'name', 'disk_format', 'container_format']
255
256class NodeAdmin(admin.ModelAdmin):
257 list_display = ('name', 'site', 'deploymentNetwork')
258 list_filter = ('deploymentNetwork',)
259
Tony Mackd90cdbf2013-04-16 22:48:40 -0400260
261class SliverForm(forms.ModelForm):
262 class Meta:
263 ip = forms.CharField(widget=PlainTextWidget)
Tony Mack18261812013-05-02 16:39:20 -0400264 instance_name = forms.CharField(widget=PlainTextWidget)
Tony Mackd90cdbf2013-04-16 22:48:40 -0400265 model = Sliver
266 widgets = {
267 'ip': PlainTextWidget(),
Tony Mack18261812013-05-02 16:39:20 -0400268 'instance_name': PlainTextWidget(),
Siobhan Tully53437282013-04-26 19:30:27 -0400269 }
Tony Mackd90cdbf2013-04-16 22:48:40 -0400270
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400271class SliverAdmin(PlanetStackBaseAdmin):
Tony Mackd90cdbf2013-04-16 22:48:40 -0400272 form = SliverForm
Tony Mackcdec0902013-04-15 00:38:49 -0400273 fieldsets = [
Tony Mack18261812013-05-02 16:39:20 -0400274 ('Sliver', {'fields': ['ip', 'instance_name', 'name', 'slice', 'numberCores', 'image', 'key', 'node', 'deploymentNetwork']})
Tony Mackcdec0902013-04-15 00:38:49 -0400275 ]
Tony Mack18261812013-05-02 16:39:20 -0400276 list_display = ['ip', 'instance_name', 'name', 'slice', 'numberCores', 'image', 'key', 'node', 'deploymentNetwork']
Tony Mack53106f32013-04-27 16:43:01 -0400277
278 def save_model(self, request, obj, form, change):
Tony Mack951dab42013-05-02 19:51:45 -0400279 # update openstack connection to use this site/tenant
280 auth = request.session.get('auth', {})
281 auth['tenant'] = obj.slice.name
282 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack53106f32013-04-27 16:43:01 -0400283 obj.save()
284
285 def delete_model(self, request, obj):
Tony Mack951dab42013-05-02 19:51:45 -0400286 # update openstack connection to use this site/tenant
287 auth = request.session.get('auth', {})
288 auth['tenant'] = obj.slice.name
289 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack53106f32013-04-27 16:43:01 -0400290 obj.delete()
Tony Mackcdec0902013-04-15 00:38:49 -0400291
Siobhan Tully53437282013-04-26 19:30:27 -0400292class UserCreationForm(forms.ModelForm):
293 """A form for creating new users. Includes all the required
294 fields, plus a repeated password."""
295 password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
296 password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
297
298 class Meta:
299 model = PLUser
300 fields = ('email', 'firstname', 'lastname', 'phone', 'site')
301
302 def clean_password2(self):
303 # Check that the two password entries match
304 password1 = self.cleaned_data.get("password1")
305 password2 = self.cleaned_data.get("password2")
306 if password1 and password2 and password1 != password2:
307 raise forms.ValidationError("Passwords don't match")
308 return password2
309
310 def save(self, commit=True):
311 # Save the provided password in hashed format
312 user = super(UserCreationForm, self).save(commit=False)
Tony Mackf9f4afb2013-05-01 21:02:12 -0400313 user.password = self.cleaned_data["password1"]
314 #user.set_password(self.cleaned_data["password1"])
Siobhan Tully53437282013-04-26 19:30:27 -0400315 if commit:
316 user.save()
317 return user
318
319
320class UserChangeForm(forms.ModelForm):
321 """A form for updating users. Includes all the fields on
322 the user, but replaces the password field with admin's
323 password hash display field.
324 """
325 password = ReadOnlyPasswordHashField()
326
327 class Meta:
328 model = PLUser
329
330 def clean_password(self):
331 # Regardless of what the user provides, return the initial value.
332 # This is done here, rather than on the field, because the
333 # field does not have access to the initial value
334 return self.initial["password"]
335
336
Tony Mack53106f32013-04-27 16:43:01 -0400337class PLUserAdmin(UserAdmin, OSModelAdmin):
Siobhan Tully53437282013-04-26 19:30:27 -0400338 class Meta:
339 app_label = "core"
340
341 # The forms to add and change user instances
342 form = UserChangeForm
343 add_form = UserCreationForm
344
345 # The fields to be used in displaying the User model.
346 # These override the definitions on the base UserAdmin
347 # that reference specific fields on auth.User.
348 list_display = ('email', 'site', 'firstname', 'lastname', 'last_login')
349 list_filter = ('site',)
350 fieldsets = (
351 (None, {'fields': ('email', 'password')}),
352 ('Personal info', {'fields': ('firstname','lastname','phone','site')}),
353 #('Important dates', {'fields': ('last_login',)}),
354 )
355 add_fieldsets = (
356 (None, {
357 'classes': ('wide',),
358 'fields': ('email', 'firstname', 'lastname', 'phone', 'site', 'password1', 'password2')}
359 ),
360 )
361 search_fields = ('email',)
362 ordering = ('email',)
363 filter_horizontal = ()
364
Tony Mack31c2b8f2013-04-26 20:01:42 -0400365# register a signal that caches the user's credentials when they log in
366def cache_credentials(sender, user, request, **kwds):
367 auth = {'username': request.POST['username'],
368 'password': request.POST['password']}
369 request.session['auth'] = auth
370user_logged_in.connect(cache_credentials)
371
Siobhan Tully53437282013-04-26 19:30:27 -0400372# Now register the new UserAdmin...
373admin.site.register(PLUser, PLUserAdmin)
374# ... and, since we're not using Django's builtin permissions,
375# unregister the Group model from admin.
376admin.site.unregister(Group)
377
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400378admin.site.register(Site, SiteAdmin)
Tony Mack00d361f2013-04-28 10:28:42 -0400379admin.site.register(SitePrivilege, SitePrivilegeAdmin)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400380admin.site.register(Slice, SliceAdmin)
Tony Mack00d361f2013-04-28 10:28:42 -0400381admin.site.register(SliceMembership, SliceMembershipAdmin)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400382admin.site.register(Subnet, SubnetAdmin)
383admin.site.register(Image, ImageAdmin)
384admin.site.register(Node, NodeAdmin)
Tony Mackcdec0902013-04-15 00:38:49 -0400385admin.site.register(Sliver, SliverAdmin)
Tony Mack759b57a2013-04-14 21:03:31 -0400386admin.site.register(Key, KeyAdmin)
Tony Mackfd24d0d2013-04-14 00:59:17 -0400387admin.site.register(Role, RoleAdmin)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400388admin.site.register(DeploymentNetwork, DeploymentNetworkAdmin)
Tony Mack7130ac32013-03-22 21:58:00 -0400389