def get(self, request, *args, **kwargs): """Show server configuration.""" 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 config = ServerConfig.objects.all() if config.count() == 0: return InfoMessage("No configurations available.").as_json theader = [{'key': 'Key'}, {'value': 'Value'}] response = { 'header': { 'type': 'TABLE', 'theader': theader }, 'data': [] } for item in config: response['data'].append({'key': item.key, 'value': item.value}) return JsonResponse(response)
def _list(self, request, machine): """Return list of available distributions for `machine`.""" machinegroup = None if not machine.has_setup_capability(): return InfoMessage("Machine has no setup capability.").as_json if machine.group and not machine.group.setup_use_architecture: machinegroup = machine.group.name grouped_records = machine.fqdn_domain.get_setup_records( machine.architecture.name, machinegroup=machinegroup) if not grouped_records: return ErrorMessage("No setup records found!").as_json response = '' theader = [{'full': 'Available Distributions'}] response = { 'header': { 'type': 'TABLE', 'theader': theader }, 'data': [] } for distribution, records in grouped_records.items(): logger.info("Distros: {} - records: {}".format( distribution, records)) for record in records: response['data'].append({ 'full': distribution + ':' + record, }) return JsonResponse(response)
def get(self, request, *args, **kwargs): """Return reservation history of machine.""" fqdn = request.GET.get('fqdn', None) option = request.GET.get('option', None) try: result = get_machine(fqdn, redirect_to='api:rescan', data=request.GET) if isinstance(result, Serializer): return result.as_json elif isinstance(result, HttpResponseRedirect): return result machine = result except Exception as e: return ErrorMessage(str(e)).as_json if option not in MachineCheck.Scan.Action.as_list: return ErrorMessage("Unknown option '{}'!".format(option)).as_json try: machine.scan(option) if not machine.collect_system_information: return InfoMessage( "Collecting system information is disabled for this machine." ).as_json except Exception as e: return ErrorMessage(str(e)).as_json return Message("OK.").as_json
def get(self, request, *args, **kwargs): """Return form for adding a remotepower.""" fqdn = request.GET.get('fqdn', None) try: result = get_machine(fqdn, redirect_to='api:remotepower_add', data=request.GET) if isinstance(result, Serializer): return result.as_json elif isinstance(result, HttpResponseRedirect): return result machine = result except Exception as e: return ErrorMessage(str(e)).as_json 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 if machine.has_remotepower(): return InfoMessage("Machine has already a remote power.").as_json form = RemotePowerAPIForm(machine=machine) input = InputSerializer(form.as_dict(), self.URL_POST.format(fqdn=machine.fqdn), form.get_order()) return input.as_json
def get(self, request, *args, **kwargs): """Return reservation history of machine.""" fqdn = request.GET.get('fqdn', None) try: result = get_machine(fqdn, redirect_to='api:history', data=request.GET) if isinstance(result, Serializer): return result.as_json elif isinstance(result, HttpResponseRedirect): return result machine = result except Exception as e: return ErrorMessage(str(e)).as_json history = ReservationHistory.objects.filter(machine__fqdn=machine.fqdn) if history.count() == 0: return InfoMessage("No history available yet.").as_json theader = [{ 'user': '******' }, { 'at': 'Reserved at' }, { 'until': 'Reserved until' }, { 'reason': 'Reason' }] response = { 'header': { 'type': 'TABLE', 'theader': theader }, 'data': [] } for item in history: response['data'].append({ 'user': item.reserved_by, 'at': item.reserved_at, 'until': item.reserved_until, 'reason': item.reserved_reason.replace('\n', '') }) return JsonResponse(response)
def post(self, request, *args, **kwargs): """Return query result.""" response = {} try: query_str = json.loads(request.body.decode('utf-8'))['data'] except (KeyError, ValueError): return ErrorMessage("Data format is invalid!").as_json try: query = APIQuery(query_str) query.execute(user=request.user) except APIQuery.EmptyResult as e: return InfoMessage(str(e)).as_json except Exception as e: return ErrorMessage(str(e)).as_json response['header'] = {'type': 'TABLE', 'theader': query.get_theader()} response['data'] = query.data return JsonResponse(response, safe=False)
def post(self, request, *args, **kwargs): """Add remote power to machine.""" if not request.user.is_superuser: return ErrorMessage( "Only superusers are allowed to perform this action!").as_json try: fqdn = request.path.split("/")[-1] result = get_machine(fqdn, redirect_to='api:remotepower_add', data=request.GET) if isinstance(result, Serializer): return result.as_json elif isinstance(result, HttpResponseRedirect): return result machine = result except Exception as e: return ErrorMessage(str(e)).as_json if machine.has_remotepower(): return InfoMessage("Machine has already a remote power.").as_json data = json.loads(request.body.decode('utf-8'))['form'] form = RemotePowerAPIForm(data, machine=machine) if form.is_valid(): try: remotepower = RemotePower(**form.cleaned_data) remotepower.save() except Exception as e: logger.exception(e) return ErrorMessage("Something went wrong!").as_json return Message('Ok.').as_json return ErrorMessage("\n{}".format( format_cli_form_errors(form))).as_json