blob: 55b2ec2675465009f4cd7ca5de8dd07092cd5156 [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 Macke59a7c82013-04-27 11:08:10 -04003from plstackapi.openstack.driver import OpenStackDriver
4from plstackapi.openstack.client import OpenStackClient
5
Tony Mack7130ac32013-03-22 21:58:00 -04006from django.contrib import admin
Siobhan Tully53437282013-04-26 19:30:27 -04007from django.contrib.auth.models import Group
Siobhan Tully4bc09f22013-04-10 21:15:21 -04008from django import forms
Tony Mackd90cdbf2013-04-16 22:48:40 -04009from django.utils.safestring import mark_safe
Tony Mack7130ac32013-03-22 21:58:00 -040010from django.contrib.auth.admin import UserAdmin
Siobhan Tully4bc09f22013-04-10 21:15:21 -040011from django.contrib.admin.widgets import FilteredSelectMultiple
Siobhan Tully53437282013-04-26 19:30:27 -040012from django.contrib.auth.forms import ReadOnlyPasswordHashField
Tony Mack31c2b8f2013-04-26 20:01:42 -040013from django.contrib.auth.signals import user_logged_in
Tony Mack7130ac32013-03-22 21:58:00 -040014
Siobhan Tully4bc09f22013-04-10 21:15:21 -040015
16class ReadonlyTabularInline(admin.TabularInline):
17 can_delete = False
18 extra = 0
19 editable_fields = []
20
21 def get_readonly_fields(self, request, obj=None):
22 fields = []
23 for field in self.model._meta.get_all_field_names():
24 if (not field == 'id'):
25 if (field not in self.editable_fields):
26 fields.append(field)
27 return fields
28
29 def has_add_permission(self, request):
30 return False
31
32class SliverInline(admin.TabularInline):
33 model = Sliver
Siobhan Tullyc47c39d2013-04-17 06:47:26 -040034 fields = ['ip', 'name', 'slice', 'flavor', 'image', 'key', 'node', 'deploymentNetwork']
Siobhan Tully4bc09f22013-04-10 21:15:21 -040035 extra = 0
36
37class SiteInline(admin.TabularInline):
38 model = Site
39 extra = 0
40
Tony Mack00d361f2013-04-28 10:28:42 -040041class SliceInline(admin.TabularInline):
42 model = Slice
43 extra = 0
44
45class UserInline(admin.TabularInline):
46 model = PLUser
47 extra = 0
48
49class RoleInline(admin.TabularInline):
50 model = Role
51 extra = 0
52
Siobhan Tully4bc09f22013-04-10 21:15:21 -040053class NodeInline(admin.TabularInline):
54 model = Node
55 extra = 0
56
Tony Mack9bcbe4f2013-04-29 08:13:27 -040057class PlainTextWidget(forms.Widget):
58 def render(self, _name, value, attrs):
59 return mark_safe(value) if value is not None else ''
60
Siobhan Tully4bc09f22013-04-10 21:15:21 -040061class PlanetStackBaseAdmin(admin.ModelAdmin):
62 save_on_top = False
63
Tony Mackfdd4d802013-04-27 13:02:33 -040064class OSModelAdmin(PlanetStackBaseAdmin):
65 """Attach client connection to openstack on delete() and save()"""
66 def save_model(self, request, obj, form, change):
67 client = OpenStackClient(tenant=request.user.site.login_base, **request.session.get('auth', {}))
68 obj.driver = OpenStackDriver(client=client)
Tony Mack9bcbe4f2013-04-29 08:13:27 -040069 obj.caller = request.user
Tony Mackfdd4d802013-04-27 13:02:33 -040070 obj.save()
71
72 def delete_model(self, request, obj):
73 client = OpenStackClient(tenant=request.user.site.login_base, **request.session.get('auth', {}))
74 obj.driver = OpenStackDriver(client=client)
Tony Mack9bcbe4f2013-04-29 08:13:27 -040075 obj.caller = request.user
Tony Mackfdd4d802013-04-27 13:02:33 -040076 obj.delete()
Tony Mack9bcbe4f2013-04-29 08:13:27 -040077
78class RoleAdmin(OSModelAdmin):
79 fieldsets = [
80 ('Role', {'fields': ['role_type']})
81 ]
82 list_display = ('role_type',)
Tony Mackfdd4d802013-04-27 13:02:33 -040083
Siobhan Tully4bc09f22013-04-10 21:15:21 -040084class DeploymentNetworkAdminForm(forms.ModelForm):
85 sites = forms.ModelMultipleChoiceField(
86 queryset=Site.objects.all(),
87 required=False,
88 widget=FilteredSelectMultiple(
89 verbose_name=('Sites'), is_stacked=False
90 )
91 )
92 class Meta:
93 model = DeploymentNetwork
94
95 def __init__(self, *args, **kwargs):
96 super(DeploymentNetworkAdminForm, self).__init__(*args, **kwargs)
97
98 if self.instance and self.instance.pk:
99 self.fields['sites'].initial = self.instance.sites.all()
100
101 def save(self, commit=True):
102 deploymentNetwork = super(DeploymentNetworkAdminForm, self).save(commit=False)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400103 if commit:
104 deploymentNetwork.save()
105
106 if deploymentNetwork.pk:
107 deploymentNetwork.sites = self.cleaned_data['sites']
108 self.save_m2m()
109
110 return deploymentNetwork
111
112class DeploymentNetworkAdmin(PlanetStackBaseAdmin):
113 form = DeploymentNetworkAdminForm
114 inlines = [NodeInline,]
115
Tony Mackfdd4d802013-04-27 13:02:33 -0400116class SiteAdmin(OSModelAdmin):
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400117 fieldsets = [
118 (None, {'fields': ['name', 'site_url', 'enabled', 'is_public', 'login_base']}),
119 ('Location', {'fields': ['latitude', 'longitude']}),
120 ('Deployment Networks', {'fields': ['deployments']})
121 ]
122 list_display = ('name', 'login_base','site_url', 'enabled')
123 filter_horizontal = ('deployments',)
124 inlines = [NodeInline,]
125 search_fields = ['name']
126
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400127class SitePrivilegeAdmin(PlanetStackBaseAdmin):
Tony Mack00d361f2013-04-28 10:28:42 -0400128 fieldsets = [
129 (None, {'fields': ['user', 'site', 'role']})
130 ]
131 list_display = ('user', 'site', 'role')
132
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400133 def save_model(self, request, obj, form, change):
134 # update openstack connection to use this site/tenant
135 client = OpenStackClient(tenant=obj.site.login_base, **request.session.get('auth', {}))
136 obj.driver = OpenStackDriver(client=client)
137 obj.caller = request.user
138 obj.save()
139
140 def delete_model(self, request, obj):
141 # update openstack connection to use this site/tenant
142 client = OpenStackClient(tenant=obj.site.login_base, **request.session.get('auth', {}))
143 obj.driver = OpenStackDriver(client=client)
144 obj.caller = request.user
145 obj.delete()
146
Tony Mackfdd4d802013-04-27 13:02:33 -0400147class KeyAdmin(OSModelAdmin):
Tony Mack759b57a2013-04-14 21:03:31 -0400148 fieldsets = [
149 ('Key', {'fields': ['name', 'key', 'type', 'blacklisted', 'user']})
150 ]
151 list_display = ['name', 'key', 'type', 'blacklisted', 'user']
Tony Mack8484bdb2013-04-14 20:26:03 -0400152
Tony Mack956104d2013-04-27 12:36:19 -0400153 def get_queryset(self, request):
154 # get keys user is allowed to see
155 qs = super(KeyAdmin, self).get_queryset(request)
156 if request.user.is_superuser:
157 return qs
Tony Mackfdd4d802013-04-27 13:02:33 -0400158 # users can only see their own keys
Tony Mack956104d2013-04-27 12:36:19 -0400159 return qs.filter(user=request.user)
160
Tony Macke59a7c82013-04-27 11:08:10 -0400161
Tony Mackfdd4d802013-04-27 13:02:33 -0400162class SliceAdmin(OSModelAdmin):
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400163 fields = ['name', 'site', 'instantiation', 'description', 'slice_url']
164 list_display = ('name', 'site','slice_url', 'instantiation')
165 inlines = [SliverInline]
166
Tony Mackfdd4d802013-04-27 13:02:33 -0400167 def get_queryset(self, request):
168 qs = super(SliceAdmin, self).get_queryset(request)
169 if request.user.is_superuser:
170 return qs
171 # users can only see slices at their site
172 return qs.filter(site=request.user.site)
173
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400174class SliceMembershipAdmin(PlanetStackBaseAdmin):
Tony Mack00d361f2013-04-28 10:28:42 -0400175 fieldsets = [
176 (None, {'fields': ['user', 'slice', 'role']})
177 ]
178 list_display = ('user', 'slice', 'role')
Tony Mack00d361f2013-04-28 10:28:42 -0400179
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400180 def save_model(self, request, obj, form, change):
181 # update openstack connection to use this slice/tenant
182 client = OpenStackClient(tenant=obj.slice.name, **request.session.get('auth', {}))
183 obj.driver = OpenStackDriver(client=client)
184 obj.caller = request.user
185 obj.save()
186
187 def delete_model(self, request, obj):
188 # update openstack connection to use this slice/tenant
189 client = OpenStackClient(tenant=obj.slice.name, **request.session.get('auth', {}))
190 obj.driver = OpenStackDriver(client=client)
191 obj.caller = request.user
192 obj.delete()
193
194class SubnetAdmin(PlanetStackBaseAdmin):
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400195 fields = ['cidr', 'ip_version', 'start', 'end', 'slice']
Tony Mackfdd4d802013-04-27 13:02:33 -0400196 list_display = ('slice','cidr', 'start', 'end', 'ip_version')
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400197
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400198 def save_model(self, request, obj, form, change):
199 # update openstack connection to use this subnet's slice/tenant
200 client = OpenStackClient(tenant=obj.slice.name, **request.session.get('auth', {}))
201 obj.driver = OpenStackDriver(client=client)
202 obj.caller = request.user
203 obj.save()
204
205 def delete_model(self, request, obj):
206 # update openstack connection to use this subnet's slice/tenant
207 client = OpenStackClient(tenant=obj.slice.name, **request.session.get('auth', {}))
208 obj.driver = OpenStackDriver(client=client)
209 obj.caller = request.user
210 obj.delete()
211
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400212class ImageAdmin(admin.ModelAdmin):
213 fields = ['image_id', 'name', 'disk_format', 'container_format']
214
215class NodeAdmin(admin.ModelAdmin):
216 list_display = ('name', 'site', 'deploymentNetwork')
217 list_filter = ('deploymentNetwork',)
218
Tony Mackd90cdbf2013-04-16 22:48:40 -0400219
220class SliverForm(forms.ModelForm):
221 class Meta:
222 ip = forms.CharField(widget=PlainTextWidget)
223 model = Sliver
224 widgets = {
225 'ip': PlainTextWidget(),
Siobhan Tully53437282013-04-26 19:30:27 -0400226 }
Tony Mackd90cdbf2013-04-16 22:48:40 -0400227
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400228class SliverAdmin(PlanetStackBaseAdmin):
Tony Mackd90cdbf2013-04-16 22:48:40 -0400229 form = SliverForm
Tony Mackcdec0902013-04-15 00:38:49 -0400230 fieldsets = [
Siobhan Tully231f4c82013-05-02 05:47:24 -0400231 ('Sliver', {'fields': ['ip', 'name', 'slice', 'numberCores', 'flavor', 'image', 'key', 'node', 'deploymentNetwork']})
Tony Mackcdec0902013-04-15 00:38:49 -0400232 ]
Tony Mackd90cdbf2013-04-16 22:48:40 -0400233 list_display = ['ip', 'name', 'slice', 'flavor', 'image', 'key', 'node', 'deploymentNetwork']
Tony Mack53106f32013-04-27 16:43:01 -0400234
235 def save_model(self, request, obj, form, change):
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400236 # update openstack connection to use this sliver's slice/tenant
Tony Mack53106f32013-04-27 16:43:01 -0400237 client = OpenStackClient(tenant=obj.slice.name, **request.session.get('auth', {}))
238 obj.driver = OpenStackDriver(client=client)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400239 obj.caller = request.user
Tony Mack53106f32013-04-27 16:43:01 -0400240 obj.save()
241
242 def delete_model(self, request, obj):
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400243 # update openstack connection to use this sliver's slice/tenant
Tony Mack53106f32013-04-27 16:43:01 -0400244 client = OpenStackClient(tenant=obj.slice.name, **request.session.get('auth', {}))
245 obj.driver = OpenStackDriver(client=client)
Tony Mack9bcbe4f2013-04-29 08:13:27 -0400246 obj.caller = request.user
Tony Mack53106f32013-04-27 16:43:01 -0400247 obj.delete()
Tony Mackfdd4d802013-04-27 13:02:33 -0400248
Tony Mackcdec0902013-04-15 00:38:49 -0400249
Siobhan Tully53437282013-04-26 19:30:27 -0400250class UserCreationForm(forms.ModelForm):
251 """A form for creating new users. Includes all the required
252 fields, plus a repeated password."""
253 password1 = forms.CharField(label='Password', widget=forms.PasswordInput)
254 password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput)
255
256 class Meta:
257 model = PLUser
258 fields = ('email', 'firstname', 'lastname', 'phone', 'site')
259
260 def clean_password2(self):
261 # Check that the two password entries match
262 password1 = self.cleaned_data.get("password1")
263 password2 = self.cleaned_data.get("password2")
264 if password1 and password2 and password1 != password2:
265 raise forms.ValidationError("Passwords don't match")
266 return password2
267
268 def save(self, commit=True):
269 # Save the provided password in hashed format
270 user = super(UserCreationForm, self).save(commit=False)
271 user.set_password(self.cleaned_data["password1"])
272 if commit:
273 user.save()
274 return user
275
276
277class UserChangeForm(forms.ModelForm):
278 """A form for updating users. Includes all the fields on
279 the user, but replaces the password field with admin's
280 password hash display field.
281 """
282 password = ReadOnlyPasswordHashField()
283
284 class Meta:
285 model = PLUser
286
287 def clean_password(self):
288 # Regardless of what the user provides, return the initial value.
289 # This is done here, rather than on the field, because the
290 # field does not have access to the initial value
291 return self.initial["password"]
292
293
Tony Mack53106f32013-04-27 16:43:01 -0400294class PLUserAdmin(UserAdmin, OSModelAdmin):
Siobhan Tully53437282013-04-26 19:30:27 -0400295 class Meta:
296 app_label = "core"
297
298 # The forms to add and change user instances
299 form = UserChangeForm
300 add_form = UserCreationForm
301
302 # The fields to be used in displaying the User model.
303 # These override the definitions on the base UserAdmin
304 # that reference specific fields on auth.User.
305 list_display = ('email', 'site', 'firstname', 'lastname', 'last_login')
306 list_filter = ('site',)
307 fieldsets = (
308 (None, {'fields': ('email', 'password')}),
309 ('Personal info', {'fields': ('firstname','lastname','phone','site')}),
310 #('Important dates', {'fields': ('last_login',)}),
311 )
312 add_fieldsets = (
313 (None, {
314 'classes': ('wide',),
315 'fields': ('email', 'firstname', 'lastname', 'phone', 'site', 'password1', 'password2')}
316 ),
317 )
318 search_fields = ('email',)
319 ordering = ('email',)
320 filter_horizontal = ()
321
Tony Mack31c2b8f2013-04-26 20:01:42 -0400322# register a signal that caches the user's credentials when they log in
323def cache_credentials(sender, user, request, **kwds):
324 auth = {'username': request.POST['username'],
325 'password': request.POST['password']}
326 request.session['auth'] = auth
327user_logged_in.connect(cache_credentials)
328
Siobhan Tully53437282013-04-26 19:30:27 -0400329# Now register the new UserAdmin...
330admin.site.register(PLUser, PLUserAdmin)
331# ... and, since we're not using Django's builtin permissions,
332# unregister the Group model from admin.
333admin.site.unregister(Group)
334
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400335admin.site.register(Site, SiteAdmin)
Tony Mack00d361f2013-04-28 10:28:42 -0400336admin.site.register(SitePrivilege, SitePrivilegeAdmin)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400337admin.site.register(Slice, SliceAdmin)
Tony Mack00d361f2013-04-28 10:28:42 -0400338admin.site.register(SliceMembership, SliceMembershipAdmin)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400339admin.site.register(Subnet, SubnetAdmin)
340admin.site.register(Image, ImageAdmin)
341admin.site.register(Node, NodeAdmin)
Tony Mackcdec0902013-04-15 00:38:49 -0400342admin.site.register(Sliver, SliverAdmin)
Tony Mack5cc16ba2013-04-09 10:30:45 -0400343admin.site.register(Flavor)
Tony Mack759b57a2013-04-14 21:03:31 -0400344admin.site.register(Key, KeyAdmin)
Tony Mackfd24d0d2013-04-14 00:59:17 -0400345admin.site.register(Role, RoleAdmin)
Siobhan Tully4bc09f22013-04-10 21:15:21 -0400346admin.site.register(DeploymentNetwork, DeploymentNetworkAdmin)
Tony Mack7130ac32013-03-22 21:58:00 -0400347