blob: 1f0744aab30660030456f9924b8ac262ad50e648 [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 Mackd685bfa2013-05-02 10:09:51 -040071 auth = request.session.get('auth', {})
72 auth['tenant'] = request.user.site.login_base
73 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mackfdd4d802013-04-27 13:02:33 -040074 obj.save()
75
76 def delete_model(self, request, obj):
Tony Mackd685bfa2013-05-02 10:09:51 -040077 auth = request.session.get('auth', {})
78 auth['tenant'] = request.user.site.login_base
79 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
80 obj.delete()
Tony Mack9bcbe4f2013-04-29 08:13:27 -040081
Tony Mackd685bfa2013-05-02 10:09:51 -040082class RoleAdmin(OSModelAdmin):
Tony Mack9bcbe4f2013-04-29 08:13:27 -040083 fieldsets = [
84 ('Role', {'fields': ['role_type']})
85 ]
86 list_display = ('role_type',)
Tony Mackfdd4d802013-04-27 13:02:33 -040087
Tony Mack02755d42013-05-02 00:00:10 -040088
Siobhan Tully4bc09f22013-04-10 21:15:21 -040089class DeploymentNetworkAdminForm(forms.ModelForm):
90 sites = forms.ModelMultipleChoiceField(
91 queryset=Site.objects.all(),
92 required=False,
93 widget=FilteredSelectMultiple(
94 verbose_name=('Sites'), is_stacked=False
95 )
96 )
97 class Meta:
98 model = DeploymentNetwork
99
100 def __init__(self, *args, **kwargs):
101 super(DeploymentNetworkAdminForm, self).__init__(*args, **kwargs)
102
103 if self.instance and self.instance.pk:
104 self.fields['sites'].initial = self.instance.sites.all()
105
106 def save(self, commit=True):
107 deploymentNetwork = super(DeploymentNetworkAdminForm, self).save(commit=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400108 if commit:
109 deploymentNetwork.save()
110
111 if deploymentNetwork.pk:
112 deploymentNetwork.sites = self.cleaned_data['sites']
113 self.save_m2m()
114
115 return deploymentNetwork
116
117class DeploymentNetworkAdmin(PlanetStackBaseAdmin):
118 form = DeploymentNetworkAdminForm
119 inlines = [NodeInline,]
120
Tony Mack5cd13202013-05-01 21:48:38 -0400121 def get_formsets(self, request, obj=None):
122 for inline in self.get_inline_instances(request, obj):
123 # hide MyInline in the add view
124 if obj is None:
125 continue
126 # give inline object access to driver and caller
Tony Macked163d72013-05-02 20:05:42 -0400127 auth = request.session.get('auth', {})
128 auth['tenant'] = request.user.site.login_base
129 inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack5cd13202013-05-01 21:48:38 -0400130 yield inline.get_formset(request, obj)
131
Tony Mackfdd4d802013-04-27 13:02:33 -0400132class SiteAdmin(OSModelAdmin):
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400133 fieldsets = [
134 (None, {'fields': ['name', 'site_url', 'enabled', 'is_public', 'login_base']}),
135 ('Location', {'fields': ['latitude', 'longitude']}),
136 ('Deployment Networks', {'fields': ['deployments']})
137 ]
138 list_display = ('name', 'login_base','site_url', 'enabled')
139 filter_horizontal = ('deployments',)
140 inlines = [NodeInline,]
141 search_fields = ['name']
142
Tony Mack5cd13202013-05-01 21:48:38 -0400143 def get_formsets(self, request, obj=None):
144 for inline in self.get_inline_instances(request, obj):
145 # hide MyInline in the add view
146 if obj is None:
147 continue
148 # give inline object access to driver and caller
Tony Mack60722062013-05-02 10:57:04 -0400149 auth = request.session.get('auth', {})
150 auth['tenant'] = request.user.site.login_base
151 inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack5cd13202013-05-01 21:48:38 -0400152 yield inline.get_formset(request, obj)
153
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400154class SitePrivilegeAdmin(PlanetStackBaseAdmin):
Tony Mack00d361f2013-04-28 10:28:42 -0400155 fieldsets = [
156 (None, {'fields': ['user', 'site', 'role']})
157 ]
158 list_display = ('user', 'site', 'role')
159
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400160 def save_model(self, request, obj, form, change):
161 # update openstack connection to use this site/tenant
Tony Mack93048c22013-05-02 11:20:26 -0400162 auth = request.session.get('auth', {})
163 auth['tenant'] = obj.site.login_base
164 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400165 obj.save()
166
167 def delete_model(self, request, obj):
168 # update openstack connection to use this site/tenant
Tony Mack93048c22013-05-02 11:20:26 -0400169 auth = request.session.get('auth', {})
170 auth['tenant'] = obj.site.login_base
171 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400172 obj.delete()
173
Tony Mackfdd4d802013-04-27 13:02:33 -0400174class KeyAdmin(OSModelAdmin):
Tony Mack759b57a2013-04-14 21:03:31 -0400175 fieldsets = [
176 ('Key', {'fields': ['name', 'key', 'type', 'blacklisted', 'user']})
177 ]
178 list_display = ['name', 'key', 'type', 'blacklisted', 'user']
Tony Mack8484bdb2013-04-14 20:26:03 -0400179
Tony Mack956104d2013-04-27 12:36:19 -0400180 def get_queryset(self, request):
181 # get keys user is allowed to see
182 qs = super(KeyAdmin, self).get_queryset(request)
183 if request.user.is_superuser:
184 return qs
Tony Mackfdd4d802013-04-27 13:02:33 -0400185 # users can only see their own keys
Tony Mack956104d2013-04-27 12:36:19 -0400186 return qs.filter(user=request.user)
187
Tony Macke59a7c82013-04-27 11:08:10 -0400188
Tony Mackfdd4d802013-04-27 13:02:33 -0400189class SliceAdmin(OSModelAdmin):
Siobhan Tullyf7914062013-05-02 05:50:30 -0400190 fields = ['name', 'site', 'serviceClass', 'instantiation', 'description', 'slice_url']
191 list_display = ('name', 'site','serviceClass', 'slice_url', 'instantiation')
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400192 inlines = [SliverInline]
193
Tony Mack79748612013-05-01 14:52:03 -0400194 def get_formsets(self, request, obj=None):
195 for inline in self.get_inline_instances(request, obj):
196 # hide MyInline in the add view
197 if obj is None:
198 continue
199 # give inline object access to driver and caller
Tony Mack93048c22013-05-02 11:20:26 -0400200 auth = request.session.get('auth', {})
201 auth['tenant'] = obj.name # meed to connect using slice's tenant
202 inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack79748612013-05-01 14:52:03 -0400203 yield inline.get_formset(request, obj)
204
Tony Mackfdd4d802013-04-27 13:02:33 -0400205 def get_queryset(self, request):
206 qs = super(SliceAdmin, self).get_queryset(request)
207 if request.user.is_superuser:
208 return qs
209 # users can only see slices at their site
210 return qs.filter(site=request.user.site)
211
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400212class SliceMembershipAdmin(PlanetStackBaseAdmin):
Tony Mack00d361f2013-04-28 10:28:42 -0400213 fieldsets = [
214 (None, {'fields': ['user', 'slice', 'role']})
215 ]
216 list_display = ('user', 'slice', 'role')
Tony Mack00d361f2013-04-28 10:28:42 -0400217
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400218 def save_model(self, request, obj, form, change):
Tony Mack93048c22013-05-02 11:20:26 -0400219 # update openstack connection to use this site/tenant
220 auth = request.session.get('auth', {})
221 auth['tenant'] = obj.slice.name
222 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400223 obj.save()
224
225 def delete_model(self, request, obj):
Tony Mack93048c22013-05-02 11:20:26 -0400226 # update openstack connection to use this site/tenant
227 auth = request.session.get('auth', {})
228 auth['tenant'] = obj.slice.name
229 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400230 obj.delete()
231
Tony Mack93048c22013-05-02 11:20:26 -0400232
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400233class SubnetAdmin(PlanetStackBaseAdmin):
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400234 fields = ['cidr', 'ip_version', 'start', 'end', 'slice']
Tony Mackfdd4d802013-04-27 13:02:33 -0400235 list_display = ('slice','cidr', 'start', 'end', 'ip_version')
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400236
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400237 def save_model(self, request, obj, form, change):
Tony Mack951dab42013-05-02 19:51:45 -0400238 # update openstack connection to use this site/tenant
239 auth = request.session.get('auth', {})
240 auth['tenant'] = obj.slice.name
241 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400242 obj.save()
243
244 def delete_model(self, request, obj):
Tony Mack951dab42013-05-02 19:51:45 -0400245 # update openstack connection to use this site/tenant
246 auth = request.session.get('auth', {})
247 auth['tenant'] = obj.slice.name
248 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400249 obj.delete()
250
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400251class ImageAdmin(admin.ModelAdmin):
252 fields = ['image_id', 'name', 'disk_format', 'container_format']
253
254class NodeAdmin(admin.ModelAdmin):
255 list_display = ('name', 'site', 'deploymentNetwork')
256 list_filter = ('deploymentNetwork',)
257
Tony Mackd90cdbf2013-04-16 22:48:40 -0400258
259class SliverForm(forms.ModelForm):
260 class Meta:
261 ip = forms.CharField(widget=PlainTextWidget)
Tony Mack18261812013-05-02 16:39:20 -0400262 instance_name = forms.CharField(widget=PlainTextWidget)
Tony Mackd90cdbf2013-04-16 22:48:40 -0400263 model = Sliver
264 widgets = {
265 'ip': PlainTextWidget(),
Tony Mack18261812013-05-02 16:39:20 -0400266 'instance_name': PlainTextWidget(),
Siobhan Tully53437282013-04-26 19:30:27 -0400267 }
Tony Mackd90cdbf2013-04-16 22:48:40 -0400268
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400269class SliverAdmin(PlanetStackBaseAdmin):
Tony Mackd90cdbf2013-04-16 22:48:40 -0400270 form = SliverForm
Tony Mackcdec0902013-04-15 00:38:49 -0400271 fieldsets = [
Tony Mack18261812013-05-02 16:39:20 -0400272 ('Sliver', {'fields': ['ip', 'instance_name', 'name', 'slice', 'numberCores', 'image', 'key', 'node', 'deploymentNetwork']})
Tony Mackcdec0902013-04-15 00:38:49 -0400273 ]
Tony Mack18261812013-05-02 16:39:20 -0400274 list_display = ['ip', 'instance_name', 'name', 'slice', 'numberCores', 'image', 'key', 'node', 'deploymentNetwork']
Tony Mack53106f32013-04-27 16:43:01 -0400275
276 def save_model(self, request, obj, form, change):
Tony Mack951dab42013-05-02 19:51:45 -0400277 # update openstack connection to use this site/tenant
278 auth = request.session.get('auth', {})
279 auth['tenant'] = obj.slice.name
280 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack53106f32013-04-27 16:43:01 -0400281 obj.save()
282
283 def delete_model(self, request, obj):
Tony Mack951dab42013-05-02 19:51:45 -0400284 # update openstack connection to use this site/tenant
285 auth = request.session.get('auth', {})
286 auth['tenant'] = obj.slice.name
287 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack53106f32013-04-27 16:43:01 -0400288 obj.delete()
Tony Mackcdec0902013-04-15 00:38:49 -0400289
Siobhan Tully53437282013-04-26 19:30:27 -0400290class UserCreationForm(forms.ModelForm):
291 """A form for creating new users. Includes all the required
292 fields, plus a repeated password."""
293 password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
294 password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
295
296 class Meta:
297 model = PLUser
298 fields = ('email', 'firstname', 'lastname', 'phone', 'site')
299
300 def clean_password2(self):
301 # Check that the two password entries match
302 password1 = self.cleaned_data.get("password1")
303 password2 = self.cleaned_data.get("password2")
304 if password1 and password2 and password1 != password2:
305 raise forms.ValidationError("Passwords don't match")
306 return password2
307
308 def save(self, commit=True):
309 # Save the provided password in hashed format
310 user = super(UserCreationForm, self).save(commit=False)
Tony Mackf9f4afb2013-05-01 21:02:12 -0400311 user.password = self.cleaned_data["password1"]
312 #user.set_password(self.cleaned_data["password1"])
Siobhan Tully53437282013-04-26 19:30:27 -0400313 if commit:
314 user.save()
315 return user
316
317
318class UserChangeForm(forms.ModelForm):
319 """A form for updating users. Includes all the fields on
320 the user, but replaces the password field with admin's
321 password hash display field.
322 """
323 password = ReadOnlyPasswordHashField()
324
325 class Meta:
326 model = PLUser
327
328 def clean_password(self):
329 # Regardless of what the user provides, return the initial value.
330 # This is done here, rather than on the field, because the
331 # field does not have access to the initial value
332 return self.initial["password"]
333
334
Tony Mack53106f32013-04-27 16:43:01 -0400335class PLUserAdmin(UserAdmin, OSModelAdmin):
Siobhan Tully53437282013-04-26 19:30:27 -0400336 class Meta:
337 app_label = "core"
338
339 # The forms to add and change user instances
340 form = UserChangeForm
341 add_form = UserCreationForm
342
343 # The fields to be used in displaying the User model.
344 # These override the definitions on the base UserAdmin
345 # that reference specific fields on auth.User.
346 list_display = ('email', 'site', 'firstname', 'lastname', 'last_login')
347 list_filter = ('site',)
348 fieldsets = (
349 (None, {'fields': ('email', 'password')}),
350 ('Personal info', {'fields': ('firstname','lastname','phone','site')}),
351 #('Important dates', {'fields': ('last_login',)}),
352 )
353 add_fieldsets = (
354 (None, {
355 'classes': ('wide',),
356 'fields': ('email', 'firstname', 'lastname', 'phone', 'site', 'password1', 'password2')}
357 ),
358 )
359 search_fields = ('email',)
360 ordering = ('email',)
361 filter_horizontal = ()
362
Tony Mack31c2b8f2013-04-26 20:01:42 -0400363# register a signal that caches the user's credentials when they log in
364def cache_credentials(sender, user, request, **kwds):
365 auth = {'username': request.POST['username'],
366 'password': request.POST['password']}
367 request.session['auth'] = auth
368user_logged_in.connect(cache_credentials)
369
Siobhan Tully53437282013-04-26 19:30:27 -0400370# Now register the new UserAdmin...
371admin.site.register(PLUser, PLUserAdmin)
372# ... and, since we're not using Django's builtin permissions,
373# unregister the Group model from admin.
374admin.site.unregister(Group)
375
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400376admin.site.register(Site, SiteAdmin)
Tony Mack00d361f2013-04-28 10:28:42 -0400377admin.site.register(SitePrivilege, SitePrivilegeAdmin)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400378admin.site.register(Slice, SliceAdmin)
Tony Mack00d361f2013-04-28 10:28:42 -0400379admin.site.register(SliceMembership, SliceMembershipAdmin)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400380admin.site.register(Subnet, SubnetAdmin)
381admin.site.register(Image, ImageAdmin)
382admin.site.register(Node, NodeAdmin)
Tony Mackcdec0902013-04-15 00:38:49 -0400383admin.site.register(Sliver, SliverAdmin)
Tony Mack759b57a2013-04-14 21:03:31 -0400384admin.site.register(Key, KeyAdmin)
Tony Mackfd24d0d2013-04-14 00:59:17 -0400385admin.site.register(Role, RoleAdmin)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400386admin.site.register(DeploymentNetwork, DeploymentNetworkAdmin)
Tony Mack7130ac32013-03-22 21:58:00 -0400387