|
21 | 21 | from google.api_core.client_options import ClientOptions
|
22 | 22 | from google.api_core.gapic_v1.method import DEFAULT, _MethodDefault
|
23 | 23 | from google.api_core.operation import Operation
|
| 24 | +from google.api_core.operation_async import AsyncOperation |
24 | 25 | from google.api_core.retry import Retry
|
25 |
| -from google.cloud.orchestration.airflow.service_v1 import EnvironmentsClient, ImageVersionsClient |
| 26 | +from google.cloud.orchestration.airflow.service_v1 import ( |
| 27 | + EnvironmentsAsyncClient, |
| 28 | + EnvironmentsClient, |
| 29 | + ImageVersionsClient, |
| 30 | +) |
26 | 31 | from google.cloud.orchestration.airflow.service_v1.services.environments.pagers import ListEnvironmentsPager
|
27 | 32 | from google.cloud.orchestration.airflow.service_v1.services.image_versions.pagers import (
|
28 | 33 | ListImageVersionsPager,
|
@@ -275,3 +280,123 @@ def list_image_versions(
|
275 | 280 | metadata=metadata,
|
276 | 281 | )
|
277 | 282 | return result
|
| 283 | + |
| 284 | + |
| 285 | +class CloudComposerAsyncHook(GoogleBaseHook): |
| 286 | + """Hook for Google Cloud Composer async APIs.""" |
| 287 | + |
| 288 | + client_options = ClientOptions(api_endpoint='composer.googleapis.com:443') |
| 289 | + |
| 290 | + def get_environment_client(self) -> EnvironmentsAsyncClient: |
| 291 | + """Retrieves client library object that allow access Environments service.""" |
| 292 | + return EnvironmentsAsyncClient( |
| 293 | + credentials=self.get_credentials(), |
| 294 | + client_info=CLIENT_INFO, |
| 295 | + client_options=self.client_options, |
| 296 | + ) |
| 297 | + |
| 298 | + def get_environment_name(self, project_id, region, environment_id): |
| 299 | + return f'projects/{project_id}/locations/{region}/environments/{environment_id}' |
| 300 | + |
| 301 | + def get_parent(self, project_id, region): |
| 302 | + return f'projects/{project_id}/locations/{region}' |
| 303 | + |
| 304 | + async def get_operation(self, operation_name): |
| 305 | + return await self.get_environment_client().transport.operations_client.get_operation( |
| 306 | + name=operation_name |
| 307 | + ) |
| 308 | + |
| 309 | + @GoogleBaseHook.fallback_to_default_project_id |
| 310 | + async def create_environment( |
| 311 | + self, |
| 312 | + project_id: str, |
| 313 | + region: str, |
| 314 | + environment: Union[Environment, Dict], |
| 315 | + retry: Union[Retry, _MethodDefault] = DEFAULT, |
| 316 | + timeout: Optional[float] = None, |
| 317 | + metadata: Sequence[Tuple[str, str]] = (), |
| 318 | + ) -> AsyncOperation: |
| 319 | + """ |
| 320 | + Create a new environment. |
| 321 | +
|
| 322 | + :param project_id: Required. The ID of the Google Cloud project that the service belongs to. |
| 323 | + :param region: Required. The ID of the Google Cloud region that the service belongs to. |
| 324 | + :param environment: The environment to create. This corresponds to the ``environment`` field on the |
| 325 | + ``request`` instance; if ``request`` is provided, this should not be set. |
| 326 | + :param retry: Designation of what errors, if any, should be retried. |
| 327 | + :param timeout: The timeout for this request. |
| 328 | + :param metadata: Strings which should be sent along with the request as metadata. |
| 329 | + """ |
| 330 | + client = self.get_environment_client() |
| 331 | + return await client.create_environment( |
| 332 | + request={'parent': self.get_parent(project_id, region), 'environment': environment}, |
| 333 | + retry=retry, |
| 334 | + timeout=timeout, |
| 335 | + metadata=metadata, |
| 336 | + ) |
| 337 | + |
| 338 | + @GoogleBaseHook.fallback_to_default_project_id |
| 339 | + async def delete_environment( |
| 340 | + self, |
| 341 | + project_id: str, |
| 342 | + region: str, |
| 343 | + environment_id: str, |
| 344 | + retry: Union[Retry, _MethodDefault] = DEFAULT, |
| 345 | + timeout: Optional[float] = None, |
| 346 | + metadata: Sequence[Tuple[str, str]] = (), |
| 347 | + ) -> AsyncOperation: |
| 348 | + """ |
| 349 | + Delete an environment. |
| 350 | +
|
| 351 | + :param project_id: Required. The ID of the Google Cloud project that the service belongs to. |
| 352 | + :param region: Required. The ID of the Google Cloud region that the service belongs to. |
| 353 | + :param environment_id: Required. The ID of the Google Cloud environment that the service belongs to. |
| 354 | + :param retry: Designation of what errors, if any, should be retried. |
| 355 | + :param timeout: The timeout for this request. |
| 356 | + :param metadata: Strings which should be sent along with the request as metadata. |
| 357 | + """ |
| 358 | + client = self.get_environment_client() |
| 359 | + name = self.get_environment_name(project_id, region, environment_id) |
| 360 | + return await client.delete_environment( |
| 361 | + request={"name": name}, retry=retry, timeout=timeout, metadata=metadata |
| 362 | + ) |
| 363 | + |
| 364 | + @GoogleBaseHook.fallback_to_default_project_id |
| 365 | + async def update_environment( |
| 366 | + self, |
| 367 | + project_id: str, |
| 368 | + region: str, |
| 369 | + environment_id: str, |
| 370 | + environment: Union[Environment, Dict], |
| 371 | + update_mask: Union[Dict, FieldMask], |
| 372 | + retry: Union[Retry, _MethodDefault] = DEFAULT, |
| 373 | + timeout: Optional[float] = None, |
| 374 | + metadata: Sequence[Tuple[str, str]] = (), |
| 375 | + ) -> AsyncOperation: |
| 376 | + r""" |
| 377 | + Update an environment. |
| 378 | +
|
| 379 | + :param project_id: Required. The ID of the Google Cloud project that the service belongs to. |
| 380 | + :param region: Required. The ID of the Google Cloud region that the service belongs to. |
| 381 | + :param environment_id: Required. The ID of the Google Cloud environment that the service belongs to. |
| 382 | + :param environment: A patch environment. Fields specified by the ``updateMask`` will be copied from |
| 383 | + the patch environment into the environment under update. |
| 384 | +
|
| 385 | + This corresponds to the ``environment`` field on the ``request`` instance; if ``request`` is |
| 386 | + provided, this should not be set. |
| 387 | + :param update_mask: Required. A comma-separated list of paths, relative to ``Environment``, of fields |
| 388 | + to update. If a dict is provided, it must be of the same form as the protobuf message |
| 389 | + :class:`~google.protobuf.field_mask_pb2.FieldMask` |
| 390 | + :param retry: Designation of what errors, if any, should be retried. |
| 391 | + :param timeout: The timeout for this request. |
| 392 | + :param metadata: Strings which should be sent along with the request as metadata. |
| 393 | + """ |
| 394 | + client = self.get_environment_client() |
| 395 | + name = self.get_environment_name(project_id, region, environment_id) |
| 396 | + |
| 397 | + return await client.update_environment( |
| 398 | + request={"name": name, "environment": environment, "update_mask": update_mask}, |
| 399 | + retry=retry, |
| 400 | + timeout=timeout, |
| 401 | + metadata=metadata, |
| 402 | + ) |
0 commit comments