-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathclients.py
209 lines (178 loc) · 7.41 KB
/
clients.py
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# Copyright 2023 Google LLC
#
# 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.
"""Clients manages the connection to Google APIs."""
import os
import typing
from typing import Optional
import google.api_core.client_info
import google.api_core.client_options
import google.api_core.exceptions
import google.api_core.gapic_v1.client_info
import google.auth.credentials
import google.cloud.bigquery as bigquery
import google.cloud.bigquery_connection_v1
import google.cloud.bigquery_storage_v1
import google.cloud.functions_v2
import google.cloud.resourcemanager_v3
import ibis
import pydata_google_auth
import bigframes.version
_ENV_DEFAULT_PROJECT = "GOOGLE_CLOUD_PROJECT"
_APPLICATION_NAME = f"bigframes/{bigframes.version.__version__}"
_SCOPES = ["https://www.googleapis.com/auth/cloud-platform"]
# BigQuery is a REST API, which requires the protocol as part of the URL.
_BIGQUERY_REGIONAL_ENDPOINT = "https://{location}-bigquery.googleapis.com"
# BigQuery Connection and Storage are gRPC APIs, which don't support the
# https:// protocol in the API endpoint URL.
_BIGQUERYCONNECTION_REGIONAL_ENDPOINT = "{location}-bigqueryconnection.googleapis.com"
_BIGQUERYSTORAGE_REGIONAL_ENDPOINT = "{location}-bigquerystorage.googleapis.com"
def _get_default_credentials_with_project():
return pydata_google_auth.default(scopes=_SCOPES, use_local_webserver=False)
def _create_user_agent(application_name: str) -> str:
user_agent = []
if application_name:
user_agent.append(application_name)
user_agent_default_template = f"ibis/{ibis.__version__}"
user_agent.append(user_agent_default_template)
return " ".join(user_agent)
class ClientsProvider:
"""Provides client instances necessary to perform cloud operations."""
def __init__(
self,
project: Optional[str],
location: Optional[str],
use_regional_endpoints: Optional[bool],
credentials: Optional[google.auth.credentials.Credentials],
application_name: Optional[str],
):
credentials_project = None
if credentials is None:
credentials, credentials_project = _get_default_credentials_with_project()
# Prefer the project in this order:
# 1. Project explicitly specified by the user
# 2. Project set in the environment
# 3. Project associated with the default credentials
project = (
project
or os.getenv(_ENV_DEFAULT_PROJECT)
or typing.cast(Optional[str], credentials_project)
)
if not project:
raise ValueError(
"Project must be set to initialize BigQuery client. "
"Try setting `bigframes.options.bigquery.project` first."
)
self._application_name = (
f"{_APPLICATION_NAME} {application_name}"
if application_name
else _APPLICATION_NAME
)
self._project = project
self._location = location
self._use_regional_endpoints = use_regional_endpoints
self._credentials = credentials
# cloud clients initialized for lazy load
self._bqclient = None
self._bqconnectionclient = None
self._bqstoragereadclient = None
self._cloudfunctionsclient = None
self._resourcemanagerclient = None
@property
def bqclient(self):
if not self._bqclient:
bq_options = None
if self._use_regional_endpoints:
bq_options = google.api_core.client_options.ClientOptions(
api_endpoint=_BIGQUERY_REGIONAL_ENDPOINT.format(
location=self._location
),
)
bq_info = google.api_core.client_info.ClientInfo(
user_agent=_create_user_agent(self._application_name)
)
self._bqclient = bigquery.Client(
client_info=bq_info,
client_options=bq_options,
credentials=self._credentials,
project=self._project,
location=self._location,
)
return self._bqclient
@property
def bqconnectionclient(self):
if not self._bqconnectionclient:
bqconnection_options = None
if self._use_regional_endpoints:
bqconnection_options = google.api_core.client_options.ClientOptions(
api_endpoint=_BIGQUERYCONNECTION_REGIONAL_ENDPOINT.format(
location=self._location
)
)
bqconnection_info = google.api_core.gapic_v1.client_info.ClientInfo(
user_agent=_create_user_agent(self._application_name)
)
self._bqconnectionclient = (
google.cloud.bigquery_connection_v1.ConnectionServiceClient(
client_info=bqconnection_info,
client_options=bqconnection_options,
credentials=self._credentials,
)
)
return self._bqconnectionclient
@property
def bqstoragereadclient(self):
if not self._bqstoragereadclient:
bqstorage_options = None
if self._use_regional_endpoints:
bqstorage_options = google.api_core.client_options.ClientOptions(
api_endpoint=_BIGQUERYSTORAGE_REGIONAL_ENDPOINT.format(
location=self._location
)
)
bqstorage_info = google.api_core.gapic_v1.client_info.ClientInfo(
user_agent=_create_user_agent(self._application_name)
)
self._bqstoragereadclient = (
google.cloud.bigquery_storage_v1.BigQueryReadClient(
client_info=bqstorage_info,
client_options=bqstorage_options,
credentials=self._credentials,
)
)
return self._bqstoragereadclient
@property
def cloudfunctionsclient(self):
if not self._cloudfunctionsclient:
functions_info = google.api_core.gapic_v1.client_info.ClientInfo(
user_agent=_create_user_agent(self._application_name)
)
self._cloudfunctionsclient = (
google.cloud.functions_v2.FunctionServiceClient(
client_info=functions_info,
credentials=self._credentials,
)
)
return self._cloudfunctionsclient
@property
def resourcemanagerclient(self):
if not self._resourcemanagerclient:
resourcemanager_info = google.api_core.gapic_v1.client_info.ClientInfo(
user_agent=_create_user_agent(self._application_name)
)
self._resourcemanagerclient = (
google.cloud.resourcemanager_v3.ProjectsClient(
credentials=self._credentials, client_info=resourcemanager_info
)
)
return self._resourcemanagerclient