Ejemplo n.º 1
0
    def delete(self, jobtype_name, software):
        """
        A ``DELETE`` to this endpoint will delete the requested software
        requirement from the specified jobtype, creating a new version of the
        jobtype in the process

        .. http:delete:: /api/v1/jobtypes/[<str:name>|<int:id>]/software_requirements/<int:id> HTTP/1.1

            **Request**

            .. sourcecode:: http

                DELETE /api/v1/jobtypes/TestJobType/software_requirements/1 HTTP/1.1
                Accept: application/json

            **Response**

            .. sourcecode:: http

                HTTP/1.1 204 NO CONTENT

        :statuscode 204:
            the software requirement was deleted or didn't exist
        """
        if isinstance(jobtype_name, STRING_TYPES):
            jobtype = JobType.query.filter_by(name=jobtype_name).first()
        else:
            jobtype = JobType.query.filter_by(id=jobtype_name).first()

        if not jobtype:
            return (jsonify(error="JobType %s not found" % jobtype_name), NOT_FOUND)

        jobtype_version = JobTypeVersion.query.filter_by(jobtype=jobtype).order_by("version desc").first()
        if not jobtype_version:
            return jsonify(error="JobType has no versions"), NOT_FOUND

        new_version = JobTypeVersion()
        for name in JobTypeVersion.types().columns:
            if name not in JobTypeVersion.types().primary_keys:
                setattr(new_version, name, getattr(jobtype_version, name))
        new_version.version += 1
        for old_req in jobtype_version.software_requirements:
            if old_req.software.software != software:
                new_req = JobTypeSoftwareRequirement()
                for name in JobTypeSoftwareRequirement.types().columns:
                    setattr(new_req, name, getattr(old_req, name))
                new_req.jobtype_version = new_version
                db.session.add(new_req)

        db.session.add(new_version)
        db.session.commit()
        logger.info(
            "Deleted software requirement %s for jobtype %s, creating " "new version %s",
            software,
            jobtype.id,
            new_version.version,
        )

        return jsonify(None), NO_CONTENT
Ejemplo n.º 2
0
    def delete(self, jobtype_name, software):
        """
        A ``DELETE`` to this endpoint will delete the requested software
        requirement from the specified jobtype, creating a new version of the
        jobtype in the process

        .. http:delete:: /api/v1/jobtypes/[<str:name>|<int:id>]/software_requirements/<int:id> HTTP/1.1

            **Request**

            .. sourcecode:: http

                DELETE /api/v1/jobtypes/TestJobType/software_requirements/1 HTTP/1.1
                Accept: application/json

            **Response**

            .. sourcecode:: http

                HTTP/1.1 204 NO CONTENT

        :statuscode 204:
            the software requirement was deleted or didn't exist
        """
        if isinstance(jobtype_name, STRING_TYPES):
            jobtype = JobType.query.filter_by(name=jobtype_name).first()
        else:
            jobtype = JobType.query.filter_by(id=jobtype_name).first()

        if not jobtype:
            return (jsonify(error="JobType %s not found" % jobtype_name),
                    NOT_FOUND)

        jobtype_version = JobTypeVersion.query.filter_by(
            jobtype=jobtype).order_by("version desc").first()
        if not jobtype_version:
            return jsonify(error="JobType has no versions"), NOT_FOUND

        new_version = JobTypeVersion()
        for name in JobTypeVersion.types().columns:
            if name not in JobTypeVersion.types().primary_keys:
                setattr(new_version, name, getattr(jobtype_version, name))
        new_version.version += 1
        for old_req in jobtype_version.software_requirements:
            if old_req.software.software != software:
                new_req = JobTypeSoftwareRequirement()
                for name in JobTypeSoftwareRequirement.types().columns:
                    setattr(new_req, name, getattr(old_req, name))
                new_req.jobtype_version = new_version
                db.session.add(new_req)

        db.session.add(new_version)
        db.session.commit()
        logger.info(
            "Deleted software requirement %s for jobtype %s, creating "
            "new version %s", software, jobtype.id, new_version.version)

        return jsonify(None), NO_CONTENT
