blob: 2365225edd5017c0273b4f76a416def4e8a3611a [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
Tony Mack3777b012013-05-07 21:38:06 -040033 fields = ['ip', 'instance_name', 'slice', 'numberCores', 'image', 'key', 'node', 'deploymentNetwork']
Siobhan Tully4bc09f22013-04-10 21:15:21 -040034 extra = 0
Tony Mack3777b012013-05-07 21:38:06 -040035 #readonly_fields = ['ip', 'instance_name', 'image']
36 readonly_fields = ['ip', 'instance_name']
Siobhan Tully4bc09f22013-04-10 21:15:21 -040037
38class SiteInline(admin.TabularInline):
39 model = Site
40 extra = 0
41
Tony Mack00d361f2013-04-28 10:28:42 -040042class SliceInline(admin.TabularInline):
43 model = Slice
44 extra = 0
45
46class UserInline(admin.TabularInline):
47 model = PLUser
48 extra = 0
49
50class RoleInline(admin.TabularInline):
51 model = Role
52 extra = 0
53
Siobhan Tully4bc09f22013-04-10 21:15:21 -040054class NodeInline(admin.TabularInline):
55 model = Node
56 extra = 0
57
Tony Mack5e71a662013-05-03 23:30:41 -040058class PlainTextWidget(forms.HiddenInput):
59 input_type = 'hidden'
60
61 def render(self, name, value, attrs=None):
62 if value is None:
63 value = ''
Tony Mack1d6b85f2013-05-07 18:49:14 -040064 return mark_safe(str(value) + super(PlainTextWidget, self).render(name, value, attrs))
Tony Mack9bcbe4f2013-04-29 08:13:27 -040065
Siobhan Tully4bc09f22013-04-10 21:15:21 -040066class PlanetStackBaseAdmin(admin.ModelAdmin):
67 save_on_top = False
68
Tony Mackfdd4d802013-04-27 13:02:33 -040069class OSModelAdmin(PlanetStackBaseAdmin):
Tony Mackd685bfa2013-05-02 10:09:51 -040070 """Attach client connection to openstack on delete() and save()"""
Tony Mack79748612013-05-01 14:52:03 -040071
Tony Mackfdd4d802013-04-27 13:02:33 -040072 def save_model(self, request, obj, form, change):
Tony Mack38e247c2013-05-05 11:48:14 -040073 if request.user.site:
74 auth = request.session.get('auth', {})
75 auth['tenant'] = request.user.site.login_base
76 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mackfdd4d802013-04-27 13:02:33 -040077 obj.save()
78
79 def delete_model(self, request, obj):
Tony Mack38e247c2013-05-05 11:48:14 -040080 if request.user.site:
81 auth = request.session.get('auth', {})
82 auth['tenant'] = request.user.site.login_base
83 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mackd685bfa2013-05-02 10:09:51 -040084 obj.delete()
Tony Mack9bcbe4f2013-04-29 08:13:27 -040085
Tony Mackd685bfa2013-05-02 10:09:51 -040086class RoleAdmin(OSModelAdmin):
Tony Mack9bcbe4f2013-04-29 08:13:27 -040087 fieldsets = [
88 ('Role', {'fields': ['role_type']})
89 ]
90 list_display = ('role_type',)
Tony Mackfdd4d802013-04-27 13:02:33 -040091
Tony Mack02755d42013-05-02 00:00:10 -040092
Siobhan Tully4bc09f22013-04-10 21:15:21 -040093class DeploymentNetworkAdminForm(forms.ModelForm):
94 sites = forms.ModelMultipleChoiceField(
95 queryset=Site.objects.all(),
96 required=False,
97 widget=FilteredSelectMultiple(
98 verbose_name=('Sites'), is_stacked=False
99 )
100 )
101 class Meta:
102 model = DeploymentNetwork
103
104 def __init__(self, *args, **kwargs):
105 super(DeploymentNetworkAdminForm, self).__init__(*args, **kwargs)
106
107 if self.instance and self.instance.pk:
108 self.fields['sites'].initial = self.instance.sites.all()
109
110 def save(self, commit=True):
111 deploymentNetwork = super(DeploymentNetworkAdminForm, self).save(commit=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400112 if commit:
113 deploymentNetwork.save()
114
115 if deploymentNetwork.pk:
116 deploymentNetwork.sites = self.cleaned_data['sites']
117 self.save_m2m()
118
119 return deploymentNetwork
120
121class DeploymentNetworkAdmin(PlanetStackBaseAdmin):
122 form = DeploymentNetworkAdminForm
123 inlines = [NodeInline,]
124
Tony Mack5cd13202013-05-01 21:48:38 -0400125 def get_formsets(self, request, obj=None):
126 for inline in self.get_inline_instances(request, obj):
127 # hide MyInline in the add view
128 if obj is None:
129 continue
130 # give inline object access to driver and caller
Tony Macked163d72013-05-02 20:05:42 -0400131 auth = request.session.get('auth', {})
132 auth['tenant'] = request.user.site.login_base
133 inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack5cd13202013-05-01 21:48:38 -0400134 yield inline.get_formset(request, obj)
135
Tony Mackfdd4d802013-04-27 13:02:33 -0400136class SiteAdmin(OSModelAdmin):
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400137 fieldsets = [
138 (None, {'fields': ['name', 'site_url', 'enabled', 'is_public', 'login_base']}),
139 ('Location', {'fields': ['latitude', 'longitude']}),
140 ('Deployment Networks', {'fields': ['deployments']})
141 ]
142 list_display = ('name', 'login_base','site_url', 'enabled')
143 filter_horizontal = ('deployments',)
144 inlines = [NodeInline,]
145 search_fields = ['name']
146
Tony Mack5cd13202013-05-01 21:48:38 -0400147 def get_formsets(self, request, obj=None):
148 for inline in self.get_inline_instances(request, obj):
149 # hide MyInline in the add view
150 if obj is None:
151 continue
152 # give inline object access to driver and caller
Tony Mack60722062013-05-02 10:57:04 -0400153 auth = request.session.get('auth', {})
154 auth['tenant'] = request.user.site.login_base
155 inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack5cd13202013-05-01 21:48:38 -0400156 yield inline.get_formset(request, obj)
157
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400158class SitePrivilegeAdmin(PlanetStackBaseAdmin):
Tony Mack00d361f2013-04-28 10:28:42 -0400159 fieldsets = [
160 (None, {'fields': ['user', 'site', 'role']})
161 ]
162 list_display = ('user', 'site', 'role')
163
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400164 def save_model(self, request, obj, form, change):
165 # update openstack connection to use this site/tenant
Tony Mack93048c22013-05-02 11:20:26 -0400166 auth = request.session.get('auth', {})
167 auth['tenant'] = obj.site.login_base
168 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400169 obj.save()
170
171 def delete_model(self, request, obj):
172 # update openstack connection to use this site/tenant
Tony Mack93048c22013-05-02 11:20:26 -0400173 auth = request.session.get('auth', {})
174 auth['tenant'] = obj.site.login_base
175 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400176 obj.delete()
177
Tony Mackfdd4d802013-04-27 13:02:33 -0400178class KeyAdmin(OSModelAdmin):
Tony Mack759b57a2013-04-14 21:03:31 -0400179 fieldsets = [
Tony Mack416c0f22013-05-09 16:59:09 -0400180 ('Key', {'fields': ['key', 'type', 'blacklisted', 'user']})
Tony Mack759b57a2013-04-14 21:03:31 -0400181 ]
Tony Mack416c0f22013-05-09 16:59:09 -0400182 list_display = ['key', 'type', 'blacklisted', 'user']
Tony Mack8484bdb2013-04-14 20:26:03 -0400183
Tony Mack956104d2013-04-27 12:36:19 -0400184 def get_queryset(self, request):
185 # get keys user is allowed to see
186 qs = super(KeyAdmin, self).get_queryset(request)
187 if request.user.is_superuser:
188 return qs
Tony Mackfdd4d802013-04-27 13:02:33 -0400189 # users can only see their own keys
Tony Mack956104d2013-04-27 12:36:19 -0400190 return qs.filter(user=request.user)
191
Tony Mackfdd4d802013-04-27 13:02:33 -0400192class SliceAdmin(OSModelAdmin):
Tony Mack659dd522013-05-06 17:06:37 -0400193 fields = ['name', 'site', 'serviceClass', 'description', 'slice_url']
194 list_display = ('name', 'site','serviceClass', 'slice_url')
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400195 inlines = [SliverInline]
196
Tony Mack79748612013-05-01 14:52:03 -0400197 def get_formsets(self, request, obj=None):
198 for inline in self.get_inline_instances(request, obj):
199 # hide MyInline in the add view
200 if obj is None:
201 continue
202 # give inline object access to driver and caller
Tony Mack93048c22013-05-02 11:20:26 -0400203 auth = request.session.get('auth', {})
204 auth['tenant'] = obj.name # meed to connect using slice's tenant
205 inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack79748612013-05-01 14:52:03 -0400206 yield inline.get_formset(request, obj)
207
Tony Mackfdd4d802013-04-27 13:02:33 -0400208 def get_queryset(self, request):
209 qs = super(SliceAdmin, self).get_queryset(request)
210 if request.user.is_superuser:
211 return qs
212 # users can only see slices at their site
213 return qs.filter(site=request.user.site)
214
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400215class SliceMembershipAdmin(PlanetStackBaseAdmin):
Tony Mack00d361f2013-04-28 10:28:42 -0400216 fieldsets = [
217 (None, {'fields': ['user', 'slice', 'role']})
218 ]
219 list_display = ('user', 'slice', 'role')
Tony Mack00d361f2013-04-28 10:28:42 -0400220
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400221 def save_model(self, request, obj, form, change):
Tony Mack93048c22013-05-02 11:20:26 -0400222 # update openstack connection to use this site/tenant
223 auth = request.session.get('auth', {})
224 auth['tenant'] = obj.slice.name
225 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400226 obj.save()
227
228 def delete_model(self, request, obj):
Tony Mack93048c22013-05-02 11:20:26 -0400229 # update openstack connection to use this site/tenant
230 auth = request.session.get('auth', {})
231 auth['tenant'] = obj.slice.name
232 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400233 obj.delete()
234
Tony Mack93048c22013-05-02 11:20:26 -0400235
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400236class ImageAdmin(admin.ModelAdmin):
237 fields = ['image_id', 'name', 'disk_format', 'container_format']
238
239class NodeAdmin(admin.ModelAdmin):
240 list_display = ('name', 'site', 'deploymentNetwork')
241 list_filter = ('deploymentNetwork',)
242
Tony Mackd90cdbf2013-04-16 22:48:40 -0400243
244class SliverForm(forms.ModelForm):
245 class Meta:
Tony Mack1d6b85f2013-05-07 18:49:14 -0400246 model = Sliver
Tony Mackd90cdbf2013-04-16 22:48:40 -0400247 ip = forms.CharField(widget=PlainTextWidget)
Tony Mack18261812013-05-02 16:39:20 -0400248 instance_name = forms.CharField(widget=PlainTextWidget)
Tony Mackd90cdbf2013-04-16 22:48:40 -0400249 widgets = {
250 'ip': PlainTextWidget(),
Tony Mack18261812013-05-02 16:39:20 -0400251 'instance_name': PlainTextWidget(),
Siobhan Tully53437282013-04-26 19:30:27 -0400252 }
Tony Mackd90cdbf2013-04-16 22:48:40 -0400253
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400254class SliverAdmin(PlanetStackBaseAdmin):
Tony Mackd90cdbf2013-04-16 22:48:40 -0400255 form = SliverForm
Tony Mackcdec0902013-04-15 00:38:49 -0400256 fieldsets = [
Tony Mack10082022013-05-06 17:15:00 -0400257 ('Sliver', {'fields': ['ip', 'instance_name', 'slice', 'numberCores', 'image', 'key', 'node', 'deploymentNetwork']})
Tony Mackcdec0902013-04-15 00:38:49 -0400258 ]
Tony Mack10082022013-05-06 17:15:00 -0400259 list_display = ['ip', 'instance_name', 'slice', 'numberCores', 'image', 'key', 'node', 'deploymentNetwork']
Tony Mack53106f32013-04-27 16:43:01 -0400260
Tony Mack1d6b85f2013-05-07 18:49:14 -0400261 def get_formsets(self, request, obj=None):
262 # make some fields read only if we are updating an existing record
263 if obj == None:
264 #self.readonly_fields = ('ip', 'instance_name')
265 self.readonly_fields = ()
266 else:
267 self.readonly_fields = ('ip', 'instance_name', 'slice', 'image', 'key')
268
269 for inline in self.get_inline_instances(request, obj):
270 # hide MyInline in the add view
271 if obj is None:
272 continue
273 # give inline object access to driver and caller
274 auth = request.session.get('auth', {})
275 auth['tenant'] = obj.name # meed to connect using slice's tenant
276 inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user)
277 yield inline.get_formset(request, obj)
278
Tony Mack53106f32013-04-27 16:43:01 -0400279 def save_model(self, request, obj, form, change):
Tony Mack951dab42013-05-02 19:51:45 -0400280 # update openstack connection to use this site/tenant
281 auth = request.session.get('auth', {})
282 auth['tenant'] = obj.slice.name
283 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack53106f32013-04-27 16:43:01 -0400284 obj.save()
285
286 def delete_model(self, request, obj):
Tony Mack951dab42013-05-02 19:51:45 -0400287 # update openstack connection to use this site/tenant
288 auth = request.session.get('auth', {})
289 auth['tenant'] = obj.slice.name
290 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack53106f32013-04-27 16:43:01 -0400291 obj.delete()
Tony Mackcdec0902013-04-15 00:38:49 -0400292
Siobhan Tully53437282013-04-26 19:30:27 -0400293class UserCreationForm(forms.ModelForm):
294 """A form for creating new users. Includes all the required
295 fields, plus a repeated password."""
296 password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
297 password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
298
299 class Meta:
300 model = PLUser
301 fields = ('email', 'firstname', 'lastname', 'phone', 'site')
302
303 def clean_password2(self):
304 # Check that the two password entries match
305 password1 = self.cleaned_data.get("password1")
306 password2 = self.cleaned_data.get("password2")
307 if password1 and password2 and password1 != password2:
308 raise forms.ValidationError("Passwords don't match")
309 return password2
310
311 def save(self, commit=True):
312 # Save the provided password in hashed format
313 user = super(UserCreationForm, self).save(commit=False)
Tony Mackf9f4afb2013-05-01 21:02:12 -0400314 user.password = self.cleaned_data["password1"]
315 #user.set_password(self.cleaned_data["password1"])
Siobhan Tully53437282013-04-26 19:30:27 -0400316 if commit:
317 user.save()
318 return user
319
320
321class UserChangeForm(forms.ModelForm):
322 """A form for updating users. Includes all the fields on
323 the user, but replaces the password field with admin's
324 password hash display field.
325 """
326 password = ReadOnlyPasswordHashField()
327
328 class Meta:
329 model = PLUser
330
331 def clean_password(self):
332 # Regardless of what the user provides, return the initial value.
333 # This is done here, rather than on the field, because the
334 # field does not have access to the initial value
335 return self.initial["password"]
336
337
Tony Mack53106f32013-04-27 16:43:01 -0400338class PLUserAdmin(UserAdmin, OSModelAdmin):
Siobhan Tully53437282013-04-26 19:30:27 -0400339 class Meta:
340 app_label = "core"
341
342 # The forms to add and change user instances
343 form = UserChangeForm
344 add_form = UserCreationForm
345
346 # The fields to be used in displaying the User model.
347 # These override the definitions on the base UserAdmin
348 # that reference specific fields on auth.User.
Tony Mack416c0f22013-05-09 16:59:09 -0400349 list_display = ('email', 'site', 'firstname', 'lastname', 'is_admin', 'last_login')
Siobhan Tully53437282013-04-26 19:30:27 -0400350 list_filter = ('site',)
351 fieldsets = (
352 (None, {'fields': ('email', 'password')}),
Tony Mack416c0f22013-05-09 16:59:09 -0400353 ('Personal info', {'fields': ('firstname','lastname','phone', 'is_admin', 'site')}),
Siobhan Tully53437282013-04-26 19:30:27 -0400354 #('Important dates', {'fields': ('last_login',)}),
355 )
356 add_fieldsets = (
357 (None, {
358 'classes': ('wide',),
Tony Mack416c0f22013-05-09 16:59:09 -0400359 'fields': ('email', 'firstname', 'lastname', 'phone', 'site', 'is_admin', 'password1', 'password2')}
Siobhan Tully53437282013-04-26 19:30:27 -0400360 ),
361 )
362 search_fields = ('email',)
363 ordering = ('email',)
364 filter_horizontal = ()
365
Tony Mack31c2b8f2013-04-26 20:01:42 -0400366# register a signal that caches the user's credentials when they log in
367def cache_credentials(sender, user, request, **kwds):
368 auth = {'username': request.POST['username'],
369 'password': request.POST['password']}
370 request.session['auth'] = auth
371user_logged_in.connect(cache_credentials)
372
Siobhan Tully53437282013-04-26 19:30:27 -0400373# Now register the new UserAdmin...
374admin.site.register(PLUser, PLUserAdmin)
375# ... and, since we're not using Django's builtin permissions,
376# unregister the Group model from admin.
377admin.site.unregister(Group)
378
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400379admin.site.register(Site, SiteAdmin)
Tony Mack00d361f2013-04-28 10:28:42 -0400380admin.site.register(SitePrivilege, SitePrivilegeAdmin)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400381admin.site.register(Slice, SliceAdmin)
Tony Mack00d361f2013-04-28 10:28:42 -0400382admin.site.register(SliceMembership, SliceMembershipAdmin)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400383admin.site.register(Node, NodeAdmin)
Tony Mackcdec0902013-04-15 00:38:49 -0400384admin.site.register(Sliver, SliverAdmin)
Tony Mack759b57a2013-04-14 21:03:31 -0400385admin.site.register(Key, KeyAdmin)
Tony Mackfd24d0d2013-04-14 00:59:17 -0400386admin.site.register(Role, RoleAdmin)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400387admin.site.register(DeploymentNetwork, DeploymentNetworkAdmin)
Tony Mack7130ac32013-03-22 21:58:00 -0400388