-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathschema_delete.py
More file actions
104 lines (82 loc) · 3.37 KB
/
schema_delete.py
File metadata and controls
104 lines (82 loc) · 3.37 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
100
101
102
103
104
# 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 create or update a schema 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
- 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 schema_delete.py \
--service_account_file /PATH/TO/service.json \
--datasources YOUR_DATASOURCE_ID
"""
import argparse
import base64
import json
import logging
import os
import google.oauth2.credentials
import googleapiclient.http
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.schema')
# 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 cloud_search_delete_schema(service, datasources):
schema = service.indexing().datasources().deleteSchema(
name="datasources/"+datasources).execute()
return schema
def main(service_account_file, datasources):
# Create a service instance
try:
service_search = get_authenticated_service(
service_account_file,
SEARCH_SCOPES,
SEARCH_API_SERVICE_NAME,
SEARCH_API_VERSION)
LOGGER.info("Delete schema - START")
# Delete Schema from the Datasource
delete = cloud_search_delete_schema(service_search, datasources)
LOGGER.info("Delete: %s" % delete)
LOGGER.info("Delete schema - END")
# Example code only. Add proper exception handling
except Exception as e:
LOGGER.error('Error %s' % e)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Example to handle CRUD for CloudSearch Schema.')
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)