|
| 1 | +"""Metadata read operations for the FinOps SDK. |
| 2 | +
|
| 3 | +This is **Step 2** of the FinOps SDK roadmap captured in |
| 4 | +``FinOps-SDK-Plan.docx``. It wraps the read-only metadata surface exposed |
| 5 | +by the FinOps Platform under ``/metadata/...``: |
| 6 | +
|
| 7 | +============================== ============================================== |
| 8 | +HTTP SDK call |
| 9 | +============================== ============================================== |
| 10 | +``GET /metadata/DataEntities`` ``client.metadata.list_data_entities(...)`` |
| 11 | +``GET /metadata/DataEntities('N')`` ``client.metadata.get_data_entity('N')`` |
| 12 | +``GET /metadata/PublicEntities`` ``client.metadata.list_public_entities(...)`` |
| 13 | +``GET /metadata/PublicEntities('N')`` ``client.metadata.get_public_entity('N')`` |
| 14 | +``GET /metadata/PublicEnumerations`` ``client.metadata.list_public_enumerations()`` |
| 15 | +============================== ============================================== |
| 16 | +
|
| 17 | +These verbs are backed by ``DataEntitiesController`` and sibling controllers in |
| 18 | +the FinOps Platform under |
| 19 | +``Source/Platform/Integration/Services/WebApi/Metadata/Source/Controllers/``. |
| 20 | +
|
| 21 | +These endpoints are read-only by design — there is no runtime metadata-write |
| 22 | +API in FinOps (schema is authored in X++ and built into the model layer; see |
| 23 | +``FinOps-SDK-Plan.docx`` §7). |
| 24 | +""" |
| 25 | +from __future__ import annotations |
| 26 | + |
| 27 | +from typing import TYPE_CHECKING, Iterator, Optional |
| 28 | +from urllib.parse import quote |
| 29 | + |
| 30 | +from ..errors import FinOpsError |
| 31 | + |
| 32 | +if TYPE_CHECKING: # pragma: no cover |
| 33 | + from ..client import FinOpsClient |
| 34 | + |
| 35 | + |
| 36 | +class MetadataOperations: |
| 37 | + """Read-only metadata operations on the FinOps ``/metadata`` surface. |
| 38 | +
|
| 39 | + Obtain via ``FinOpsClient.metadata`` — do not instantiate directly. |
| 40 | + """ |
| 41 | + |
| 42 | + def __init__(self, client: "FinOpsClient") -> None: |
| 43 | + self._client = client |
| 44 | + |
| 45 | + # ------------------------------------------------------------------ # |
| 46 | + # /metadata/DataEntities # |
| 47 | + # ------------------------------------------------------------------ # |
| 48 | + def list_data_entities( |
| 49 | + self, |
| 50 | + *, |
| 51 | + filter: Optional[str] = None, |
| 52 | + top: Optional[int] = None, |
| 53 | + ) -> Iterator[dict]: |
| 54 | + """``GET /metadata/DataEntities`` — yield every public data entity descriptor. |
| 55 | +
|
| 56 | + Returns one row at a time, transparently following the |
| 57 | + ``@odata.nextLink`` continuation token. |
| 58 | +
|
| 59 | + .. note:: |
| 60 | + The metadata controllers do **not** support ``$select`` (the server |
| 61 | + replies HTTP 400). ``$top`` is accepted but currently ignored |
| 62 | + server-side, so this SDK enforces ``top`` as a client-side cap. |
| 63 | + """ |
| 64 | + yield from self._paginate("DataEntities", filter=filter, top=top) |
| 65 | + |
| 66 | + def get_data_entity(self, name: str) -> dict: |
| 67 | + """``GET /metadata/DataEntities('Name')`` — single entity descriptor.""" |
| 68 | + if not name: |
| 69 | + raise ValueError("entity name is required") |
| 70 | + url = f"{self._metadata_url()}/DataEntities('{_escape(name)}')" |
| 71 | + return self._client._http.request("GET", url, expected=(200,)).json() |
| 72 | + |
| 73 | + # ------------------------------------------------------------------ # |
| 74 | + # /metadata/PublicEntities # |
| 75 | + # ------------------------------------------------------------------ # |
| 76 | + def list_public_entities( |
| 77 | + self, |
| 78 | + *, |
| 79 | + filter: Optional[str] = None, |
| 80 | + top: Optional[int] = None, |
| 81 | + ) -> Iterator[dict]: |
| 82 | + """``GET /metadata/PublicEntities`` — yield every public entity (with column metadata). |
| 83 | +
|
| 84 | + See :meth:`list_data_entities` for ``$select``/``$top`` caveats. |
| 85 | + """ |
| 86 | + yield from self._paginate("PublicEntities", filter=filter, top=top) |
| 87 | + |
| 88 | + def get_public_entity(self, name: str) -> dict: |
| 89 | + """``GET /metadata/PublicEntities('Name')`` — single entity with column metadata.""" |
| 90 | + if not name: |
| 91 | + raise ValueError("entity name is required") |
| 92 | + url = f"{self._metadata_url()}/PublicEntities('{_escape(name)}')" |
| 93 | + return self._client._http.request("GET", url, expected=(200,)).json() |
| 94 | + |
| 95 | + # ------------------------------------------------------------------ # |
| 96 | + # /metadata/PublicEnumerations # |
| 97 | + # ------------------------------------------------------------------ # |
| 98 | + def list_public_enumerations( |
| 99 | + self, |
| 100 | + *, |
| 101 | + filter: Optional[str] = None, |
| 102 | + top: Optional[int] = None, |
| 103 | + ) -> Iterator[dict]: |
| 104 | + """``GET /metadata/PublicEnumerations`` — yield every public enum descriptor.""" |
| 105 | + yield from self._paginate("PublicEnumerations", filter=filter, top=top) |
| 106 | + |
| 107 | + def get_public_enumeration(self, name: str) -> dict: |
| 108 | + """``GET /metadata/PublicEnumerations('Name')`` — single enum descriptor.""" |
| 109 | + if not name: |
| 110 | + raise ValueError("enumeration name is required") |
| 111 | + url = f"{self._metadata_url()}/PublicEnumerations('{_escape(name)}')" |
| 112 | + return self._client._http.request("GET", url, expected=(200,)).json() |
| 113 | + |
| 114 | + # ------------------------------------------------------------------ # |
| 115 | + # internals # |
| 116 | + # ------------------------------------------------------------------ # |
| 117 | + def _metadata_url(self) -> str: |
| 118 | + return f"{self._client.environment_url}/metadata" |
| 119 | + |
| 120 | + def _paginate( |
| 121 | + self, |
| 122 | + collection: str, |
| 123 | + *, |
| 124 | + filter: Optional[str] = None, |
| 125 | + top: Optional[int] = None, |
| 126 | + ) -> Iterator[dict]: |
| 127 | + params: dict = {} |
| 128 | + if filter: |
| 129 | + params["$filter"] = filter |
| 130 | + if top is not None: |
| 131 | + if top <= 0: |
| 132 | + return |
| 133 | + # Server currently ignores $top on /metadata/* but sending it is |
| 134 | + # harmless and lets us upgrade transparently if/when it lands. |
| 135 | + params["$top"] = str(top) |
| 136 | + |
| 137 | + url: Optional[str] = f"{self._metadata_url()}/{collection}" |
| 138 | + request_params: Optional[dict] = params or None |
| 139 | + yielded = 0 |
| 140 | + while url: |
| 141 | + resp = self._client._http.request( |
| 142 | + "GET", url, params=request_params, expected=(200,) |
| 143 | + ) |
| 144 | + payload = resp.json() |
| 145 | + for row in payload.get("value", []): |
| 146 | + if top is not None and yielded >= top: |
| 147 | + return |
| 148 | + yield row |
| 149 | + yielded += 1 |
| 150 | + url = payload.get("@odata.nextLink") |
| 151 | + request_params = None |
| 152 | + |
| 153 | + |
| 154 | +def _escape(name: str) -> str: |
| 155 | + if "'" in name: |
| 156 | + # OData v4 string literal escape: single quotes are doubled. |
| 157 | + return name.replace("'", "''") |
| 158 | + return name |
0 commit comments