Adds pxe field to host in admin, minor refactor
This commit is contained in:
1 parent
33c6d61d2d
commit
836a4d136e
4 files changed
+19
-19
No files matched your search
+4
-4
@@ -20,17 +20,17 @@ class HostAdmin(admin.ModelAdmin):
|
||||
ordering = ('hostname',)
|
||||
inlines = [InterfaceInline]
|
||||
list_display = ['hostname', 'owner', 'host_type', 'location', 'mac_addresses', 'ip_addresses', 'in_domain',
|
||||
'ipv6_enabled']
|
||||
list_filter = ['host_type', 'owner', 'location']
|
||||
'pxe_installable', 'ipv6_enabled']
|
||||
list_filter = ['host_type', 'owner', 'location', 'pxe_installable']
|
||||
readonly_fields = ['kerberos_principal_name', 'kerberos_principal_created_date',
|
||||
'kerberos_principal_created']
|
||||
search_fields = ['hostname', 'location', 'interface__macaddr', 'interface__ip4address__address']
|
||||
fieldsets = (
|
||||
('Owner Information', {
|
||||
'fields': ('owner', 'location', 'description' )
|
||||
'fields': ('owner', 'location', 'description')
|
||||
}),
|
||||
('Hardware and Software Information', {
|
||||
'fields': (('brand', 'model', 'serial_number'), ('hostname', 'host_type'), ('operating_system', 'virtual'))
|
||||
'fields': (('hostname', 'host_type'), ('pxe_key', 'pxe_installable'), ('brand', 'model'), 'serial_number', ('operating_system', 'virtual'))
|
||||
}),
|
||||
# FIXME: Not in use
|
||||
# ('Domain and Kerberos Information', {
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
from mdb.models import Host, Interface, Domain
|
||||
from mdb.models import Host
|
||||
from rest_framework import serializers
|
||||
from rest_framework.exceptions import ValidationError
|
||||
|
||||
|
||||
class HostValidateSecretSerializer(serializers.Serializer):
|
||||
class HostPXEValidateSerializer(serializers.Serializer):
|
||||
certname = serializers.CharField(required=True)
|
||||
pxe_key = serializers.CharField(required=True)
|
||||
host = None
|
||||
|
||||
def _host_in_domain_exists(self, attrs):
|
||||
def _get_host_by_fqdn(self, attrs):
|
||||
fqdn = attrs['certname']
|
||||
host_name = fqdn.split('.')[0]
|
||||
domain = '.'.join(fqdn.split('.')[1:])
|
||||
@@ -20,19 +20,19 @@ class HostValidateSecretSerializer(serializers.Serializer):
|
||||
|
||||
def validate(self, attrs):
|
||||
# custom validation
|
||||
host = self._host_in_domain_exists(attrs)
|
||||
host = self._get_host_by_fqdn(attrs)
|
||||
|
||||
if host is None:
|
||||
raise ValidationError('Host \'{}\' not found'.format(attrs['certname']))
|
||||
raise ValidationError('Host \'{}\' not found.'.format(attrs['certname']))
|
||||
|
||||
if not host.pxe_installable:
|
||||
raise ValidationError('Host is marked as not installable via PXE.')
|
||||
raise ValidationError('Host \'{}\' is marked as not installable via PXE.'.format(attrs['certname']))
|
||||
|
||||
if not host.pxe_key:
|
||||
raise ValidationError('Host has no pxe_key.')
|
||||
raise ValidationError('Host \'{}\' has no pxe_key.'.format(attrs['certname']))
|
||||
|
||||
if host.pxe_key != attrs['pxe_key']:
|
||||
raise ValidationError('Supplied pxe_key does not match host.')
|
||||
raise ValidationError('Supplied pxe_key does not match host \'{}\'.'.format(attrs['certname']))
|
||||
|
||||
self.host = host
|
||||
|
||||
|
||||
+5
-5
@@ -3,18 +3,18 @@ from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
|
||||
from mdb.api.serializers import HostValidateSecretSerializer
|
||||
from mdb.api.serializers import HostPXEValidateSerializer
|
||||
|
||||
|
||||
class ValidatePuppetHostSecret(APIView):
|
||||
class HostPXEValidate(APIView):
|
||||
authentication_classes = [TokenAuthentication]
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
def post(self, request):
|
||||
serializer = HostValidateSecretSerializer(data=request.data)
|
||||
serializer = HostPXEValidateSerializer(data=request.data)
|
||||
|
||||
serializer.is_valid(raise_exception=True)
|
||||
|
||||
serializer.save() # sets pxe_installable=False
|
||||
host = serializer.save() # sets Host.pxe_installable=False
|
||||
|
||||
return Response({'valid_secret': True})
|
||||
return Response({'valid_pxe_key': True})
|
||||
+2
-2
@@ -2,14 +2,14 @@ from django.conf.urls import include, url
|
||||
from django.contrib import admin
|
||||
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
|
||||
from django.views.generic.base import RedirectView
|
||||
from mdb.api.views import ValidatePuppetHostSecret
|
||||
from mdb.api.views import HostPXEValidate
|
||||
|
||||
admin.autodiscover()
|
||||
|
||||
urlpatterns = [
|
||||
url(r'^$', RedirectView.as_view(url='admin/', permanent=True)),
|
||||
url(r'^admin/', include(admin.site.urls)),
|
||||
url(r'^api/validate-host-secret/', ValidatePuppetHostSecret.as_view(), name='api-validate-host-secret')
|
||||
url(r'^api/validate-host-secret/', HostPXEValidate.as_view(), name='api-validate-host-secret')
|
||||
]
|
||||
|
||||
urlpatterns += staticfiles_urlpatterns()
|
||||
Reference in new issue
Block a user