This repository was archived by the owner on Feb 26, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy path_http.py
More file actions
525 lines (418 loc) · 19 KB
/
_http.py
File metadata and controls
525 lines (418 loc) · 19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# Copyright 2016 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Interact with Cloud Logging via JSON-over-HTTP."""
import functools
from google.api_core import page_iterator
from google.cloud import _http
from google.cloud.logging_v2 import __version__
from google.cloud.logging_v2._helpers import entry_from_resource
from google.cloud.logging_v2.sink import Sink
from google.cloud.logging_v2.metric import Metric
class Connection(_http.JSONConnection):
DEFAULT_API_ENDPOINT = "https://logging.googleapis.com"
def __init__(self, client, *, client_info=None, api_endpoint=DEFAULT_API_ENDPOINT):
"""A connection to Google Cloud Logging via the JSON REST API.
Args:
client (google.cloud.logging_v2.cliet.Client):
The client that owns the current connection.
client_info (Optional[google.api_core.client_info.ClientInfo]):
Instance used to generate user agent.
client_options (Optional[google.api_core.client_options.ClientOptions]):
Client options used to set user options
on the client. API Endpoint should be set through client_options.
"""
super(Connection, self).__init__(client, client_info)
self.API_BASE_URL = api_endpoint
self._client_info.gapic_version = __version__
self._client_info.client_library_version = __version__
API_VERSION = "v2"
"""The version of the API, used in building the API call's URL."""
API_URL_TEMPLATE = "{api_base_url}/{api_version}{path}"
"""A template for the URL of a particular API call."""
class _LoggingAPI(object):
"""Helper mapping logging-related APIs.
See
https://cloud.google.com/logging/docs/reference/v2/rest/v2/entries
https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.logs
:type client: :class:`~google.cloud.logging.client.Client`
:param client: The client used to make API requests.
"""
def __init__(self, client):
self._client = client
self.api_request = client._connection.api_request
def list_entries(
self,
resource_names,
*,
filter_=None,
order_by=None,
page_size=None,
page_token=None,
):
"""Return a page of log entry resources.
Args:
resource_names (Sequence[str]): Names of one or more parent resources
from which to retrieve log entries:
::
"projects/[PROJECT_ID]"
"organizations/[ORGANIZATION_ID]"
"billingAccounts/[BILLING_ACCOUNT_ID]"
"folders/[FOLDER_ID]"
filter_ (str): a filter expression. See
https://cloud.google.com/logging/docs/view/advanced_filters
order_by (str) One of :data:`~logging_v2.ASCENDING`
or :data:`~logging_v2.DESCENDING`.
page_size (int): maximum number of entries to return, If not passed,
defaults to a value set by the API.
page_token (str): opaque marker for the next "page" of entries. If not
passed, the API will return the first page of
entries.
Returns:
Iterator[~logging_v2.LogEntry]
"""
extra_params = {"resourceNames": resource_names}
if filter_ is not None:
extra_params["filter"] = filter_
if order_by is not None:
extra_params["orderBy"] = order_by
if page_size is not None:
extra_params["pageSize"] = page_size
path = "/entries:list"
# We attach a mutable loggers dictionary so that as Logger
# objects are created by entry_from_resource, they can be
# re-used by other log entries from the same logger.
loggers = {}
item_to_value = functools.partial(_item_to_entry, loggers=loggers)
iterator = page_iterator.HTTPIterator(
client=self._client,
api_request=self._client._connection.api_request,
path=path,
item_to_value=item_to_value,
items_key="entries",
page_token=page_token,
extra_params=extra_params,
)
# This method uses POST to make a read-only request.
iterator._HTTP_METHOD = "POST"
return iterator
def write_entries(
self,
entries,
*,
logger_name=None,
resource=None,
labels=None,
partial_success=False,
dry_run=False,
):
"""Log an entry resource via a POST request
See
https://cloud.google.com/logging/docs/reference/v2/rest/v2/entries/write
Args:
entries (Sequence[Mapping[str, ...]]): sequence of mappings representing
the log entry resources to log.
logger_name (Optional[str]): name of default logger to which to log the entries;
individual entries may override.
resource(Optional[Mapping[str, ...]]): default resource to associate with entries;
individual entries may override.
labels (Optional[Mapping[str, ...]]): default labels to associate with entries;
individual entries may override.
partial_success (Optional[bool]): Whether valid entries should be written even if
some other entries fail due to INVALID_ARGUMENT or
PERMISSION_DENIED errors. If any entry is not written, then
the response status is the error associated with one of the
failed entries and the response includes error details keyed
by the entries' zero-based index in the ``entries.write``
method.
dry_run (Optional[bool]):
If true, the request should expect normal response,
but the entries won't be persisted nor exported.
Useful for checking whether the logging API endpoints are working
properly before sending valuable data.
"""
data = {
"entries": list(entries),
"partialSuccess": partial_success,
"dry_run": dry_run,
}
if logger_name is not None:
data["logName"] = logger_name
if resource is not None:
data["resource"] = resource
if labels is not None:
data["labels"] = labels
self.api_request(method="POST", path="/entries:write", data=data)
def logger_delete(self, logger_name):
"""Delete all entries in a logger.
Args:
logger_name (str): The resource name of the log to delete:
::
"projects/[PROJECT_ID]/logs/[LOG_ID]"
"organizations/[ORGANIZATION_ID]/logs/[LOG_ID]"
"billingAccounts/[BILLING_ACCOUNT_ID]/logs/[LOG_ID]"
"folders/[FOLDER_ID]/logs/[LOG_ID]"
``[LOG_ID]`` must be URL-encoded. For example,
``"projects/my-project-id/logs/syslog"``,
``"organizations/1234567890/logs/cloudresourcemanager.googleapis.com%2Factivity"``.
"""
path = f"/{logger_name}"
self.api_request(method="DELETE", path=path)
class _SinksAPI(object):
"""Helper mapping sink-related APIs.
See
https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks
"""
def __init__(self, client):
self._client = client
self.api_request = client._connection.api_request
def list_sinks(self, parent, *, page_size=None, page_token=None):
"""List sinks for the parent resource.
See
https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks/list
Args:
parent (str): The parent resource whose sinks are to be listed:
::
"projects/[PROJECT_ID]"
"organizations/[ORGANIZATION_ID]"
"billingAccounts/[BILLING_ACCOUNT_ID]"
"folders/[FOLDER_ID]".
page_size (Optional[int]): Maximum number of sinks to return, If not passed,
defaults to a value set by the API.
page_token (Optional[str]): Opaque marker for the next "page" of sinks. If not
passed, the API will return the first page of
sinks.
Returns:
Iterator[~logging_v2.Sink]
"""
extra_params = {}
if page_size is not None:
extra_params["pageSize"] = page_size
path = f"/{parent}/sinks"
return page_iterator.HTTPIterator(
client=self._client,
api_request=self._client._connection.api_request,
path=path,
item_to_value=_item_to_sink,
items_key="sinks",
page_token=page_token,
extra_params=extra_params,
)
def sink_create(
self, parent, sink_name, filter_, destination, *, unique_writer_identity=False
):
"""Create a sink resource.
See
https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.sinks/create
Args:
parent(str): The resource in which to create the sink:
::
"projects/[PROJECT_ID]"
"organizations/[ORGANIZATION_ID]"
"billingAccounts/[BILLING_ACCOUNT_ID]"
"folders/[FOLDER_ID]".
sink_name (str): The name of the sink.
filter_ (str): The advanced logs filter expression defining the
entries exported by the sink.
destination (str): Destination URI for the entries exported by
the sink.
unique_writer_identity (Optional[bool]): determines the kind of
IAM identity returned as writer_identity in the new sink.
Returns:
dict: The sink resource returned from the API.
"""
target = f"/{parent}/sinks"
data = {"name": sink_name, "filter": filter_, "destination": destination}
query_params = {"uniqueWriterIdentity": unique_writer_identity}
return self.api_request(
method="POST", path=target, data=data, query_params=query_params
)
def sink_get(self, sink_name):
"""Retrieve a sink resource.
Args:
sink_name (str): The resource name of the sink:
::
"projects/[PROJECT_ID]/sinks/[SINK_ID]"
"organizations/[ORGANIZATION_ID]/sinks/[SINK_ID]"
"billingAccounts/[BILLING_ACCOUNT_ID]/sinks/[SINK_ID]"
"folders/[FOLDER_ID]/sinks/[SINK_ID]"
Returns:
dict: The JSON sink object returned from the API.
"""
target = f"/{sink_name}"
return self.api_request(method="GET", path=target)
def sink_update(
self, sink_name, filter_, destination, *, unique_writer_identity=False
):
"""Update a sink resource.
Args:
sink_name (str): Required. The resource name of the sink:
::
"projects/[PROJECT_ID]/sinks/[SINK_ID]"
"organizations/[ORGANIZATION_ID]/sinks/[SINK_ID]"
"billingAccounts/[BILLING_ACCOUNT_ID]/sinks/[SINK_ID]"
"folders/[FOLDER_ID]/sinks/[SINK_ID]"
filter_ (str): The advanced logs filter expression defining the
entries exported by the sink.
destination (str): destination URI for the entries exported by
the sink.
unique_writer_identity (Optional[bool]): determines the kind of
IAM identity returned as writer_identity in the new sink.
Returns:
dict: The returned (updated) resource.
"""
target = f"/{sink_name}"
name = sink_name.split("/")[-1] # parse name out of full resoure name
data = {"name": name, "filter": filter_, "destination": destination}
query_params = {"uniqueWriterIdentity": unique_writer_identity}
return self.api_request(
method="PUT", path=target, query_params=query_params, data=data
)
def sink_delete(self, sink_name):
"""Delete a sink resource.
Args:
sink_name (str): Required. The full resource name of the sink to delete,
including the parent resource and the sink identifier:
::
"projects/[PROJECT_ID]/sinks/[SINK_ID]"
"organizations/[ORGANIZATION_ID]/sinks/[SINK_ID]"
"billingAccounts/[BILLING_ACCOUNT_ID]/sinks/[SINK_ID]"
"folders/[FOLDER_ID]/sinks/[SINK_ID]"
Example: ``"projects/my-project-id/sinks/my-sink-id"``.
"""
target = f"/{sink_name}"
self.api_request(method="DELETE", path=target)
class _MetricsAPI(object):
"""Helper mapping sink-related APIs."""
def __init__(self, client):
self._client = client
self.api_request = client._connection.api_request
def list_metrics(self, project, *, page_size=None, page_token=None):
"""List metrics for the project associated with this client.
See
https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.metrics/list
Args:
page_size (Optional[int]): The maximum number of sinks in each
page of results from this request. Non-positive values are ignored. Defaults to a
sensible value set by the API.
page_token (Optional[str]): If present, return the next batch of sinks, using the
value, which must correspond to the ``nextPageToken`` value
returned in the previous response. Deprecated: use the ``pages``
property ofthe returned iterator instead of manually passing the
token.
Returns:
Iterator[google.cloud.logging_v2.metric.Metric]
"""
extra_params = {}
if page_size is not None:
extra_params["pageSize"] = page_size
path = f"/projects/{project}/metrics"
return page_iterator.HTTPIterator(
client=self._client,
api_request=self._client._connection.api_request,
path=path,
item_to_value=_item_to_metric,
items_key="metrics",
page_token=page_token,
extra_params=extra_params,
)
def metric_create(self, project, metric_name, filter_, description):
"""Create a metric resource.
See
https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.metrics/create
Args:
project (str): ID of the project in which to create the metric.
metric_name (str): The name of the metric
filter_ (str): The advanced logs filter expression defining the
entries exported by the metric.
description (str): description of the metric.
"""
target = f"/projects/{project}/metrics"
data = {"name": metric_name, "filter": filter_, "description": description}
self.api_request(method="POST", path=target, data=data)
def metric_get(self, project, metric_name):
"""Retrieve a metric resource.
Args:
project (str): ID of the project containing the metric.
metric_name (str): The name of the metric
Returns:
dict: The JSON metric object returned from the API.
"""
target = f"/projects/{project}/metrics/{metric_name}"
return self.api_request(method="GET", path=target)
def metric_update(self, project, metric_name, filter_, description):
"""Update a metric resource.
See
https://cloud.google.com/logging/docs/reference/v2/rest/v2/projects.metrics/update
Args:
project (str): ID of the project containing the metric.
metric_name (str): the name of the metric
filter_ (str): the advanced logs filter expression defining the
entries exported by the metric.
description (str): description of the metric.
Returns:
dict: The returned (updated) resource.
"""
target = f"/projects/{project}/metrics/{metric_name}"
data = {"name": metric_name, "filter": filter_, "description": description}
return self.api_request(method="PUT", path=target, data=data)
def metric_delete(self, project, metric_name):
"""Delete a metric resource.
Args:
project (str): ID of the project containing the metric.
metric_name (str): The name of the metric
"""
target = f"/projects/{project}/metrics/{metric_name}"
self.api_request(method="DELETE", path=target)
def _item_to_entry(iterator, resource, loggers):
"""Convert a log entry resource to the native object.
.. note::
This method does not have the correct signature to be used as
the ``item_to_value`` argument to
:class:`~google.api_core.page_iterator.Iterator`. It is intended to be
patched with a mutable ``loggers`` argument that can be updated
on subsequent calls. For an example, see how the method is
used above in :meth:`_LoggingAPI.list_entries`.
Args:
iterator (google.api_core.page_iterator.Iterator): The iterator that
is currently in use.
resource (dict): Log entry JSON resource returned from the API.
loggers (Mapping[str, logging_v2.logger.Logger]):
A mapping of logger fullnames -> loggers. If the logger
that owns the entry is not in ``loggers``, the entry
will have a newly-created logger.
Returns:
~logging_v2.entries._BaseEntry: The next log entry in the page.
"""
return entry_from_resource(resource, iterator.client, loggers)
def _item_to_sink(iterator, resource):
"""Convert a sink resource to the native object.
Args:
iterator (google.api_core.page_iterator.Iterator): The iterator that
is currently in use.
resource (dict): Sink JSON resource returned from the API.
Returns:
~logging_v2.sink.Sink: The next sink in the page.
"""
return Sink.from_api_repr(resource, iterator.client)
def _item_to_metric(iterator, resource):
"""Convert a metric resource to the native object.
Args:
iterator (google.api_core.page_iterator.Iterator): The iterator that
is currently in use.
resource (dict): Sink JSON resource returned from the API.
Returns:
~logging_v2.metric.Metric:
The next metric in the page.
"""
return Metric.from_api_repr(resource, iterator.client)