bleemeo 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- bleemeo/__init__.py +59 -0
- bleemeo/_authenticator.py +155 -0
- bleemeo/client.py +473 -0
- bleemeo/enums.py +113 -0
- bleemeo/exceptions.py +97 -0
- bleemeo/py.typed +0 -0
- bleemeo/resources.py +63 -0
- bleemeo-0.1.0.dist-info/LICENSE +201 -0
- bleemeo-0.1.0.dist-info/METADATA +106 -0
- bleemeo-0.1.0.dist-info/RECORD +11 -0
- bleemeo-0.1.0.dist-info/WHEEL +4 -0
bleemeo/__init__.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# Copyright 2015-2024 Bleemeo
|
|
2
|
+
#
|
|
3
|
+
# bleemeo.com an infrastructure monitoring solution in the Cloud
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may 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, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
|
|
17
|
+
from .client import Client
|
|
18
|
+
from .enums import (
|
|
19
|
+
AgentType,
|
|
20
|
+
ConfigItemSource,
|
|
21
|
+
ConfigItemType,
|
|
22
|
+
DisconnectionReason,
|
|
23
|
+
GloutonDiagnostics,
|
|
24
|
+
Graph,
|
|
25
|
+
ReportIncluded,
|
|
26
|
+
ReportPeriod,
|
|
27
|
+
Status,
|
|
28
|
+
TagType,
|
|
29
|
+
)
|
|
30
|
+
from .exceptions import (
|
|
31
|
+
APIError,
|
|
32
|
+
AuthenticationError,
|
|
33
|
+
BadRequestError,
|
|
34
|
+
ConfigurationError,
|
|
35
|
+
ResourceNotFoundError,
|
|
36
|
+
ThrottleError,
|
|
37
|
+
)
|
|
38
|
+
from .resources import Resource
|
|
39
|
+
|
|
40
|
+
__all__ = [
|
|
41
|
+
"Client",
|
|
42
|
+
"AgentType",
|
|
43
|
+
"ConfigItemSource",
|
|
44
|
+
"ConfigItemType",
|
|
45
|
+
"DisconnectionReason",
|
|
46
|
+
"GloutonDiagnostics",
|
|
47
|
+
"Graph",
|
|
48
|
+
"ReportIncluded",
|
|
49
|
+
"ReportPeriod",
|
|
50
|
+
"Status",
|
|
51
|
+
"TagType",
|
|
52
|
+
"APIError",
|
|
53
|
+
"AuthenticationError",
|
|
54
|
+
"BadRequestError",
|
|
55
|
+
"ConfigurationError",
|
|
56
|
+
"ResourceNotFoundError",
|
|
57
|
+
"ThrottleError",
|
|
58
|
+
"Resource",
|
|
59
|
+
]
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# Copyright 2015-2024 Bleemeo
|
|
2
|
+
#
|
|
3
|
+
# bleemeo.com an infrastructure monitoring solution in the Cloud
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may 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, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import threading
|
|
19
|
+
from urllib import parse
|
|
20
|
+
|
|
21
|
+
import requests
|
|
22
|
+
|
|
23
|
+
from .exceptions import APIError, AuthenticationError
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class Authenticator:
|
|
27
|
+
def __init__(
|
|
28
|
+
self,
|
|
29
|
+
api_url: str,
|
|
30
|
+
session: requests.Session,
|
|
31
|
+
oauth_id: str,
|
|
32
|
+
oauth_secret: str | None = None,
|
|
33
|
+
username: str | None = None,
|
|
34
|
+
password: str | None = None,
|
|
35
|
+
oauth_initial_refresh_token: str | None = None,
|
|
36
|
+
):
|
|
37
|
+
self.api_url = api_url
|
|
38
|
+
self.session = session
|
|
39
|
+
self.oauth_client_id = oauth_id
|
|
40
|
+
self.oauth_client_secret = oauth_secret
|
|
41
|
+
self.username = username
|
|
42
|
+
self.password = password
|
|
43
|
+
|
|
44
|
+
self._lock = threading.Lock()
|
|
45
|
+
self._current_token: str | None = None
|
|
46
|
+
self._current_refresh: str | None = oauth_initial_refresh_token
|
|
47
|
+
|
|
48
|
+
@property
|
|
49
|
+
def tokens(self) -> tuple[str, str]:
|
|
50
|
+
with self._lock:
|
|
51
|
+
if not isinstance(self._current_token, str):
|
|
52
|
+
self.__authenticate()
|
|
53
|
+
|
|
54
|
+
return str(self._current_token), str(self._current_refresh)
|
|
55
|
+
|
|
56
|
+
def get_token(self, force_refetch: bool = False) -> str:
|
|
57
|
+
with self._lock:
|
|
58
|
+
if not self._current_token or force_refetch:
|
|
59
|
+
self.__authenticate()
|
|
60
|
+
|
|
61
|
+
return str(self._current_token)
|
|
62
|
+
|
|
63
|
+
def __authenticate(self) -> None:
|
|
64
|
+
url = parse.urljoin(self.api_url, "/o/token/")
|
|
65
|
+
|
|
66
|
+
if self._current_refresh:
|
|
67
|
+
data = {
|
|
68
|
+
"grant_type": "refresh_token",
|
|
69
|
+
"refresh_token": self._current_refresh,
|
|
70
|
+
"client_id": self.oauth_client_id,
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if self.oauth_client_secret:
|
|
74
|
+
data["client_secret"] = self.oauth_client_secret
|
|
75
|
+
|
|
76
|
+
response = self.session.post(
|
|
77
|
+
url,
|
|
78
|
+
headers={
|
|
79
|
+
"X-Requested-With": "XMLHttpRequest",
|
|
80
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
81
|
+
},
|
|
82
|
+
data=data,
|
|
83
|
+
timeout=10,
|
|
84
|
+
)
|
|
85
|
+
if response.status_code == 200:
|
|
86
|
+
response_data = response.json()
|
|
87
|
+
self._current_token = response_data["access_token"]
|
|
88
|
+
self._current_refresh = response_data["refresh_token"]
|
|
89
|
+
|
|
90
|
+
return
|
|
91
|
+
|
|
92
|
+
if not self.username:
|
|
93
|
+
raise AuthenticationError(
|
|
94
|
+
"The provided initial refresh token is invalid.", response
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
data = {
|
|
98
|
+
"grant_type": "password",
|
|
99
|
+
"username": str(self.username),
|
|
100
|
+
"password": self.password or "",
|
|
101
|
+
"client_id": self.oauth_client_id,
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if self.oauth_client_secret:
|
|
105
|
+
data["client_secret"] = self.oauth_client_secret
|
|
106
|
+
|
|
107
|
+
response = self.session.post(
|
|
108
|
+
url,
|
|
109
|
+
headers={
|
|
110
|
+
"X-Requested-With": "XMLHttpRequest",
|
|
111
|
+
"Content-Type": "application/x-www-form-urlencoded",
|
|
112
|
+
},
|
|
113
|
+
data=data,
|
|
114
|
+
timeout=10,
|
|
115
|
+
)
|
|
116
|
+
if response.status_code != 200:
|
|
117
|
+
raise AuthenticationError(
|
|
118
|
+
f"Failed to retrieve OAuth, status={response.status_code}", response
|
|
119
|
+
)
|
|
120
|
+
|
|
121
|
+
response_data = response.json()
|
|
122
|
+
self._current_token = response_data["access_token"]
|
|
123
|
+
self._current_refresh = response_data["refresh_token"]
|
|
124
|
+
|
|
125
|
+
def logout(self) -> None:
|
|
126
|
+
with self._lock:
|
|
127
|
+
if not self._current_refresh:
|
|
128
|
+
return
|
|
129
|
+
|
|
130
|
+
url = parse.urljoin(self.api_url, "/o/revoke_token/")
|
|
131
|
+
data = {
|
|
132
|
+
"token": self._current_refresh,
|
|
133
|
+
"client_id": self.oauth_client_id,
|
|
134
|
+
"token_type_hint": "refresh_token",
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
if self.oauth_client_secret:
|
|
138
|
+
data["client_secret"] = self.oauth_client_secret
|
|
139
|
+
|
|
140
|
+
response = self.session.post(
|
|
141
|
+
url,
|
|
142
|
+
headers={
|
|
143
|
+
"X-Requested-With": "XMLHttpRequest",
|
|
144
|
+
"Content-type": "application/x-www-form-urlencoded",
|
|
145
|
+
},
|
|
146
|
+
data=data,
|
|
147
|
+
timeout=10,
|
|
148
|
+
)
|
|
149
|
+
if response.status_code != 200:
|
|
150
|
+
raise APIError(
|
|
151
|
+
f"Failed to revoke token, status={response.status_code}", response
|
|
152
|
+
)
|
|
153
|
+
|
|
154
|
+
self._current_token = None
|
|
155
|
+
self._current_refresh = None
|
bleemeo/client.py
ADDED
|
@@ -0,0 +1,473 @@
|
|
|
1
|
+
# Copyright 2015-2024 Bleemeo
|
|
2
|
+
#
|
|
3
|
+
# bleemeo.com an infrastructure monitoring solution in the Cloud
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may 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, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
import math
|
|
19
|
+
import os
|
|
20
|
+
import time
|
|
21
|
+
from collections.abc import Iterator, Sequence
|
|
22
|
+
from datetime import datetime, timedelta
|
|
23
|
+
from typing import Any
|
|
24
|
+
from urllib import parse
|
|
25
|
+
|
|
26
|
+
from requests import PreparedRequest, Request, Response, Session
|
|
27
|
+
|
|
28
|
+
from ._authenticator import Authenticator
|
|
29
|
+
from .exceptions import (
|
|
30
|
+
APIError,
|
|
31
|
+
AuthenticationError,
|
|
32
|
+
BadRequestError,
|
|
33
|
+
ConfigurationError,
|
|
34
|
+
ResourceNotFoundError,
|
|
35
|
+
ThrottleError,
|
|
36
|
+
)
|
|
37
|
+
from .resources import Resource
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class Client:
|
|
41
|
+
"""
|
|
42
|
+
A Client is a helper to interact with the Bleemeo API,
|
|
43
|
+
providing methods to retrieve, list, create, update and delete resources.
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
DEFAULT_ENDPOINT = "https://api.bleemeo.com"
|
|
47
|
+
DEFAULT_OAUTH_CLIENT_ID = "1fc6de3e-8750-472e-baea-3ba22bb4eb56"
|
|
48
|
+
DEFAULT_THROTTLE_MAX_AUTO_RETRY_DELAY = 60 # seconds
|
|
49
|
+
DEFAULT_USER_AGENT = "Bleemeo Python Client"
|
|
50
|
+
|
|
51
|
+
def __init__(
|
|
52
|
+
self,
|
|
53
|
+
*,
|
|
54
|
+
api_url: str | None = None,
|
|
55
|
+
account_id: str | None = None,
|
|
56
|
+
username: str | None = None,
|
|
57
|
+
password: str | None = None,
|
|
58
|
+
oauth_client_id: str | None = None,
|
|
59
|
+
oauth_client_secret: str | None = None,
|
|
60
|
+
oauth_initial_refresh_token: str | None = None,
|
|
61
|
+
custom_headers: dict[str, Any] | None = None,
|
|
62
|
+
throttle_max_auto_retry_delay: int
|
|
63
|
+
| None = DEFAULT_THROTTLE_MAX_AUTO_RETRY_DELAY,
|
|
64
|
+
load_from_env: bool = False,
|
|
65
|
+
):
|
|
66
|
+
"""
|
|
67
|
+
The credentials (username or initial refresh token) are the only mandatory parameters.
|
|
68
|
+
They can alternatively be provided as environment variables, by setting `load_from_env` to True.
|
|
69
|
+
See the [README](https://github.com/bleemeo/bleemeo-python/#configuration) for extended details.
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
api_url (str | None): Base URL to access the Bleemeo API.
|
|
73
|
+
account_id (str | None): Your Bleemeo account ID.
|
|
74
|
+
username (str | None): Your Bleemeo username.
|
|
75
|
+
password (str | None): Your Bleemeo password.
|
|
76
|
+
oauth_client_id (str | None): Your Bleemeo OAuth client ID.
|
|
77
|
+
oauth_client_secret (str | None): Your Bleemeo OAuth client secret.
|
|
78
|
+
oauth_initial_refresh_token (str | None): An initial OAuth refresh token
|
|
79
|
+
(as an alternative to username/password credentials).
|
|
80
|
+
custom_headers (dict[str, Any]): Additional headers to pass to the Bleemeo API.
|
|
81
|
+
throttle_max_auto_retry_delay (int | None): Maximum number of seconds to wait
|
|
82
|
+
for retrying a throttled request (can be set to 0 or None not to retry at all).
|
|
83
|
+
load_from_env (bool): Whether to load environment variables as Client parameters.
|
|
84
|
+
|
|
85
|
+
Raises:
|
|
86
|
+
ConfigurationError: If neither credentials nor initial refresh token are provided.
|
|
87
|
+
"""
|
|
88
|
+
if load_from_env:
|
|
89
|
+
api_url = api_url or os.environ.get("BLEEMEO_API_URL")
|
|
90
|
+
account_id = account_id or os.environ.get("BLEEMEO_ACCOUNT_ID")
|
|
91
|
+
username = username or os.environ.get("BLEEMEO_USER")
|
|
92
|
+
password = password or os.environ.get("BLEEMEO_PASSWORD")
|
|
93
|
+
oauth_client_id = oauth_client_id or os.environ.get(
|
|
94
|
+
"BLEEMEO_OAUTH_CLIENT_ID"
|
|
95
|
+
)
|
|
96
|
+
oauth_client_secret = oauth_client_secret or os.environ.get(
|
|
97
|
+
"BLEEMEO_OAUTH_CLIENT_SECRET"
|
|
98
|
+
)
|
|
99
|
+
oauth_initial_refresh_token = oauth_initial_refresh_token or os.environ.get(
|
|
100
|
+
"BLEEMEO_OAUTH_INITIAL_REFRESH_TOKEN"
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
if not username and not oauth_initial_refresh_token:
|
|
104
|
+
raise ConfigurationError(
|
|
105
|
+
"Either a username or an initial OAuth refresh token must be provided."
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
self.api_url = api_url or self.DEFAULT_ENDPOINT
|
|
109
|
+
self.username = username
|
|
110
|
+
self.password = password
|
|
111
|
+
self.oauth_client_id = oauth_client_id or self.DEFAULT_OAUTH_CLIENT_ID
|
|
112
|
+
self.oauth_client_secret = oauth_client_secret
|
|
113
|
+
self.oauth_initial_refresh_token = oauth_initial_refresh_token
|
|
114
|
+
self.throttle_max_auto_retry_delay = throttle_max_auto_retry_delay
|
|
115
|
+
self.session = Session()
|
|
116
|
+
self.session.headers = {
|
|
117
|
+
"User-Agent": self.DEFAULT_USER_AGENT,
|
|
118
|
+
"Accept": "application/json",
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
if account_id:
|
|
122
|
+
self.session.headers["X-Bleemeo-Account"] = account_id
|
|
123
|
+
if custom_headers:
|
|
124
|
+
self.session.headers.update(custom_headers)
|
|
125
|
+
|
|
126
|
+
self._throttle_deadline = datetime.min
|
|
127
|
+
self._authenticator = Authenticator(
|
|
128
|
+
self.api_url,
|
|
129
|
+
self.session,
|
|
130
|
+
self.oauth_client_id,
|
|
131
|
+
self.oauth_client_secret,
|
|
132
|
+
self.username,
|
|
133
|
+
self.password,
|
|
134
|
+
self.oauth_initial_refresh_token,
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
def __enter__(self) -> Client:
|
|
138
|
+
return self
|
|
139
|
+
|
|
140
|
+
def __exit__(self, *exc_info: Any) -> None:
|
|
141
|
+
self.logout()
|
|
142
|
+
|
|
143
|
+
@property
|
|
144
|
+
def account_id(self) -> str | None:
|
|
145
|
+
return (
|
|
146
|
+
str(self.session.headers["X-Bleemeo-Account"])
|
|
147
|
+
if "X-Bleemeo-Account" in self.session.headers
|
|
148
|
+
else None
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
@account_id.setter
|
|
152
|
+
def account_id(self, account_id: str | None) -> None:
|
|
153
|
+
if account_id:
|
|
154
|
+
self.session.headers["X-Bleemeo-Account"] = account_id
|
|
155
|
+
else:
|
|
156
|
+
del self.session.headers["X-Bleemeo-Account"]
|
|
157
|
+
|
|
158
|
+
@property
|
|
159
|
+
def tokens(self) -> tuple[str, str]:
|
|
160
|
+
"""tokens returns a tuple containing the current access and refresh tokens."""
|
|
161
|
+
return self._authenticator.tokens
|
|
162
|
+
|
|
163
|
+
def logout(self) -> None:
|
|
164
|
+
"""logout revokes the OAuth token, preventing it from being reused."""
|
|
165
|
+
self._authenticator.logout()
|
|
166
|
+
self.session.close()
|
|
167
|
+
|
|
168
|
+
def _build_url(self, *parts: str) -> str:
|
|
169
|
+
url = self.api_url
|
|
170
|
+
for part in parts:
|
|
171
|
+
url = parse.urljoin(url, part)
|
|
172
|
+
if not url.endswith("/"):
|
|
173
|
+
url = url + "/"
|
|
174
|
+
return url
|
|
175
|
+
|
|
176
|
+
def _send(self, req: PreparedRequest, settings: Any) -> Response:
|
|
177
|
+
"""_send wraps `Session.send()` to make it easily mockable."""
|
|
178
|
+
return self.session.send(req, **settings)
|
|
179
|
+
|
|
180
|
+
def _do_request(
|
|
181
|
+
self, request: Request, authenticated: bool, is_retry: bool = False
|
|
182
|
+
) -> Response:
|
|
183
|
+
if request.headers is None:
|
|
184
|
+
request.headers = {}
|
|
185
|
+
|
|
186
|
+
if "Content-Type" not in request.headers and request.json:
|
|
187
|
+
request.headers["Content-Type"] = "application/json"
|
|
188
|
+
|
|
189
|
+
if authenticated:
|
|
190
|
+
request.headers.update(
|
|
191
|
+
{
|
|
192
|
+
"Authorization": f"Bearer {self._authenticator.get_token(force_refetch=is_retry)}"
|
|
193
|
+
}
|
|
194
|
+
)
|
|
195
|
+
|
|
196
|
+
prep = self.session.prepare_request(request)
|
|
197
|
+
# Merge environment settings into session
|
|
198
|
+
settings = self.session.merge_environment_settings(
|
|
199
|
+
prep.url, {}, None, None, None
|
|
200
|
+
)
|
|
201
|
+
|
|
202
|
+
resp = self._send(prep, settings)
|
|
203
|
+
if resp.status_code == 401 and authenticated and not is_retry:
|
|
204
|
+
resp = self._do_request(request, authenticated=True, is_retry=True)
|
|
205
|
+
|
|
206
|
+
return resp
|
|
207
|
+
|
|
208
|
+
def _do_with_error_handling(self, authenticated: bool, req: Request) -> Response:
|
|
209
|
+
response = self._do_request(req, authenticated)
|
|
210
|
+
if response.status_code >= 400:
|
|
211
|
+
if response.status_code == 400: # Bad Request
|
|
212
|
+
raise BadRequestError(response)
|
|
213
|
+
if response.status_code == 401: # Unauthorized
|
|
214
|
+
raise AuthenticationError(
|
|
215
|
+
f"Authentication failed on {req.url}", response
|
|
216
|
+
)
|
|
217
|
+
if response.status_code == 404: # Not Found
|
|
218
|
+
raise ResourceNotFoundError(req.url, response)
|
|
219
|
+
if response.status_code == 429: # Too Many Requests
|
|
220
|
+
throttle_error = ThrottleError(response)
|
|
221
|
+
self._throttle_deadline = throttle_error.throttle_deadline
|
|
222
|
+
raise throttle_error
|
|
223
|
+
|
|
224
|
+
raise APIError(
|
|
225
|
+
f"Request {req.method} on {req.url} failed with status {response.status_code}",
|
|
226
|
+
response,
|
|
227
|
+
)
|
|
228
|
+
return response
|
|
229
|
+
|
|
230
|
+
def do(
|
|
231
|
+
self,
|
|
232
|
+
method: str,
|
|
233
|
+
url: str,
|
|
234
|
+
authenticated: bool = True,
|
|
235
|
+
params: dict[str, Any] | None = None,
|
|
236
|
+
data: Any | None = None,
|
|
237
|
+
) -> Response:
|
|
238
|
+
"""
|
|
239
|
+
do is a lower-level method to build and execute the request according to the given parameters.
|
|
240
|
+
It raises errors depending on the received response, and handles throttled requests retry.
|
|
241
|
+
|
|
242
|
+
When possible, prefer the higher-level get, get_page, count, iterate, create, update and delete.
|
|
243
|
+
|
|
244
|
+
Args:
|
|
245
|
+
method (str): The HTTP method to execute the request as.
|
|
246
|
+
url (str): The (relative) URL on which to execute the request.
|
|
247
|
+
authenticated (bool): Whether the request should be authenticated.
|
|
248
|
+
params (dict[str, Any]): The parameters to pass to the request.
|
|
249
|
+
data (Any | None): The body of the request in case of a POST or a PATCH.
|
|
250
|
+
|
|
251
|
+
Returns:
|
|
252
|
+
Response: The response to the request.
|
|
253
|
+
|
|
254
|
+
Raises:
|
|
255
|
+
AuthenticationError: If the authentication fails.
|
|
256
|
+
ThrottleError: If too many requests are sent too close.
|
|
257
|
+
APIError: When receiving a non-successful status code not covered by the above exceptions.
|
|
258
|
+
"""
|
|
259
|
+
time_to_wait = self._throttle_deadline - datetime.now()
|
|
260
|
+
if time_to_wait > timedelta(seconds=0):
|
|
261
|
+
raise ThrottleError.prevent(math.ceil(time_to_wait.total_seconds()))
|
|
262
|
+
|
|
263
|
+
req = Request(
|
|
264
|
+
method=method,
|
|
265
|
+
url=self._build_url(url),
|
|
266
|
+
json=data,
|
|
267
|
+
params=params,
|
|
268
|
+
)
|
|
269
|
+
|
|
270
|
+
try:
|
|
271
|
+
response = self._do_with_error_handling(authenticated, req)
|
|
272
|
+
except ThrottleError as throttle_error:
|
|
273
|
+
if (
|
|
274
|
+
not self.throttle_max_auto_retry_delay
|
|
275
|
+
or throttle_error.delay_seconds > self.throttle_max_auto_retry_delay
|
|
276
|
+
):
|
|
277
|
+
raise throttle_error
|
|
278
|
+
|
|
279
|
+
time.sleep(throttle_error.delay_seconds)
|
|
280
|
+
|
|
281
|
+
response = self._do_with_error_handling(authenticated, req)
|
|
282
|
+
|
|
283
|
+
return response
|
|
284
|
+
|
|
285
|
+
def get(
|
|
286
|
+
self, resource: Resource | str, id: str, fields: Sequence[str] | None = None
|
|
287
|
+
) -> Response:
|
|
288
|
+
"""
|
|
289
|
+
get retrieves the resource with the given id with only the given fields, if any.
|
|
290
|
+
|
|
291
|
+
Args:
|
|
292
|
+
resource (Resource | str): The kind of resource to retrieve.
|
|
293
|
+
id (str): The id of the resource to retrieve.
|
|
294
|
+
fields (Sequence[str] | None, optional): Specific fields to retrieve. Defaults to None (only the default ones).
|
|
295
|
+
|
|
296
|
+
Returns:
|
|
297
|
+
Response: The response to the request.
|
|
298
|
+
|
|
299
|
+
Raises:
|
|
300
|
+
AuthenticationError: If the authentication fails.
|
|
301
|
+
ThrottleError: If too many requests are sent too close.
|
|
302
|
+
APIError: When receiving a non-successful status code not covered by the above exceptions.
|
|
303
|
+
"""
|
|
304
|
+
url = parse.urljoin(
|
|
305
|
+
resource.value if isinstance(resource, Resource) else resource, id
|
|
306
|
+
)
|
|
307
|
+
params = {"fields": ",".join(fields)} if fields else None
|
|
308
|
+
|
|
309
|
+
return self.do("GET", url, params=params)
|
|
310
|
+
|
|
311
|
+
def get_page(
|
|
312
|
+
self,
|
|
313
|
+
resource: Resource | str,
|
|
314
|
+
*,
|
|
315
|
+
page: int = 1,
|
|
316
|
+
page_size: int = 25,
|
|
317
|
+
params: dict[str, Any] | None = None,
|
|
318
|
+
) -> Response:
|
|
319
|
+
"""
|
|
320
|
+
get_page retrieves a given-page-sized list of resources matching the given params at the given page.
|
|
321
|
+
To collect all the resources matching some params (i.e., instead of querying all pages one by one),
|
|
322
|
+
prefer using the `iterate()` method which is faster.
|
|
323
|
+
|
|
324
|
+
Args:
|
|
325
|
+
resource (Resource | str): The kind of resource to retrieve.
|
|
326
|
+
page (int): The number of the page to retrieve.
|
|
327
|
+
page_size (int): The size of the page to retrieve.
|
|
328
|
+
params (dict[str, Any]): The parameters the resources should match.
|
|
329
|
+
|
|
330
|
+
Returns:
|
|
331
|
+
Response: The response to the request.
|
|
332
|
+
|
|
333
|
+
Raises:
|
|
334
|
+
AuthenticationError: If the authentication fails.
|
|
335
|
+
ThrottleError: If too many requests are sent too close.
|
|
336
|
+
APIError: When receiving a non-successful status code not covered by the above exceptions.
|
|
337
|
+
"""
|
|
338
|
+
url = resource.value if isinstance(resource, Resource) else resource
|
|
339
|
+
params = params.copy() if params else {}
|
|
340
|
+
params.update({"page": page, "page_size": page_size})
|
|
341
|
+
|
|
342
|
+
return self.do("GET", url, params=params)
|
|
343
|
+
|
|
344
|
+
def count(
|
|
345
|
+
self, resource: Resource | str, params: dict[str, Any] | None = None
|
|
346
|
+
) -> int:
|
|
347
|
+
"""
|
|
348
|
+
count returns the number of resources matching the given params.
|
|
349
|
+
|
|
350
|
+
Args:
|
|
351
|
+
resource (Resource | str): The kind of resource to count.
|
|
352
|
+
params (dict[str, Any]): The parameters the resources should match.
|
|
353
|
+
|
|
354
|
+
Returns:
|
|
355
|
+
Response: The response to the request.
|
|
356
|
+
|
|
357
|
+
Raises:
|
|
358
|
+
AuthenticationError: If the authentication fails.
|
|
359
|
+
ThrottleError: If too many requests are sent too close.
|
|
360
|
+
APIError: When receiving a non-successful status code not covered by the above exceptions.
|
|
361
|
+
"""
|
|
362
|
+
resp = self.get_page(resource, page=1, page_size=0, params=params)
|
|
363
|
+
|
|
364
|
+
return int(resp.json()["count"])
|
|
365
|
+
|
|
366
|
+
def iterate(
|
|
367
|
+
self, resource: Resource | str, params: dict[str, Any] | None = None
|
|
368
|
+
) -> Iterator[dict[str, Any]]:
|
|
369
|
+
"""
|
|
370
|
+
iterate yields the resources of the given kind matching the given params.
|
|
371
|
+
|
|
372
|
+
Args:
|
|
373
|
+
resource (Resource | str): The kind of resource to iterate.
|
|
374
|
+
params (dict[str, Any]): The parameters the resources should match.
|
|
375
|
+
|
|
376
|
+
Raises:
|
|
377
|
+
AuthenticationError: If the authentication fails.
|
|
378
|
+
ThrottleError: If too many requests are sent too close.
|
|
379
|
+
APIError: When receiving a non-successful status code not covered by the above exceptions.
|
|
380
|
+
"""
|
|
381
|
+
params = params.copy() if params else {}
|
|
382
|
+
params.update({"page_size": 2500})
|
|
383
|
+
|
|
384
|
+
next_url: str | None = self._build_url(
|
|
385
|
+
resource.value if isinstance(resource, Resource) else resource
|
|
386
|
+
)
|
|
387
|
+
while next_url is not None:
|
|
388
|
+
resp = self.do("GET", next_url, params=params)
|
|
389
|
+
data = resp.json()
|
|
390
|
+
yield from data.get("results", [])
|
|
391
|
+
|
|
392
|
+
next_url = data.get("next", None)
|
|
393
|
+
params = None # Avoid duplicating the params, which are already given back in the next URL.
|
|
394
|
+
|
|
395
|
+
def create(
|
|
396
|
+
self, resource: Resource | str, data: Any, fields: Sequence[str] | None = None
|
|
397
|
+
) -> Response:
|
|
398
|
+
"""
|
|
399
|
+
create creates a new resource of the given kind with the given data.
|
|
400
|
+
Fields expected to be returned can be specified as varargs.
|
|
401
|
+
|
|
402
|
+
Args:
|
|
403
|
+
resource (Resource | str): The kind of resource to create.
|
|
404
|
+
data (Any): The body of the POST request.
|
|
405
|
+
fields (Sequence[str] | None): Specific fields to retrieve. Defaults to None (only the default ones).
|
|
406
|
+
|
|
407
|
+
Returns:
|
|
408
|
+
Response: The response to the request.
|
|
409
|
+
|
|
410
|
+
Raises:
|
|
411
|
+
AuthenticationError: If the authentication fails.
|
|
412
|
+
ThrottleError: If too many requests are sent too close.
|
|
413
|
+
APIError: When receiving a non-successful status code not covered by the above exceptions.
|
|
414
|
+
"""
|
|
415
|
+
url = resource.value if isinstance(resource, Resource) else resource
|
|
416
|
+
params = {"fields": ",".join(fields)} if fields else None
|
|
417
|
+
|
|
418
|
+
return self.do("POST", url, data=data, params=params)
|
|
419
|
+
|
|
420
|
+
def update(
|
|
421
|
+
self,
|
|
422
|
+
resource: Resource | str,
|
|
423
|
+
id: str,
|
|
424
|
+
data: Any,
|
|
425
|
+
fields: Sequence[str] | None = None,
|
|
426
|
+
) -> Response:
|
|
427
|
+
"""
|
|
428
|
+
update updates the resource of the given kind and id with the given data.
|
|
429
|
+
Since the request is sent as a PATCH, only the fields specified in the body (data) will be updated.
|
|
430
|
+
Fields expected to be returned can be specified as varargs.
|
|
431
|
+
|
|
432
|
+
Args:
|
|
433
|
+
resource (Resource | str): The kind of resource to update.
|
|
434
|
+
id (str): The ID of the resource to update.
|
|
435
|
+
data (Any): The body of the PATCH request.
|
|
436
|
+
fields (Sequence[str] | None): Specific fields to retrieve. Defaults to None (only the default ones).
|
|
437
|
+
|
|
438
|
+
Returns:
|
|
439
|
+
Response: The response to the request.
|
|
440
|
+
|
|
441
|
+
Raises:
|
|
442
|
+
AuthenticationError: If the authentication fails.
|
|
443
|
+
ThrottleError: If too many requests are sent too close.
|
|
444
|
+
APIError: When receiving a non-successful status code not covered by the above exceptions.
|
|
445
|
+
"""
|
|
446
|
+
url = parse.urljoin(
|
|
447
|
+
resource.value if isinstance(resource, Resource) else resource, id
|
|
448
|
+
)
|
|
449
|
+
params = {"fields": ",".join(fields)} if fields else None
|
|
450
|
+
|
|
451
|
+
return self.do("PATCH", url, data=data, params=params)
|
|
452
|
+
|
|
453
|
+
def delete(self, resource: Resource | str, id: str) -> Response:
|
|
454
|
+
"""
|
|
455
|
+
delete deletes the resource of the given kind and id from the server.
|
|
456
|
+
|
|
457
|
+
Args:
|
|
458
|
+
resource (Resource | str): The kind of resource to delete.
|
|
459
|
+
id (str): The ID of the resource to delete.
|
|
460
|
+
|
|
461
|
+
Returns:
|
|
462
|
+
Response: The response to the request.
|
|
463
|
+
|
|
464
|
+
Raises:
|
|
465
|
+
AuthenticationError: If the authentication fails.
|
|
466
|
+
ThrottleError: If too many requests are sent too close.
|
|
467
|
+
APIError: When receiving a non-successful status code not covered by the above exceptions.
|
|
468
|
+
"""
|
|
469
|
+
url = parse.urljoin(
|
|
470
|
+
resource.value if isinstance(resource, Resource) else resource, id
|
|
471
|
+
)
|
|
472
|
+
|
|
473
|
+
return self.do("DELETE", url)
|
bleemeo/enums.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# Copyright 2015-2024 Bleemeo
|
|
2
|
+
#
|
|
3
|
+
# bleemeo.com an infrastructure monitoring solution in the Cloud
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may 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, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class AgentType:
|
|
19
|
+
AWS_ACCOUNT = "aws_account"
|
|
20
|
+
AWS_TRUSTED_ADVISOR = "aws_trusted_advisor"
|
|
21
|
+
AWS_DYNAMODB = "aws_dynamodb"
|
|
22
|
+
AWS_EC2 = "aws_ec2"
|
|
23
|
+
AWS_ELB = "aws_elb"
|
|
24
|
+
AWS_RDS = "aws_rds"
|
|
25
|
+
AWS_S3 = "aws_s3"
|
|
26
|
+
AGENT = "agent"
|
|
27
|
+
MONITOR = "connection_check"
|
|
28
|
+
SNMP = "snmp"
|
|
29
|
+
K8S = "kubernetes"
|
|
30
|
+
VSPHERE_CLUSTER = "vsphere_cluster"
|
|
31
|
+
VSPHERE_HOST = "vsphere_host"
|
|
32
|
+
VSPHERE_VM = "vsphere_vm"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class DisconnectionReason:
|
|
36
|
+
CLEAN_SHUTDOWN = 1
|
|
37
|
+
AGENT_TIMEOUT = 2
|
|
38
|
+
AGENT_AUTO_UPGRADE = 3
|
|
39
|
+
AGENT_UPGRADE = 4
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class GloutonDiagnostics:
|
|
43
|
+
CRASH = 0
|
|
44
|
+
ON_DEMAND = 1
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class Graph:
|
|
48
|
+
LINE = 0
|
|
49
|
+
STACK = 1
|
|
50
|
+
PIE = 2
|
|
51
|
+
GAUGE = 3
|
|
52
|
+
AVAILABILITY_TIMELINE = 4
|
|
53
|
+
NUMBER = 5
|
|
54
|
+
STATUS = 6
|
|
55
|
+
SNMP_STATUS = 7
|
|
56
|
+
TEXT = 8
|
|
57
|
+
IMAGE = 9
|
|
58
|
+
HEATMAP_STATUS = 10
|
|
59
|
+
BAR = 11
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
class ReportPeriod:
|
|
63
|
+
WEEKLY = 0
|
|
64
|
+
MONTHLY = 1
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
class ReportIncluded:
|
|
68
|
+
NONE = 0
|
|
69
|
+
PARTIAL = 1
|
|
70
|
+
FULL = 2
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
class ConfigItemSource:
|
|
74
|
+
UNKNOWN = 0
|
|
75
|
+
DEFAULT = 1
|
|
76
|
+
FILE = 2
|
|
77
|
+
ENV = 3
|
|
78
|
+
API = 4
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class ConfigItemType:
|
|
82
|
+
ANY = 0
|
|
83
|
+
INT = 1
|
|
84
|
+
FLOAT = 2
|
|
85
|
+
BOOL = 3
|
|
86
|
+
STRING = 4
|
|
87
|
+
LIST_STR = 10
|
|
88
|
+
LIST_INT = 11
|
|
89
|
+
MAP_STR_STR = 20
|
|
90
|
+
MAP_STR_INT = 21
|
|
91
|
+
THRESHOLDS = 30
|
|
92
|
+
SERVICES = 31
|
|
93
|
+
NAME_INSTANCES = 32
|
|
94
|
+
BLACKBOX_TARGETS = 33
|
|
95
|
+
PROMETHEUS_TARGETS = 34
|
|
96
|
+
TYPE_SNMP_TARGETS = 35
|
|
97
|
+
TYPE_LOG_INPUTS = 36
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
class Status:
|
|
101
|
+
OK = 0
|
|
102
|
+
WARNING = 1
|
|
103
|
+
CRITICAL = 2
|
|
104
|
+
UNKNOWN = 3
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
class TagType:
|
|
108
|
+
AUTOMATIC_API = 0
|
|
109
|
+
CREATED_BY_GLOUTON = 1
|
|
110
|
+
CREATED_BY_FRONTEND = 2
|
|
111
|
+
AUTOMATIC_GLOUTON = 3
|
|
112
|
+
AUTOMATIC_API_SERVICE = 4
|
|
113
|
+
NO_TYPE = 10
|
bleemeo/exceptions.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# Copyright 2015-2024 Bleemeo
|
|
2
|
+
#
|
|
3
|
+
# bleemeo.com an infrastructure monitoring solution in the Cloud
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may 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, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
from __future__ import annotations
|
|
17
|
+
|
|
18
|
+
from datetime import datetime, timedelta
|
|
19
|
+
|
|
20
|
+
import requests
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class ConfigurationError(Exception):
|
|
24
|
+
"""A ConfigurationError is raised when a misconfiguration is detected."""
|
|
25
|
+
|
|
26
|
+
pass
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class APIError(Exception):
|
|
30
|
+
"""
|
|
31
|
+
An APIError is raised when the API returns a non-successful status code.
|
|
32
|
+
It serves as superclass for AuthenticationError and ThrottleError.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
def __init__(self, message: str, response: requests.Response) -> None:
|
|
36
|
+
if response.status_code:
|
|
37
|
+
message = f"{response.status_code}: {message}"
|
|
38
|
+
|
|
39
|
+
super().__init__(message)
|
|
40
|
+
self.response = response
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
class BadRequestError(APIError):
|
|
44
|
+
"""A BadRequestError is raised when the API returns a 400 - Bad Request status code."""
|
|
45
|
+
|
|
46
|
+
def __init__(self, response: requests.Response) -> None:
|
|
47
|
+
fields = response.json()
|
|
48
|
+
message = "Bad request:\n" + "\n".join(
|
|
49
|
+
[f"- {field}: {' / '.join(errors)}" for field, errors in fields.items()]
|
|
50
|
+
)
|
|
51
|
+
super().__init__(message, response)
|
|
52
|
+
self.error_fields = fields
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
class AuthenticationError(APIError):
|
|
56
|
+
"""An AuthenticationError is raised when the API returns a 401 - Unauthorized status code."""
|
|
57
|
+
|
|
58
|
+
pass
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
class ResourceNotFoundError(APIError):
|
|
62
|
+
"""A ResourceNotFoundError is raised when the API returns a 404 - Not Found status code."""
|
|
63
|
+
|
|
64
|
+
def __init__(self, url: str, response: requests.Response) -> None:
|
|
65
|
+
super().__init__(f"Resource {url} not found", response)
|
|
66
|
+
self.url = url
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class ThrottleError(APIError):
|
|
70
|
+
"""
|
|
71
|
+
A ThrottleError is raised when the API returns a 429 - Too Many Requests status code.
|
|
72
|
+
It contains the delay and the deadline until a new request can safely be sent.
|
|
73
|
+
If the error is not raised from the throttle-prevention mechanism, it also contains the API response.
|
|
74
|
+
"""
|
|
75
|
+
|
|
76
|
+
def __init__(
|
|
77
|
+
self,
|
|
78
|
+
response: requests.Response | None = None,
|
|
79
|
+
delay_seconds: int | None = None,
|
|
80
|
+
) -> None:
|
|
81
|
+
ds = 30
|
|
82
|
+
if delay_seconds is not None:
|
|
83
|
+
ds = delay_seconds
|
|
84
|
+
elif response is not None and "Retry-After":
|
|
85
|
+
ds = int(response.headers.get("Retry-After") or 30)
|
|
86
|
+
|
|
87
|
+
super().__init__(
|
|
88
|
+
f"Throttle error: request must be retried after {ds}s.",
|
|
89
|
+
response or requests.Response(),
|
|
90
|
+
)
|
|
91
|
+
self.delay_seconds = ds
|
|
92
|
+
self.throttle_deadline = datetime.now() + timedelta(seconds=ds)
|
|
93
|
+
|
|
94
|
+
@classmethod
|
|
95
|
+
def prevent(cls, delay_seconds: int) -> ThrottleError:
|
|
96
|
+
"""prevent can be used to build a ThrottleError without actually sending the request to the API."""
|
|
97
|
+
return cls(response=None, delay_seconds=delay_seconds)
|
bleemeo/py.typed
ADDED
|
File without changes
|
bleemeo/resources.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
# Copyright 2015-2024 Bleemeo
|
|
2
|
+
#
|
|
3
|
+
# bleemeo.com an infrastructure monitoring solution in the Cloud
|
|
4
|
+
#
|
|
5
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
6
|
+
# you may 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, software
|
|
12
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
13
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
14
|
+
# See the License for the specific language governing permissions and
|
|
15
|
+
# limitations under the License.
|
|
16
|
+
|
|
17
|
+
from enum import Enum
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class Resource(Enum):
|
|
21
|
+
"""A Resource represents a route to a model on the Bleemeo API."""
|
|
22
|
+
|
|
23
|
+
ACCOUNT = "v1/account/"
|
|
24
|
+
ACCOUNT_CONFIG = "v1/accountconfig/"
|
|
25
|
+
AGENT_CONFIG = "v1/agentconfig/"
|
|
26
|
+
APPLICATION = "v1/application/"
|
|
27
|
+
AUDITLOG = "v1/auditlog/"
|
|
28
|
+
AGENT = "v1/agent/"
|
|
29
|
+
AGENT_FACT = "v1/agentfact/"
|
|
30
|
+
AGENT_TYPE = "v1/agenttype/"
|
|
31
|
+
AWS_INTEGRATION = "v1/awsintegration/"
|
|
32
|
+
CONTACTS_GROUP = "v1/contactsgroup/"
|
|
33
|
+
CONTAINER = "v1/container/"
|
|
34
|
+
DASHBOARD = "v1/dashboard/"
|
|
35
|
+
EVENT = "v1/event/"
|
|
36
|
+
GLOUTON_CONFIG_ITEM = "v1/gloutonconfigitem/"
|
|
37
|
+
GLOUTON_CRASH_REPORT = "v1/gloutoncrashreport/"
|
|
38
|
+
GLOUTON_DIAGNOSTIC = "v1/gloutondiagnostic/"
|
|
39
|
+
HEALTHCHECK = "v1/healthcheck/"
|
|
40
|
+
INTEGRATION = "v1/integration/"
|
|
41
|
+
INTEGRATION_TEMPLATE = "v1/integrationtemplate/"
|
|
42
|
+
LIMIT = "v1/limit/"
|
|
43
|
+
METRIC = "v1/metric/"
|
|
44
|
+
METRIC_ANNOTATION = "v1/metricannotation/"
|
|
45
|
+
METRIC_NAME = "v1/metricname/"
|
|
46
|
+
METRIC_OPERATION = "v1/metricoperation/"
|
|
47
|
+
METRIC_TEMPLATE_GROUP = "v1/metrictemplategroup/"
|
|
48
|
+
NOTIFICATION_EXECUTION = "v1/notificationexecution/"
|
|
49
|
+
NOTIFICATION_RULE = "v1/notificationrule/"
|
|
50
|
+
RECORDING_RULE = "v1/recordingrule/"
|
|
51
|
+
REPORT = "v1/report/"
|
|
52
|
+
SILENCE = "v1/silence/"
|
|
53
|
+
SILENCE_RECURRENT = "v1/silencerecurrent/"
|
|
54
|
+
SLO = "v1/slo/"
|
|
55
|
+
SERVER_GROUP = "v1/servergroup/"
|
|
56
|
+
SERVICE = "v1/service/"
|
|
57
|
+
SESSION = "v1/session/"
|
|
58
|
+
TAG = "v1/tag/"
|
|
59
|
+
USER = "v1/user/"
|
|
60
|
+
WIDGET = "v1/widget/"
|
|
61
|
+
|
|
62
|
+
def __str__(self) -> str:
|
|
63
|
+
return self.value
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: bleemeo
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A simple Python3 client for interacting with the Bleemeo API.
|
|
5
|
+
Author: Bleemeo Packaging Team
|
|
6
|
+
Author-email: packaging-team@bleemeo.com
|
|
7
|
+
Requires-Python: >=3.8,<4.0
|
|
8
|
+
Classifier: Programming Language :: Python :: 3
|
|
9
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
10
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Requires-Dist: requests (>=2.32.3,<3.0.0)
|
|
15
|
+
Description-Content-Type: text/markdown
|
|
16
|
+
|
|
17
|
+
# Bleemeo Python [](https://www.python.org/downloads/release/python-380/) [](https://github.com/bleemeo/bleemeo-python/blob/main/LICENSE)
|
|
18
|
+
|
|
19
|
+
Python library for interacting with the Bleemeo API
|
|
20
|
+
|
|
21
|
+
## Requirements
|
|
22
|
+
|
|
23
|
+
- Python3.8 or later
|
|
24
|
+
- An account on [Bleemeo](https://bleemeo.com/)
|
|
25
|
+
|
|
26
|
+
## Installation
|
|
27
|
+
|
|
28
|
+
Take a look at [venv](https://docs.python.org/3/tutorial/venv.html).
|
|
29
|
+
This tool provides isolated Python environments, which are more practical than installing packages system-wide.
|
|
30
|
+
|
|
31
|
+
After you’ve created and activated a virtual environment, run the command:
|
|
32
|
+
|
|
33
|
+
```shell
|
|
34
|
+
python -m pip install --upgrade bleemeo
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Documentation
|
|
38
|
+
|
|
39
|
+
The Python library is documented using docstring
|
|
40
|
+
in [the source code](https://github.com/bleemeo/bleemeo-python/bleemeo).
|
|
41
|
+
|
|
42
|
+
Some examples of library usage can be found in [examples](./examples).
|
|
43
|
+
|
|
44
|
+
## Basic usage
|
|
45
|
+
|
|
46
|
+
Listing the first 10 agents of your account:
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
from bleemeo import Client, Resource, APIError
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def list_agents():
|
|
53
|
+
with Client(load_from_env=True) as client:
|
|
54
|
+
try:
|
|
55
|
+
resp_page = client.get_page(Resource.AGENT, page=1, page_size=10,
|
|
56
|
+
params={"active": True, "fields": "id,fqdn,display_name"})
|
|
57
|
+
for agent in resp_page.json()["results"]:
|
|
58
|
+
print(f"* Agent {agent['display_name']} (fqdn = {agent['fqdn']}, id = {agent['id']})")
|
|
59
|
+
except APIError as e:
|
|
60
|
+
print(f"API error: {e}:\n{e.response.text}")
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
if __name__ == '__main__':
|
|
64
|
+
list_agents()
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Save this file as list_agents.py.
|
|
68
|
+
|
|
69
|
+
Run it with:
|
|
70
|
+
|
|
71
|
+
```shell
|
|
72
|
+
BLEEMEO_USER=user-email@domain.com BLEEMEO_PASSWORD=password python3 list_agents.py
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
> More examples can be found in [examples](./examples)
|
|
76
|
+
|
|
77
|
+
To run an example from a clone of this repository, run the following:
|
|
78
|
+
|
|
79
|
+
```shell
|
|
80
|
+
BLEEMEO_USER=user-email@domain.com BLEEMEO_PASSWORD=password python3 examples/list_metrics.py
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Environment
|
|
84
|
+
|
|
85
|
+
At least the following options should be configured (as environment variables or with options):
|
|
86
|
+
|
|
87
|
+
- Credentials OR initial refresh token
|
|
88
|
+
- All other configuration options are optional and could be omitted
|
|
89
|
+
|
|
90
|
+
> Ways to provide those options are referenced in the [Configuration](#configuration) section.
|
|
91
|
+
|
|
92
|
+
## Configuration
|
|
93
|
+
|
|
94
|
+
**For environment variables to be taken into account, the parameter `load_from_env` of the `Client` constructor must be
|
|
95
|
+
set to `True`.**
|
|
96
|
+
|
|
97
|
+
| Property | Constructor parameter(s) | Env variable(s) | Default values |
|
|
98
|
+
|-------------------------------|-------------------------------------------|-----------------------------------------------------------|----------------------------------------------------------------------|
|
|
99
|
+
| Credentials | `username` & `password` | `BLEEMEO_USER` & `BLEEMEO_PASSWORD` | None. This option is required (unless initial refresh token is used) |
|
|
100
|
+
| Bleemeo account header | `account_id` | `BLEEMEO_ACCOUNT_ID` | The first account associated with used credentials. |
|
|
101
|
+
| OAuth client ID/secret | `oauth_client_id` & `oauth_client_secret` | `BLEEMEO_OAUTH_CLIENT_ID` & `BLEEMEO_OAUTH_CLIENT_SECRET` | The default SDK OAuth client ID |
|
|
102
|
+
| API URL | `api_url` | `BLEEMEO_API_URL` | `https://api.bleemeo.com` |
|
|
103
|
+
| Initial refresh token | `oauth_initial_refresh_token` | `BLEEMEO_OAUTH_INITIAL_REFRESH_TOKEN` | None. This is an alternative to username & password credentials. |
|
|
104
|
+
| Custom headers | `custom_headers` | - | `{"User-Agent": "Bleemeo Python Client"}` |
|
|
105
|
+
| Throttle max auto retry delay | `throttle_max_auto_retry_delay` | - | 1 minute. |
|
|
106
|
+
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
bleemeo/__init__.py,sha256=kjg04H_Q7VdvD9ylqkbBmOEZ2-2PCgmoFTc1JBeUsUc,1447
|
|
2
|
+
bleemeo/_authenticator.py,sha256=kHu-E4UWMmQD4a5yW1BYkUKUUt_4UUSNoCCKiKS53L0,5084
|
|
3
|
+
bleemeo/client.py,sha256=mlg3th8Hck1HBvQD0Xb1Q3LqB8SH4d49_fpnrFaRI8s,18494
|
|
4
|
+
bleemeo/enums.py,sha256=0ce-FiT6ftGx7Dj0yPrCsBG6LwISTvB_e9kOB6soEu0,2252
|
|
5
|
+
bleemeo/exceptions.py,sha256=UG6sYHGM79RIrhK2pj_1SURgwZwYPizeoCYOBczwl4c,3350
|
|
6
|
+
bleemeo/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
+
bleemeo/resources.py,sha256=twkDD2WuJWuynPoXLKWjUiTBHL_DecKOlR_SxbJIL6g,2183
|
|
8
|
+
bleemeo-0.1.0.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
9
|
+
bleemeo-0.1.0.dist-info/METADATA,sha256=qz3kPoX_bHflxi3ikyZBKypzhc5sS7VHj4qW8xn-leY,5035
|
|
10
|
+
bleemeo-0.1.0.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
|
|
11
|
+
bleemeo-0.1.0.dist-info/RECORD,,
|