Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GoogleDrive: Get Parents API #31945

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
25 changes: 25 additions & 0 deletions Packs/GoogleDrive/Integrations/GoogleDrive/GoogleDrive.yml
Expand Up @@ -3076,6 +3076,31 @@ 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.
- name: user_id
description: The user's primary email address.
- 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 = file_get_parents(gsuite_client, args)

assert len(result.outputs.get('GoogleDrive.File.Parents')) == 1
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. | Optional |
| user_id | The user's primary email address. | Optional |
| 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