Skip to content

Commit 22e90e7

Browse files
packs/GoogleDrive: add modify labels command (#28962)
* packs/GoogleDrive: add modify labels command Add a command to modify labels in google drive Signed-off-by: Gal Nakash <[email protected]> * packs/GoogleDrive: add modify labels command readme Generate docs based on yaml Signed-off-by: Gal Nakash <[email protected]> * packs/GoogleDrive: add get labels command Get labels command to get all labels in google drive Signed-off-by: Gal Nakash <[email protected]> * packs/GoogleDrive: get-file-labels command Add command to get labels attached to file Signed-off-by: Gal Nakash <[email protected]> * packs/GoogleDrive: update version 1.2.40 Signed-off-by: Gal Nakash <[email protected]> * packs/GoogleDrive: update release notes Signed-off-by: Gal Nakash <[email protected]> * Update Packs/GoogleDrive/Integrations/GoogleDrive/GoogleDrive.py Co-authored-by: ostolero <[email protected]> * packs/GoogleDrive: fix return value Return result instead of return Signed-off-by: Gal Nakash <[email protected]> * packs/GoogleDrive: add a test for list labels Signed-off-by: Gal Nakash <[email protected]> * packs/GoogleDrive: add a test for modify label Signed-off-by: Gal Nakash <[email protected]> * packs/GoogleDrive: rename test files Signed-off-by: Gal Nakash <[email protected]> * packs/GoogleDrive: added context path for GoogleDrive modify lables Signed-off-by: Gal Nakash <[email protected]> * packs/GoogleDrive: added context path for GoogleDrive get file labels Signed-off-by: Gal Nakash <[email protected]> * packs/GoogleDrive: added context path for GoogleDrive get labels Signed-off-by: Gal Nakash <[email protected]> * packs/GoogleDrive: format file and fix test Signed-off-by: Gal Nakash <[email protected]> * update readme * update docker * packs/reco: fix test Signed-off-by: Gal Nakash <[email protected]> --------- Signed-off-by: Gal Nakash <[email protected]> Co-authored-by: ostolero <[email protected]> Co-authored-by: ostolero <[email protected]>
1 parent 61d8de7 commit 22e90e7

File tree

8 files changed

+1022
-23
lines changed

8 files changed

+1022
-23
lines changed

Packs/GoogleDrive/Integrations/GoogleDrive/GoogleDrive.py

+145-3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
'EXCEPTION_LIST_GENERIC': 'Exception searching for {}: {}',
4040

4141
'EXCEPTION_GENERIC': 'Exception handling a {} request: {}',
42+
'MODIFY_LABEL_SUCCESS': 'Modify label successfully assigned to {}.',
43+
'GET_LABEL_SUCCESS': 'Label successfully retrieved.',
44+
'GET_LABELS_SUCCESS': 'Labels successfully retrieved.',
4245
}
4346

4447
SCOPES: dict[str, list[str]] = {
@@ -98,12 +101,17 @@
98101
'FILE_PERMISSIONS_CRUD': [
99102
'https://www.googleapis.com/auth/drive',
100103
'https://www.googleapis.com/auth/drive.file',
101-
]
104+
],
102105

106+
'MODIFY_LABELS_PERMISSIONS_CRUD': [
107+
'https://www.googleapis.com/auth/drive',
108+
'https://www.googleapis.com/auth/drive.labels',
109+
]
103110
}
104111

105112
URLS: dict[str, str] = {
106-
'DRIVE_ACTIVITY': 'https://driveactivity.googleapis.com/v2/activity:query'
113+
'DRIVE_ACTIVITY': 'https://driveactivity.googleapis.com/v2/activity:query',
114+
'DRIVE_LABELS': 'https://drivelabels.googleapis.com/v2/labels'
107115
}
108116
URL_SUFFIX: dict[str, str] = {
109117
'DRIVE_CHANGES': 'drive/v3/changes',
@@ -120,6 +128,8 @@
120128
'FILE_PERMISSION_CREATE': 'drive/v3/files/{}/permissions',
121129
'FILE_PERMISSION_UPDATE': 'drive/v3/files/{}/permissions/{}',
122130
'FILE_PERMISSION_DELETE': 'drive/v3/files/{}/permissions/{}',
131+
'FILE_MODIFY_LABEL': 'drive/v3/files/{}/modifyLabels',
132+
'FILE_GET_LABELS': 'drive/v3/files/{}/listLabels'
123133
}
124134

