Skip to content

Commit 5ff9ba9

Browse files
committed
cover the search query get v1alpha api resource
1 parent 33e45fc commit 5ff9ba9

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed

sdk/commands/search.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
from search.v1alpha import asset_events_find
2525
from search.v1alpha import raw_logs_find
26+
from search.v1alpha import search_query_get
2627
from search.v1alpha import udm_events_find
2728

2829
SCOPES = [
@@ -171,3 +172,31 @@ def find_udm_events_cmd(ctx, tokens, event_ids, return_unenriched_data, return_a
171172
return_unenriched_data,
172173
return_all_events_for_log,
173174
)
175+
176+
177+
@search.command("get-search-query")
178+
@click.option(
179+
"--user-id",
180+
required=True,
181+
help="ID of the user who owns the search query.",
182+
)
183+
@click.option(
184+
"--query-id",
185+
required=True,
186+
help="ID of the search query to retrieve.",
187+
)
188+
@click.pass_context
189+
def get_search_query_cmd(ctx, user_id, query_id):
190+
"""Get a search query by ID."""
191+
auth_session = chronicle_auth.initialize_http_session(
192+
ctx.obj["credentials_file"],
193+
SCOPES,
194+
)
195+
search_query_get.get_search_query(
196+
auth_session,
197+
ctx.obj["project_id"],
198+
ctx.obj["project_instance"],
199+
ctx.obj["region"],
200+
user_id,
201+
query_id,
202+
)

search/v1alpha/search_query_get.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#!/usr/bin/env python3
2+
3+
# Copyright 2025 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
# pylint: disable=line-too-long
18+
r"""Executable and reusable v1alpha API sample for getting a search query in Chronicle.
19+
20+
API reference:
21+
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.users.searchQueries/get
22+
"""
23+
# pylint: enable=line-too-long
24+
25+
import argparse
26+
27+
from common import chronicle_auth
28+
from common import project_id
29+
from common import project_instance
30+
from common import regions
31+
from google.auth.transport import requests
32+
33+
CHRONICLE_API_BASE_URL = "https://chronicle.googleapis.com"
34+
SCOPES = [
35+
"https://www.googleapis.com/auth/cloud-platform",
36+
]
37+
38+
39+
def get_search_query(http_session: requests.AuthorizedSession,
40+
proj_id: str,
41+
proj_instance: str,
42+
proj_region: str,
43+
user_id: str,
44+
query_id: str) -> None:
45+
"""Get a search query by ID from Chronicle.
46+
47+
Args:
48+
http_session: Authorized session for HTTP requests.
49+
proj_id: GCP project id or number to which the target instance belongs.
50+
proj_instance: Customer ID (uuid with dashes) for the instance.
51+
proj_region: region in which the target project is located.
52+
user_id: ID of the user who owns the search query.
53+
query_id: ID of the search query to retrieve.
54+
55+
Raises:
56+
requests.exceptions.HTTPError: HTTP request resulted in an error
57+
(response.status_code >= 400).
58+
59+
Requires the following IAM permission on the instance resource:
60+
chronicle.searchQueries.get
61+
"""
62+
base_url_with_region = regions.url_always_prepend_region(
63+
CHRONICLE_API_BASE_URL, proj_region)
64+
instance = f"projects/{proj_id}/locations/{proj_region}/instances/{proj_instance}"
65+
url = f"{base_url_with_region}/v1alpha/{instance}/users/{user_id}/searchQueries/{query_id}"
66+
67+
response = http_session.request("GET", url)
68+
if response.status_code >= 400:
69+
print(response.text)
70+
response.raise_for_status()
71+
print(response.text)
72+
73+
74+
if __name__ == "__main__":
75+
parser = argparse.ArgumentParser()
76+
# common
77+
chronicle_auth.add_argument_credentials_file(parser)
78+
project_instance.add_argument_project_instance(parser)
79+
project_id.add_argument_project_id(parser)
80+
regions.add_argument_region(parser)
81+
# local
82+
parser.add_argument(
83+
"--user_id",
84+
type=str,
85+
required=True,
86+
help="ID of the user who owns the search query"
87+
)
88+
parser.add_argument(
89+
"--query_id",
90+
type=str,
91+
required=True,
92+
help="ID of the search query to retrieve"
93+
)
94+
95+
args = parser.parse_args()
96+
97+
auth_session = chronicle_auth.initialize_http_session(
98+
args.credentials_file,
99+
SCOPES,
100+
)
101+
get_search_query(auth_session, args.project_id, args.project_instance,
102+
args.region, args.user_id, args.query_id)

0 commit comments

Comments
 (0)