Example #1
0
def create(cdDetails):
    """
    This function creates a new cd in the cd list
    based on the passed in cd data

    :param cd: cd to create in cd structure
    :return:        201 on success, 406 on cd exists
    """
    key = cdDetails.get("key", None)

    # Does the cd exist already?
    existing_cd = (CD.query.filter(CD.key == key).one_or_none())

    if existing_cd is None:
        schema = CDSchema()
        new_cd = schema.load(cdDetails, session=db.session)
        db.session.add(new_cd)
        db.session.commit()

        # Serialize and return the newly created deployment
        # in the response
        data = schema.dump(new_cd)

        return data, 201

    # Otherwise, it already exists, that's an error
    else:
        abort(406, f"CD already exists")
Example #2
0
def update(key, cdDetails):
    """
    This function updates an existing cd in the cd list

    :param key:    key of the cd to update in the cd list
    :param cd:   cd to update
    :return:       updated cd
    """

    app.logger.debug(pformat(cdDetails))

    if cdDetails["key"] != key:
        abort(400, f"Key mismatch in path and body")

    # Does the cd exist in cd list?
    existing_cd = CD.query.filter(CD.key == key).one_or_none()

    # Does cd exist?

    if existing_cd is not None:
        schema = CDSchema()
        update_cd = schema.load(cdDetails, session=db.session)
        update_cd.key = cdDetails['key']

        db.session.merge(update_cd)
        db.session.commit()

        # return the updted cd in the response
        data = schema.dump(update_cd)
        return data, 200

    # otherwise, nope, deployment doesn't exist, so that's an error
    else:
        abort(404, f"CD not found")
Example #3
0
def create(cdDetails):
    """
    This function creates a new cd in the cd list
    based on the passed in cd data

    :param cd: cd to create in cd structure
    :return:        201 on success, 406 on cd exists
    """
    # Remove id as it's created automatically
    if "id" in cdDetails:
        del cdDetails["id"]
    # Does the cd exist already?
    existing_cd = (
        db.session.query(CD).filter(CD.value == cdDetails["value"]).one_or_none()
    )

    if existing_cd is None:
        schema = CDSchema()
        new_cd = schema.load(cdDetails, session=db.session)
        db.session.add(new_cd)
        db.session.commit()

        # Serialize and return the newly created deployment
        # in the response
        data = schema.dump(new_cd)

        return data, 201

    # Otherwise, it already exists, that's an error
    else:
        abort(406, "CD already exists")