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
//! Operation contracts for the Cloud Logging API API (v2).
//!
//! Auto-generated from the GCP Discovery Document.
//! **Do not edit manually** — modify the manifest and re-run codegen.
//!
//! These are the raw HTTP operations with correct URLs, methods,
//! and parameter ordering. The hand-written `api/logging.rs` wraps
//! these with ergonomic builders, operation polling, etc.
use crate::types::logging::*;
use crate::{GcpHttpClient, Result};
/// Raw HTTP operations for the Cloud Logging API API.
///
/// These methods encode the correct URL paths, HTTP methods, and
/// parameter ordering from the GCP Discovery Document.
/// They are `pub(crate)` — use the ergonomic wrappers in
/// [`super::logging::LoggingClient`] instead.
pub struct LoggingOps<'a> {
pub(crate) client: &'a GcpHttpClient,
}
impl<'a> LoggingOps<'a> {
pub(crate) fn new(client: &'a GcpHttpClient) -> Self {
Self { client }
}
fn base_url(&self) -> &str {
#[cfg(any(test, feature = "test-support"))]
{
if let Some(ref base) = self.client.base_url {
return base.trim_end_matches('/');
}
}
"https://logging.googleapis.com"
}
/// Lists sinks.
///
/// **GCP API**: `GET v2/{+parent}/sinks`
///
/// # Path Parameters
/// - `parent` — Required. The parent resource whose sinks are to be listed: "projects/[PROJECT_ID]" "organizations/[ORGANIZATION_ID]" "b *(required)*
///
/// # Query Parameters
/// - `filter` — Optional. A filter expression to constrain the sinks returned. Today, this only supports the following strings: '' 'in_s
/// - `pageSize` — Optional. The maximum number of results to return from this request. Non-positive values are ignored. The presence of ne
/// - `pageToken` — Optional. If present, then retrieve the next batch of results from the preceding call to this method. pageToken must be
///
/// # Response
/// [`ListSinksResponse`]
#[allow(dead_code)]
pub(crate) async fn list_sinks(
&self,
parent: &str,
page_size: &str,
page_token: &str,
filter: &str,
) -> Result<ListSinksResponse> {
let url = format!("{}/v2/{}/sinks", self.base_url(), parent,);
let url = crate::append_query_params(
url,
&[
("pageSize", page_size),
("pageToken", page_token),
("filter", filter),
],
);
let response = self.client.get(&url).await?;
serde_json::from_slice(&response).map_err(|e| crate::GcpError::InvalidResponse {
message: format!("Failed to parse list_sinks response: {e}"),
body: Some(String::from_utf8_lossy(&response).to_string()),
})
}
/// Gets a sink.
///
/// **GCP API**: `GET v2/{+sinkName}`
///
/// # Path Parameters
/// - `sinkName` — Required. The resource name of the sink: "projects/[PROJECT_ID]/sinks/[SINK_ID]" "organizations/[ORGANIZATION_ID]/sinks/ *(required)*
///
/// # Response
/// [`LogSink`]
#[allow(dead_code)]
pub(crate) async fn get_sink(&self, sink_name: &str) -> Result<LogSink> {
let url = format!("{}/v2/{}", self.base_url(), sink_name,);
let response = self.client.get(&url).await?;
serde_json::from_slice(&response).map_err(|e| crate::GcpError::InvalidResponse {
message: format!("Failed to parse get_sink response: {e}"),
body: Some(String::from_utf8_lossy(&response).to_string()),
})
}
/// Creates a sink that exports specified log entries to a destination. The export begins
/// upon ingress, unless the sink's writer_identity is not permitted to write to the
/// destination. A sink can export log entries only from the resource owning the sink.
///
/// **GCP API**: `POST v2/{+parent}/sinks`
///
/// # Path Parameters
/// - `parent` — Required. The resource in which to create the sink: "projects/[PROJECT_ID]" "organizations/[ORGANIZATION_ID]" "billingAc *(required)*
///
/// # Query Parameters
/// - `customWriterIdentity` — Optional. The service account provided by the caller that will be used to write the log entries. The format must be serv
/// - `uniqueWriterIdentity` — Optional. Determines the kind of IAM identity returned as writer_identity in the new sink. If this value is omitted or s
///
/// # Request Body
/// [`LogSink`]
///
/// # Response
/// [`LogSink`]
#[allow(dead_code)]
pub(crate) async fn create_sink(&self, parent: &str, body: &LogSink) -> Result<LogSink> {
let url = format!("{}/v2/{}/sinks", self.base_url(), parent,);
let response = self.client.post(&url, body).await?;
serde_json::from_slice(&response).map_err(|e| crate::GcpError::InvalidResponse {
message: format!("Failed to parse create_sink response: {e}"),
body: Some(String::from_utf8_lossy(&response).to_string()),
})
}
/// Deletes a sink. If the sink has a unique writer_identity, then that service account is
/// also deleted.
///
/// **GCP API**: `DELETE v2/{+sinkName}`
///
/// # Path Parameters
/// - `sinkName` — Required. The full resource name of the sink to delete, including the parent resource and the sink identifier: "projects *(required)*
///
/// # Response
/// [`LoggingEmpty`]
#[allow(dead_code)]
pub(crate) async fn delete_sink(&self, sink_name: &str) -> Result<LoggingEmpty> {
let url = format!("{}/v2/{}", self.base_url(), sink_name,);
let response = self.client.delete(&url).await?;
serde_json::from_slice(&response).map_err(|e| crate::GcpError::InvalidResponse {
message: format!("Failed to parse delete_sink response: {e}"),
body: Some(String::from_utf8_lossy(&response).to_string()),
})
}
/// Lists logs-based metrics.
///
/// **GCP API**: `GET v2/{+parent}/metrics`
///
/// # Path Parameters
/// - `parent` — Required. The name of the project containing the metrics: "projects/[PROJECT_ID]" *(required)*
///
/// # Query Parameters
/// - `pageSize` — Optional. The maximum number of results to return from this request. Non-positive values are ignored. The presence of ne
/// - `pageToken` — Optional. If present, then retrieve the next batch of results from the preceding call to this method. pageToken must be
///
/// # Response
/// [`ListLogMetricsResponse`]
#[allow(dead_code)]
pub(crate) async fn list_metrics(
&self,
parent: &str,
page_size: &str,
page_token: &str,
) -> Result<ListLogMetricsResponse> {
let url = format!("{}/v2/{}/metrics", self.base_url(), parent,);
let url =
crate::append_query_params(url, &[("pageSize", page_size), ("pageToken", page_token)]);
let response = self.client.get(&url).await?;
serde_json::from_slice(&response).map_err(|e| crate::GcpError::InvalidResponse {
message: format!("Failed to parse list_metrics response: {e}"),
body: Some(String::from_utf8_lossy(&response).to_string()),
})
}
/// Gets a logs-based metric.
///
/// **GCP API**: `GET v2/{+metricName}`
///
/// # Path Parameters
/// - `metricName` — Required. The resource name of the desired metric: "projects/[PROJECT_ID]/metrics/[METRIC_ID]" *(required)*
///
/// # Response
/// [`LogMetric`]
#[allow(dead_code)]
pub(crate) async fn get_metric(&self, metric_name: &str) -> Result<LogMetric> {
let url = format!("{}/v2/{}", self.base_url(), metric_name,);
let response = self.client.get(&url).await?;
serde_json::from_slice(&response).map_err(|e| crate::GcpError::InvalidResponse {
message: format!("Failed to parse get_metric response: {e}"),
body: Some(String::from_utf8_lossy(&response).to_string()),
})
}
/// Creates a logs-based metric.
///
/// **GCP API**: `POST v2/{+parent}/metrics`
///
/// # Path Parameters
/// - `parent` — Required. The resource name of the project in which to create the metric: "projects/[PROJECT_ID]" The new metric must be *(required)*
///
/// # Request Body
/// [`LogMetric`]
///
/// # Response
/// [`LogMetric`]
#[allow(dead_code)]
pub(crate) async fn create_metric(&self, parent: &str, body: &LogMetric) -> Result<LogMetric> {
let url = format!("{}/v2/{}/metrics", self.base_url(), parent,);
let response = self.client.post(&url, body).await?;
serde_json::from_slice(&response).map_err(|e| crate::GcpError::InvalidResponse {
message: format!("Failed to parse create_metric response: {e}"),
body: Some(String::from_utf8_lossy(&response).to_string()),
})
}
/// Deletes a logs-based metric.
///
/// **GCP API**: `DELETE v2/{+metricName}`
///
/// # Path Parameters
/// - `metricName` — Required. The resource name of the metric to delete: "projects/[PROJECT_ID]/metrics/[METRIC_ID]" *(required)*
///
/// # Response
/// [`LoggingEmpty`]
#[allow(dead_code)]
pub(crate) async fn delete_metric(&self, metric_name: &str) -> Result<LoggingEmpty> {
let url = format!("{}/v2/{}", self.base_url(), metric_name,);
let response = self.client.delete(&url).await?;
serde_json::from_slice(&response).map_err(|e| crate::GcpError::InvalidResponse {
message: format!("Failed to parse delete_metric response: {e}"),
body: Some(String::from_utf8_lossy(&response).to_string()),
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_list_sinks() {
let mut mock = crate::MockClient::new();
mock.expect_get("/v2/test-parent/sinks?pageSize=test-pageSize&pageToken=test-pageToken&filter=test-filter")
.returning_json(serde_json::to_value(ListSinksResponse::fixture()).unwrap());
let client = crate::GcpHttpClient::from_mock(mock);
let ops = LoggingOps::new(&client);
let result = ops
.list_sinks(
"test-parent",
"test-pageSize",
"test-pageToken",
"test-filter",
)
.await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_get_sink() {
let mut mock = crate::MockClient::new();
mock.expect_get("/v2/test-sinkName")
.returning_json(serde_json::to_value(LogSink::fixture()).unwrap());
let client = crate::GcpHttpClient::from_mock(mock);
let ops = LoggingOps::new(&client);
let result = ops.get_sink("test-sinkName").await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_create_sink() {
let mut mock = crate::MockClient::new();
mock.expect_post("/v2/test-parent/sinks")
.returning_json(serde_json::to_value(LogSink::fixture()).unwrap());
let client = crate::GcpHttpClient::from_mock(mock);
let ops = LoggingOps::new(&client);
let body = LogSink::fixture();
let result = ops.create_sink("test-parent", &body).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_delete_sink() {
let mut mock = crate::MockClient::new();
mock.expect_delete("/v2/test-sinkName")
.returning_json(serde_json::to_value(LoggingEmpty::fixture()).unwrap());
let client = crate::GcpHttpClient::from_mock(mock);
let ops = LoggingOps::new(&client);
let result = ops.delete_sink("test-sinkName").await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_list_metrics() {
let mut mock = crate::MockClient::new();
mock.expect_get("/v2/test-parent/metrics?pageSize=test-pageSize&pageToken=test-pageToken")
.returning_json(serde_json::to_value(ListLogMetricsResponse::fixture()).unwrap());
let client = crate::GcpHttpClient::from_mock(mock);
let ops = LoggingOps::new(&client);
let result = ops
.list_metrics("test-parent", "test-pageSize", "test-pageToken")
.await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_get_metric() {
let mut mock = crate::MockClient::new();
mock.expect_get("/v2/test-metricName")
.returning_json(serde_json::to_value(LogMetric::fixture()).unwrap());
let client = crate::GcpHttpClient::from_mock(mock);
let ops = LoggingOps::new(&client);
let result = ops.get_metric("test-metricName").await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_create_metric() {
let mut mock = crate::MockClient::new();
mock.expect_post("/v2/test-parent/metrics")
.returning_json(serde_json::to_value(LogMetric::fixture()).unwrap());
let client = crate::GcpHttpClient::from_mock(mock);
let ops = LoggingOps::new(&client);
let body = LogMetric::fixture();
let result = ops.create_metric("test-parent", &body).await;
assert!(result.is_ok());
}
#[tokio::test]
async fn test_delete_metric() {
let mut mock = crate::MockClient::new();
mock.expect_delete("/v2/test-metricName")
.returning_json(serde_json::to_value(LoggingEmpty::fixture()).unwrap());
let client = crate::GcpHttpClient::from_mock(mock);
let ops = LoggingOps::new(&client);
let result = ops.delete_metric("test-metricName").await;
assert!(result.is_ok());
}
}