Resolve the DMSUnit for the current tenant. Resolution priority: 1. Explicit config/environment override (``DATA_AGENT_DMS_UNIT``). 2. dms-enterprise ``GetActiveRouteUnit`` (2018-11-01). 3. Region fallback. ``region_id`` lets callers resolve for a non-
(self, force_refresh: bool = False, region_id: Optional[str] = None)
| 144 | self._initialize_client() |
| 145 | |
| 146 | def _resolve_dms_unit(self, force_refresh: bool = False, region_id: Optional[str] = None) -> str: |
| 147 | """Resolve the DMSUnit for the current tenant. |
| 148 | |
| 149 | Resolution priority: |
| 150 | 1. Explicit config/environment override (``DATA_AGENT_DMS_UNIT``). |
| 151 | 2. dms-enterprise ``GetActiveRouteUnit`` (2018-11-01). |
| 152 | 3. Region fallback. |
| 153 | |
| 154 | ``region_id`` lets callers resolve for a non-default RegionId while |
| 155 | still honoring an explicit DMSUnit override. |
| 156 | |
| 157 | Falls back to the selected region when: |
| 158 | - the response has no ``Route`` object (tenant belongs to the |
| 159 | default unit, which equals the current region); |
| 160 | - the call fails for any reason (network, auth, etc.). |
| 161 | |
| 162 | Result is cached at both instance and process level. Set |
| 163 | ``force_refresh=True`` to bypass the cache. |
| 164 | """ |
| 165 | if self._config.dms_unit: |
| 166 | self._dms_unit = self._config.dms_unit |
| 167 | self._dms_unit_region = region_id or self._config.region |
| 168 | self._dms_unit_source = "config" |
| 169 | return self._dms_unit |
| 170 | |
| 171 | region = region_id or self._config.region |
| 172 | if not force_refresh and self._dms_unit and self._dms_unit_region == region: |
| 173 | return self._dms_unit |
| 174 | |
| 175 | cache_key = (self._auth_type, region) |
| 176 | if not force_refresh and cache_key in DataAgentClient._dms_unit_cache: |
| 177 | self._dms_unit = DataAgentClient._dms_unit_cache[cache_key] |
| 178 | self._dms_unit_source = "cache" |
| 179 | return self._dms_unit |
| 180 | |
| 181 | resolved = region # safe default |
| 182 | source = "fallback" |
| 183 | try: |
| 184 | resp = self._call_dms_enterprise_route_unit(region) |
| 185 | route = resp.get("Route") or resp.get("data") or resp.get("Data") |
| 186 | if isinstance(route, dict): |
| 187 | unit = route.get("RegionId") or route.get("regionId") |
| 188 | if isinstance(unit, str) and unit: |
| 189 | resolved = unit |
| 190 | source = "route" |
| 191 | except Exception: |
| 192 | # Silent fallback: DMSUnit resolution must never block API calls. |
| 193 | pass |
| 194 | |
| 195 | self._dms_unit = resolved |
| 196 | self._dms_unit_region = region |
| 197 | self._dms_unit_source = source |
| 198 | DataAgentClient._dms_unit_cache[cache_key] = resolved |
| 199 | return resolved |
| 200 | |
| 201 | def _call_dms_enterprise_route_unit(self, region_id: Optional[str] = None) -> dict: |
| 202 | """Call dms-enterprise.GetActiveRouteUnit via a short-lived OpenApiClient. |
no test coverage detected