Skip to content

Commit

Permalink
GoogleDrive: Get Parents API (#31975)
Browse files Browse the repository at this point in the history
* GoogleDrive: Get Parents API (#31945)

Implement get parents api to get the tree of folders in google drive for a file

Signed-off-by: Gal Nakash <gal@recolabs.ai>

* fixes

* fixes

* Empty-Commit

---------

Signed-off-by: Gal Nakash <gal@recolabs.ai>
Co-authored-by: GalNakash-RecoLabs <71227802+GalNakash-RecoLabs@users.noreply.github.com>
Co-authored-by: YuvHayun <yhayun@paloaltonetworks.com>
  • Loading branch information
3 people authored and maimorag committed Jan 7, 2024
1 parent 5db7c53 commit db45990
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 3 deletions.
47 changes: 45 additions & 2 deletions Packs/GoogleDrive/Integrations/GoogleDrive/GoogleDrive.py
Expand Up @@ -134,7 +134,8 @@
'FILE_PERMISSION_UPDATE': 'drive/v3/files/{}/permissions/{}',
'FILE_PERMISSION_DELETE': 'drive/v3/files/{}/permissions/{}',
'FILE_MODIFY_LABEL': 'drive/v3/files/{}/modifyLabels',
'FILE_GET_LABELS': 'drive/v3/files/{}/listLabels'
'FILE_GET_LABELS': 'drive/v3/files/{}/listLabels',
'FILE_GET_PARENTS': 'drive/v2/files/{}/parents'
}

OUTPUT_PREFIX: dict[str, str] = {
Expand All @@ -158,7 +159,8 @@
'GOOGLE_DRIVE_FILE_PERMISSION_HEADER': 'GoogleDrive.FilePermission',
'FILE_PERMISSION': 'FilePermission',

'LABELS': 'GoogleDrive.Labels'
'LABELS': 'GoogleDrive.Labels',
'PARENTS': 'GoogleDrive.File.Parents'

}

Expand Down Expand Up @@ -1056,6 +1058,46 @@ def file_get_command(client: 'GSuiteClient', args: dict[str, str]) -> CommandRes
return handle_response_single_file(response, args)


@logger
def file_get_parents(client: 'GSuiteClient', args: dict[str, str]) -> CommandResults:
"""
google-drive-get-file-parents
Query a single file in Google Drive to retrieve its parents.
:param client: Client object.
:param args: Command arguments.
:return: Command Result.
"""
# Specific file
is_root = False
parents = []
file_id = args.get('file_id')
while not is_root:
modify_label_request_res = prepare_file_modify_labels_request(
client, args, scopes=COMMAND_SCOPES['FILES'])
http_request_params = modify_label_request_res['http_request_params']
http_request_params['fileId'] = file_id
url_suffix = URL_SUFFIX['FILE_GET_PARENTS'].format(file_id)
response = client.http_request(url_suffix=url_suffix, method='GET', params=http_request_params)

for parent in response.get('items', []):
is_root = parent.get('isRoot', False)
parents.append(parent.get('id', ''))
file_id = parent.get('id', '')
break

outputs: dict = {
OUTPUT_PREFIX['PARENTS']: parents
}

return CommandResults(
outputs=outputs,
readable_output=f"Parents of file {args.get('file_id')} are {parents}",
raw_response=response,
)


def handle_response_files_list(response: dict[str, Any]) -> CommandResults:
outputs_context = []
readable_output = ''
Expand Down Expand Up @@ -1897,6 +1939,7 @@ def main() -> None: # pragma: no cover
'google-drive-file-modify-label': modify_label_command,
'google-drive-get-labels': get_labels_command,
'google-drive-get-file-labels': get_file_labels_command,
'google-drive-file-get-parents': file_get_parents,
}
command = demisto.command()

Expand Down
27 changes: 27 additions & 0 deletions Packs/GoogleDrive/Integrations/GoogleDrive/GoogleDrive.yml
Expand Up @@ -3076,6 +3076,33 @@ script:
- contextPath: GoogleDrive.File.File.mimeType
description: The MIME type of the copied file.
type: String
- name: google-drive-file-get-parents
description: Get parents of a Google Drive file.
arguments:
- name: file_id
description: ID of the requested file. Can be retrieved using the `google-drive-files-list` command.
required: true
- name: user_id
description: The user's primary email address.
required: true
- auto: PREDEFINED
defaultValue: "false"
description: 'Whether both My Drive and shared drive items should be included in the results.'
name: include_items_from_all_drives
predefined:
- "true"
- "false"
- auto: PREDEFINED
description: Whether the requesting application supports both My Drives and shared drives.
name: supports_all_drives
defaultValue: "False"
predefined:
- "True"
- "False"
outputs:
- contextPath: GoogleDrive.File.Parents
description: The IDs of the parent folders which contain the file.
type: String
dockerimage: demisto/googleapi-python3:1.0.0.83805
isfetch: true
runonce: false
Expand Down
30 changes: 30 additions & 0 deletions Packs/GoogleDrive/Integrations/GoogleDrive/GoogleDrive_test.py
Expand Up @@ -1084,3 +1084,33 @@ def raise_error():
'user_id': 'test_user_id',
}
)