125135
OUTPUT_PREFIX: dict[str, str] = {
@@ -143,6 +153,8 @@
143153
'GOOGLE_DRIVE_FILE_PERMISSION_HEADER': 'GoogleDrive.FilePermission',
144154
'FILE_PERMISSION': 'FilePermission',
145155

156+
'LABELS': 'GoogleDrive.Labels'
157+
146158
}
147159

148160
DATE_FORMAT: str = '%Y-%m-%d' # sample - 2020-08-23
@@ -1196,6 +1208,102 @@ def file_replace_existing_command(client: 'GSuiteClient', args: dict[str, str])
11961208
return handle_response_file_single(file, args)
11971209

11981210

1211+
@logger
1212+
def modify_label_command(client: 'GSuiteClient', args: dict[str, str]) -> CommandResults:
1213+
modify_label_request_res = prepare_file_modify_labels_request(
1214+
client, args, scopes=COMMAND_SCOPES['MODIFY_LABELS_PERMISSIONS_CRUD'])
1215+
http_request_params = modify_label_request_res['http_request_params']
1216+
1217+
url_suffix = URL_SUFFIX['FILE_MODIFY_LABEL'].format(args.get('file_id'))
1218+
body_request = {
1219+
"kind": "drive#modifyLabelsRequest",
1220+
"labelModifications": [
1221+
{
1222+
"fieldModifications": [
1223+
{
1224+
"kind": "drive#labelFieldModification",
1225+
"fieldId": args.get('field_id'),
1226+
"setSelectionValues": [
1227+
args.get('selection_label_id')
1228+
]
1229+
}
1230+
],
1231+
"kind": "drive#labelModification",
1232+
"labelId": args.get('label_id'),
1233+
"removeLabel": args.get('remove_label', False)
1234+
}
1235+
]
1236+
}
1237+
1238+
response = client.http_request(url_suffix=url_suffix, method='POST', params=http_request_params, body=body_request)
1239+
1240+
table_hr_md = tableToMarkdown(HR_MESSAGES['MODIFY_LABEL_SUCCESS'].format(args.get('file_id')),
1241+
response,
1242+
headerTransform=pascalToSpace,
1243+
removeNull=False)
1244+
outputs_context = {
1245+
OUTPUT_PREFIX['LABELS']: response
1246+
}
1247+
1248+
return CommandResults(
1249+
outputs=outputs_context,
1250+
raw_response=response,
1251+
readable_output=table_hr_md,
1252+
)
1253+
1254+
1255+
def get_file_labels_command(client: 'GSuiteClient', args: dict[str, str]) -> CommandResults:
1256+
modify_label_request_res = prepare_file_modify_labels_request(
1257+
client, args, scopes=COMMAND_SCOPES['MODIFY_LABELS_PERMISSIONS_CRUD'])
1258+
http_request_params = modify_label_request_res['http_request_params']
1259+
1260+
url_suffix = URL_SUFFIX['FILE_GET_LABELS'].format(args.get('file_id'))
1261+
1262+
response = client.http_request(url_suffix=url_suffix, method='GET', params=http_request_params)
1263+
1264+
outputs_context = {
1265+
OUTPUT_PREFIX['LABELS']: response,
1266+
OUTPUT_PREFIX['GOOGLE_DRIVE_FILE_HEADER']: {
1267+
OUTPUT_PREFIX['FILE']: {
1268+
'id': args.get('file_id'),
1269+
},
1270+
}
1271+
}
1272+
1273+
table_hr_md = tableToMarkdown(HR_MESSAGES['GET_LABEL_SUCCESS'].format(args.get('file_id')),
1274+
response['labels'],
1275+
headerTransform=pascalToSpace,
1276+
removeNull=False)
1277+
1278+
return CommandResults(
1279+
outputs=outputs_context,
1280+
readable_output=table_hr_md,
1281+
)
1282+
1283+
1284+
def get_labels_command(client: 'GSuiteClient', args: dict[str, str]) -> CommandResults:
1285+
modify_label_request_res = prepare_get_labels_request(
1286+
client, args, scopes=COMMAND_SCOPES['MODIFY_LABELS_PERMISSIONS_CRUD'])
1287+
http_request_params = modify_label_request_res['http_request_params']
1288+
1289+
full_url = URLS['DRIVE_LABELS'] + '?' + urllib.parse.urlencode(http_request_params)
1290+
demisto.info(f'full url for get labels is: {full_url}')
1291+
response = client.http_request(full_url=full_url, method='GET')
1292+
1293+
outputs_context = {
1294+
OUTPUT_PREFIX['LABELS']: response
1295+
}
1296+
1297+
table_hr_md = tableToMarkdown(HR_MESSAGES['GET_LABELS_SUCCESS'],
1298+
response['labels'],
1299+
headerTransform=pascalToSpace,
1300+
removeNull=False)
1301+
return CommandResults(
1302+
readable_output=table_hr_md,
1303+
outputs=outputs_context
1304+
)
1305+
1306+
11991307
@logger
12001308
def file_delete_command(client: 'GSuiteClient', args: dict[str, str]) -> CommandResults:
12011309
"""
@@ -1319,6 +1427,37 @@ def prepare_file_permission_request(client: 'GSuiteClient', args: dict[str, str]
13191427
}
13201428

13211429

1430+
def prepare_file_modify_labels_request(client: 'GSuiteClient', args: dict[str, str], scopes: list[str]) -> dict[str, Any]:
1431+
# user_id can be overridden in the args
1432+
user_id = args.get('user_id') or client.user_id
1433+
client.set_authorized_http(scopes=scopes, subject=user_id)
1434+
# Prepare generic HTTP request params
1435+
http_request_params: dict[str, str] = assign_params(
1436+
fileId=args.get('file_id')
1437+
)
1438+
1439+
return {
1440+
'client': client,
1441+
'http_request_params': http_request_params,
1442+
'user_id': user_id,
1443+
}
1444+
1445+
1446+
def prepare_get_labels_request(client: 'GSuiteClient', args: dict[str, str], scopes: list[str]) -> dict[str, Any]:
1447+
# user_id can be overridden in the args
1448+
user_id = args.get('user_id') or client.user_id
1449+
client.set_authorized_http(scopes=scopes, subject=user_id)
1450+
http_request_params: dict[str, str] = assign_params(
1451+
view='LABEL_VIEW_FULL'
1452+
)
1453+
1454+
return {
1455+
'client': client,
1456+
'http_request_params': http_request_params,
1457+
'user_id': user_id,
1458+
}
1459+
1460+
13221461
@logger
13231462
def file_permission_list_command(client: 'GSuiteClient', args: dict[str, str]) -> CommandResults:
13241463
"""
@@ -1675,6 +1814,9 @@ def main() -> None:
16751814
'google-drive-file-permission-create': file_permission_create_command,
16761815
'google-drive-file-permission-update': file_permission_update_command,
16771816
'google-drive-file-permission-delete': file_permission_delete_command,
1817+
'google-drive-file-modify-label': modify_label_command,
1818+
'google-drive-get-labels': get_labels_command,
1819+
'google-drive-get-file-labels': get_file_labels_command,
16781820
}
16791821
command = demisto.command()
16801822

@@ -1709,7 +1851,7 @@ def main() -> None:
17091851
# This is the call made when pressing the integration Test button.
17101852
if demisto.command() == 'test-module':
17111853
result = test_module(gsuite_client, demisto.getLastRun(), params)
1712-
demisto.results(result)
1854+
return_results(result)
17131855
elif demisto.command() == 'fetch-incidents':
17141856

17151857
incidents, next_run = fetch_incidents(gsuite_client,

0 commit comments

Comments
 (0)