Skip to content

Commit 96fdbba

Browse files
committed
revert changes to create_list.py
1 parent daea913 commit 96fdbba

File tree

1 file changed

+59
-54
lines changed

1 file changed

+59
-54
lines changed

lists/v1alpha/create_list.py

Lines changed: 59 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python3
22

3-
# Copyright 2025 Google LLC
3+
# Copyright 2024 Google LLC
44
#
55
# Licensed under the Apache License, Version 2.0 (the "License");
66
# you may not use this file except in compliance with the License.
@@ -15,37 +15,29 @@
1515
# limitations under the License.
1616
#
1717
# pylint: disable=line-too-long
18-
r"""Executable and reusable v1alpha API sample for creating a Reference List.
19-
20-
Usage:
21-
python -m lists.v1alpha.create_list \
22-
--project_id=<PROJECT_ID> \
23-
--project_instance=<PROJECT_INSTANCE> \
24-
--region=<REGION> \
25-
--name=<LIST_NAME> \
26-
--description=<LIST_DESCRIPTION> \
27-
--list_file=<PATH_TO_LIST_FILE>
28-
29-
API reference:
30-
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.referenceLists/create
18+
"""Executable and reusable sample for creating a Reference List.
19+
20+
Requires the following IAM permission on the parent resource:
21+
chronicle.referenceLists.create
22+
23+
https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.referenceLists/create
3124
"""
3225
# pylint: enable=line-too-long
3326

3427
import argparse
35-
import json
3628
from typing import Any, Dict, Optional, Sequence
3729

30+
from google.auth.transport import requests
31+
3832
from common import chronicle_auth
3933
from common import project_id
4034
from common import project_instance
4135
from common import regions
42-
from google.auth.transport import requests
4336

4437
CHRONICLE_API_BASE_URL = "https://chronicle.googleapis.com"
4538
SCOPES = [
4639
"https://www.googleapis.com/auth/cloud-platform",
4740
]
48-
4941
PREFIX = "REFERENCE_LIST_SYNTAX_TYPE_"
5042
SYNTAX_TYPE_ENUM = [
5143
f"{PREFIX}UNSPECIFIED", # Defaults to ..._PLAIN_TEXT_STRING.
@@ -66,7 +58,7 @@ def create_list(
6658
content_type: str,
6759
scope_name: Optional[str] | None = None,
6860
) -> Dict[str, Any]:
69-
"""Creates a reference list using the Create Reference List API.
61+
"""Creates a list.
7062
7163
Args:
7264
http_session: Authorized session for HTTP requests.
@@ -78,33 +70,35 @@ def create_list(
7870
content_lines: Array containing each line of the list's content.
7971
content_type: Type of list content, indicating how to interpret this list.
8072
scope_name: (Optional) Data RBAC scope name for the list.
81-
8273
Returns:
8374
Dictionary representation of the created Reference List.
8475
8576
Raises:
8677
requests.exceptions.HTTPError: HTTP request resulted in an error
8778
(response.status_code >= 400).
88-
89-
Requires the following IAM permission on the parent resource:
90-
chronicle.referenceLists.create
9179
"""
80+
# pylint: disable=line-too-long
9281
base_url_with_region = regions.url_always_prepend_region(
93-
CHRONICLE_API_BASE_URL, proj_region)
94-
# pylint: disable-next=line-too-long
82+
CHRONICLE_API_BASE_URL,
83+
proj_region
84+
)
9585
parent = f"projects/{proj_id}/locations/{proj_region}/instances/{proj_instance}"
9686
url = f"{base_url_with_region}/v1alpha/{parent}/referenceLists"
87+
# pylint: enable=line-too-long
9788

98-
# Create entries in format [{"value": <string>}, ...]
99-
entries = [{"value": line.strip()} for line in content_lines]
89+
# entries are list like [{"value": <string>}, ...]
90+
# pylint: disable-next=line-too-long
91+
# https://cloud.google.com/chronicle/docs/reference/rest/v1alpha/projects.locations.instances.referenceLists#resource:-referencelist
92+
entries = []
93+
for content_line in content_lines:
94+
entries.append({"value": content_line.strip()})
10095

