def put(self, jobtype_name):
        """
        A ``PUT`` to this endpoint will create a new jobtype under the given URI.
        If a jobtype already exists under that URI, a new version will be created
        with the given data.

        You should only call this by id for updating an existing jobtype or if
        you have a reserved jobtype id. There is currently no way to reserve a
        jobtype id.

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

            **Request**

            .. sourcecode:: http

                PUT /api/v1/jobtypes/TestJobType HTTP/1.1
                Accept: application/json

                {
                    "name": "TestJobType",
                    "description": "Jobtype for testing inserts and queries",
                    "code": "\\nfrom pyfarm.jobtypes.core.jobtype import "
                            "JobType\\n\\nclass TestJobType(JobType):\\n"
                            "    def get_command(self):\\n"
                            "        return \"/usr/bin/touch\"\\n\\n"
                            "    def get_arguments(self):\\n"
                            "           return [os.path.join("
                            "self.assignment_data[\"job\"][\"data\"][\"path\"], "
                            "\"%04d\" % self.assignment_data[\"tasks\"]"
                            "[0][\"frame\"])]\\n"
                }

            **Response**

            .. sourcecode:: http

                HTTP/1.1 201 CREATED
                Content-Type: application/json

                {
                    "batch_contiguous": true,
                    "classname": null,
                    "code": "\\nfrom pyfarm.jobtypes.core.jobtype import "
                            "JobType\\n\\nclass TestJobType(JobType):\\n"
                            "    def get_command(self):\\n"
                            "        return \"/usr/bin/touch\"\\n\\n"
                            "    def get_arguments(self):\\n"
                            "           return [os.path.join("
                            "self.assignment_data[\"job\"][\"data\"][\"path\"], "
                            "\"%04d\" % self.assignment_data[\"tasks\"]"
                            "[0][\"frame\"])]\\n",
                    "id": 1,
                    "max_batch": 1,
                    "name": "TestJobType", 
                    "description": "Jobtype for testing inserts and queries",
                    "software_requirements": []
                }

        :statuscode 201:
            a new jobtype was created

        :statuscode 400:
            there was something wrong with the request (such as
            invalid columns being included)
        """
        if isinstance(jobtype_name, STRING_TYPES):
            jobtype = JobType.query.filter(
                JobType.name == jobtype_name).first()
        else:
            jobtype = JobType.query.filter_by(id=jobtype_name).first()

        max_version = None
        new = False if jobtype else True
        if jobtype:
            logger.debug(
                "jobtype %s will get a new version with data %r on commit",
                jobtype.name, g.json)
            max_version, = db.session.query(func.max(
                JobTypeVersion.version)).filter_by(jobtype=jobtype).first()
        else:
            jobtype = JobType()

        if max_version is not None:
            version = max_version + 1
        else:
            version = 1

        try:
            jobtype.name = g.json.pop("name")
            jobtype.description = g.json.pop("description", None)
            jobtype_version = JobTypeVersion()
            jobtype_version.jobtype = jobtype
            jobtype_version.version = version
            jobtype_version.code = g.json.pop("code")
            jobtype_version.classname = g.json.pop("classname", None)
            jobtype_version.batch_contiguous = g.json.pop(
                "batch_contiguous", None)
            jobtype_version.no_automatic_start_time =\
                g.json.pop("no_automatic_start_time", None)
            if "max_batch" in g.json and g.json["max_batch"] is None:
                g.json.pop("max_batch")
                jobtype_version.max_batch = sql.null()
            else:
                jobtype_version.max_batch = g.json.pop("max_batch", None)
        except KeyError as e:
            return (jsonify(error="Missing key in input: %r" % e.args),
                    BAD_REQUEST)

        if "software_requirements" in g.json:
            try:
                for r in parse_requirements(g.json["software_requirements"]):
                    r.jobtype_version = jobtype_version
                    db.session.add(r)
            except (TypeError, ValueError) as e:
                return jsonify(error=e.args), BAD_REQUEST
            except ObjectNotFound as e:
                return jsonify(error=e.args), NOT_FOUND
            del g.json["software_requirements"]
        elif not new:
            # If the user did not specify a list of software requirements and
            # this jobtype is not new, retain the requirements from the previous
            # version
            previous_version = JobTypeVersion.query.filter(
                JobTypeVersion.jobtype == jobtype, JobTypeVersion.version !=
                version).order_by("version desc").first()

            if previous_version:
                for old_req in previous_version.software_requirements:
                    new_req = JobTypeSoftwareRequirement()
                    new_req.jobtype_version = jobtype_version
                    new_req.software_id = old_req.software_id
                    new_req.min_version_id = old_req.min_version_id
                    new_req.max_version_id = old_req.max_version_id
                    db.session.add(new_req)

        if g.json:
            return (jsonify(error="Unexpected keys in input: %s" %
                            g.json.keys()), BAD_REQUEST)

        db.session.add_all([jobtype, jobtype_version])
        db.session.commit()
        jobtype_data = jobtype_version.to_dict(
            unpack_relationships=["software_requirements"])
        jobtype_data.update(jobtype.to_dict(unpack_relationships=False))
        del jobtype_data["jobtype_id"]
        logger.info("%s jobtype %s in put: %r",
                    "created" if new else "updated", jobtype.name,
                    jobtype_data)

        return jsonify(jobtype_data), CREATED
