Skip to content

Commit 98514cc

Browse files
author
Bradley Bonitatibus
authored
Add optional location to bigquery data transfer service (#15088) (#20221)
1 parent bc76126 commit 98514cc

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

airflow/providers/google/cloud/hooks/bigquery_dts.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,15 @@ def __init__(
5151
self,
5252
gcp_conn_id: str = "google_cloud_default",
5353
delegate_to: Optional[str] = None,
54+
location: Optional[str] = None,
5455
impersonation_chain: Optional[Union[str, Sequence[str]]] = None,
5556
) -> None:
5657
super().__init__(
5758
gcp_conn_id=gcp_conn_id,
5859
delegate_to=delegate_to,
5960
impersonation_chain=impersonation_chain,
6061
)
62+
self.location = location
6163

6264
@staticmethod
6365
def _disable_auto_scheduling(config: Union[dict, TransferConfig]) -> TransferConfig:
@@ -133,6 +135,9 @@ def create_transfer_config(
133135
"""
134136
client = self.get_conn()
135137
parent = f"projects/{project_id}"
138+
if self.location:
139+
parent = f"{parent}/locations/{self.location}"
140+
136141
return client.create_transfer_config(
137142
request={
138143
'parent': parent,
@@ -174,7 +179,11 @@ def delete_transfer_config(
174179
:return: None
175180
"""
176181
client = self.get_conn()
177-
name = f"projects/{project_id}/transferConfigs/{transfer_config_id}"
182+
project = f"projects/{project_id}"
183+
if self.location:
184+
project = f"/{project}/locations/{self.location}"
185+
186+
name = f"{project}/transferConfigs/{transfer_config_id}"
178187
return client.delete_transfer_config(
179188
request={'name': name}, retry=retry, timeout=timeout, metadata=metadata or ()
180189
)
@@ -223,7 +232,11 @@ def start_manual_transfer_runs(
223232
:return: An ``google.cloud.bigquery_datatransfer_v1.types.StartManualTransferRunsResponse`` instance.
224233
"""
225234
client = self.get_conn()
226-
parent = f"projects/{project_id}/transferConfigs/{transfer_config_id}"
235+
project = f"projects/{project_id}"
236+
if self.location:
237+
project = f"{project}/locations/{self.location}"
238+
239+
parent = f"{project}/transferConfigs/{transfer_config_id}"
227240
return client.start_manual_transfer_runs(
228241
request={
229242
'parent': parent,
@@ -268,7 +281,11 @@ def get_transfer_run(
268281
:return: An ``google.cloud.bigquery_datatransfer_v1.types.TransferRun`` instance.
269282
"""
270283
client = self.get_conn()
271-
name = f"projects/{project_id}/transferConfigs/{transfer_config_id}/runs/{run_id}"
284+
project = f"projects/{project_id}"
285+
if self.location:
286+
project = f"{project}/locations/{self.location}"
287+
288+
name = "f{project}/transferConfigs/{transfer_config_id}/runs/{run_id}"
272289
return client.get_transfer_run(
273290
request={'name': name}, retry=retry, timeout=timeout, metadata=metadata or ()
274291
)

airflow/providers/google/cloud/operators/bigquery_dts.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class BigQueryCreateDataTransferOperator(BaseOperator):
3939
created. If set to None or missing, the default project_id from the Google Cloud connection
4040
is used.
4141
:type project_id: str
42+
:param: location: BigQuery Transfer Service location for regional transfers.
43+
:type location: Optional[str]
4244
:param authorization_code: authorization code to use with this transfer configuration.
4345
This is required if new credentials are needed.
4446
:type authorization_code: Optional[str]
@@ -77,6 +79,7 @@ def __init__(
7779
*,
7880
transfer_config: dict,
7981
project_id: Optional[str] = None,
82+
location: Optional[str] = None,
8083
authorization_code: Optional[str] = None,
8184
retry: Retry = None,
8285
timeout: Optional[float] = None,
@@ -89,6 +92,7 @@ def __init__(
8992
self.transfer_config = transfer_config
9093
self.authorization_code = authorization_code
9194
self.project_id = project_id
95+
self.location = location
9296
self.retry = retry
9397
self.timeout = timeout
9498
self.metadata = metadata
@@ -97,7 +101,7 @@ def __init__(
97101

98102
def execute(self, context):
99103
hook = BiqQueryDataTransferServiceHook(
100-
gcp_conn_id=self.gcp_conn_id, impersonation_chain=self.impersonation_chain
104+
gcp_conn_id=self.gcp_conn_id, impersonation_chain=self.impersonation_chain, location=self.location
101105
)
102106
self.log.info("Creating DTS transfer config")
103107
response = hook.create_transfer_config(
@@ -127,6 +131,8 @@ class BigQueryDeleteDataTransferConfigOperator(BaseOperator):
127131
:param project_id: The BigQuery project id where the transfer configuration should be
128132
created. If set to None or missing, the default project_id from the Google Cloud connection is used.
129133
:type project_id: str
134+
:param: location: BigQuery Transfer Service location for regional transfers.
135+
:type location: Optional[str]
130136
:param retry: A retry object used to retry requests. If `None` is
131137
specified, requests will not be retried.
132138
:type retry: Optional[google.api_core.retry.Retry]
@@ -161,6 +167,7 @@ def __init__(
161167
*,
162168
transfer_config_id: str,
163169
project_id: Optional[str] = None,
170+
location: Optional[str] = None,
164171
retry: Retry = None,
165172
timeout: Optional[float] = None,
166173
metadata: Optional[Sequence[Tuple[str, str]]] = None,
@@ -170,6 +177,7 @@ def __init__(
170177
) -> None:
171178
super().__init__(**kwargs)
172179
self.project_id = project_id
180+
self.location = location
173181
self.transfer_config_id = transfer_config_id
174182
self.retry = retry
175183
self.timeout = timeout
@@ -179,7 +187,7 @@ def __init__(
179187

180188
def execute(self, context) -> None:
181189
hook = BiqQueryDataTransferServiceHook(
182-
gcp_conn_id=self.gcp_conn_id, impersonation_chain=self.impersonation_chain
190+
gcp_conn_id=self.gcp_conn_id, impersonation_chain=self.impersonation_chain, location=self.location
183191
)
184192
hook.delete_transfer_config(
185193
transfer_config_id=self.transfer_config_id,
@@ -215,6 +223,8 @@ class BigQueryDataTransferServiceStartTransferRunsOperator(BaseOperator):
215223
:param project_id: The BigQuery project id where the transfer configuration should be
216224
created. If set to None or missing, the default project_id from the Google Cloud connection is used.
217225
:type project_id: str
226+
:param: location: BigQuery Transfer Service location for regional transfers.
227+
:type location: Optional[str]
218228
:param retry: A retry object used to retry requests. If `None` is
219229
specified, requests will not be retried.
220230
:type retry: Optional[google.api_core.retry.Retry]
@@ -251,6 +261,7 @@ def __init__(
251261
*,
252262
transfer_config_id: str,
253263
project_id: Optional[str] = None,
264+
location: Optional[str] = None,
254265
requested_time_range: Optional[dict] = None,
255266
requested_run_time: Optional[dict] = None,
256267
retry: Retry = None,
@@ -262,6 +273,7 @@ def __init__(
262273
) -> None:
263274
super().__init__(**kwargs)
264275
self.project_id = project_id
276+
self.location = location
265277
self.transfer_config_id = transfer_config_id
266278
self.requested_time_range = requested_time_range
267279
self.requested_run_time = requested_run_time
@@ -273,7 +285,7 @@ def __init__(
273285

274286
def execute(self, context):
275287
hook = BiqQueryDataTransferServiceHook(
276-
gcp_conn_id=self.gcp_conn_id, impersonation_chain=self.impersonation_chain
288+
gcp_conn_id=self.gcp_conn_id, impersonation_chain=self.impersonation_chain, location=self.location
277289
)
278290
self.log.info('Submitting manual transfer for %s', self.transfer_config_id)
279291
response = hook.start_manual_transfer_runs(

0 commit comments

Comments
 (0)