@patch(MOCKER_HTTP_METHOD)
def test_drive_get_file_parents_success(self, mocker_http_request, gsuite_client):
"""
Scenario: For file_get_parents command successful run.
Given:
- Command args.
When:
- Calling google-drive-get-file-parents command with the parameters provided.
Then:
- Ensure command's raw_response, outputs should be as expected.
"""
from GoogleDrive import file_get_parents

with open('test_data/get_parents_list.txt', encoding='utf-8') as data:
mock_response = json.load(data)
mocker_http_request.return_value = mock_response

args = {
'use_domain_admin_access': True,
'file_id': 'test',
'user_id': 'test'
}
result: CommandResults = file_get_parents(gsuite_client, args)

assert len(result.outputs.get('GoogleDrive.File.Parents', [])) == 1 # type: ignore
assert result.raw_response == mock_response
23 changes: 23 additions & 0 deletions Packs/GoogleDrive/Integrations/GoogleDrive/README.md
Expand Up @@ -1357,3 +1357,26 @@ Make a copy of a Google Drive file.
>|---|---|---|---|
>| 1JBZfuJcRpnpv5wS5-RBxT5OGjfKMP1cCmqOBHCe7GPw | drive#file | application/vnd.google-apps.spreadsheet | New Copy |
### google-drive-file-get-parents

***
Get parents of a Google Drive file.

#### Base Command

`google-drive-file-get-parents`

#### Input

| **Argument Name** | **Description** | **Required** |
| --- | --- | --- |
| file_id | ID of the requested file. Can be retrieved using the `google-drive-files-list` command. | Required |
| user_id | The user's primary email address. | Required |
| include_items_from_all_drives | Whether both My Drive and shared drive items should be included in the results. Possible values are: true, false. Default is false. | Optional |
| supports_all_drives | Whether the requesting application supports both My Drives and shared drives. Possible values are: True, False. Default is False. | Optional |

#### Context Output

| **Path** | **Type** | **Description** |
| --- | --- | --- |
| GoogleDrive.File.Parents | String | The IDs of the parent folders which contain the file. |
@@ -0,0 +1,14 @@
{
"kind": "drive#parentList",
"etag": "\"B6Ts0uRGIRS5cehymN8hytHFo-M\"",
"selfLink": "https://www.googleapis.com/drive/v2/files/1OKPUTs6C2PmFvAj1co7qcz2kTk9xZPsOhPjMEwPmlM8/parents",
"items": [
{
"selfLink": "https://www.googleapis.com/drive/v2/files/1OKPUTs6C2PmFvAj1co7qcz2kTk9xZPsOhPjMEwPmlM8/parents/1FlYv4F2x0JK_fudwEsNvNVjyUV3L89IB",
"id": "1FlYv4F2x0JK_fudwEsNvNVjyUV3L89IB",
"isRoot": true,
"kind": "drive#parentReference",
"parentLink": "https://www.googleapis.com/drive/v2/files/1FlYv4F2x0JK_fudwEsNvNVjyUV3L89IB"
}
]
}
7 changes: 7 additions & 0 deletions Packs/GoogleDrive/ReleaseNotes/1_3_0.md
@@ -0,0 +1,7 @@

#### Integrations

##### Google Drive

- Added the **google-drive-file-get-parents** command.
- Updated the Docker image to: *demisto/googleapi-python3:1.0.0.83805*.
2 changes: 1 addition & 1 deletion Packs/GoogleDrive/pack_metadata.json
Expand Up @@ -2,7 +2,7 @@
"name": "Google Drive",
"description": "Google Drive allows users to store files on their servers, synchronize files across devices, and share files. This integration helps you to create a new drive, query past activity and view change logs performed by the users, as well as list drives and files, and manage their permissions.",
"support": "xsoar",
"currentVersion": "1.2.48",
"currentVersion": "1.3.0",
"author": "Cortex XSOAR",
"url": "https://www.paloaltonetworks.com/cortex",
"email": "",
Expand Down

0 comments on commit db45990

Please sign in to comment.