Esempio n. 1
0
    def nodes(self,
              unreported=2,
              with_status=False,
              with_event_numbers=True,
              **kwargs):
        """Query for nodes by either name or query. If both aren't
        provided this will return a list of all nodes. This method
        also (optionally) fetches the nodes status and (optionally)
        event counts of the latest report from puppetdb.

        :param with_status: (optional) include the node status in the\
                           returned nodes
        :type with_status: :bool:
        :param unreported: (optional) amount of hours when a node gets
                           marked as unreported
        :type unreported: :obj:`None` or integer
        :param with_event_numbers: (optional) include the exact number of\
                           changed/unchanged/failed/noop events when\
                           with_status is set to True. If set to False
                           only "some" string is provided if there are
                           resources with such status in the last report.
                           This provides performance benefits as potentially
                           slow event-counts query is omitted completely.
        :type with_event_numbers: :bool:
        :param \*\*kwargs: The rest of the keyword arguments are passed
                           to the _query function

        :returns: A generator yieling Nodes.
        :rtype: :class:`pypuppetdb.types.Node`
        """
        nodes = self._query('nodes', **kwargs)
        now = datetime.utcnow()

        # If we happen to only get one node back it
        # won't be inside a list so iterating over it
        # goes boom. Therefor we wrap a list around it.
        if type(nodes) == dict:
            nodes = [
                nodes,
            ]

        latest_events = None
        if with_status and with_event_numbers:
            latest_events = self._query(
                'event-counts',
                query=EqualsOperator("latest_report?", True),
                summarize_by='certname',
            )

        for node in nodes:
            yield Node.create_from_dict(self, node, with_status,
                                        with_event_numbers, latest_events, now,
                                        unreported)
Esempio n. 2
0
    def pql(self,
            pql,
            with_status=False,
            unreported=2,
            with_event_numbers=True):
        """Makes a PQL (Puppet Query Language) and tries to cast results
        to a rich type. If it won't work, returns plain dicts.

        :param pql: PQL query
        :type pql: :obj:`string`

        :param with_status: (optional, only for queries for nodes) include
                           the node status in the returned nodes
        :type with_status: :bool:
        :param unreported: (optional, only for queries for nodes) amount
                           of hours when a node gets marked as unreported
        :type unreported: :obj:`None` or integer
        :param with_event_numbers: (optional, only for queries for nodes)
                           include the exact number of
                           changed/unchanged/failed/noop events when
                           with_status is set to True. If set to False
                           only "some" string is provided if there are
                           resources with such status in the last report.
                           This provides performance benefits as potentially
                           slow event-counts query is omitted completely.
        :type with_event_numbers: :bool:

        :returns: A generator yielding elements of a rich type or plain dicts
        """

        type_class = self._get_type_from_query(pql)

        if type_class == Node and (with_status or unreported != 2
                                   or not with_event_numbers):
            log.error(
                "with_status, unreported and with_event_numbers are used only"
                " for queries for nodes!")
            raise APIError

        for element in self._pql(pql=pql):
            if type_class == Node:

                # TODO: deduplicate this - see QueryAPI.nodes()

                now = datetime.utcnow()

                latest_events = None
                if with_status and with_event_numbers:
                    latest_events = self._query(
                        'event-counts',
                        query=EqualsOperator("latest_report?", True),
                        summarize_by='certname',
                    )

                yield Node.create_from_dict(self, element, with_status,
                                            with_event_numbers, latest_events,
                                            now, unreported)

            elif type_class == Report:
                yield Report.create_from_dict(self, element)
            elif type_class:
                yield type_class.create_from_dict(element)
            else:
                yield element