|
| 1 | +from verlib2 import Version |
| 2 | + |
| 3 | +from ..base import Base |
| 4 | + |
| 5 | +VERSION_8_2 = Version("8.2") |
| 6 | + |
| 7 | + |
| 8 | +class LibraryElement(Base): |
| 9 | + Panel: int = 1 |
| 10 | + Variable: int = 2 |
| 11 | + |
| 12 | + def __init__(self, client, api): |
| 13 | + super(LibraryElement, self).__init__(client) |
| 14 | + self.client = client |
| 15 | + self.api = api |
| 16 | + |
| 17 | + async def get_library_element(self, element_uid: str) -> any: |
| 18 | + """ |
| 19 | +
|
| 20 | + :param element_uid: |
| 21 | + :return: |
| 22 | + """ |
| 23 | + if await self.api.version and Version(await self.api.version) < VERSION_8_2: |
| 24 | + raise DeprecationWarning("Grafana versions earlier than 8.2 do not support library elements") |
| 25 | + get_element_path = f"/library-elements/{element_uid}" |
| 26 | + return await self.client.GET(get_element_path) |
| 27 | + |
| 28 | + async def get_library_element_by_name(self, element_name: str) -> any: |
| 29 | + """ |
| 30 | +
|
| 31 | + :param element_name: |
| 32 | + :return: |
| 33 | + """ |
| 34 | + if await self.api.version and Version(await self.api.version) < VERSION_8_2: |
| 35 | + raise DeprecationWarning("Grafana versions earlier than 8.2 do not support library elements") |
| 36 | + get_element_path = f"/library-elements/name/{element_name}" |
| 37 | + return await self.client.GET(get_element_path) |
| 38 | + |
| 39 | + async def get_library_element_connections(self, element_uid: str) -> any: |
| 40 | + """ |
| 41 | +
|
| 42 | + :param element_uid: |
| 43 | + :return: |
| 44 | + """ |
| 45 | + if await self.api.version and Version(await self.api.version) < VERSION_8_2: |
| 46 | + raise DeprecationWarning("Grafana versions earlier than 8.2 do not support library elements") |
| 47 | + get_element_connections_path = f"/library-elements/{element_uid}/connections" |
| 48 | + return await self.client.GET(get_element_connections_path) |
| 49 | + |
| 50 | + async def create_library_element( |
| 51 | + self, model: dict, name: str = None, kind: int = Panel, uid: str = None, folder_uid: str = None |
| 52 | + ): |
| 53 | + """ |
| 54 | +
|
| 55 | + :param model: |
| 56 | + :param name: |
| 57 | + :param kind: |
| 58 | + :param uid: |
| 59 | + :param folder_uid: |
| 60 | + :return: |
| 61 | + """ |
| 62 | + if await self.api.version and Version(await self.api.version) < VERSION_8_2: |
| 63 | + raise DeprecationWarning("Grafana versions earlier than 8.2 do not support library elements") |
| 64 | + json: dict = dict() |
| 65 | + # If the model contains a "meta" entry, use the "folderUid" entry if folder_uid isn't given |
| 66 | + if folder_uid is not None: |
| 67 | + json["folderUid"] = folder_uid |
| 68 | + elif "meta" in model and "folderUid" in model["meta"]: |
| 69 | + json["folderUid"] = model["meta"]["folderUid"] |
| 70 | + # If the model contains a "model" entry, use the "uid" entry if uid isn't given |
| 71 | + if uid is not None: |
| 72 | + json["uid"] = uid |
| 73 | + elif "model" in model and "uid" in model["model"]: |
| 74 | + json["uid"] = model["model"]["uid"] |
| 75 | + # If the model contains a "model" entry, use the "title" entry if title isn't given |
| 76 | + if name is not None: |
| 77 | + json["name"] = name |
| 78 | + elif "model" in model and "title" in model["model"]: |
| 79 | + json["name"] = model["model"]["title"] |
| 80 | + if "model" in model: |
| 81 | + json["model"] = model["model"] |
| 82 | + else: |
| 83 | + json["model"] = model |
| 84 | + # If the model contains a "kind" entry, use that instead of the specified kind |
| 85 | + if "kind" in model: |
| 86 | + json["kind"] = model["kind"] |
| 87 | + else: |
| 88 | + json["kind"] = kind |
| 89 | + create_element_path = "/library-elements" |
| 90 | + return await self.client.POST(create_element_path, json=json) |
| 91 | + |
| 92 | + async def update_library_element( |
| 93 | + self, uid: str, model: dict, name: str = None, kind: int = Panel, folder_uid: str = None, version: int = None |
| 94 | + ): |
| 95 | + """ |
| 96 | +
|
| 97 | + :param uid: |
| 98 | + :param model: |
| 99 | + :param name: |
| 100 | + :param kind: |
| 101 | + :param folder_uid: |
| 102 | + :return: |
| 103 | + """ |
| 104 | + if await self.api.version and Version(await self.api.version) < VERSION_8_2: |
| 105 | + raise DeprecationWarning("Grafana versions earlier than 8.2 do not support library elements") |
| 106 | + json: dict = dict() |
| 107 | + # If the model contains a "meta" entry, use the "folderUid" entry if folder_uid isn't given |
| 108 | + if folder_uid is not None: |
| 109 | + json["folderUid"] = folder_uid |
| 110 | + elif "meta" in model and "folderUid" in model["meta"]: |
| 111 | + json["folderUid"] = model["meta"]["folderUid"] |
| 112 | + json["uid"] = uid |
| 113 | + # If the model contains a "model" entry, use the "title" entry if title isn't given |
| 114 | + if name is not None: |
| 115 | + json["name"] = name |
| 116 | + elif "model" in model and "title" in model["model"]: |
| 117 | + json["name"] = model["model"]["title"] |
| 118 | + if "model" in model: |
| 119 | + json["model"] = model["model"] |
| 120 | + else: |
| 121 | + json["model"] = model |
| 122 | + # If the model contains a "kind" entry, use that instead of the specified kind |
| 123 | + if "kind" in model: |
| 124 | + json["kind"] = model["kind"] |
| 125 | + else: |
| 126 | + json["kind"] = kind |
| 127 | + if version is None: |
| 128 | + current_element = await self.get_library_element(uid) |
| 129 | + if "version" in current_element: |
| 130 | + json["version"] = current_element["version"] |
| 131 | + else: |
| 132 | + raise ValueError("version must be specified") |
| 133 | + else: |
| 134 | + json["version"] = version |
| 135 | + |
| 136 | + update_element_path = f"/library-elements/{uid}" |
| 137 | + return await self.client.PATCH(update_element_path, json=json) |
| 138 | + |
| 139 | + async def delete_library_element(self, element_uid: str) -> any: |
| 140 | + """ |
| 141 | +
|
| 142 | + :param element_uid: |
| 143 | + :return: |
| 144 | + """ |
| 145 | + if await self.api.version and Version(await self.api.version) < VERSION_8_2: |
| 146 | + raise DeprecationWarning("Grafana versions earlier than 8.2 do not support library elements") |
| 147 | + delete_element_path = f"/library-elements/{element_uid}" |
| 148 | + return await self.client.DELETE(delete_element_path) |
| 149 | + |
| 150 | + async def list_library_elements( |
| 151 | + self, |
| 152 | + search_string: str = None, |
| 153 | + kind: int = None, |
| 154 | + sort_direction: str = None, |
| 155 | + type_filter: str = None, |
| 156 | + exclude_uid: str = None, |
| 157 | + folder_filter: str = None, |
| 158 | + per_page: int = None, |
| 159 | + page: int = None, |
| 160 | + ) -> any: |
| 161 | + """ |
| 162 | +
|
| 163 | + :param search_string: |
| 164 | + :param kind: |
| 165 | + :param sort_direction: |
| 166 | + :param type_filter: |
| 167 | + :param exclude_uid: |
| 168 | + :param folder_filter: |
| 169 | + :param per_page: |
| 170 | + :param page: |
| 171 | +
|
| 172 | + :return: |
| 173 | + """ |
| 174 | + if await self.api.version and Version(await self.api.version) < VERSION_8_2: |
| 175 | + raise DeprecationWarning("Grafana versions earlier than 8.2 do not support library elements") |
| 176 | + list_elements_path = "/library-elements" |
| 177 | + params = [] |
| 178 | + |
| 179 | + if search_string is not None: |
| 180 | + params.append("searchString=%s" % search_string) |
| 181 | + if kind is not None: |
| 182 | + params.append("kind=%d" % kind) |
| 183 | + if sort_direction is not None: |
| 184 | + params.append("sortDirection=%s" % sort_direction) |
| 185 | + if type_filter is not None: |
| 186 | + params.append("typeFilter=%s" % type_filter) |
| 187 | + if exclude_uid is not None: |
| 188 | + params.append("excludeUid=%s" % exclude_uid) |
| 189 | + if folder_filter is not None: |
| 190 | + params.append("folderFilter=%s" % folder_filter) |
| 191 | + if per_page is not None: |
| 192 | + params.append("perPage=%d" % per_page) |
| 193 | + if page is not None: |
| 194 | + params.append("page=%d" % page) |
| 195 | + |
| 196 | + list_elements_path += "?" |
| 197 | + list_elements_path += "&".join(params) |
| 198 | + return await self.client.GET(list_elements_path) |
0 commit comments