Example #1
0
def get_single_targets(target_id):
    """Returns a single target."""

    logger.info("GET: path={}, client={}, target_id={}".format(request.path, request.remote_addr, target_id))

    try:
        tg_entry = Target.get(Target.id == target_id)
    except Target.DoesNotExist:
        abort(404, "Target with this ID doesn't exists.")

    target = {"id": tg_entry.id,
              "target": tg_entry.host,
              "job": tg_entry.job.name,
              "labels": create_labels_hash(tg_entry.labels)
              }

    logger.debug("Showing target ID {}.".format(tg_entry.id))
    return jsonify(target)
Example #2
0
def get_targets():
    """Returns all targets."""

    logger.info("GET: path={}, client={}".format(request.path, request.remote_addr))

    targets = []
    for target in Target.select():

        # Add target to array, so we can return an array of all targets.
        targets.append({
            "id": target.id,
            "target": target.host,
            "job": target.job.name,
            "labels": create_labels_hash(target.labels)
        })

    logger.debug("Showing all targets.")
    return jsonify(targets), 200
Example #3
0
def update_target(target_id):
    """Creates or updates an existing target."""

    document = request.json

    logger.info("PUT: path={}, client={}, ID={}".format(request.path, request.remote_addr, target_id))
    logger.debug("PUT: document={}".format(document))

    # Check if we received a valid document and fail with error message.
    validate_document(document)

    # Update target if it exists, else create it
    if Target.get_or_none(Target.id == target_id):
        logger.debug("Updating record with ID {}".format(target_id))

        # Get target
        target = Target.get(Target.id == target_id)

        # Remove existing labels, will readd them later
        Label.delete().where(Label.target == target_id).execute()

        # Update target
        target.host = document['target']
        target.job = Job.get(Job.name == document['job'])
        target.save()

        # Create labels if they are passed in document
        if 'labels' in document:
            for label, value in document['labels'].items():
                Label.create(target=target, label=label, value=value)

        return_code = 200
        logger.info("Target '{}' updated.".format(target.host))

    else:
        logger.debug("Creating record with ID {}".format(target_id))

        # Create new target
        target = Target.create( id=target_id,
                                host=document['target'],
                                job=Job.get(Job.name == document['job']))

        # Create labels if they are passed in document
        if 'labels' in document:
            for label, value in document['labels'].items():
                Label.create(target=target, label=label, value=value)

        return_code = 201
        logger.info("Target '{}' created.".format(target.host))

    # Generate YAML files
    generate_targets_yaml()

    # Add ID field in returned document.
    document['id'] = target.id

    # Return document with correct return code based on event (update or create)
    return jsonify(document), return_code
Example #4
0
def validate_document(document):
    """Check if received document is valid for ingestion."""

    # Fail if not sent in JSON format
    if not document:
        logger.debug("Received document was invalid.")
        abort(400)

    # Fail if missing required fields
    if 'target' not in document:
        logger.debug("Request is missing a target field.")
        abort(422, 'Request is missing a target field.')
    if 'job' not in document:
        logger.debug("Request is missing a job field.")
        abort(422, 'Request is missing a job field.')

    # Fail if labels not dict
    if 'labels' in document:
        if not isinstance(document['labels'], dict):
            logger.debug("Request doesn't have labels in array.")
            abort(422, 'Labels should be passed as an array.')
Example #5
0
def create_target():
    """
    Creates a new target.
    ---
    tags:
      - targets
    definitions:
      - schema:
          id: Target
          properties:
            target:
             type: string
            job:
             type: string
            labels:
              type: hash
              items:
                $ref: "#/definitions/Label"
          id: Label
          properties:
            target_id:
              type: integer
            label:
              type: string
            value:
              type: string

    # parameters:
    #   - in: body
    #     name: body
    #     schema:
    #       id: Target
    #       required:
    #         - target
    #         - job
    #       properties:
    #         target:
    #           type: string
    #           description: target's fqdn
    #         job:
    #           type: string
    #           description: exporter's name
    #         labels:
    #           type: object
    #           properties:
    #             label_name:
    #               type: string
    #           description: list of labels
    responses:
      201:
        description: Target created
      400:
        description: Mailformed JSON, labels not array or missing field.
    """

    document = request.json

    logger.info("POST: path={}, client={}".format(request.path, request.remote_addr))
    logger.debug("POST: document={}".format(document))

    # Check if we received a valid document and fail with error message.
    validate_document(document)

    # Check if this target/job pair already exists
    target_exists = Target.select().where(
        Target.host == document['target'],
        Target.job == Job.select().where(Job.name == document['job'])
    ).exists()
    if target_exists:
        logger.debug("Target {} exists.".format(document['target']))
        abort(409, 'This target already exists. If you want to modify it, use PUT request.')

    # Get job object
    job = Job.get(Job.name == document['job'])

    # Create target
    target = Target.create(host=document['target'], job=job)

    # Create labels if they are passed in document
    if 'labels' in document:
        for label, value in document['labels'].items():
            Label.create(target=target, label=label, value=value)

    # Generate YAML files
    generate_targets_yaml()

    # Add ID field in returned document.
    document['id'] = target.id

    logger.info("Target '{}' added.".format(target.host))
    return jsonify(document), 201