Beispiel #1
0
    def post(self, request, *args, **kwargs):
        """Delete serial console."""
        if not request.user.is_superuser:
            return ErrorMessage(
                "Only superusers are allowed to perform this action!").as_json

        data = json.loads(request.body.decode('utf-8'))['form']
        form = DeleteSerialConsoleAPIForm(data)

        if form.is_valid():

            try:
                cleaned_data = form.cleaned_data

                machine = Machine.objects.get(
                    fqdn__iexact=cleaned_data['fqdn'])

                if not machine.has_serialconsole():
                    return ErrorMessage(
                        "Machine has no serial console!").as_json

                result = machine.serialconsole.delete()

                theader = [
                    {
                        'objects': 'Deleted objects'
                    },
                    {
                        'count': '#'
                    },
                ]

                response = {
                    'header': {
                        'type': 'TABLE',
                        'theader': theader
                    },
                    'data': [],
                }
                for key, value in result[1].items():
                    response['data'].append({
                        'objects': key.replace('data.', ''),
                        'count': value
                    })
                return JsonResponse(response)

            except Exception as e:
                logger.exception(e)
                return ErrorMessage("Something went wrong!").as_json

        return ErrorMessage("\n{}".format(
            format_cli_form_errors(form))).as_json
Beispiel #2
0
    def get(self, request, *args, **kwargs):
        """Return form for deleting a serial console."""
        if isinstance(request.user, AnonymousUser) or not request.auth:
            return AuthRequiredSerializer().as_json

        if not request.user.is_superuser:
            return ErrorMessage(
                "Only superusers are allowed to perform this action!").as_json

        form = DeleteSerialConsoleAPIForm()

        input = InputSerializer(form.as_dict(), self.URL_POST,
                                form.get_order())
        return input.as_json