Exemplo n.º 1
0
def add_host(host):
    """
    Add or update a host

    Required parameters:
     - at least one of the canonical facts fields is required
     - account number
    """
    logger.debug("add_host(%s)" % host)

    account_number = host.get("account", None)

    if current_identity.account_number != account_number:
        raise InventoryException(
            title="Invalid request",
            detail="The account number associated with the user does not "
            "match the account number associated with the host")

    input_host = Host.from_json(host)

    canonical_facts = input_host.canonical_facts

    if not canonical_facts:
        raise InventoryException(title="Invalid request",
                                 detail="At least one of the canonical fact "
                                 "fields must be present.")

    existing_host = find_existing_host(account_number, canonical_facts)

    if existing_host:
        return update_existing_host(existing_host, input_host)
    else:
        return create_new_host(input_host)
Exemplo n.º 2
0
def update_system_profile(input_host, identity, staleness_offset, fields):
    if not input_host.system_profile_facts:
        raise InventoryException(
            title="Invalid request",
            detail=
            "Cannot update System Profile, since no System Profile data was provided."
        )

    with session_guard(db.session):
        if input_host.id:
            existing_host = find_existing_host_by_id(identity, input_host.id)
        else:
            existing_host = find_existing_host(identity,
                                               input_host.canonical_facts)

        if existing_host:
            logger.debug("Updating system profile on an existing host")
            logger.debug(f"existing host = {existing_host}")

            existing_host.update_system_profile(
                input_host.system_profile_facts)
            db.session.commit()

            metrics.update_host_count.inc()
            logger.debug("Updated system profile for host:%s", existing_host)

            output_host = serialize_host(existing_host, staleness_offset,
                                         fields)
            insights_id = existing_host.canonical_facts.get("insights_id")
            return output_host, existing_host.id, insights_id, AddHostResult.updated
        else:
            raise InventoryException(
                title="Invalid request",
                detail=
                "Could not find an existing host with the provided facts.")
Exemplo n.º 3
0
    def __init__(
        self,
        canonical_facts,
        display_name=None,
        ansible_host=None,
        account=None,
        facts=None,
        tags={},
        system_profile_facts=None,
        stale_timestamp=None,
        reporter=None,
        per_reporter_staleness=None,
    ):
        if not canonical_facts:
            raise InventoryException(
                title="Invalid request", detail="At least one of the canonical fact fields must be present."
            )

        if not stale_timestamp or not reporter:
            raise InventoryException(
                title="Invalid request", detail="Both stale_timestamp and reporter fields must be present."
            )

        if tags is None:
            raise InventoryException(title="Invalid request", detail="The tags field cannot be null.")

        super().__init__(canonical_facts, display_name, ansible_host, account, facts, tags, system_profile_facts)
        self.stale_timestamp = stale_timestamp
        self.reporter = reporter
        self.per_reporter_staleness = per_reporter_staleness or {}
        if not per_reporter_staleness:
            self._update_per_reporter_staleness(stale_timestamp, reporter)
Exemplo n.º 4
0
def _add_host(host):
    """
    Add or update a host

    Required parameters:
     - at least one of the canonical facts fields is required
     - account number
    """
    validated_input_host_dict = HostSchema(strict=True).load(host)

    input_host = Host.from_json(validated_input_host_dict.data)

    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",
        )

    existing_host = find_existing_host(input_host.account,
                                       input_host.canonical_facts)

    if existing_host:
        return update_existing_host(existing_host, input_host)
    else:
        return create_new_host(input_host)
    def __init__(
        self,
        canonical_facts,
        display_name=display_name,
        ansible_host=None,
        account=account,
        facts=None,
        system_profile_facts=None,
    ):

        if not canonical_facts:
            raise InventoryException(title="Invalid request",
                                     detail="At least one of the canonical "
                                     "fact fields must be present.")

        self.canonical_facts = canonical_facts

        if display_name:
            # Only set the display_name field if input the display_name has
            # been set...this will make it so that the "default" logic will
            # get called during the save to fill in an empty display_name
            self.display_name = display_name
        self._update_ansible_host(ansible_host)
        self.account = account
        self.facts = facts
        self.system_profile_facts = system_profile_facts or {}
Exemplo n.º 6
0
    def patch(self, patch_data):
        logger.debug("patching host (id=%s) with data: %s", self.id, patch_data)

        if not patch_data:
            raise InventoryException(title="Bad Request", detail="Patch json document cannot be empty.")

        self.update_display_name(patch_data.get("display_name"))
        self._update_ansible_host(patch_data.get("ansible_host"))
Exemplo n.º 7
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)
Exemplo n.º 8
0
    def _update_tags(self, tags_dict):
        if self.tags is None:
            raise InventoryException(
                title="Invalid request", detail="Tags must be either an object or an array, and cannot be null."
            )

        for namespace, ns_tags in tags_dict.items():
            if ns_tags:
                self._replace_tags_in_namespace(namespace, ns_tags)
            else:
                self._delete_tags_namespace(namespace)
Exemplo n.º 9
0
    def __init__(
        self,
        canonical_facts,
        display_name=None,
        ansible_host=None,
        account=None,
        facts=None,
        tags=None,
        system_profile_facts=None,
        stale_timestamp=None,
        reporter=None,
    ):

        if not canonical_facts:
            raise InventoryException(
                title="Invalid request",
                detail=
                "At least one of the canonical fact fields must be present.")

        if (not stale_timestamp and reporter) or (stale_timestamp
                                                  and not reporter):
            raise InventoryException(
                title="Invalid request",
                detail=
                "Both stale_timestamp and reporter fields must be present.")

        self.canonical_facts = canonical_facts

        if display_name:
            # Only set the display_name field if input the display_name has
            # been set...this will make it so that the "default" logic will
            # get called during the save to fill in an empty display_name
            self.display_name = display_name
        self._update_ansible_host(ansible_host)
        self.account = account
        self.facts = facts or {}
        self.tags = tags or {}
        self.system_profile_facts = system_profile_facts or {}
        self.stale_timestamp = stale_timestamp
        self.reporter = reporter