Exemplo n.º 1
0
    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
Exemplo n.º 2
0
    def get(self, request, *args, **kwargs):
        """Return reservation form for valid machine."""
        fqdn = request.GET.get('fqdn', None)

        try:
            result = get_machine(fqdn,
                                 redirect_to='api:reserve',
                                 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

        form = ReserveMachineAPIForm(username=request.user.username,
                                     reason=machine.reserved_reason)

        input = InputSerializer(form.as_dict(),
                                self.URL_POST.format(id=machine.pk),
                                form.get_order())
        return input.as_json
Exemplo n.º 3
0
    def get(self, request, *args, **kwargs):
        """Return form for adding a virtual machine."""
        architectures = self._get_available_architectures()
        architecture = request.GET.get('arch', None)

        if architecture.lower() not in architectures:
            return Message("Available architectures: {}".format(
                '|'.join(architectures))).as_json

        if isinstance(request.user, AnonymousUser) or not request.auth:
            return AuthRequiredSerializer().as_json

        hosts = Machine.api.filter(vm_dedicated_host=True,
                                   architecture__name=architecture)
        host = None

        for host_ in hosts:
            if host_.virtualization_api and (
                    host_.get_virtual_machines().count() < host_.vm_max):
                host = host_
                break

        if host is None:
            return ErrorMessage("No virtual machine hosts left!").as_json

        form = VirtualMachineAPIForm(
            virtualization_api=host.virtualization_api)

        input = InputSerializer(form.as_dict(host),
                                self.URL_POST.format(arch=architecture),
                                form.get_order())
        return input.as_json
Exemplo n.º 4
0
    def get(self, request, *args, **kwargs):
        """Return form for adding an annotation."""
        fqdn = request.GET.get('fqdn', None)
        try:
            result = get_machine(
                fqdn,
                redirect_to='api:annotation_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

        form = AnnotationAPIForm(machine=machine)

        input = InputSerializer(
            form.as_dict(),
            self.URL_POST.format(fqdn=machine.fqdn),
            form.get_order()
        )
        return input.as_json
Exemplo n.º 5
0
    def get(self, request, *args, **kwargs):
        """Return form for deleting a remote power."""
        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 = DeleteRemotePowerAPIForm()

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