Exemple #2
0
    def put(self, jobtype_name):
        """
        A ``PUT`` to this endpoint will create a new jobtype under the given URI.
        If a jobtype already exists under that URI, a new version will be created
        with the given data.

        You should only call this by id for updating an existing jobtype or if
        you have a reserved jobtype id. There is currently no way to reserve a
        jobtype id.

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

            **Request**

            .. sourcecode:: http

                PUT /api/v1/jobtypes/TestJobType HTTP/1.1
                Accept: application/json

                {
                    "name": "TestJobType",
                    "description": "Jobtype for testing inserts and queries",
                    "code": "\\nfrom pyfarm.jobtypes.core.jobtype import "
                            "JobType\\n\\nclass TestJobType(JobType):\\n"
                            "    def get_command(self):\\n"
                            "        return \"/usr/bin/touch\"\\n\\n"
                            "    def get_arguments(self):\\n"
                            "           return [os.path.join("
                            "self.assignment_data[\"job\"][\"data\"][\"path\"], "
                            "\"%04d\" % self.assignment_data[\"tasks\"]"
                            "[0][\"frame\"])]\\n"
                }

            **Response**

            .. sourcecode:: http

                HTTP/1.1 201 CREATED
                Content-Type: application/json

                {
                    "batch_contiguous": true,
                    "classname": null,
                    "code": "\\nfrom pyfarm.jobtypes.core.jobtype import "
                            "JobType\\n\\nclass TestJobType(JobType):\\n"
                            "    def get_command(self):\\n"
                            "        return \"/usr/bin/touch\"\\n\\n"
                            "    def get_arguments(self):\\n"
                            "           return [os.path.join("
                            "self.assignment_data[\"job\"][\"data\"][\"path\"], "
                            "\"%04d\" % self.assignment_data[\"tasks\"]"
                            "[0][\"frame\"])]\\n",
                    "id": 1,
                    "max_batch": 1,
                    "name": "TestJobType", 
                    "description": "Jobtype for testing inserts and queries",
                    "software_requirements": []
                }

        :statuscode 201:
            a new jobtype was created

        :statuscode 400:
            there was something wrong with the request (such as
            invalid columns being included)
        """
        if isinstance(jobtype_name, STRING_TYPES):
            jobtype = JobType.query.filter(JobType.name == jobtype_name).first()
        else:
            jobtype = JobType.query.filter_by(id=jobtype_name).first()

        max_version = None
        new = False if jobtype else True
        if jobtype:
            logger.debug("jobtype %s will get a new version with data %r on commit", jobtype.name, g.json)
            max_version, = db.session.query(func.max(JobTypeVersion.version)).filter_by(jobtype=jobtype).first()
        else:
            jobtype = JobType()

        if max_version is not None:
            version = max_version + 1
        else:
            version = 1

        try:
            jobtype.name = g.json.pop("name")
            jobtype.description = g.json.pop("description", None)
            jobtype_version = JobTypeVersion()
            jobtype_version.jobtype = jobtype
            jobtype_version.version = version
            jobtype_version.code = g.json.pop("code")
            jobtype_version.classname = g.json.pop("classname", None)
            jobtype_version.batch_contiguous = g.json.pop("batch_contiguous", None)
            jobtype_version.no_automatic_start_time = g.json.pop("no_automatic_start_time", None)
            if "max_batch" in g.json and g.json["max_batch"] is None:
                g.json.pop("max_batch")
                jobtype_version.max_batch = sql.null()
            else:
                jobtype_version.max_batch = g.json.pop("max_batch", None)
        except KeyError as e:
            return (jsonify(error="Missing key in input: %r" % e.args), BAD_REQUEST)

        if "software_requirements" in g.json:
            try:
                for r in parse_requirements(g.json["software_requirements"]):
                    r.jobtype_version = jobtype_version
                    db.session.add(r)
            except (TypeError, ValueError) as e:
                return jsonify(error=e.args), BAD_REQUEST
            except ObjectNotFound as e:
                return jsonify(error=e.args), NOT_FOUND
            del g.json["software_requirements"]
        elif not new:
            # If the user did not specify a list of software requirements and
            # this jobtype is not new, retain the requirements from the previous
            # version
            previous_version = (
                JobTypeVersion.query.filter(JobTypeVersion.jobtype == jobtype, JobTypeVersion.version != version)
                .order_by("version desc")
                .first()
            )

            if previous_version:
                for old_req in previous_version.software_requirements:
                    new_req = JobTypeSoftwareRequirement()
                    new_req.jobtype_version = jobtype_version
                    new_req.software_id = old_req.software_id
                    new_req.min_version_id = old_req.min_version_id
                    new_req.max_version_id = old_req.max_version_id
                    db.session.add(new_req)

        if g.json:
            return (jsonify(error="Unexpected keys in input: %s" % g.json.keys()), BAD_REQUEST)

        db.session.add_all([jobtype, jobtype_version])
        db.session.commit()
        jobtype_data = jobtype_version.to_dict(unpack_relationships=["software_requirements"])
        jobtype_data.update(jobtype.to_dict(unpack_relationships=False))
        del jobtype_data["jobtype_id"]
        logger.info("%s jobtype %s in put: %r", "created" if new else "updated", jobtype.name, jobtype_data)

        return jsonify(jobtype_data), CREATED
    def post(self):
        """
        A ``POST`` to this endpoint will create a new jobtype.

        .. http:post:: /api/v1/jobtypes/ HTTP/1.1

            **Request**

            .. sourcecode:: http

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

                {
                    "name": "TestJobType",
                    "classname": "TestJobType",
                    "description": "Jobtype for testing inserts and queries",
                    "code": "\\nfrom pyfarm.jobtypes.core.jobtype import "
                            "JobType\\n\\nclass TestJobType(JobType):\\n"
                            "    def get_command(self):\\n"
                            "        return \"/usr/bin/touch\"\\n\\n"
                            "    def get_arguments(self):\\n"
                            "           return [os.path.join("
                            "self.assignment_data[\"job\"][\"data\"][\"path\"], "
                            "\"%04d\" % self.assignment_data[\"tasks\"]"
                            "[0][\"frame\"])]\\n"
                }

            **Response**

            .. sourcecode:: http

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

                {
                    "id": 1,
                    "batch_contiguous": true,
                    "software_requirements": [],
                    "version": 1,
                    "max_batch": 1,
                    "name": "TestJobType",
                    "classname": "TestJobType",
                    "description": "Jobtype for testing inserts and queries",
                    "code": "\\nfrom pyfarm.jobtypes.core.jobtype import "
                            "JobType\\n\\nclass TestJobType(JobType):\\n"
                            "    def get_command(self):\\n"
                            "        return \"/usr/bin/touch\"\\n\\n"
                            "    def get_arguments(self):\\n"
                            "           return [os.path.join("
                            "self.assignment_data[\"job\"][\"data\"][\"path\"], "
                            "\"%04d\" % self.assignment_data[\"tasks\"]"
                            "[0][\"frame\"])]\\n"
                }

        :statuscode 201:
            a new jobtype item was created

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

        :statuscode 409:
            a conflicting jobtype already exists
        """
        if "name" not in g.json:
            return jsonify(
                error="Jobtype does not specify a name"), BAD_REQUEST
        jobtype = JobType.query.filter_by(name=g.json["name"]).first()

        if jobtype:
            return (jsonify(error="Jobtype %s already exixts" %
                            g.json["name"]), CONFLICT)

        try:
            jobtype = JobType()
            jobtype.name = g.json.pop("name")
            jobtype.description = g.json.pop("description", None)
            jobtype_version = JobTypeVersion()
            jobtype_version.jobtype = jobtype
            jobtype_version.version = 1
            jobtype_version.code = g.json.pop("code")
            jobtype_version.classname = g.json.pop("classname", None)
            jobtype_version.batch_contiguous = g.json.pop(
                "batch_contiguous", None)
            jobtype_version.no_automatic_start_time = g.json.pop(
                "no_automatic_start_time", None)
            if "max_batch" in g.json and g.json["max_batch"] is None:
                g.json.pop("max_batch")
                jobtype_version.max_batch = sql.null()
            else:
                jobtype_version.max_batch = g.json.pop("max_batch", None)
        except KeyError as e:
            return (jsonify(error="Missing key in input: %r" % e.args),
                    BAD_REQUEST)

        if "software_requirements" in g.json:
            try:
                for r in parse_requirements(g.json["software_requirements"]):
                    r.jobtype_version = jobtype_version
                    db.session.add(r)
            except (TypeError, ValueError) as e:
                return jsonify(error=e.args), BAD_REQUEST
            except ObjectNotFound as e:
                return jsonify(error=e.args), NOT_FOUND
            del g.json["software_requirements"]

        if g.json:
            return (jsonify(error="Unexpected keys in input: %r" %
                            g.json.keys()), BAD_REQUEST)

        db.session.add_all([jobtype, jobtype_version])
        db.session.commit()
        jobtype_data = jobtype_version.to_dict(unpack_relationships=False)
        jobtype_data.update(
            jobtype.to_dict(unpack_relationships=["software_requirements"]))
        del jobtype_data["jobtype_id"]
        logger.info("created jobtype %s: %r", jobtype.name, jobtype_data)

        return jsonify(jobtype_data), CREATED
