|
| 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 Cloud Memorystore links.""" |
| 19 | +from typing import TYPE_CHECKING, Optional |
| 20 | + |
| 21 | +from airflow.models import BaseOperator |
| 22 | +from airflow.providers.google.cloud.links.base import BaseGoogleLink |
| 23 | + |
| 24 | +if TYPE_CHECKING: |
| 25 | + from airflow.utils.context import Context |
| 26 | + |
| 27 | +BASE_LINK = "https://console.cloud.google.com/memorystore" |
| 28 | +MEMCACHED_LINK = ( |
| 29 | + BASE_LINK + "/memcached/locations/{location_id}/instances/{instance_id}/details?project={project_id}" |
| 30 | +) |
| 31 | +MEMCACHED_LIST_LINK = BASE_LINK + "/memcached/instances?project={project_id}" |
| 32 | +REDIS_LINK = ( |
| 33 | + BASE_LINK + "/redis/locations/{location_id}/instances/{instance_id}/details/overview?project={project_id}" |
| 34 | +) |
| 35 | +REDIS_LIST_LINK = BASE_LINK + "/redis/instances?project={project_id}" |
| 36 | + |
| 37 | + |
| 38 | +class MemcachedInstanceDetailsLink(BaseGoogleLink): |
| 39 | + """Helper class for constructing Memorystore Memcached Instance Link""" |
| 40 | + |
| 41 | + name = "Memorystore Memcached Instance" |
| 42 | + key = "memcached_instance" |
| 43 | + format_str = MEMCACHED_LINK |
| 44 | + |
| 45 | + @staticmethod |
| 46 | + def persist( |
| 47 | + context: "Context", |
| 48 | + task_instance: BaseOperator, |
| 49 | + instance_id: str, |
| 50 | + location_id: str, |
| 51 | + project_id: Optional[str], |
| 52 | + ): |
| 53 | + task_instance.xcom_push( |
| 54 | + context, |
| 55 | + key=MemcachedInstanceDetailsLink.key, |
| 56 | + value={"instance_id": instance_id, "location_id": location_id, "project_id": project_id}, |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +class MemcachedInstanceListLink(BaseGoogleLink): |
| 61 | + """Helper class for constructing Memorystore Memcached List of Instances Link""" |
| 62 | + |
| 63 | + name = "Memorystore Memcached List of Instances" |
| 64 | + key = "memcached_instances" |
| 65 | + format_str = MEMCACHED_LIST_LINK |
| 66 | + |
| 67 | + @staticmethod |
| 68 | + def persist( |
| 69 | + context: "Context", |
| 70 | + task_instance: BaseOperator, |
| 71 | + project_id: Optional[str], |
| 72 | + ): |
| 73 | + task_instance.xcom_push( |
| 74 | + context, |
| 75 | + key=MemcachedInstanceListLink.key, |
| 76 | + value={"project_id": project_id}, |
| 77 | + ) |
| 78 | + |
| 79 | + |
| 80 | +class RedisInstanceDetailsLink(BaseGoogleLink): |
| 81 | + """Helper class for constructing Memorystore Redis Instance Link""" |
| 82 | + |
| 83 | + name = "Memorystore Redis Instance" |
| 84 | + key = "redis_instance" |
| 85 | + format_str = REDIS_LINK |
| 86 | + |
| 87 | + @staticmethod |
| 88 | + def persist( |
| 89 | + context: "Context", |
| 90 | + task_instance: BaseOperator, |
| 91 | + instance_id: str, |
| 92 | + location_id: str, |
| 93 | + project_id: Optional[str], |
| 94 | + ): |
| 95 | + task_instance.xcom_push( |
| 96 | + context, |
| 97 | + key=RedisInstanceDetailsLink.key, |
| 98 | + value={"instance_id": instance_id, "location_id": location_id, "project_id": project_id}, |
| 99 | + ) |
| 100 | + |
| 101 | + |
| 102 | +class RedisInstanceListLink(BaseGoogleLink): |
| 103 | + """Helper class for constructing Memorystore Redis List of Instances Link""" |
| 104 | + |
| 105 | + name = "Memorystore Redis List of Instances" |
| 106 | + key = "redis_instances" |
| 107 | + format_str = REDIS_LIST_LINK |
| 108 | + |
| 109 | + @staticmethod |
| 110 | + def persist( |
| 111 | + context: "Context", |
| 112 | + task_instance: BaseOperator, |
| 113 | + project_id: Optional[str], |
| 114 | + ): |
| 115 | + task_instance.xcom_push( |
| 116 | + context, |
| 117 | + key=RedisInstanceListLink.key, |
| 118 | + value={"project_id": project_id}, |
| 119 | + ) |
0 commit comments