blob: 86b48a293c0065c6133537dfe017d3e318858a04 [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 Mack9bcbe4f2013-04-29 08:13:27 -040056class PlainTextWidget(forms.Widget):
57 def render(self, _name, value, attrs):
58 return mark_safe(value) if value is not None else ''
59
Siobhan Tully4bc09f22013-04-10 21:15:21 -040060class PlanetStackBaseAdmin(admin.ModelAdmin):
61 save_on_top = False
62
Tony Mackfdd4d802013-04-27 13:02:33 -040063class OSModelAdmin(PlanetStackBaseAdmin):
Tony Mackd685bfa2013-05-02 10:09:51 -040064 """Attach client connection to openstack on delete() and save()"""
Tony Mack79748612013-05-01 14:52:03 -040065
Tony Mackfdd4d802013-04-27 13:02:33 -040066 def save_model(self, request, obj, form, change):
Tony Mackd685bfa2013-05-02 10:09:51 -040067 auth = request.session.get('auth', {})
68 auth['tenant'] = request.user.site.login_base
69 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mackfdd4d802013-04-27 13:02:33 -040070 obj.save()
71
72 def delete_model(self, request, obj):
Tony Mackd685bfa2013-05-02 10:09:51 -040073 auth = request.session.get('auth', {})
74 auth['tenant'] = request.user.site.login_base
75 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
76 obj.delete()
Tony Mack9bcbe4f2013-04-29 08:13:27 -040077
Tony Mackd685bfa2013-05-02 10:09:51 -040078class RoleAdmin(OSModelAdmin):
Tony Mack9bcbe4f2013-04-29 08:13:27 -040079 fieldsets = [
80 ('Role', {'fields': ['role_type']})
81 ]
82 list_display = ('role_type',)
Tony Mackfdd4d802013-04-27 13:02:33 -040083
Tony Mack02755d42013-05-02 00:00:10 -040084
Siobhan Tully4bc09f22013-04-10 21:15:21 -040085class DeploymentNetworkAdminForm(forms.ModelForm):
86 sites = forms.ModelMultipleChoiceField(
87 queryset=Site.objects.all(),
88 required=False,
89 widget=FilteredSelectMultiple(
90 verbose_name=('Sites'), is_stacked=False
91 )
92 )
93 class Meta:
94 model = DeploymentNetwork
95
96 def __init__(self, *args, **kwargs):
97 super(DeploymentNetworkAdminForm, self).__init__(*args, **kwargs)
98
99 if self.instance and self.instance.pk:
100 self.fields['sites'].initial = self.instance.sites.all()
101
102 def save(self, commit=True):
103 deploymentNetwork = super(DeploymentNetworkAdminForm, self).save(commit=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400104 if commit:
105 deploymentNetwork.save()
106
107 if deploymentNetwork.pk:
108 deploymentNetwork.sites = self.cleaned_data['sites']
109 self.save_m2m()
110
111 return deploymentNetwork
112
113class DeploymentNetworkAdmin(PlanetStackBaseAdmin):
114 form = DeploymentNetworkAdminForm
115 inlines = [NodeInline,]
116
Tony Mack5cd13202013-05-01 21:48:38 -0400117 def get_formsets(self, request, obj=None):
118 for inline in self.get_inline_instances(request, obj):
119 # hide MyInline in the add view
120 if obj is None:
121 continue
122 # give inline object access to driver and caller
Tony Macked163d72013-05-02 20:05:42 -0400123 auth = request.session.get('auth', {})
124 auth['tenant'] = request.user.site.login_base
125 inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack5cd13202013-05-01 21:48:38 -0400126 yield inline.get_formset(request, obj)
127
Tony Mackfdd4d802013-04-27 13:02:33 -0400128class SiteAdmin(OSModelAdmin):
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400129 fieldsets = [
130 (None, {'fields': ['name', 'site_url', 'enabled', 'is_public', 'login_base']}),
131 ('Location', {'fields': ['latitude', 'longitude']}),
132 ('Deployment Networks', {'fields': ['deployments']})
133 ]
134 list_display = ('name', 'login_base','site_url', 'enabled')
135 filter_horizontal = ('deployments',)
136 inlines = [NodeInline,]
137 search_fields = ['name']
138
Tony Mack5cd13202013-05-01 21:48:38 -0400139 def get_formsets(self, request, obj=None):
140 for inline in self.get_inline_instances(request, obj):
141 # hide MyInline in the add view
142 if obj is None:
143 continue
144 # give inline object access to driver and caller
Tony Mack60722062013-05-02 10:57:04 -0400145 auth = request.session.get('auth', {})
146 auth['tenant'] = request.user.site.login_base
147 inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack5cd13202013-05-01 21:48:38 -0400148 yield inline.get_formset(request, obj)
149
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400150class SitePrivilegeAdmin(PlanetStackBaseAdmin):
Tony Mack00d361f2013-04-28 10:28:42 -0400151 fieldsets = [
152 (None, {'fields': ['user', 'site', 'role']})
153 ]
154 list_display = ('user', 'site', 'role')
155
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400156 def save_model(self, request, obj, form, change):
157 # update openstack connection to use this site/tenant
Tony Mack93048c22013-05-02 11:20:26 -0400158 auth = request.session.get('auth', {})
159 auth['tenant'] = obj.site.login_base
160 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400161 obj.save()
162
163 def delete_model(self, request, obj):
164 # update openstack connection to use this site/tenant
Tony Mack93048c22013-05-02 11:20:26 -0400165 auth = request.session.get('auth', {})
166 auth['tenant'] = obj.site.login_base
167 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400168 obj.delete()
169
Tony Mackfdd4d802013-04-27 13:02:33 -0400170class KeyAdmin(OSModelAdmin):
Tony Mack759b57a2013-04-14 21:03:31 -0400171 fieldsets = [
172 ('Key', {'fields': ['name', 'key', 'type', 'blacklisted', 'user']})
173 ]
174 list_display = ['name', 'key', 'type', 'blacklisted', 'user']
Tony Mack8484bdb2013-04-14 20:26:03 -0400175
Tony Mack956104d2013-04-27 12:36:19 -0400176 def get_queryset(self, request):
177 # get keys user is allowed to see
178 qs = super(KeyAdmin, self).get_queryset(request)
179 if request.user.is_superuser:
180 return qs
Tony Mackfdd4d802013-04-27 13:02:33 -0400181 # users can only see their own keys
Tony Mack956104d2013-04-27 12:36:19 -0400182 return qs.filter(user=request.user)
183
Tony Macke59a7c82013-04-27 11:08:10 -0400184
Tony Mackfdd4d802013-04-27 13:02:33 -0400185class SliceAdmin(OSModelAdmin):
Siobhan Tullyf7914062013-05-02 05:50:30 -0400186 fields = ['name', 'site', 'serviceClass', 'instantiation', 'description', 'slice_url']
187 list_display = ('name', 'site','serviceClass', 'slice_url', 'instantiation')
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400188 inlines = [SliverInline]
189
Tony Mack79748612013-05-01 14:52:03 -0400190 def get_formsets(self, request, obj=None):
191 for inline in self.get_inline_instances(request, obj):
192 # hide MyInline in the add view
193 if obj is None:
194 continue
195 # give inline object access to driver and caller
Tony Mack93048c22013-05-02 11:20:26 -0400196 auth = request.session.get('auth', {})
197 auth['tenant'] = obj.name # meed to connect using slice's tenant
198 inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack79748612013-05-01 14:52:03 -0400199 yield inline.get_formset(request, obj)
200
Tony Mackfdd4d802013-04-27 13:02:33 -0400201 def get_queryset(self, request):
202 qs = super(SliceAdmin, self).get_queryset(request)
203 if request.user.is_superuser:
204 return qs
205 # users can only see slices at their site
206 return qs.filter(site=request.user.site)
207
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400208class SliceMembershipAdmin(PlanetStackBaseAdmin):
Tony Mack00d361f2013-04-28 10:28:42 -0400209 fieldsets = [
210 (None, {'fields': ['user', 'slice', 'role']})
211 ]
212 list_display = ('user', 'slice', 'role')
Tony Mack00d361f2013-04-28 10:28:42 -0400213
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400214 def save_model(self, request, obj, form, change):
Tony Mack93048c22013-05-02 11:20:26 -0400215 # update openstack connection to use this site/tenant
216 auth = request.session.get('auth', {})
217 auth['tenant'] = obj.slice.name
218 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400219 obj.save()
220
221 def delete_model(self, request, obj):
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.delete()
227
Tony Mack93048c22013-05-02 11:20:26 -0400228
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400229class SubnetAdmin(PlanetStackBaseAdmin):
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400230 fields = ['cidr', 'ip_version', 'start', 'end', 'slice']
Tony Mackfdd4d802013-04-27 13:02:33 -0400231 list_display = ('slice','cidr', 'start', 'end', 'ip_version')
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400232
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400233 def save_model(self, request, obj, form, change):
Tony Mack951dab42013-05-02 19:51:45 -0400234 # update openstack connection to use this site/tenant
235 auth = request.session.get('auth', {})
236 auth['tenant'] = obj.slice.name
237 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400238 obj.save()
239
240 def delete_model(self, request, obj):
Tony Mack951dab42013-05-02 19:51:45 -0400241 # update openstack connection to use this site/tenant
242 auth = request.session.get('auth', {})
243 auth['tenant'] = obj.slice.name
244 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400245 obj.delete()
246
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400247class ImageAdmin(admin.ModelAdmin):
248 fields = ['image_id', 'name', 'disk_format', 'container_format']
249
250class NodeAdmin(admin.ModelAdmin):
251 list_display = ('name', 'site', 'deploymentNetwork')
252 list_filter = ('deploymentNetwork',)
253
Tony Mackd90cdbf2013-04-16 22:48:40 -0400254
255class SliverForm(forms.ModelForm):
256 class Meta:
257 ip = forms.CharField(widget=PlainTextWidget)
Tony Mack18261812013-05-02 16:39:20 -0400258 instance_name = forms.CharField(widget=PlainTextWidget)
Tony Mackd90cdbf2013-04-16 22:48:40 -0400259 model = Sliver
260 widgets = {
261 'ip': PlainTextWidget(),
Tony Mack18261812013-05-02 16:39:20 -0400262 'instance_name': PlainTextWidget(),
Siobhan Tully53437282013-04-26 19:30:27 -0400263 }
Tony Mackd90cdbf2013-04-16 22:48:40 -0400264
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400265class SliverAdmin(PlanetStackBaseAdmin):
Tony Mackd90cdbf2013-04-16 22:48:40 -0400266 form = SliverForm
Tony Mackcdec0902013-04-15 00:38:49 -0400267 fieldsets = [
Tony Mack18261812013-05-02 16:39:20 -0400268 ('Sliver', {'fields': ['ip', 'instance_name', 'name', 'slice', 'numberCores', 'image', 'key', 'node', 'deploymentNetwork']})
Tony Mackcdec0902013-04-15 00:38:49 -0400269 ]
Tony Mack18261812013-05-02 16:39:20 -0400270 list_display = ['ip', 'instance_name', 'name', 'slice', 'numberCores', 'image', 'key', 'node', 'deploymentNetwork']
Tony Mack53106f32013-04-27 16:43:01 -0400271
272 def save_model(self, request, obj, form, change):
Tony Mack951dab42013-05-02 19:51:45 -0400273 # update openstack connection to use this site/tenant
274 auth = request.session.get('auth', {})
275 auth['tenant'] = obj.slice.name
276 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack53106f32013-04-27 16:43:01 -0400277 obj.save()
278
279 def delete_model(self, request, obj):
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.delete()
Tony Mackcdec0902013-04-15 00:38:49 -0400285
Siobhan Tully53437282013-04-26 19:30:27 -0400286class UserCreationForm(forms.ModelForm):
287 """A form for creating new users. Includes all the required
288 fields, plus a repeated password."""
289 password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
290 password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
291
292 class Meta:
293 model = PLUser
294 fields = ('email', 'firstname', 'lastname', 'phone', 'site')
295
296 def clean_password2(self):
297 # Check that the two password entries match
298 password1 = self.cleaned_data.get("password1")
299 password2 = self.cleaned_data.get("password2")
300 if password1 and password2 and password1 != password2:
301 raise forms.ValidationError("Passwords don't match")
302 return password2
303
304 def save(self, commit=True):
305 # Save the provided password in hashed format
306 user = super(UserCreationForm, self).save(commit=False)
Tony Mackf9f4afb2013-05-01 21:02:12 -0400307 user.password = self.cleaned_data["password1"]
308 #user.set_password(self.cleaned_data["password1"])
Siobhan Tully53437282013-04-26 19:30:27 -0400309 if commit:
310 user.save()
311 return user
312
313
314class UserChangeForm(forms.ModelForm):
315 """A form for updating users. Includes all the fields on
316 the user, but replaces the password field with admin's
317 password hash display field.
318 """
319 password = ReadOnlyPasswordHashField()
320
321 class Meta:
322 model = PLUser
323
324 def clean_password(self):
325 # Regardless of what the user provides, return the initial value.
326 # This is done here, rather than on the field, because the
327 # field does not have access to the initial value
328 return self.initial["password"]
329
330
Tony Mack53106f32013-04-27 16:43:01 -0400331class PLUserAdmin(UserAdmin, OSModelAdmin):
Siobhan Tully53437282013-04-26 19:30:27 -0400332 class Meta:
333 app_label = "core"
334
335 # The forms to add and change user instances
336 form = UserChangeForm
337 add_form = UserCreationForm
338
339 # The fields to be used in displaying the User model.
340 # These override the definitions on the base UserAdmin
341 # that reference specific fields on auth.User.
342 list_display = ('email', 'site', 'firstname', 'lastname', 'last_login')
343 list_filter = ('site',)
344 fieldsets = (
345 (None, {'fields': ('email', 'password')}),
346 ('Personal info', {'fields': ('firstname','lastname','phone','site')}),
347 #('Important dates', {'fields': ('last_login',)}),
348 )
349 add_fieldsets = (
350 (None, {
351 'classes': ('wide',),
352 'fields': ('email', 'firstname', 'lastname', 'phone', 'site', 'password1', 'password2')}
353 ),
354 )
355 search_fields = ('email',)
356 ordering = ('email',)
357 filter_horizontal = ()
358
Tony Mack31c2b8f2013-04-26 20:01:42 -0400359# register a signal that caches the user's credentials when they log in
360def cache_credentials(sender, user, request, **kwds):
361 auth = {'username': request.POST['username'],
362 'password': request.POST['password']}
363 request.session['auth'] = auth
364user_logged_in.connect(cache_credentials)
365
Siobhan Tully53437282013-04-26 19:30:27 -0400366# Now register the new UserAdmin...
367admin.site.register(PLUser, PLUserAdmin)
368# ... and, since we're not using Django's builtin permissions,
369# unregister the Group model from admin.
370admin.site.unregister(Group)
371
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400372admin.site.register(Site, SiteAdmin)
Tony Mack00d361f2013-04-28 10:28:42 -0400373admin.site.register(SitePrivilege, SitePrivilegeAdmin)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400374admin.site.register(Slice, SliceAdmin)
Tony Mack00d361f2013-04-28 10:28:42 -0400375admin.site.register(SliceMembership, SliceMembershipAdmin)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400376admin.site.register(Subnet, SubnetAdmin)
377admin.site.register(Image, ImageAdmin)
378admin.site.register(Node, NodeAdmin)
Tony Mackcdec0902013-04-15 00:38:49 -0400379admin.site.register(Sliver, SliverAdmin)
Tony Mack759b57a2013-04-14 21:03:31 -0400380admin.site.register(Key, KeyAdmin)
Tony Mackfd24d0d2013-04-14 00:59:17 -0400381admin.site.register(Role, RoleAdmin)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400382admin.site.register(DeploymentNetwork, DeploymentNetworkAdmin)
Tony Mack7130ac32013-03-22 21:58:00 -0400383