コード例 #1
0
ファイル: views.py プロジェクト: d9pouces/Penates-Server
def register_service(request, check_command):
    try:
        fqdn = hostname_from_principal(request.user.username)
    except ValueError:
        return HttpResponse(status=401, content='Invalid username')
    status = 204
    if request.method == 'POST':
        if request.body:
            content = json.loads(request.body.decode('utf-8'))
            s = ShinkenService(**content)
            s.host_name = fqdn
            values = s.to_dict()
        for key in ('host_name', 'check_command'):
            if key in values:
                del values[key]
        if ShinkenService.objects.filter(host_name=fqdn, check_command=check_command)\
                .update(**values) == 0:
            ShinkenService(host_name=fqdn, check_command=check_command, **values).save()
            return HttpResponse(status=201)
    elif request.method == 'GET':
        form = ShinkenServiceForm(request.GET)
        if not form.is_valid():
            return HttpResponse(status=400)
        values = {key: value for key, value in form.cleaned_data.items() if value}
        if ShinkenService.objects.filter(host_name=fqdn, check_command=check_command)\
                .update(**values) == 0:
            ShinkenService(host_name=fqdn, check_command=check_command, **values).save()
            return HttpResponse(status=201)
    elif request.method == 'DELETE':
        ShinkenService.objects.filter(host_name=fqdn, check_command=check_command).delete()
        status = 202
    return HttpResponse(status=status)
コード例 #2
0
def register_service(request, check_command):
    try:
        fqdn = hostname_from_principal(request.user.username)
    except ValueError:
        return HttpResponse(status=401, content='Invalid username')
    status = 204
    if request.method == 'POST':
        if request.body:
            content = json.loads(request.body.decode('utf-8'))
            s = ShinkenService(**content)
            s.host_name = fqdn
            values = s.to_dict()
        for key in ('host_name', 'check_command'):
            if key in values:
                del values[key]
        if ShinkenService.objects.filter(host_name=fqdn, check_command=check_command)\
                .update(**values) == 0:
            ShinkenService(host_name=fqdn,
                           check_command=check_command,
                           **values).save()
            return HttpResponse(status=201)
    elif request.method == 'GET':
        form = ShinkenServiceForm(request.GET)
        if not form.is_valid():
            return HttpResponse(status=400)
        values = {
            key: value
            for key, value in list(form.cleaned_data.items()) if value
        }
        if ShinkenService.objects.filter(host_name=fqdn, check_command=check_command)\
                .update(**values) == 0:
            ShinkenService(host_name=fqdn,
                           check_command=check_command,
                           **values).save()
            return HttpResponse(status=201)
    elif request.method == 'DELETE':
        ShinkenService.objects.filter(host_name=fqdn,
                                      check_command=check_command).delete()
        status = 202
    return HttpResponse(status=status)