|
39 | 39 | 'EXCEPTION_LIST_GENERIC': 'Exception searching for {}: {}',
|
40 | 40 |
|
41 | 41 | '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.', |
42 | 45 | }
|
43 | 46 |
|
44 | 47 | SCOPES: dict[str, list[str]] = {
|
|
98 | 101 | 'FILE_PERMISSIONS_CRUD': [
|
99 | 102 | 'https://www.googleapis.com/auth/drive',
|
100 | 103 | 'https://www.googleapis.com/auth/drive.file',
|
101 |
| - ] |
| 104 | + ], |
102 | 105 |
|
| 106 | + 'MODIFY_LABELS_PERMISSIONS_CRUD': [ |
| 107 | + 'https://www.googleapis.com/auth/drive', |
| 108 | + 'https://www.googleapis.com/auth/drive.labels', |
| 109 | + ] |
103 | 110 | }
|
104 | 111 |
|
105 | 112 | 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' |
107 | 115 | }
|
108 | 116 | URL_SUFFIX: dict[str, str] = {
|
109 | 117 | 'DRIVE_CHANGES': 'drive/v3/changes',
|
|
120 | 128 | 'FILE_PERMISSION_CREATE': 'drive/v3/files/{}/permissions',
|
121 | 129 | 'FILE_PERMISSION_UPDATE': 'drive/v3/files/{}/permissions/{}',
|
122 | 130 | 'FILE_PERMISSION_DELETE': 'drive/v3/files/{}/permissions/{}',
|
| 131 | + 'FILE_MODIFY_LABEL': 'drive/v3/files/{}/modifyLabels', |
| 132 | + 'FILE_GET_LABELS': 'drive/v3/files/{}/listLabels' |
123 | 133 | }
|
124 | 134 |
|
125 | 135 | OUTPUT_PREFIX: dict[str, str] = {
|
|
143 | 153 | 'GOOGLE_DRIVE_FILE_PERMISSION_HEADER': 'GoogleDrive.FilePermission',
|
144 | 154 | 'FILE_PERMISSION': 'FilePermission',
|
145 | 155 |
|
| 156 | + 'LABELS': 'GoogleDrive.Labels' |
| 157 | + |
146 | 158 | }
|
147 | 159 |
|
148 | 160 | 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])
|
1196 | 1208 | return handle_response_file_single(file, args)
|
1197 | 1209 |
|
1198 | 1210 |
|
| 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 | + |
1199 | 1307 | @logger
|
1200 | 1308 | def file_delete_command(client: 'GSuiteClient', args: dict[str, str]) -> CommandResults:
|
1201 | 1309 | """
|
@@ -1319,6 +1427,37 @@ def prepare_file_permission_request(client: 'GSuiteClient', args: dict[str, str]
|
1319 | 1427 | }
|
1320 | 1428 |
|
1321 | 1429 |
|
| 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 | + |
1322 | 1461 | @logger
|
1323 | 1462 | def file_permission_list_command(client: 'GSuiteClient', args: dict[str, str]) -> CommandResults:
|
1324 | 1463 | """
|
@@ -1675,6 +1814,9 @@ def main() -> None:
|
1675 | 1814 | 'google-drive-file-permission-create': file_permission_create_command,
|
1676 | 1815 | 'google-drive-file-permission-update': file_permission_update_command,
|
1677 | 1816 | '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, |
1678 | 1820 | }
|
1679 | 1821 | command = demisto.command()
|
1680 | 1822 |
|
@@ -1709,7 +1851,7 @@ def main() -> None:
|
1709 | 1851 | # This is the call made when pressing the integration Test button.
|
1710 | 1852 | if demisto.command() == 'test-module':
|
1711 | 1853 | result = test_module(gsuite_client, demisto.getLastRun(), params)
|
1712 |
| - demisto.results(result) |
| 1854 | + return_results(result) |
1713 | 1855 | elif demisto.command() == 'fetch-incidents':
|
1714 | 1856 |
|
1715 | 1857 | incidents, next_run = fetch_incidents(gsuite_client,
|
|
0 commit comments