Exemple #4
0
    def post(self):
        """
        A ``POST`` to this endpoint will create a new jobtype.

        .. http:post:: /api/v1/jobtypes/ HTTP/1.1

            **Request**

            .. sourcecode:: http

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

                {
                    "name": "TestJobType",
                    "classname": "TestJobType",
                    "description": "Jobtype for testing inserts and queries",
                    "code": "\\nfrom pyfarm.jobtypes.core.jobtype import "
                            "JobType\\n\\nclass TestJobType(JobType):\\n"
                            "    def get_command(self):\\n"
                            "        return \"/usr/bin/touch\"\\n\\n"
                            "    def get_arguments(self):\\n"
                            "           return [os.path.join("
                            "self.assignment_data[\"job\"][\"data\"][\"path\"], "
                            "\"%04d\" % self.assignment_data[\"tasks\"]"
                            "[0][\"frame\"])]\\n"
                }

            **Response**

            .. sourcecode:: http

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

                {
                    "id": 1,
                    "batch_contiguous": true,
                    "software_requirements": [],
                    "version": 1,
                    "max_batch": 1,
                    "name": "TestJobType",
                    "classname": "TestJobType",
                    "description": "Jobtype for testing inserts and queries",
                    "code": "\\nfrom pyfarm.jobtypes.core.jobtype import "
                            "JobType\\n\\nclass TestJobType(JobType):\\n"
                            "    def get_command(self):\\n"
                            "        return \"/usr/bin/touch\"\\n\\n"
                            "    def get_arguments(self):\\n"
                            "           return [os.path.join("
                            "self.assignment_data[\"job\"][\"data\"][\"path\"], "
                            "\"%04d\" % self.assignment_data[\"tasks\"]"
                            "[0][\"frame\"])]\\n"
                }

        :statuscode 201:
            a new jobtype item was created

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

        :statuscode 409:
            a conflicting jobtype already exists
        """
        if "name" not in g.json:
            return jsonify(error="Jobtype does not specify a name"), BAD_REQUEST
        jobtype = JobType.query.filter_by(name=g.json["name"]).first()

        if jobtype:
            return (jsonify(error="Jobtype %s already exixts" % g.json["name"]), CONFLICT)

        try:
            jobtype = JobType()
            jobtype.name = g.json.pop("name")
            jobtype.description = g.json.pop("description", None)
            jobtype_version = JobTypeVersion()
            jobtype_version.jobtype = jobtype
            jobtype_version.version = 1
            jobtype_version.code = g.json.pop("code")
            jobtype_version.classname = g.json.pop("classname", None)
            jobtype_version.batch_contiguous = g.json.pop("batch_contiguous", None)
            jobtype_version.no_automatic_start_time = g.json.pop("no_automatic_start_time", None)
            if "max_batch" in g.json and g.json["max_batch"] is None:
                g.json.pop("max_batch")
                jobtype_version.max_batch = sql.null()
            else:
                jobtype_version.max_batch = g.json.pop("max_batch", None)
        except KeyError as e:
            return (jsonify(error="Missing key in input: %r" % e.args), BAD_REQUEST)

        if "software_requirements" in g.json:
            try:
                for r in parse_requirements(g.json["software_requirements"]):
                    r.jobtype_version = jobtype_version
                    db.session.add(r)
            except (TypeError, ValueError) as e:
                return jsonify(error=e.args), BAD_REQUEST
            except ObjectNotFound as e:
                return jsonify(error=e.args), NOT_FOUND
            del g.json["software_requirements"]

        if g.json:
            return (jsonify(error="Unexpected keys in input: %r" % g.json.keys()), BAD_REQUEST)

        db.session.add_all([jobtype, jobtype_version])
        db.session.commit()
        jobtype_data = jobtype_version.to_dict(unpack_relationships=False)
        jobtype_data.update(jobtype.to_dict(unpack_relationships=["software_requirements"]))
        del jobtype_data["jobtype_id"]
        logger.info("created jobtype %s: %r", jobtype.name, jobtype_data)

        return jsonify(jobtype_data), CREATED