Ejemplo n.º 3
0
    def post(self, jobtype_name, version=None):
        """
        A ``POST`` to this endpoint will create a new software_requirement for
        the specified jobtype.
        This will transparently create a new jobtype version

        .. http:post:: /api/v1/jobtypes/[<str:name>|<int:id>]/software_requirements/ HTTP/1.1

            **Request**

            .. sourcecode:: http

                POST /api/v1/jobtypes/TestJobType/software_requirements/ HTTP/1.1
                Accept: application/json

                {
                    "software": "blender",
                    "min_version": "2.69"
                }

            **Response**

            .. sourcecode:: http

                HTTP/1.1 200 OK
                Content-Type: application/json

                {
                    "jobtype_version": {
                        "id": 8,
                        "jobtype": "TestJobType",
                        "version": 7
                    },
                    "max_version": null,
                    "min_version": {
                        "id": 2,
                        "version": "1.69"
                    },
                    "software": {
                        "id": 2,
                        "software": "blender"
                    }
                }

        :statuscode 201:
            a new software requirement was created

        :statuscode 400:
            there was something wrong with the request (such as
            invalid columns being included)

        :statuscode 405:
            you tried calling this method on a specific version

        :statuscode 409:
            a conflicting software requirement already exists
        """
        if version is not None:
            return (jsonify(
                error="POST not allowed for specific jobtype versions"),
                    METHOD_NOT_ALLOWED)

        if isinstance(jobtype_name, STRING_TYPES):
            jobtype = JobType.query.filter_by(name=jobtype_name).first()
        else:
            jobtype = JobType.query.filter_by(id=jobtype_name).first()

        if not jobtype:
            return (jsonify(error="JobType %s not found" % jobtype_name),
                    NOT_FOUND)

        jobtype_version = JobTypeVersion.query.filter_by(
            jobtype=jobtype).order_by("version desc").first()
        if not jobtype_version:
            return jsonify(error="JobType has no versions"), NOT_FOUND

        if ("software" not in g.json
                or not isinstance(g.json["software"], STRING_TYPES)):
            return (jsonify(error="Software not specified or not a string"),
                    BAD_REQUEST)

        software = Software.query.filter_by(
            software=g.json["software"]).first()
        if not software:
            return jsonify(error="Software not found"), NOT_FOUND

        existing_requirement = JobTypeSoftwareRequirement.query.filter(
            JobTypeSoftwareRequirement.jobtype_version == jobtype_version,
            JobTypeSoftwareRequirement.software == software).first()
        if existing_requirement:
            return jsonify(error="A software requirement for this jobtype "
                           "version and this software exists"), CONFLICT

        new_version = JobTypeVersion()
        for name in JobTypeVersion.types().columns:
            if name not in JobTypeVersion.types().primary_keys:
                setattr(new_version, name, getattr(jobtype_version, name))
        new_version.version += 1
        db.session.add(new_version)
        for old_req in jobtype_version.software_requirements:
            new_req = JobTypeSoftwareRequirement()
            for name in JobTypeSoftwareRequirement.types().columns:
                setattr(new_req, name, getattr(old_req, name))
            new_req.jobtype_version = new_version
            db.session.add(new_req)

        min_version = None
        if "min_version" in g.json:
            if not isinstance(g.json["min_version"], STRING_TYPES):
                return jsonify(error="min_version not a string"), BAD_REQUEST
            min_version = SoftwareVersion.query.filter_by(
                version=g.json["min_version"]).first()
            if not min_version:
                return jsonify(error="min_version not found"), NOT_FOUND

        max_version = None
        if "max_version" in g.json:
            if not isinstance(g.json["max_version"], STRING_TYPES):
                return jsonify(error="max_version not a string"), BAD_REQUEST
            max_version = SoftwareVersion.query.filter_by(
                version=g.json["max_version"]).first()
            if not max_version:
                return jsonify(error="max_version not found"), NOT_FOUND

        requirement = JobTypeSoftwareRequirement()
        requirement.jobtype_version = new_version
        requirement.software = software
        requirement.min_version = min_version
        requirement.max_version = max_version

        db.session.add(requirement)
        db.session.commit()
        requirement_data = requirement.to_dict()
        del requirement_data["jobtype_version_id"]
        del requirement_data["software_id"]
        del requirement_data["min_version_id"]
        del requirement_data["max_version_id"]
        logger.info("Created new software requirement for jobtype %s: %r",
                    jobtype.id, requirement_data)

        return jsonify(requirement_data), CREATED
