|
| 1 | +"""Namespace utilities for data plane and control plane API calls.""" |
| 2 | + |
| 3 | +import warnings |
| 4 | +from typing import Dict, List, Optional |
| 5 | + |
| 6 | + |
| 7 | +def build_namespace_params(namespace: Optional[str] = None, namespace_path: Optional[str] = None) -> Dict[str, str]: |
| 8 | + """Build the namespace kwargs for a data plane API call. |
| 9 | +
|
| 10 | + Exactly one of ``namespace`` (exact match) or ``namespace_path`` |
| 11 | + (hierarchical path prefix) must be provided. Wildcards (``*``) are not |
| 12 | + supported in either field. |
| 13 | +
|
| 14 | + Raises: |
| 15 | + ValueError: if both arguments are provided, neither is provided, or |
| 16 | + the provided value contains a wildcard. |
| 17 | + """ |
| 18 | + if namespace is not None and namespace_path is not None: |
| 19 | + raise ValueError("'namespace' and 'namespace_path' are mutually exclusive.") |
| 20 | + if namespace is None and namespace_path is None: |
| 21 | + raise ValueError("At least one of 'namespace' or 'namespace_path' must be provided.") |
| 22 | + |
| 23 | + value = namespace if namespace is not None else namespace_path |
| 24 | + if "*" in value: |
| 25 | + raise ValueError("Wildcards (*) are not supported in namespaces.") |
| 26 | + |
| 27 | + if namespace is not None: |
| 28 | + return {"namespace": namespace} |
| 29 | + return {"namespacePath": namespace_path} |
| 30 | + |
| 31 | + |
| 32 | +def resolve_namespace_templates( |
| 33 | + namespaces: Optional[List[str]] = None, |
| 34 | + namespace_templates: Optional[List[str]] = None, |
| 35 | + param_name: str = "namespaces", |
| 36 | + new_param_name: Optional[str] = None, |
| 37 | +) -> Optional[List[str]]: |
| 38 | + """Resolve the deprecated ``namespaces`` kwarg and the new ``namespace_templates`` kwarg. |
| 39 | +
|
| 40 | + Used by control-plane strategy methods. Exactly one (or neither) may be provided. |
| 41 | + If the deprecated form is used, a ``DeprecationWarning`` is emitted. Returns the |
| 42 | + resolved list, or ``None`` if neither was provided. |
| 43 | +
|
| 44 | + Args: |
| 45 | + namespaces: The deprecated parameter value, if any. |
| 46 | + namespace_templates: The new parameter value, if any. |
| 47 | + param_name: Base name used in error/warning messages for the deprecated form |
| 48 | + (e.g. "namespaces" or "reflection_namespaces"). |
| 49 | + new_param_name: Name of the replacement form to reference in messages. Defaults |
| 50 | + to ``param_name`` with ``"namespaces"`` replaced by ``"namespace_templates"``. |
| 51 | + Override when the replacement identifier doesn't follow that pattern (for |
| 52 | + example, a dict key like ``reflection_config['namespaceTemplates']``). |
| 53 | +
|
| 54 | + Raises: |
| 55 | + ValueError: if both arguments are provided. |
| 56 | + """ |
| 57 | + if new_param_name is None: |
| 58 | + new_param_name = param_name.replace("namespaces", "namespace_templates") |
| 59 | + |
| 60 | + if namespaces is not None and namespace_templates is not None: |
| 61 | + raise ValueError( |
| 62 | + f"'{param_name}' and '{new_param_name}' are mutually exclusive. " |
| 63 | + f"Prefer '{new_param_name}' ('{param_name}' is deprecated)." |
| 64 | + ) |
| 65 | + |
| 66 | + if namespaces is not None: |
| 67 | + warnings.warn( |
| 68 | + f"The '{param_name}' parameter is deprecated and will be removed in a future release. " |
| 69 | + f"Use '{new_param_name}' instead.", |
| 70 | + DeprecationWarning, |
| 71 | + stacklevel=3, |
| 72 | + ) |
| 73 | + return namespaces |
| 74 | + |
| 75 | + return namespace_templates |
0 commit comments