-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add v1alpha samples for Get, Patch (Add, Delete, Replace) Reference L…
…ist. PiperOrigin-RevId: 606380691
- Loading branch information
1 parent
0fbad34
commit 78adea3
Showing
6 changed files
with
599 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
0f6b9d2ada67cebc8c0f03786c442c61c05cef5b92641ec4c1bdd8f5baeb2ee1 | ||
A949ec428116489f5e77cefc67fea475017e0f50d2289e17c3eb053072adcf24 | ||
C97acea1a6ef59d58a498f1e1f0e0648d6979c4325de3ee726038df1fc2e831d | ||
Ac270310b5410e7430fe7e36a079525cd8724b002b38e13a6ee6e09b326f4847 | ||
84523ddad722e205e2d52eedfb682026928b63f919a7bf1ce6f1ad4180d0f507 | ||
37c52481711631a5c73a6341bd8bea302ad57f02199db7624b580058547fb5a9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
foo | ||
bar | ||
baz | ||
foo | ||
bar | ||
foo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# Copyright 2024 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. | ||
# | ||
r"""Executable and reusable sample for getting a Reference List. | ||
Usage: | ||
python -m lists.v1alpha.get_list \ | ||
--project_id=<PROJECT_ID> \ | ||
--project_instance=<PROJECT_INSTANCE> \ | ||
--name="COLDRIVER_SHA256" | ||
API reference: | ||
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.referenceLists/get | ||
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.referenceLists#ReferenceList | ||
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.referenceLists#ReferenceListEntry | ||
""" | ||
|
||
import argparse | ||
import json | ||
from typing import Dict | ||
|
||
from common import chronicle_auth | ||
from common import project_id | ||
from common import project_instance | ||
from common import regions | ||
|
||
from google.auth.transport import requests | ||
|
||
CHRONICLE_API_BASE_URL = "https://chronicle.googleapis.com" | ||
SCOPES = [ | ||
"https://www.googleapis.com/auth/cloud-platform", | ||
] | ||
|
||
|
||
def get_list( | ||
http_session: requests.AuthorizedSession, | ||
proj_id: str, | ||
proj_instance: str, | ||
proj_region: str, | ||
name: str, | ||
) -> Dict[str, any]: | ||
"""Gets a Reference List. | ||
Args: | ||
http_session: Authorized session for HTTP requests. | ||
proj_id: GCP project id or number to which the target instance belongs. | ||
proj_instance: Customer ID (uuid with dashes) for the Chronicle instance. | ||
proj_region: region in which the target project is located. | ||
name: name that identifies the list to get. | ||
Returns: | ||
Dictionary representation of the Reference List | ||
Raises: | ||
requests.exceptions.HTTPError: HTTP request resulted in an error | ||
(response.status_code >= 400). | ||
""" | ||
base_url_with_region = regions.url_always_prepend_region( | ||
CHRONICLE_API_BASE_URL, | ||
proj_region | ||
) | ||
# pylint: disable-next=line-too-long | ||
parent = f"projects/{proj_id}/locations/{proj_region}/instances/{proj_instance}" | ||
url = f"{base_url_with_region}/v1alpha/{parent}/referenceLists/{name}" | ||
|
||
response = http_session.request("GET", url) | ||
# Expected server response is described in: | ||
# https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.referenceLists#ReferenceList | ||
if response.status_code >= 400: | ||
print(response.text) | ||
response.raise_for_status() | ||
return response.json() | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
chronicle_auth.add_argument_credentials_file(parser) | ||
project_instance.add_argument_project_instance(parser) | ||
project_id.add_argument_project_id(parser) | ||
regions.add_argument_region(parser) | ||
parser.add_argument( | ||
"-n", "--name", type=str, required=True, | ||
help="name that identifies the list to get" | ||
) | ||
args = parser.parse_args() | ||
|
||
auth_session = chronicle_auth.initialize_http_session( | ||
args.credentials_file, | ||
SCOPES, | ||
) | ||
a_list = get_list( | ||
auth_session, | ||
args.project_id, | ||
args.project_instance, | ||
args.region, | ||
args.name, | ||
) | ||
print(json.dumps(a_list, indent=2)) |
Oops, something went wrong.