Ejemplo n.º 4
0
    def post(self, jobtype_name, version=None):
        """
        A ``POST`` to this endpoint will create a new software_requirement for
        the specified jobtype.
        This will transparently create a new jobtype version

        .. http:post:: /api/v1/jobtypes/[<str:name>|<int:id>]/software_requirements/ HTTP/1.1

            **Request**

            .. sourcecode:: http

                POST /api/v1/jobtypes/TestJobType/software_requirements/ HTTP/1.1
                Accept: application/json

                {
                    "software": "blender",
                    "min_version": "2.69"
                }

            **Response**

            .. sourcecode:: http

                HTTP/1.1 200 OK
                Content-Type: application/json

                {
                    "jobtype_version": {
                        "id": 8,
                        "jobtype": "TestJobType",
                        "version": 7
                    },
                    "max_version": null,
                    "min_version": {
                        "id": 2,
                        "version": "1.69"
                    },
                    "software": {
                        "id": 2,
                        "software": "blender"
                    }
                }

        :statuscode 201:
            a new software requirement was created

        :statuscode 400:
            there was something wrong with the request (such as
            invalid columns being included)

        :statuscode 405:
            you tried calling this method on a specific version

        :statuscode 409:
            a conflicting software requirement already exists
        """
        if version is not None:
            return (jsonify(error="POST not allowed for specific jobtype versions"), METHOD_NOT_ALLOWED)

        if isinstance(jobtype_name, STRING_TYPES):
            jobtype = JobType.query.filter_by(name=jobtype_name).first()
        else:
            jobtype = JobType.query.filter_by(id=jobtype_name).first()

        if not jobtype:
            return (jsonify(error="JobType %s not found" % jobtype_name), NOT_FOUND)

        jobtype_version = JobTypeVersion.query.filter_by(jobtype=jobtype).order_by("version desc").first()
        if not jobtype_version:
            return jsonify(error="JobType has no versions"), NOT_FOUND

        if "software" not in g.json or not isinstance(g.json["software"], STRING_TYPES):
            return (jsonify(error="Software not specified or not a string"), BAD_REQUEST)

        software = Software.query.filter_by(software=g.json["software"]).first()
        if not software:
            return jsonify(error="Software not found"), NOT_FOUND

        existing_requirement = JobTypeSoftwareRequirement.query.filter(
            JobTypeSoftwareRequirement.jobtype_version == jobtype_version,
            JobTypeSoftwareRequirement.software == software,
        ).first()
        if existing_requirement:
            return (
                jsonify(error="A software requirement for this jobtype " "version and this software exists"),
                CONFLICT,
            )

        new_version = JobTypeVersion()
        for name in JobTypeVersion.types().columns:
            if name not in JobTypeVersion.types().primary_keys:
                setattr(new_version, name, getattr(jobtype_version, name))
        new_version.version += 1
        db.session.add(new_version)
        for old_req in jobtype_version.software_requirements:
            new_req = JobTypeSoftwareRequirement()
            for name in JobTypeSoftwareRequirement.types().columns:
                setattr(new_req, name, getattr(old_req, name))
            new_req.jobtype_version = new_version
            db.session.add(new_req)

        min_version = None
        if "min_version" in g.json:
            if not isinstance(g.json["min_version"], STRING_TYPES):
                return jsonify(error="min_version not a string"), BAD_REQUEST
            min_version = SoftwareVersion.query.filter_by(version=g.json["min_version"]).first()
            if not min_version:
                return jsonify(error="min_version not found"), NOT_FOUND

        max_version = None
        if "max_version" in g.json:
            if not isinstance(g.json["max_version"], STRING_TYPES):
                return jsonify(error="max_version not a string"), BAD_REQUEST
            max_version = SoftwareVersion.query.filter_by(version=g.json["max_version"]).first()
            if not max_version:
                return jsonify(error="max_version not found"), NOT_FOUND

        requirement = JobTypeSoftwareRequirement()
        requirement.jobtype_version = new_version
        requirement.software = software
        requirement.min_version = min_version
        requirement.max_version = max_version

        db.session.add(requirement)
        db.session.commit()
        requirement_data = requirement.to_dict()
        del requirement_data["jobtype_version_id"]
        del requirement_data["software_id"]
        del requirement_data["min_version_id"]
        del requirement_data["max_version_id"]
        logger.info("Created new software requirement for jobtype %s: %r", jobtype.id, requirement_data)

        return jsonify(requirement_data), CREATED