Esempio n. 1
0
    def test_software(self):
        for agent_foobar in self.models(limit=1):
            db.session.add(agent_foobar)

            # create some software version tags
            software_version_objects = []
            for software_name in ("foo", "bar", "baz"):
                software = Software()
                software.agents = [agent_foobar]
                software.software = software_name
                software_version = SoftwareVersion()
                software_version.software = software
                software_version.version = "1"
                software_version.rank = 1
                software_version_objects.append(
                    (software.software, software_version.version))
                agent_foobar.software_versions.append(software_version)

            db.session.commit()
            agent_id = agent_foobar.id
            db.session.remove()

            agent = Agent.query.filter_by(id=agent_id).first()
            self.assertIsNotNone(agent)

            agent_software_versions = list(
                (str(i.software.software), str(i.version))
                for i in agent.software_versions)
            software_version_objects.sort()
            agent_software_versions.sort()
            self.assertListEqual(agent_software_versions,
                                 software_version_objects)
Esempio n. 2
0
    def test_software(self):
        for agent_foobar in self.models(limit=1):
            db.session.add(agent_foobar)

            # create some software version tags
            software_version_objects = []
            for software_name in ("foo", "bar", "baz"):
                software = Software()
                software.agents = [agent_foobar]
                software.software = software_name
                software_version = SoftwareVersion()
                software_version.software = software
                software_version.version = "1"
                software_version.rank = 1
                software_version_objects.append((software.software,
                                                 software_version.version))
                agent_foobar.software_versions.append(software_version)

            db.session.commit()
            agent_id = agent_foobar.id
            db.session.remove()

            agent = Agent.query.filter_by(id=agent_id).first()
            self.assertIsNotNone(agent)

            agent_software_versions = list(
                (str(i.software.software), str(i.version))
                for i in agent.software_versions)
            software_version_objects.sort()
            agent_software_versions.sort()
            self.assertListEqual(agent_software_versions,
                                 software_version_objects)
Esempio n. 3
0
def add_software_version(software_id):
    software = Software.query.filter_by(id=software_id).first()
    if not software:
        return (render_template("pyfarm/error.html",
                                error="Software %s not found" % software_id),
                NOT_FOUND)

    version = SoftwareVersion(software=software,
                              version=request.form["version"],
                              rank=int(request.form["rank"]))
    db.session.add(version)
    db.session.commit()

    flash("Version %s has been added." % version.version)

    return redirect(url_for("single_software_ui", software_id=software.id),
                    SEE_OTHER)
Esempio n. 4
0
    def post(self, software_rq):
        """
        A ``POST`` to this endpoint will create a new version for this software.

        A rank can optionally be included.  If it isn't, it is assumed that this
        is the newest version for this software

        .. http:post:: /api/v1/software/versions/ HTTP/1.1

            **Request**

            .. sourcecode:: http

                POST /api/v1/software/blender/versions/ HTTP/1.1
                Accept: application/json

                {
                    "version": "1.70"
                }

            **Response**

            .. sourcecode:: http

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

                {
                    "id": 4,
                    "version": "1.70",
                    "rank": "100"
                }

        :statuscode 201: a new software version was created
        :statuscode 400: there was something wrong with the request (such as
                            invalid columns being included)
        :statuscode 409: a software version with that name already exists
        """
        if isinstance(software_rq, STRING_TYPES):
            software = Software.query.filter_by(software=software_rq).first()
        else:
            software = Software.query.filter_by(id=software_rq).first()

        if not software:
            return jsonify(error="Requested software not found"), NOT_FOUND

        existing_version = SoftwareVersion.query.filter(
            SoftwareVersion.software == software,
            SoftwareVersion.version == g.json["version"]).first()
        if existing_version:
            return (jsonify(error="Version %s already exixts" %
                            g.json["version"]), CONFLICT)

        with db.session.no_autoflush:
            version = SoftwareVersion(**g.json)
            version.software = software

            if version.rank is None:
                max_rank, = db.session.query(
                    func.max(SoftwareVersion.rank)).filter_by(
                        software=software).one()
                version.rank = max_rank + 100 if max_rank is not None else 100
                if version.rank % 100 != 0:
                    version.rank += 100 - (version.rank % 100)

            db.session.add(version)
            try:
                db.session.commit()
            except DatabaseError as e:
                logger.error("DatabaseError error on SoftwareVersion POST: %r",
                            e.args)
                return jsonify(error="Database error"), INTERNAL_SERVER_ERROR

            version_data = {
                "id": version.id,
                "version": version.version,
                "rank": version.rank
                }
            logger.info("created software version %s for software %s: %r",
                        version.id, software.software, version_data)

            return jsonify(version_data), CREATED
