blob: d43a5000b6dcbefb11fd703fd3175203b2bb1017 [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 -04004from plstackapi.openstack.driver import OpenStackDriver
5from plstackapi.openstack.client import OpenStackClient
6
Tony Mack7130ac32013-03-22 21:58:00 -04007from django.contrib import admin
Siobhan Tully53437282013-04-26 19:30:27 -04008from django.contrib.auth.models import Group
Siobhan Tully4bc09f22013-04-10 21:15:21 -04009from django import forms
Tony Mackd90cdbf2013-04-16 22:48:40 -040010from django.utils.safestring import mark_safe
Tony Mack7130ac32013-03-22 21:58:00 -040011from django.contrib.auth.admin import UserAdmin
Siobhan Tully4bc09f22013-04-10 21:15:21 -040012from django.contrib.admin.widgets import FilteredSelectMultiple
Siobhan Tully53437282013-04-26 19:30:27 -040013from django.contrib.auth.forms import ReadOnlyPasswordHashField
Tony Mack31c2b8f2013-04-26 20:01:42 -040014from django.contrib.auth.signals import user_logged_in
Tony Mack7130ac32013-03-22 21:58:00 -040015
Siobhan Tully4bc09f22013-04-10 21:15:21 -040016
17class ReadonlyTabularInline(admin.TabularInline):
18 can_delete = False
19 extra = 0
20 editable_fields = []
21
22 def get_readonly_fields(self, request, obj=None):
23 fields = []
24 for field in self.model._meta.get_all_field_names():
25 if (not field == 'id'):
26 if (field not in self.editable_fields):
27 fields.append(field)
28 return fields
29
30 def has_add_permission(self, request):
31 return False
32
33class SliverInline(admin.TabularInline):
34 model = Sliver
Siobhan Tullyd1cd5df2013-05-02 06:02:36 -040035 fields = ['ip', 'name', 'slice', 'numberCores', 'image', 'key', 'node', 'deploymentNetwork']
Siobhan Tully4bc09f22013-04-10 21:15:21 -040036 extra = 0
37
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 Mack9bcbe4f2013-04-29 08:13:27 -040058class PlainTextWidget(forms.Widget):
59 def render(self, _name, value, attrs):
60 return mark_safe(value) if value is not None else ''
61
Siobhan Tully4bc09f22013-04-10 21:15:21 -040062class PlanetStackBaseAdmin(admin.ModelAdmin):
63 save_on_top = False
64
Tony Mackfdd4d802013-04-27 13:02:33 -040065class OSModelAdmin(PlanetStackBaseAdmin):
Tony Mackd685bfa2013-05-02 10:09:51 -040066 """Attach client connection to openstack on delete() and save()"""
Tony Mack79748612013-05-01 14:52:03 -040067
Tony Mackfdd4d802013-04-27 13:02:33 -040068 def save_model(self, request, obj, form, change):
Tony Mackd685bfa2013-05-02 10:09:51 -040069 auth = request.session.get('auth', {})
70 auth['tenant'] = request.user.site.login_base
71 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mackfdd4d802013-04-27 13:02:33 -040072 obj.save()
73
74 def delete_model(self, request, obj):
Tony Mackd685bfa2013-05-02 10:09:51 -040075 auth = request.session.get('auth', {})
76 auth['tenant'] = request.user.site.login_base
77 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
78 obj.delete()
Tony Mack9bcbe4f2013-04-29 08:13:27 -040079
Tony Mackd685bfa2013-05-02 10:09:51 -040080class RoleAdmin(OSModelAdmin):
Tony Mack9bcbe4f2013-04-29 08:13:27 -040081 fieldsets = [
82 ('Role', {'fields': ['role_type']})
83 ]
84 list_display = ('role_type',)
Tony Mackfdd4d802013-04-27 13:02:33 -040085
Tony Mack02755d42013-05-02 00:00:10 -040086
Siobhan Tully4bc09f22013-04-10 21:15:21 -040087class DeploymentNetworkAdminForm(forms.ModelForm):
88 sites = forms.ModelMultipleChoiceField(
89 queryset=Site.objects.all(),
90 required=False,
91 widget=FilteredSelectMultiple(
92 verbose_name=('Sites'), is_stacked=False
93 )
94 )
95 class Meta:
96 model = DeploymentNetwork
97
98 def __init__(self, *args, **kwargs):
99 super(DeploymentNetworkAdminForm, self).__init__(*args, **kwargs)
100
101 if self.instance and self.instance.pk:
102 self.fields['sites'].initial = self.instance.sites.all()
103
104 def save(self, commit=True):
105 deploymentNetwork = super(DeploymentNetworkAdminForm, self).save(commit=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400106 if commit:
107 deploymentNetwork.save()
108
109 if deploymentNetwork.pk:
110 deploymentNetwork.sites = self.cleaned_data['sites']
111 self.save_m2m()
112
113 return deploymentNetwork
114
115class DeploymentNetworkAdmin(PlanetStackBaseAdmin):
116 form = DeploymentNetworkAdminForm
117 inlines = [NodeInline,]
118
Tony Mack5cd13202013-05-01 21:48:38 -0400119 def get_formsets(self, request, obj=None):
120 for inline in self.get_inline_instances(request, obj):
121 # hide MyInline in the add view
122 if obj is None:
123 continue
124 # give inline object access to driver and caller
125 client = OpenStackClient(tenant=request.user.site.login_base, **request.session.get('auth', {}))
126 inline.model.driver = OpenStackDriver(client=client)
127 inline.model.caller = request.user
128 yield inline.get_formset(request, obj)
129
Tony Mackfdd4d802013-04-27 13:02:33 -0400130class SiteAdmin(OSModelAdmin):
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400131 fieldsets = [
132 (None, {'fields': ['name', 'site_url', 'enabled', 'is_public', 'login_base']}),
133 ('Location', {'fields': ['latitude', 'longitude']}),
134 ('Deployment Networks', {'fields': ['deployments']})
135 ]
136 list_display = ('name', 'login_base','site_url', 'enabled')
137 filter_horizontal = ('deployments',)
138 inlines = [NodeInline,]
139 search_fields = ['name']
140
Tony Mack5cd13202013-05-01 21:48:38 -0400141 def get_formsets(self, request, obj=None):
142 for inline in self.get_inline_instances(request, obj):
143 # hide MyInline in the add view
144 if obj is None:
145 continue
146 # give inline object access to driver and caller
Tony Mack60722062013-05-02 10:57:04 -0400147 auth = request.session.get('auth', {})
148 auth['tenant'] = request.user.site.login_base
149 inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack5cd13202013-05-01 21:48:38 -0400150 yield inline.get_formset(request, obj)
151
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400152class SitePrivilegeAdmin(PlanetStackBaseAdmin):
Tony Mack00d361f2013-04-28 10:28:42 -0400153 fieldsets = [
154 (None, {'fields': ['user', 'site', 'role']})
155 ]
156 list_display = ('user', 'site', 'role')
157
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400158 def save_model(self, request, obj, form, change):
159 # update openstack connection to use this site/tenant
Tony Mack93048c22013-05-02 11:20:26 -0400160 auth = request.session.get('auth', {})
161 auth['tenant'] = obj.site.login_base
162 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400163 obj.save()
164
165 def delete_model(self, request, obj):
166 # update openstack connection to use this site/tenant
Tony Mack93048c22013-05-02 11:20:26 -0400167 auth = request.session.get('auth', {})
168 auth['tenant'] = obj.site.login_base
169 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400170 obj.delete()
171
Tony Mackfdd4d802013-04-27 13:02:33 -0400172class KeyAdmin(OSModelAdmin):
Tony Mack759b57a2013-04-14 21:03:31 -0400173 fieldsets = [
174 ('Key', {'fields': ['name', 'key', 'type', 'blacklisted', 'user']})
175 ]
176 list_display = ['name', 'key', 'type', 'blacklisted', 'user']
Tony Mack8484bdb2013-04-14 20:26:03 -0400177
Tony Mack956104d2013-04-27 12:36:19 -0400178 def get_queryset(self, request):
179 # get keys user is allowed to see
180 qs = super(KeyAdmin, self).get_queryset(request)
181 if request.user.is_superuser:
182 return qs
Tony Mackfdd4d802013-04-27 13:02:33 -0400183 # users can only see their own keys
Tony Mack956104d2013-04-27 12:36:19 -0400184 return qs.filter(user=request.user)
185
Tony Macke59a7c82013-04-27 11:08:10 -0400186
Tony Mackfdd4d802013-04-27 13:02:33 -0400187class SliceAdmin(OSModelAdmin):
Siobhan Tullyf7914062013-05-02 05:50:30 -0400188 fields = ['name', 'site', 'serviceClass', 'instantiation', 'description', 'slice_url']
189 list_display = ('name', 'site','serviceClass', 'slice_url', 'instantiation')
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400190 inlines = [SliverInline]
191
Tony Mack79748612013-05-01 14:52:03 -0400192 def get_formsets(self, request, obj=None):
193 for inline in self.get_inline_instances(request, obj):
194 # hide MyInline in the add view
195 if obj is None:
196 continue
197 # give inline object access to driver and caller
Tony Mack93048c22013-05-02 11:20:26 -0400198 auth = request.session.get('auth', {})
199 auth['tenant'] = obj.name # meed to connect using slice's tenant
200 inline.model.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack79748612013-05-01 14:52:03 -0400201 yield inline.get_formset(request, obj)
202
Tony Mackfdd4d802013-04-27 13:02:33 -0400203 def get_queryset(self, request):
204 qs = super(SliceAdmin, self).get_queryset(request)
205 if request.user.is_superuser:
206 return qs
207 # users can only see slices at their site
208 return qs.filter(site=request.user.site)
209
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400210class SliceMembershipAdmin(PlanetStackBaseAdmin):
Tony Mack00d361f2013-04-28 10:28:42 -0400211 fieldsets = [
212 (None, {'fields': ['user', 'slice', 'role']})
213 ]
214 list_display = ('user', 'slice', 'role')
Tony Mack00d361f2013-04-28 10:28:42 -0400215
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400216 def save_model(self, request, obj, form, change):
Tony Mack93048c22013-05-02 11:20:26 -0400217 # update openstack connection to use this site/tenant
218 auth = request.session.get('auth', {})
219 auth['tenant'] = obj.slice.name
220 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400221 obj.save()
222
223 def delete_model(self, request, obj):
Tony Mack93048c22013-05-02 11:20:26 -0400224 # update openstack connection to use this site/tenant
225 auth = request.session.get('auth', {})
226 auth['tenant'] = obj.slice.name
227 obj.os_manager = OpenStackManager(auth=auth, caller=request.user)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400228 obj.delete()
229
Tony Mack93048c22013-05-02 11:20:26 -0400230
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400231class SubnetAdmin(PlanetStackBaseAdmin):
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400232 fields = ['cidr', 'ip_version', 'start', 'end', 'slice']
Tony Mackfdd4d802013-04-27 13:02:33 -0400233 list_display = ('slice','cidr', 'start', 'end', 'ip_version')
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400234
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400235 def save_model(self, request, obj, form, change):
236 # update openstack connection to use this subnet's slice/tenant
237 client = OpenStackClient(tenant=obj.slice.name, **request.session.get('auth', {}))
238 obj.driver = OpenStackDriver(client=client)
239 obj.caller = request.user
240 obj.save()
241
242 def delete_model(self, request, obj):
243 # update openstack connection to use this subnet's slice/tenant
244 client = OpenStackClient(tenant=obj.slice.name, **request.session.get('auth', {}))
245 obj.driver = OpenStackDriver(client=client)
246 obj.caller = request.user
247 obj.delete()
248
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400249class ImageAdmin(admin.ModelAdmin):
250 fields = ['image_id', 'name', 'disk_format', 'container_format']
251
252class NodeAdmin(admin.ModelAdmin):
253 list_display = ('name', 'site', 'deploymentNetwork')
254 list_filter = ('deploymentNetwork',)
255
Tony Mackd90cdbf2013-04-16 22:48:40 -0400256
257class SliverForm(forms.ModelForm):
258 class Meta:
259 ip = forms.CharField(widget=PlainTextWidget)
260 model = Sliver
261 widgets = {
262 'ip': 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 = [
Siobhan Tullyd1cd5df2013-05-02 06:02:36 -0400268 ('Sliver', {'fields': ['ip', 'name', 'slice', 'numberCores', 'image', 'key', 'node', 'deploymentNetwork']})
Tony Mackcdec0902013-04-15 00:38:49 -0400269 ]
Siobhan Tullyd1cd5df2013-05-02 06:02:36 -0400270 list_display = ['ip', '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 Mack9bcbe4f2013-04-29 08:13:27 -0400273 # update openstack connection to use this sliver's slice/tenant
Tony Mack53106f32013-04-27 16:43:01 -0400274 client = OpenStackClient(tenant=obj.slice.name, **request.session.get('auth', {}))
275 obj.driver = OpenStackDriver(client=client)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400276 obj.caller = request.user
Tony Mack53106f32013-04-27 16:43:01 -0400277 obj.save()
278
279 def delete_model(self, request, obj):
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400280 # update openstack connection to use this sliver's slice/tenant
Tony Mack53106f32013-04-27 16:43:01 -0400281 client = OpenStackClient(tenant=obj.slice.name, **request.session.get('auth', {}))
282 obj.driver = OpenStackDriver(client=client)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400283 obj.caller = request.user
Tony Mack53106f32013-04-27 16:43:01 -0400284 obj.delete()
Tony Mackfdd4d802013-04-27 13:02:33 -0400285
Tony Mackcdec0902013-04-15 00:38:49 -0400286
Siobhan Tully53437282013-04-26 19:30:27 -0400287class UserCreationForm(forms.ModelForm):
288 """A form for creating new users. Includes all the required
289 fields, plus a repeated password."""
290 password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
291 password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
292
293 class Meta:
294 model = PLUser
295 fields = ('email', 'firstname', 'lastname', 'phone', 'site')
296
297 def clean_password2(self):
298 # Check that the two password entries match
299 password1 = self.cleaned_data.get("password1")
300 password2 = self.cleaned_data.get("password2")
301 if password1 and password2 and password1 != password2:
302 raise forms.ValidationError("Passwords don't match")
303 return password2
304
305 def save(self, commit=True):
306 # Save the provided password in hashed format
307 user = super(UserCreationForm, self).save(commit=False)
Tony Mackf9f4afb2013-05-01 21:02:12 -0400308 user.password = self.cleaned_data["password1"]
309 #user.set_password(self.cleaned_data["password1"])
Siobhan Tully53437282013-04-26 19:30:27 -0400310 if commit:
311 user.save()
312 return user
313
314
315class UserChangeForm(forms.ModelForm):
316 """A form for updating users. Includes all the fields on
317 the user, but replaces the password field with admin's
318 password hash display field.
319 """
320 password = ReadOnlyPasswordHashField()
321
322 class Meta:
323 model = PLUser
324
325 def clean_password(self):
326 # Regardless of what the user provides, return the initial value.
327 # This is done here, rather than on the field, because the
328 # field does not have access to the initial value
329 return self.initial["password"]
330
331
Tony Mack53106f32013-04-27 16:43:01 -0400332class PLUserAdmin(UserAdmin, OSModelAdmin):
Siobhan Tully53437282013-04-26 19:30:27 -0400333 class Meta:
334 app_label = "core"
335
336 # The forms to add and change user instances
337 form = UserChangeForm
338 add_form = UserCreationForm
339
340 # The fields to be used in displaying the User model.
341 # These override the definitions on the base UserAdmin
342 # that reference specific fields on auth.User.
343 list_display = ('email', 'site', 'firstname', 'lastname', 'last_login')
344 list_filter = ('site',)
345 fieldsets = (
346 (None, {'fields': ('email', 'password')}),
347 ('Personal info', {'fields': ('firstname','lastname','phone','site')}),
348 #('Important dates', {'fields': ('last_login',)}),
349 )
350 add_fieldsets = (
351 (None, {
352 'classes': ('wide',),
353 'fields': ('email', 'firstname', 'lastname', 'phone', 'site', 'password1', 'password2')}
354 ),
355 )
356 search_fields = ('email',)
357 ordering = ('email',)
358 filter_horizontal = ()
359
Tony Mack31c2b8f2013-04-26 20:01:42 -0400360# register a signal that caches the user's credentials when they log in
361def cache_credentials(sender, user, request, **kwds):
362 auth = {'username': request.POST['username'],
363 'password': request.POST['password']}
364 request.session['auth'] = auth
365user_logged_in.connect(cache_credentials)
366
Siobhan Tully53437282013-04-26 19:30:27 -0400367# Now register the new UserAdmin...
368admin.site.register(PLUser, PLUserAdmin)
369# ... and, since we're not using Django's builtin permissions,
370# unregister the Group model from admin.
371admin.site.unregister(Group)
372
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400373admin.site.register(Site, SiteAdmin)
Tony Mack00d361f2013-04-28 10:28:42 -0400374admin.site.register(SitePrivilege, SitePrivilegeAdmin)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400375admin.site.register(Slice, SliceAdmin)
Tony Mack00d361f2013-04-28 10:28:42 -0400376admin.site.register(SliceMembership, SliceMembershipAdmin)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400377admin.site.register(Subnet, SubnetAdmin)
378admin.site.register(Image, ImageAdmin)
379admin.site.register(Node, NodeAdmin)
Tony Mackcdec0902013-04-15 00:38:49 -0400380admin.site.register(Sliver, SliverAdmin)
Tony Mack759b57a2013-04-14 21:03:31 -0400381admin.site.register(Key, KeyAdmin)
Tony Mackfd24d0d2013-04-14 00:59:17 -0400382admin.site.register(Role, RoleAdmin)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400383admin.site.register(DeploymentNetwork, DeploymentNetworkAdmin)
Tony Mack7130ac32013-03-22 21:58:00 -0400384