1
1
# SPDX-License-Identifier: Apache-2.0
2
- # Copyright 2024 Atlan Pte. Ltd.
2
+ # Copyright 2025 Atlan Pte. Ltd.
3
3
from __future__ import annotations
4
4
5
5
import logging
6
6
import threading
7
- from typing import Dict , Optional , Union
7
+ from typing import TYPE_CHECKING , Optional , Union
8
8
9
9
from pyatlan .cache .abstract_asset_cache import AbstractAssetCache , AbstractAssetName
10
- from pyatlan .client .atlan import AtlanClient
11
10
from pyatlan .model .assets import Asset , Connection
12
11
from pyatlan .model .enums import AtlanConnectorType
13
12
from pyatlan .model .fluent_search import FluentSearch
14
13
from pyatlan .model .search import Term
15
14
16
- LOGGER = logging .getLogger (__name__ )
15
+ if TYPE_CHECKING :
16
+ from pyatlan .client .atlan import AtlanClient
17
17
18
18
lock = threading .Lock ()
19
+ LOGGER = logging .getLogger (__name__ )
19
20
20
21
21
22
class ConnectionCache (AbstractAssetCache ):
@@ -37,24 +38,11 @@ class ConnectionCache(AbstractAssetCache):
37
38
Connection .CONNECTOR_NAME ,
38
39
]
39
40
SEARCH_ATTRIBUTES = [field .atlan_field_name for field in _SEARCH_FIELDS ]
40
- caches : Dict [int , ConnectionCache ] = dict ()
41
41
42
42
def __init__ (self , client : AtlanClient ):
43
43
super ().__init__ (client )
44
44
45
- @classmethod
46
- def get_cache (cls ) -> ConnectionCache :
47
- from pyatlan .client .atlan import AtlanClient
48
-
49
- with lock :
50
- default_client = AtlanClient .get_default_client ()
51
- cache_key = default_client .cache_key
52
- if cache_key not in cls .caches :
53
- cls .caches [cache_key ] = ConnectionCache (client = default_client )
54
- return cls .caches [cache_key ]
55
-
56
- @classmethod
57
- def get_by_guid (cls , guid : str , allow_refresh : bool = True ) -> Connection :
45
+ def get_by_guid (self , guid : str , allow_refresh : bool = True ) -> Connection :
58
46
"""
59
47
Retrieve a connection from the cache by its UUID.
60
48
If the asset is not found, it will be looked up and added to the cache.
@@ -66,11 +54,10 @@ def get_by_guid(cls, guid: str, allow_refresh: bool = True) -> Connection:
66
54
:raises NotFoundError: if the connection cannot be found (does not exist) in Atlan
67
55
:raises InvalidRequestError: if no UUID was provided for the connection to retrieve
68
56
"""
69
- return cls . get_cache () ._get_by_guid (guid = guid , allow_refresh = allow_refresh )
57
+ return self ._get_by_guid (guid = guid , allow_refresh = allow_refresh )
70
58
71
- @classmethod
72
59
def get_by_qualified_name (
73
- cls , qualified_name : str , allow_refresh : bool = True
60
+ self , qualified_name : str , allow_refresh : bool = True
74
61
) -> Connection :
75
62
"""
76
63
Retrieve a connection from the cache by its unique Atlan-internal name.
@@ -84,13 +71,12 @@ def get_by_qualified_name(
84
71
:raises NotFoundError: if the connection cannot be found (does not exist) in Atlan
85
72
:raises InvalidRequestError: if no qualified_name was provided for the connection to retrieve
86
73
"""
87
- return cls . get_cache () ._get_by_qualified_name (
74
+ return self ._get_by_qualified_name (
88
75
qualified_name = qualified_name , allow_refresh = allow_refresh
89
76
)
90
77
91
- @classmethod
92
78
def get_by_name (
93
- cls , name : ConnectionName , allow_refresh : bool = True
79
+ self , name : ConnectionName , allow_refresh : bool = True
94
80
) -> Connection :
95
81
"""
96
82
Retrieve an connection from the cache by its uniquely identifiable name.
@@ -104,7 +90,7 @@ def get_by_name(
104
90
:raises NotFoundError: if the connection cannot be found (does not exist) in Atlan
105
91
:raises InvalidRequestError: if no name was provided for the connection to retrieve
106
92
"""
107
- return cls . get_cache () ._get_by_name (name = name , allow_refresh = allow_refresh )
93
+ return self ._get_by_name (name = name , allow_refresh = allow_refresh )
108
94
109
95
def lookup_by_guid (self , guid : str ) -> None :
110
96
if not guid :
@@ -139,21 +125,22 @@ def lookup_by_qualified_name(self, connection_qn: str) -> None:
139
125
def lookup_by_name (self , name : ConnectionName ) -> None :
140
126
if not isinstance (name , ConnectionName ):
141
127
return
142
- results = self .client .asset .find_connections_by_name (
143
- name = name .name ,
144
- connector_type = name .type ,
145
- attributes = self .SEARCH_ATTRIBUTES ,
146
- )
147
- if not results :
148
- return
149
- if len (results ) > 1 :
150
- LOGGER .warning (
151
- (
152
- "Found multiple connections of the same type with the same name, caching only the first: %s"
153
- ),
154
- name ,
128
+ with self .lock :
129
+ results = self .client .asset .find_connections_by_name (
130
+ name = name .name , # type: ignore[arg-type]
131
+ connector_type = name .type , # type: ignore[arg-type]
132
+ attributes = self .SEARCH_ATTRIBUTES ,
155
133
)
156
- self .cache (results [0 ])
134
+ if not results :
135
+ return
136
+ if len (results ) > 1 :
137
+ LOGGER .warning (
138
+ (
139
+ "Found multiple connections of the same type with the same name, caching only the first: %s"
140
+ ),
141
+ name ,
142
+ )
143
+ self .cache (results [0 ])
157
144
158
145
def get_name (self , asset : Asset ):
159
146
if not isinstance (asset , Connection ):
@@ -188,7 +175,7 @@ def __init__(
188
175
elif isinstance (connection , str ):
189
176
tokens = connection .split ("/" )
190
177
if len (tokens ) > 1 :
191
- self .type = AtlanConnectorType (tokens [0 ]) # type: ignore[call-arg]
178
+ self .type = AtlanConnectorType (tokens [0 ]). value # type: ignore[call-arg]
192
179
self .name = connection [len (tokens [0 ]) + 1 :] # noqa
193
180
194
181
def __hash__ (self ):
0 commit comments