Esempio n. 5
0
    def put(self, software_rq):
        """
        A ``PUT`` to this endpoint will create a new software tag under the
        given URI or update an existing software tag if one exists.
        Renaming existing software tags via this call is supported, but when
        creating new ones, the included software name must be equal to the one in
        the URI.

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

        .. http:put:: /api/v1/software/<str:softwarename> HTTP/1.1

            **Request**

            .. sourcecode:: http

                PUT /api/v1/software/blender HTTP/1.1
                Accept: application/json

                {
                    "software": "blender"
                }

            **Response**

            .. sourcecode:: http

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

                {
                    "id": 4,
                    "software": "blender",
                    "versions": []
                }

            **Request**

            .. sourcecode:: http

                PUT /api/v1/software/blender HTTP/1.1
                Accept: application/json

                {
                    "software": "blender",
                    "version": [
                        {
                            "version": "1.69"
                        }
                    ]
                }

            **Response**

            .. sourcecode:: http

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

                {
                    "id": 4,
                    "software": "blender",
                    "versions": [
                        {
                            "version": "1.69",
                            "id": 1,
                            "rank": 100
                        }
                    ]
                }

        :statuscode 200: an existing software tag was updated
        :statuscode 201: a new software tag was created
        :statuscode 400: there was something wrong with the request (such as
                            invalid columns being included)
        """
        if isinstance(software_rq, STRING_TYPES):
            if g.json["software"] != software_rq:
                return jsonify(error="""The name of the software must be equal
                               to the one in the URI."""), BAD_REQUEST
            software = Software.query.filter_by(software=software_rq).first()
        else:
            software = Software.query.filter_by(id=software_rq).first()

        new = False if software else True
        if not software:
            software = Software()
            # This is only checked when creating new software.  Otherwise,
            # renaming is allowed
            if g.json["software"] != software_rq:
                return jsonify(error="""The name of the software must be equal
                                     to the one in the URI."""), BAD_REQUEST

        # If this endpoint specified by id, make sure to create the new
        # software under this same id, too
        if isinstance(software_rq, int):
            software.id = software_rq

        software.software = g.json["software"]

        if "versions" in g.json:
            software.versions = []
            db.session.flush()
            versions = extract_version_dicts(g.json)
            current_rank = 100
            for version_dict in versions:
                version_dict.setdefault("rank", current_rank)
                version = SoftwareVersion(**version_dict)
                version.software = software
                current_rank = max(version.rank, current_rank) + 100

        db.session.add(software)
        try:
            db.session.commit()
        except DatabaseError:
            return jsonify(error="Database error"), INTERNAL_SERVER_ERROR
        software_data = software.to_dict()
        logger.info("created software %s: %r", software.id, software_data)

        return jsonify(software_data), CREATED if new else OK
Esempio n. 6
0
    def post(self):
        """
        A ``POST`` to this endpoint will create a new software tag.

        A list of versions can be included.  If the software item already exists
        the listed versions will be added to the existing ones.  Versions with no
        explicit rank are assumed to be the newest version available.  Users
        should not mix versions with an explicit rank with versions without one.

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

            **Request**

            .. sourcecode:: http

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

                {
                    "software": "blender"
                }


            **Response (new software item create)**

            .. sourcecode:: http

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

                {
                    "id": 4,
                    "software": "blender",
                    "versions": []
                }

        :statuscode 201: a new software item was created
        :statuscode 400: there was something wrong with the request (such as
                            invalid columns being included)
        :statuscode 409: a software tag with that name already exists
        """
        # Collect versions to add to the software object
        # Note: This can probably be done a lot simpler with generic parsing
        # of relations
        try:
            versions = extract_version_dicts(g.json)
        except VersionParseError as e:
            return jsonify(error=e.args[0]), BAD_REQUEST
        software = Software.query.filter_by(software=g.json["software"]).first()

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

        software = Software(**g.json)
        current_rank = 100
        for version_dict in versions:
            version_dict.setdefault("rank", current_rank)
            version = SoftwareVersion(**version_dict)
            version.software = software
            current_rank = max(version.rank, current_rank) + 100

        db.session.add(software)
        try:
            db.session.commit()
        except DatabaseError:
            return jsonify(error="Database error"), INTERNAL_SERVER_ERROR
        software_data = software.to_dict()
        logger.info("created software %s: %r", software.id, software_data)

        return jsonify(software_data), CREATED