|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +# or more contributor license agreements. See the NOTICE file |
| 4 | +# distributed with this work for additional information |
| 5 | +# regarding copyright ownership. The ASF licenses this file |
| 6 | +# to you under the Apache License, Version 2.0 (the |
| 7 | +# "License"); you may not use this file except in compliance |
| 8 | +# with the License. You may obtain a copy of the License at |
| 9 | +# |
| 10 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +# |
| 12 | +# Unless required by applicable law or agreed to in writing, |
| 13 | +# software distributed under the License is distributed on an |
| 14 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +# KIND, either express or implied. See the License for the |
| 16 | +# specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | +"""This module contains Google Compute Engine links.""" |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +from typing import TYPE_CHECKING |
| 22 | + |
| 23 | +from airflow.models import BaseOperator |
| 24 | +from airflow.providers.google.cloud.links.base import BaseGoogleLink |
| 25 | + |
| 26 | +if TYPE_CHECKING: |
| 27 | + from airflow.utils.context import Context |
| 28 | + |
| 29 | +COMPUTE_BASE_LINK = "https://console.cloud.google.com/compute" |
| 30 | +COMPUTE_LINK = ( |
| 31 | + COMPUTE_BASE_LINK + "/instancesDetail/zones/{location_id}/instances/{resource_id}?project={project_id}" |
| 32 | +) |
| 33 | +COMPUTE_TEMPLATE_LINK = COMPUTE_BASE_LINK + "/instanceTemplates/details/{resource_id}?project={project_id}" |
| 34 | +COMPUTE_GROUP_MANAGER_LINK = ( |
| 35 | + COMPUTE_BASE_LINK + "/instanceGroups/details/{location_id}/{resource_id}?project={project_id}" |
| 36 | +) |
| 37 | + |
| 38 | + |
| 39 | +class ComputeInstanceDetailsLink(BaseGoogleLink): |
| 40 | + """Helper class for constructing Compute Instance details Link""" |
| 41 | + |
| 42 | + name = "Compute Instance details" |
| 43 | + key = "compute_instance_details" |
| 44 | + format_str = COMPUTE_LINK |
| 45 | + |
| 46 | + @staticmethod |
| 47 | + def persist( |
| 48 | + context: Context, |
| 49 | + task_instance: BaseOperator, |
| 50 | + location_id: str, |
| 51 | + resource_id: str, |
| 52 | + project_id: str | None, |
| 53 | + ): |
| 54 | + task_instance.xcom_push( |
| 55 | + context, |
| 56 | + key=ComputeInstanceDetailsLink.key, |
| 57 | + value={ |
| 58 | + "location_id": location_id, |
| 59 | + "resource_id": resource_id, |
| 60 | + "project_id": project_id, |
| 61 | + }, |
| 62 | + ) |
| 63 | + |
| 64 | + |
| 65 | +class ComputeInstanceTemplateDetailsLink(BaseGoogleLink): |
| 66 | + """Helper class for constructing Compute Instance Template details Link""" |
| 67 | + |
| 68 | + name = "Compute Instance Template details" |
| 69 | + key = "compute_instance_template_details" |
| 70 | + format_str = COMPUTE_TEMPLATE_LINK |
| 71 | + |
| 72 | + @staticmethod |
| 73 | + def persist( |
| 74 | + context: Context, |
| 75 | + task_instance: BaseOperator, |
| 76 | + resource_id: str, |
| 77 | + project_id: str | None, |
| 78 | + ): |
| 79 | + task_instance.xcom_push( |
| 80 | + context, |
| 81 | + key=ComputeInstanceTemplateDetailsLink.key, |
| 82 | + value={ |
| 83 | + "resource_id": resource_id, |
| 84 | + "project_id": project_id, |
| 85 | + }, |
| 86 | + ) |
| 87 | + |
| 88 | + |
| 89 | +class ComputeInstanceGroupManagerDetailsLink(BaseGoogleLink): |
| 90 | + """Helper class for constructing Compute Instance Group Manager details Link""" |
| 91 | + |
| 92 | + name = "Compute Instance Group Manager" |
| 93 | + key = "compute_instance_group_manager_details" |
| 94 | + format_str = COMPUTE_GROUP_MANAGER_LINK |
| 95 | + |
| 96 | + @staticmethod |
| 97 | + def persist( |
| 98 | + context: Context, |
| 99 | + task_instance: BaseOperator, |
| 100 | + location_id: str, |
| 101 | + resource_id: str, |
| 102 | + project_id: str | None, |
| 103 | + ): |
| 104 | + task_instance.xcom_push( |
| 105 | + context, |
| 106 | + key=ComputeInstanceGroupManagerDetailsLink.key, |
| 107 | + value={ |
| 108 | + "location_id": location_id, |
| 109 | + "resource_id": resource_id, |
| 110 | + "project_id": project_id, |
| 111 | + }, |
| 112 | + ) |
0 commit comments