Debian Codegraph @

docs/developer.rst

raw
1 .. _developer:
2
3 Developer Guide
4 ===============
5
6 .. module:: pypuppetdb
7
8 This part of the documentation covers all the interfaces of PyPuppetDB.
9 It will cover how the API is set up and how to configure which version of
10 the API to use.
11
12 Lazy objects
13 ------------
14
15 .. note::
16
17 Reading in the response from PuppetDB is currently greedy, it will read in
18 the complete response no matter the size. This will change once streaming
19 and pagination support are added to PuppetDB's endpoints.
20
21 In order for pypuppetdb to be able to deal with big datasets those functions
22 that are expected to return more than a single item are implemented as
23 generators.
24
25 This is usually the case for functions with a plural name like
26 :func:`~pypuppetdb.api.QueryAPI.nodes` or :func:`~pypuppetdb.api.QueryAPI.facts`.
27
28 Because of this we'll only query PuppetDB once you start iterating over the
29 generator object. Until that time not a single request is fired at PuppetDB.
30
31 Most singular functions are implemented by calling their plural counterpart
32 and then iterating over the generator, immediately exhausting the generator
33 and returning a single/the first object.
34
35
36 API and BaseAPI classes
37 -----------------------
38
39 The starting point for the library is the :func:`connect` method used to set up a
40 connection with PuppetDB.
41
42 .. autofunction:: connect
43
44 This method returns the ``API`` class instance.
45
46 That class is built using mixins from the endpoint-specific classes, such as
47 ``PqlAPI`` (see the next section for more info about them).
48
49 All the endpoint-specific classes inherit from the abstract ``BaseAPI`` class
50 which contains the logic shared by all the endpoints, such as:
51
52 * connecting to the PuppetDB (in the constructor),
53 * disconnecting from the PuppetDB (``disconnect()``),
54 * ``_query()`` and ``_make_request()`` that prepare and make HTTP(S) requests
55 to PuppetDB,
56
57
58 Endpoint classes
59 ----------------
60
61 The supported PuppetDB endpoint types are implemented in classes with
62 corresponding names:
63
64 * pdb/query/v4 - ``PqlAPI``
65 * pdb/query/v4/* - ``QueryAPI``
66 * pdb/cmd/v1 - ``CommandAPI``
67 * status/v1/** - ``StatusAPI``
68 * pdb/meta/v1* - ``MetadataAPI``
69 * metrics/* - ``MetricsAPI``
70
71
72 PqlAPI
73 ^^^^^^
74
75 .. autoclass:: pypuppetdb.api.PqlAPI
76 :members:
77 :private-members:
78
79 QueryAPI
80 ^^^^^^^^
81
82 .. autoclass:: pypuppetdb.api.QueryAPI
83 :members:
84 :private-members:
85
86 CommandAPI
87 ^^^^^^^^^^
88
89 .. autoclass:: pypuppetdb.api.CommandAPI
90 :members:
91 :private-members:
92
93 StatusAPI
94 ^^^^^^^^^
95
96 .. autoclass:: pypuppetdb.api.StatusAPI
97 :members:
98 :private-members:
99
100 MetadataAPI
101 ^^^^^^^^^^^
102
103 .. autoclass:: pypuppetdb.api.MetadataAPI
104 :members:
105 :private-members:
106
107 MetricsAPI
108 ^^^^^^^^^^
109
110 .. autoclass:: pypuppetdb.api.MetricsAPI
111 :members:
112 :private-members:
113
114
115 Types
116 -----
117
118 In order to facilitate working with the API most methods like
119 :meth:`~pypuppetdb.api.QueryAPI.nodes` don't return the decoded
120 JSON response but return an object representation of the querried
121 endpoints data.
122
123 .. autoclass:: pypuppetdb.types.Node
124 :members:
125 .. autoclass:: pypuppetdb.types.Fact
126 .. autoclass:: pypuppetdb.types.Resource
127 .. autoclass:: pypuppetdb.types.Event
128 .. autoclass:: pypuppetdb.types.Report
129 :members:
130 .. autoclass:: pypuppetdb.types.Catalog
131 :members:
132 .. autoclass:: pypuppetdb.types.Edge
133 .. autoclass:: pypuppetdb.types.Inventory
134
135 Errors
136 ------
137
138 Unfortunately things can go haywire. PuppetDB might not be reachable
139 or complain about our query, requests might have to wait too long to
140 receive a response or the body is just too big to handle.
141
142 In that case, we'll throw an exception at you.
143
144 .. autoexception:: pypuppetdb.errors.APIError
145 .. autoexception:: pypuppetdb.errors.ImproperlyConfiguredError
146 :show-inheritance:
147 .. autoexception:: pypuppetdb.errors.DoesNotComputeError
148 :show-inheritance:
149 .. autoexception:: pypuppetdb.errors.EmptyResponseError
150 :show-inheritance:
151
152 Query Builder
153 -------------
154
155 With the increasing complexities of some queries it can be very difficult
156 to maintain query strings as string objects. The 0.3.0 release introduces
157 a new feature called the Query Builder which removes the need of string
158 object concatenation into an Object-Oriented fashion.
159
160 In order to create the following query:
161
162 ``'["and", ["=", "facts_environment", "production"], ["=", "catalog_environment", "production"]]'``
163
164 Users can use the following code block to create the same thing:
165
166 .. code-block:: python
167
168 >>> from pypuppetdb.QueryBuilder import *
169 >>> op = AndOperator()
170 >>> op.add(EqualOperator("facts_environment", "production"))
171 >>> op.add(EqualOperator("catalog_environment", "production"))
172
173 The ``op`` object can then be directly input to the query parameter of any
174 PuppetDB call.
175
176 .. code-block:: python
177
178 >>> pypuppetdb.nodes(query=op)
179
180 .. autoclass:: pypuppetdb.QueryBuilder.BinaryOperator
181 :members:
182 .. autoclass:: pypuppetdb.QueryBuilder.EqualsOperator
183 .. autoclass:: pypuppetdb.QueryBuilder.GreaterOperator
184 .. autoclass:: pypuppetdb.QueryBuilder.LessOperator
185 .. autoclass:: pypuppetdb.QueryBuilder.GreaterEqualOperator
186 .. autoclass:: pypuppetdb.QueryBuilder.LessEqualOperator
187 .. autoclass:: pypuppetdb.QueryBuilder.RegexOperator
188 .. autoclass:: pypuppetdb.QueryBuilder.RegexArrayOperator
189 .. autoclass:: pypuppetdb.QueryBuilder.NullOperator
190 .. autoclass:: pypuppetdb.QueryBuilder.BooleanOperator
191 :members:
192 .. autoclass:: pypuppetdb.QueryBuilder.AndOperator
193 .. autoclass:: pypuppetdb.QueryBuilder.OrOperator
194 .. autoclass:: pypuppetdb.QueryBuilder.NotOperator
195 .. autoclass:: pypuppetdb.QueryBuilder.ExtractOperator
196 :members:
197 .. autoclass:: pypuppetdb.QueryBuilder.FunctionOperator
198 :members:
199 .. autoclass:: pypuppetdb.QueryBuilder.SubqueryOperator
200 :members:
201 .. autoclass:: pypuppetdb.QueryBuilder.InOperator
202 :members:
203 .. autoclass:: pypuppetdb.QueryBuilder.FromOperator
204 :members:
205
206 Utilities
207 ---------
208
209 A few functions that are used across this library have been put
210 into their own :mod:`utils` module.
211
212 .. autoclass:: pypuppetdb.utils.UTC
213 .. autofunction:: pypuppetdb.utils.json_to_datetime