Esempio n. 1
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 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)
Esempio n. 2
0
 def handle(self, *args, **options):
     values = {
         k: options[k]
         for k in ShinkenService.get_field_list() if options[k] is not None
     }
     check_command = options['check_command']
     host_name = options['host_name']
     if ShinkenService.objects.filter(host_name=host_name, check_command=check_command)\
             .update(**values) == 0:
         ShinkenService(**values).save()
         self.stdout.write(
             self.style.WARNING('%s:%s created') %
             (host_name, check_command))
     elif options['delete']:
         ShinkenService.objects.filter(
             host_name=host_name, check_command=check_command).delete()
         self.stdout.write(
             self.style.ERROR('%s:%s deleted') % (host_name, check_command))
Esempio n. 3
0
 def handle(self, *args, **options):
     values = {k: options[k] for k in ShinkenService.get_field_list() if options[k] is not None}
     check_command = options['check_command']
     host_name = options['host_name']
     if ShinkenService.objects.filter(host_name=host_name, check_command=check_command)\
             .update(**values) == 0:
         ShinkenService(**values).save()
         self.stdout.write(self.style.WARNING('%s:%s created') % (host_name, check_command))
     elif options['delete']:
         ShinkenService.objects.filter(host_name=host_name, check_command=check_command).delete()
         self.stdout.write(self.style.ERROR('%s:%s deleted') % (host_name, check_command))
Esempio n. 4
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)