10196
body = {
10297
"name": name,
10398
"description": description,
10499
"entries": entries,
105100
"syntax_type": content_type,
106101
}
107-
108102
if scope_name:
109103
body["scope_info"] = {
110104
"referenceListScope": {
@@ -115,52 +109,62 @@ def create_list(
115109
}
116110
else:
117111
body["scope_info"] = None
118-
119112
params = {"referenceListId": name}
120113
response = http_session.request("POST", url, params=params, json=body)
114+
# Expected server response:
115+
# ['name', 'displayName', 'revisionCreateTime', 'description',
116+
# 'entries', 'syntaxType'])
121117
if response.status_code >= 400:
122118
print(response.text)
123119
response.raise_for_status()
124-
125120
return response.json()
126121

127122

128123
if __name__ == "__main__":
129124
parser = argparse.ArgumentParser()
130-
# common
131125
chronicle_auth.add_argument_credentials_file(parser)
132126
project_instance.add_argument_project_instance(parser)
133127
project_id.add_argument_project_id(parser)
134128
regions.add_argument_region(parser)
135-
# local
136-
parser.add_argument("--name",
137-
type=str,
138-
required=True,
139-
help="Unique name for the list")
140-
parser.add_argument("--description",
141-
type=str,
142-
required=True,
143-
help="Description of the list")
144-
parser.add_argument("--scope_name",
145-
type=str,
146-
help="Data RBAC scope name for the list")
147-
parser.add_argument("--syntax_type",
148-
type=str,
149-
required=False,
150-
default="REFERENCE_LIST_SYNTAX_TYPE_PLAIN_TEXT_STRING",
151-
choices=SYNTAX_TYPE_ENUM,
152-
help="Syntax type of the list, used for validation")
153129
parser.add_argument(
130+
"-n", "--name", type=str, required=True, help="unique name for the list"
131+
)
132+
parser.add_argument(
133+
"-d",
134+
"--description",
135+
type=str,
136+
required=True,
137+
help="description of the list",
138+
)
139+
parser.add_argument(
140+
"-s", "--scope_name", type=str, help="data RBAC scope name for the list"
141+
)
142+
parser.add_argument(
143+
"-t",
144+
"--syntax_type",
145+
type=str,
146+
required=False,
147+
default="REFERENCE_LIST_SYNTAX_TYPE_PLAIN_TEXT_STRING",
148+
choices=SYNTAX_TYPE_ENUM,
149+
# pylint: disable-next=line-too-long
150+
help="syntax type of the list, used for validation (default: REFERENCE_LIST_SYNTAX_TYPE_PLAIN_TEXT_STRING)",
151+
)
152+
parser.add_argument(
153+
"-f",
154154
"--list_file",
155155
type=argparse.FileType("r"),
156156
required=True,
157-
help="Path to file containing list content, or - for STDIN")
158-
157+
# File example:
158+
# python3 -m lists.v1alpha.create_list <other args> -f <path>
159+
# STDIN example:
160+
# cat <path> | python3 -m lists.v1alpha.create_list <other args> -f -
161+
help="path of a file containing the list content, or - for STDIN",
162+
)
159163
args = parser.parse_args()
160164

161-
auth_session = chronicle_auth.initialize_http_session(args.credentials_file,
162-
SCOPES)
163-
result = create_list(
165+
# pylint: disable-next=line-too-long
166+
auth_session = chronicle_auth.initialize_http_session(args.credentials_file, SCOPES)
167+
response_json = create_list(
164168
auth_session,
165169
args.project_id,
166170
args.project_instance,
@@ -171,4 +175,5 @@ def create_list(
171175
args.syntax_type,
172176
args.scope_name,
173177
)
174-
print(json.dumps(result, indent=2))
178+
print("New list created successfully, at "
179+
f"{response_json.get('revisionCreateTime')}")

0 commit comments

Comments
 (0)