-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathitem_delete.py
More file actions
99 lines (78 loc) · 3.3 KB
/
item_delete.py
File metadata and controls
99 lines (78 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# Copyright 2020 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""This script delete items in a Google Cloud Search datasource using
the Cloud Search API.
Prerequisites:
- Google Cloud Search enable on the gSuite organization
- Created a Google Cloud Third-party data sources ID
- GCP project
- Google Cloud Search API enabled in the project
- GCS bucket (Publicly readable)
- GCP service account
To run this script, you will need Python3 packages listed in REQUIREMENTS.txt.
You can easily install them with virtualenv and pip by running these commands:
virtualenv -p python3 env
source ./env/bin/activate
pip install -r REQUIREMENTS.txt
You can than run the script as follow:
python item_delete.py \
--service_account_file /PATH/TO/service.json \
--datasources YOUR_DATASOURCE_ID
"""
import argparse
import base64
import cloudsearch
import google.oauth2.credentials
import googleapiclient.http
import logging
import time
from google.oauth2 import service_account
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from google_auth_oauthlib.flow import InstalledAppFlow
logging.basicConfig(level=logging.INFO)
LOGGER = logging.getLogger('cloudsearch.item_list')
# Scope grants [CLOUD SEARCH]
SEARCH_SCOPES = ['https://www.googleapis.com/auth/cloud_search']
SEARCH_API_SERVICE_NAME = 'cloudsearch'
SEARCH_API_VERSION = 'v1'
def get_authenticated_service(service_account_file, scope, service_name, version):
# Create credentials from Service Account File
credentials = service_account.Credentials.from_service_account_file(
service_account_file, scopes=scope)
return build(service_name, version, credentials=credentials, cache_discovery=False)
def main(service_account_file,
datasources):
LOGGER.info('Delete documents - START')
service_search = get_authenticated_service(service_account_file,
SEARCH_SCOPES,
SEARCH_API_SERVICE_NAME,
SEARCH_API_VERSION)
itemService = cloudsearch.ItemsService(service_search, datasources)
items = itemService.list()
for item in items:
itemService.delete(item.get("name"), item.get("version"))
LOGGER.info('Document: %s - Deleted' % item.get("name"))
LOGGER.info('Delete documents - END')
return
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Example to parse HTML and send to CloudSearch.')
parser.add_argument('--service_account_file', dest='service_account_file',
help='File name for the service account.')
parser.add_argument('--datasources', dest='datasources',
help='DataSource to update.')
args = parser.parse_args()
main(args.service_account_file,
args.datasources)