Example #1
0
def add_host(host_data):
    payload_tracker = get_payload_tracker(payload_id=threadctx.request_id)

    with PayloadTrackerProcessingContext(
            payload_tracker, processing_status_message="adding/updating host"
    ) as payload_tracker_processing_ctx:

        try:
            logger.info("Attempting to add host...")
            input_host = deserialize_host(host_data)
            (output_host,
             add_results) = host_repository.add_host(input_host,
                                                     staleness_offset())
            metrics.add_host_success.labels(
                add_results.name,
                host_data.get("reporter", "null")).inc()  # created vs updated
            logger.info(
                "Host added"
            )  # This definitely needs to be more specific (added vs updated?)
            payload_tracker_processing_ctx.inventory_id = output_host["id"]
            return (output_host, add_results)
        except InventoryException:
            logger.exception("Error adding host ", extra={"host": host_data})
            metrics.add_host_failure.labels("InventoryException",
                                            host_data.get("reporter",
                                                          "null")).inc()
            raise
        except Exception:
            logger.exception("Error while adding host",
                             extra={"host": host_data})
            metrics.add_host_failure.labels("Exception",
                                            host_data.get("reporter",
                                                          "null")).inc()
            raise
def add_host(host_data):
    payload_tracker = get_payload_tracker(request_id=threadctx.request_id)

    with PayloadTrackerProcessingContext(
            payload_tracker, processing_status_message="adding/updating host"
    ) as payload_tracker_processing_ctx:

        try:
            input_host = deserialize_host(host_data)
            staleness_timestamps = Timestamps.from_config(inventory_config())
            logger.info(
                "Attempting to add host",
                extra={
                    "input_host": {
                        "account": input_host.account,
                        "display_name": input_host.display_name,
                        "canonical_facts": input_host.canonical_facts,
                        "reporter": input_host.reporter,
                        "stale_timestamp":
                        input_host.stale_timestamp.isoformat(),
                        "tags": input_host.tags,
                    }
                },
            )
            (output_host,
             add_results) = host_repository.add_host(input_host,
                                                     staleness_timestamps,
                                                     fields=EGRESS_HOST_FIELDS)
            metrics.add_host_success.labels(
                add_results.name,
                host_data.get("reporter", "null")).inc()  # created vs updated
            # log all the incoming host data except facts and system_profile b/c they can be quite large
            logger.info(
                "Host %s",
                add_results.name,
                extra={
                    "host": {
                        i: output_host[i]
                        for i in output_host
                        if i not in ("facts", "system_profile")
                    }
                },
            )
            payload_tracker_processing_ctx.inventory_id = output_host["id"]
            return (output_host, add_results)
        except InventoryException:
            logger.exception("Error adding host ", extra={"host": host_data})
            metrics.add_host_failure.labels("InventoryException",
                                            host_data.get("reporter",
                                                          "null")).inc()
            raise
        except Exception:
            logger.exception("Error while adding host",
                             extra={"host": host_data})
            metrics.add_host_failure.labels("Exception",
                                            host_data.get("reporter",
                                                          "null")).inc()
            raise
Example #3
0
def _add_host(input_host):
    if not current_identity.is_trusted_system and current_identity.account_number != input_host.account:
        raise InventoryException(
            title="Invalid request",
            detail="The account number associated with the user does not match the account number associated with the "
            "host",
        )

    return add_host(input_host, staleness_timestamps(), update_system_profile=False)
Example #4
0
def add_host(host_data, platform_metadata):
    payload_tracker = get_payload_tracker(request_id=threadctx.request_id)

    with PayloadTrackerProcessingContext(
            payload_tracker,
            processing_status_message="adding/updating host",
            current_operation="adding/updating host"
    ) as payload_tracker_processing_ctx:

        try:
            identity = _get_identity(host_data, platform_metadata)
            # basic-auth does not need owner_id
            if identity.identity_type == IdentityType.SYSTEM:
                host_data = _set_owner(host_data, identity)

            input_host = deserialize_host(host_data)
            staleness_timestamps = Timestamps.from_config(inventory_config())
            log_add_host_attempt(logger, input_host)
            output_host, host_id, insights_id, add_result = host_repository.add_host(
                input_host,
                identity,
                staleness_timestamps,
                fields=EGRESS_HOST_FIELDS)
            log_add_update_host_succeeded(logger, add_result, host_data,
                                          output_host)
            payload_tracker_processing_ctx.inventory_id = output_host["id"]
            return output_host, host_id, insights_id, add_result
        except ValidationException:
            metrics.add_host_failure.labels("ValidationException",
                                            host_data.get("reporter",
                                                          "null")).inc()
            raise
        except InventoryException as ie:
            log_add_host_failure(logger, str(ie.detail), host_data)
            raise
        except OperationalError as oe:
            log_db_access_failure(logger, f"Could not access DB {str(oe)}",
                                  host_data)
            raise oe
        except Exception:
            logger.exception("Error while adding host",
                             extra={"host": host_data})
            metrics.add_host_failure.labels("Exception",
                                            host_data.get("reporter",
                                                          "null")).inc()
            raise