|
| 1 | +# Licensed to Elasticsearch B.V. under one or more contributor |
| 2 | +# license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright |
| 4 | +# ownership. Elasticsearch B.V. licenses this file to you under |
| 5 | +# the Apache License, Version 2.0 (the "License"); you may |
| 6 | +# not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | + |
| 19 | +import logging |
| 20 | +import typing as t |
| 21 | + |
| 22 | +from elastic_transport import ApiResponse, HttpHeaders, ObjectApiResponse, Transport |
| 23 | +from elastic_transport.client_utils import DEFAULT, DefaultType |
| 24 | + |
| 25 | +from elasticsearch._async.client.utils import ( |
| 26 | + _TYPE_HOSTS, |
| 27 | + _base64_auth_header, |
| 28 | + _quote, |
| 29 | + _quote_query, |
| 30 | + _rewrite_parameters, |
| 31 | + client_node_configs, |
| 32 | +) |
| 33 | + |
| 34 | +logger = logging.getLogger("elasticsearch.kibana") |
| 35 | + |
| 36 | + |
| 37 | +def resolve_auth_headers( |
| 38 | + api_key: t.Union[DefaultType, None, t.Tuple[str, str], str] = DEFAULT, |
| 39 | +) -> HttpHeaders: |
| 40 | + headers = HttpHeaders() |
| 41 | + if api_key is not DEFAULT and api_key is not None: |
| 42 | + headers["authorization"] = f"ApiKey {_base64_auth_header(api_key)}" |
| 43 | + |
| 44 | + return headers |
| 45 | + |
| 46 | + |
| 47 | +class Kibana: |
| 48 | + def __init__( |
| 49 | + self, |
| 50 | + hosts: _TYPE_HOSTS, |
| 51 | + *, |
| 52 | + api_key: t.Optional[t.Union[str, t.Tuple[str, str]]] = None, |
| 53 | + basic_auth: t.Optional[t.Union[str, t.Tuple[str, str]]] = None, |
| 54 | + ) -> None: |
| 55 | + node_configs = client_node_configs(hosts, cloud_id=None) |
| 56 | + self._headers = resolve_auth_headers(api_key=api_key) |
| 57 | + self.transport = Transport(node_configs) |
| 58 | + |
| 59 | + def __enter__(self) -> "Kibana": |
| 60 | + try: |
| 61 | + self.transport._async_call() |
| 62 | + except AttributeError: |
| 63 | + pass |
| 64 | + return self |
| 65 | + |
| 66 | + def __exit__(self, *_: t.Any) -> None: |
| 67 | + self.close() |
| 68 | + |
| 69 | + def close(self) -> None: |
| 70 | + """Closes the Transport and all internal connections""" |
| 71 | + self.transport.close() |
| 72 | + |
| 73 | + def perform_request( |
| 74 | + self, |
| 75 | + method: str, |
| 76 | + path: str, |
| 77 | + *, |
| 78 | + params: t.Optional[t.Mapping[str, t.Any]] = None, |
| 79 | + headers: t.Optional[t.Mapping[str, str]] = None, |
| 80 | + body: t.Optional[t.Any] = None, |
| 81 | + endpoint_id, |
| 82 | + path_parts, |
| 83 | + ) -> ApiResponse[t.Any]: |
| 84 | + if headers: |
| 85 | + request_headers = self._headers.copy() |
| 86 | + request_headers.update(headers) |
| 87 | + else: |
| 88 | + request_headers = self._headers |
| 89 | + |
| 90 | + if params: |
| 91 | + target = f"{path}?{_quote_query(params)}" |
| 92 | + else: |
| 93 | + target = path |
| 94 | + |
| 95 | + meta, resp_body = self.transport.perform_request( |
| 96 | + method, target, headers=request_headers, body=body |
| 97 | + ) |
| 98 | + |
| 99 | + return ObjectApiResponse(body=resp_body, meta=meta) |
| 100 | + |
| 101 | + # AUTO-GENERATED-API-DEFINITIONS # |
| 102 | + |
| 103 | + @_rewrite_parameters() |
| 104 | + def system_status( |
| 105 | + self, |
| 106 | + *, |
| 107 | + v7format: t.Optional[bool] = None, |
| 108 | + v8format: t.Optional[bool] = None, |
| 109 | + ) -> ObjectApiResponse[t.Any]: |
| 110 | + """ |
| 111 | + Get Kibana's current status |
| 112 | +
|
| 113 | + :param v7format: Set to "true" to get the response in v7 format. |
| 114 | + :param v8format: Set to "true" to get the response in v8 format. |
| 115 | + """ |
| 116 | + __path_parts: t.Dict[str, str] = {} |
| 117 | + __path = "/api/status" |
| 118 | + __query: t.Dict[str, t.Any] = {} |
| 119 | + if v7format is not None: |
| 120 | + __query["v7format"] = v7format |
| 121 | + if v8format is not None: |
| 122 | + __query["v8format"] = v8format |
| 123 | + __headers = {"accept": "application/json"} |
| 124 | + return self.perform_request( # type: ignore[return-value] |
| 125 | + "GET", |
| 126 | + __path, |
| 127 | + params=__query, |
| 128 | + headers=__headers, |
| 129 | + endpoint_id="system_status", |
| 130 | + path_parts=__path_parts, |
| 131 | + ) |
0 commit comments