/
admin.py
372 lines (309 loc) · 12.1 KB
/
admin.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# -*- coding: utf-8 -*-
from django.conf import settings
from django.core.urlresolvers import reverse
from django.db.models import Count
from django.utils.translation import ugettext_lazy as _
from ralph.admin import RalphAdmin, RalphAdminForm, RalphTabularInline, register
from ralph.admin.filters import (
IPFilter,
TagsListFilter,
TreeRelatedAutocompleteFilterWithDescendants
)
from ralph.assets.models.components import Ethernet
from ralph.cross_validator.views import ShowDiffMessageMixin
from ralph.data_center.models.virtual import BaseObjectCluster
from ralph.lib.custom_fields.admin import CustomFieldValueAdminMixin
from ralph.lib.transitions.admin import TransitionAdminMixin
from ralph.networks.forms import SimpleNetworkForm
from ralph.networks.views import NetworkView
from ralph.security.views import SecurityInfo
from ralph.virtual.models import (
CloudFlavor,
CloudHost,
CloudProject,
CloudProvider,
VirtualServer,
VirtualServerType
)
if settings.ENABLE_DNSAAS_INTEGRATION:
from ralph.dns.views import DNSView
class VirtualServerDNSView(DNSView):
namespace = None
class VirtaulServerSecurityInfoView(SecurityInfo):
url_name = 'security_virtualserver_security_info'
class CloudHostSecurityInfoView(SecurityInfo):
url_name = 'security_cloudhost_security_info'
@register(VirtualServerType)
class VirtualServerTypeForm(RalphAdmin):
pass
class VirtualServerForm(RalphAdminForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['parent'].label = _('Hypervisor')
self.fields['parent'].required = True
class VirtualServerNetworkView(NetworkView):
pass
@register(VirtualServer)
class VirtualServerAdmin(
CustomFieldValueAdminMixin,
TransitionAdminMixin,
ShowDiffMessageMixin,
RalphAdmin
):
form = VirtualServerForm
search_fields = ['hostname', 'sn']
list_filter = [
'sn', 'hostname', 'service_env', IPFilter,
'parent', TagsListFilter,
('configuration_path__module', TreeRelatedAutocompleteFilterWithDescendants) # noqa
]
list_display = [
'hostname', 'type', 'sn', 'service_env', 'configuration_path'
]
raw_id_fields = ['parent', 'service_env', 'configuration_path']
fields = [
'hostname', 'type', 'status', 'sn', 'service_env',
'configuration_path', 'parent', 'tags'
]
list_select_related = [
'service_env__service', 'service_env__environment', 'type',
'configuration_path__module'
]
change_views = [
VirtualServerNetworkView,
VirtaulServerSecurityInfoView,
]
if settings.ENABLE_DNSAAS_INTEGRATION:
change_views += [VirtualServerDNSView]
# TODO: add the same tabs as in DCAsset
class ClusterBaseObjectInline(RalphTabularInline):
model = BaseObjectCluster
fk_name = 'base_object'
raw_id_fields = ('cluster',)
extra = 1
verbose_name = _('Base Object')
inlines = [ClusterBaseObjectInline]
class CloudHostTabularInline(RalphTabularInline):
can_delete = False
model = CloudHost
fk_name = 'parent'
fields = ['get_hostname', 'get_hypervisor', 'get_ip_addresses', 'created',
'tags', 'remarks']
readonly_fields = fields
def get_hostname(self, obj):
return '<a href="{}">{}</a>'.format(
reverse("admin:virtual_cloudhost_change", args=(obj.id,)),
obj.hostname
)
get_hostname.short_description = _('Hostname')
get_hostname.allow_tags = True
def get_hypervisor(self, obj):
if obj.hypervisor is None:
return _('Not set')
return '<a href="{}">{}</a>'.format(
reverse(
"admin:data_center_datacenterasset_change",
args=(obj.hypervisor.id,)
),
obj.hypervisor.hostname
)
get_hypervisor.short_description = _('Hypervisor')
get_hypervisor.allow_tags = True
def get_ip_addresses(self, obj):
ips = obj.ethernet_set.values_list(
'ipaddress__address', flat=True
)
if not ips:
return '–'
return '\n'.join(ips)
get_ip_addresses.short_description = _('IP Addresses')
get_ip_addresses.allow_tags = True
def has_add_permission(self, request, obj=None):
return False
def get_queryset(self, *args, **kwargs):
return super().get_queryset(*args, **kwargs).select_related(
'hypervisor',
).prefetch_related('tags')
class CloudHostNetworkForm(SimpleNetworkForm):
class Meta(SimpleNetworkForm.Meta):
fields = ['address', 'hostname']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
for field in ['address', 'hostname']:
self.fields[field].widget.attrs['readonly'] = True
class CloudNetworkInline(RalphTabularInline):
can_delete = False
form = CloudHostNetworkForm
model = Ethernet
def has_add_permission(self, request, obj=None):
return False
@register(CloudHost)
class CloudHostAdmin(CustomFieldValueAdminMixin, RalphAdmin):
list_display = ['hostname', 'get_ip_addresses', 'service_env',
'get_cloudproject', 'cloudflavor_name', 'host_id',
'created', 'image_name', 'get_tags']
list_filter = ['cloudprovider', 'service_env', 'cloudflavor', 'tags']
list_select_related = [
'cloudflavor', 'cloudprovider', 'parent__cloudproject',
'service_env__service', 'service_env__environment'
]
readonly_fields = ['cloudflavor_name', 'created', 'hostname', 'host_id',
'get_cloudproject', 'get_cloudprovider',
'get_cpu', 'get_disk', 'get_hypervisor', 'get_memory',
'modified', 'parent', 'service_env', 'image_name']
search_fields = ['cloudflavor__name', 'hostname', 'host_id']
raw_id_override_parent = {'parent': CloudProject}
inlines = [CloudNetworkInline]
change_views = [
CloudHostSecurityInfoView
]
fieldsets = (
(None, {
'fields': ['hostname', 'get_hypervisor', 'host_id', 'created',
'get_cloudprovider', 'tags', 'remarks']
}),
('Cloud Project', {
'fields': ['get_cloudproject', 'service_env'],
}),
('Components', {
'fields': ['cloudflavor_name', 'get_cpu', 'get_memory', 'get_disk',
'image_name']
}),
)
def get_queryset(self, request):
return super().get_queryset(request).prefetch_related('tags')
def has_delete_permission(self, request, obj=None):
return False
def get_tags(self, obj):
return ', '.join([tag.name for tag in obj.tags.all()])
get_tags.short_description = _('Tags')
def get_cloudprovider(self, obj):
return obj.cloudprovider.name
get_cloudprovider.short_description = _('Cloud provider')
get_cloudprovider.admin_order_field = 'cloudprovider__name'
def get_hostname(self, obj):
return '<a href="{}">{}</a>'.format(
reverse("admin:virtual_cloudhost_change", args=(obj.id,)),
obj.hostname
)
get_hostname.short_description = _('Hostname')
get_hostname.allow_tags = True
def get_hypervisor(self, obj):
if obj.hypervisor is None:
return _('Not set')
return '<a href="{}">{}</a>'.format(
reverse("admin:data_center_datacenterasset_change",
args=(obj.hypervisor.id,)),
obj.hypervisor.hostname
)
get_hypervisor.short_description = _('Hypervisor')
get_hypervisor.admin_order_field = 'hypervisor__hostname'
get_hypervisor.allow_tags = True
def cloudflavor_name(self, obj):
return '<a href="{}">{}</a>'.format(
reverse("admin:virtual_cloudflavor_change",
args=(obj.cloudflavor.id,)),
obj.cloudflavor.name
)
cloudflavor_name.short_description = _('Cloud Flavor')
cloudflavor_name.admin_order_field = 'cloudflavor__name'
cloudflavor_name.allow_tags = True
def get_ip_addresses(self, obj):
ips = obj.ethernet_set.values_list(
'ipaddress__address', flat=True
).select_related('ipaddress').all()
if not ips:
return '–'
return '\n'.join(ips)
get_ip_addresses.short_description = _('IP Addresses')
get_ip_addresses.allow_tags = True
def get_cloudproject(self, obj):
return '<a href="{}">{}</a>'.format(
reverse(
"admin:virtual_cloudproject_change",
args=(obj.parent.id,)
),
obj.parent.cloudproject.name
)
get_cloudproject.short_description = _('Cloud Project')
get_cloudproject.admin_order_field = 'parent'
get_cloudproject.allow_tags = True
get_cloudproject._permission_field = 'parent'
def get_cpu(self, obj):
return obj.cloudflavor.cores
get_cpu.short_description = _('vCPU cores')
def get_memory(self, obj):
return obj.cloudflavor.memory
get_memory.short_description = _('RAM size (MiB)')
def get_disk(self, obj):
return obj.cloudflavor.disk / 1024 if obj.cloudflavor.disk else None
get_disk.short_description = _('Disk size (GiB)')
@register(CloudFlavor)
class CloudFlavorAdmin(RalphAdmin):
list_display = ['name', 'flavor_id', 'cores', 'memory', 'disk', 'get_tags',
'instances_count']
search_fields = ['name']
readonly_fields = ['name', 'cloudprovider', 'flavor_id', 'cores',
'memory', 'disk', 'instances_count']
list_filter = ['cloudprovider', 'tags']
fieldsets = (
('Cloud Flavor', {
'fields': ['name', 'cloudprovider', 'flavor_id', 'tags',
'instances_count']
}),
('Components', {
'fields': ['cores', 'memory', 'disk']
}),
)
def has_delete_permission(self, request, obj=None):
return False
def get_queryset(self, request):
return super().get_queryset(request).prefetch_related(
'virtualcomponent_set__model', 'tags'
).annotate(instances_count=Count('cloudhost'))
def get_tags(self, obj):
return ', '.join([tag.name for tag in obj.tags.all()])
get_tags.short_description = _('Tags')
def get_cpu(self, obj):
return obj.cores
get_cpu.short_description = _('vCPU cores')
def get_memory(self, obj):
return obj.memory
get_memory.short_description = _('RAM size (MiB)')
def get_disk(self, obj):
return obj.disk / 1024
get_disk.short_description = _('Disk size (GiB)')
def instances_count(self, obj):
return obj.instances_count
instances_count.short_description = _('instances count')
instances_count.admin_order_field = 'instances_count'
@register(CloudProject)
class CloudProjectAdmin(CustomFieldValueAdminMixin, RalphAdmin):
fields = ['name', 'project_id', 'cloudprovider', 'service_env', 'tags',
'remarks', 'instances_count']
list_display = ['name', 'service_env', 'instances_count']
list_select_related = ['cloudprovider__name', 'service_env__environment',
'service_env__service']
list_filter = ['service_env', 'cloudprovider', 'tags']
readonly_fields = ['name', 'project_id', 'cloudprovider', 'created',
'instances_count']
search_fields = ['name', 'project_id']
raw_id_fields = ['service_env']
inlines = [CloudHostTabularInline]
def has_delete_permission(self, request, obj=None):
return False
def get_queryset(self, request):
return super().get_queryset(request).annotate(
instances_count=Count('children')
)
def get_cloudprovider(self, obj):
return obj.cloudprovider.name
get_cloudprovider.short_description = _('Cloud provider')
get_cloudprovider.admin_order_field = 'cloudprovider__name'
def instances_count(self, obj):
return obj.instances_count
instances_count.short_description = _('instances count')
instances_count.admin_order_field = 'instances_count'
@register(CloudProvider)
class CloudProviderAdmin(RalphAdmin):
pass