Skip to content

Commit

Permalink
Merge pull request #156 from tklecker/cmk2.2
Browse files Browse the repository at this point in the history
Anpassung auf die API ab Version 2.1
  • Loading branch information
gurubert authored Apr 19, 2024
2 parents 6874786 + 6277a89 commit 4c1b8e6
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion check_mk_api/lib/python3/checkmkapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def __init__(self, site_url=None, api_user=None, api_secret=None):
site_url = _site_url()
if not api_secret:
api_user, api_secret = _site_creds(api_user)
self._api_url = '%sapi/v0' % _check_mk_url(site_url)
self._api_url = '%sapi/1.0' % _check_mk_url(site_url)
self._session = requests.session()
self._session.headers['Authorization'] = f"Bearer {api_user} {api_secret}"
self._session.headers['Accept'] = 'application/json'
Expand All @@ -94,6 +94,15 @@ def _get_url(self, uri, etag=None, data={}):
)
)

def _get_abolute_url(self, uri, etag=None, data={}):
return self._check_response(
self._session.get(
f"{uri}",
params=data,
allow_redirects=False,
)
)

def _post_url(self, uri, etag=None, data={}):
headers={
"Content-Type": 'application/json',
Expand Down Expand Up @@ -391,6 +400,24 @@ def get_all_hosts(self, effective_attr=False, attributes=True):
etags[hostdata['id']] = etag
else:
hosts[hinfo['title']] = hinfo['href']
elif hinfo.get('domainType') == 'host_config':
if attributes:
self_urls = [
link['href']
for link in hinfo.get("links", [])
if link["rel"] == "self"]
hostdata, etag, resp = self._get_abolute_url(
self_urls[0],
data={"effective_attributes": "true" if effective_attr else "false"}
)
if resp.status_code != 200:
resp.raise_for_status()
if hostdata.get('domainType') == 'host_config':
hosts[hostdata['id']] = hostdata['extensions']
etags[hostdata['id']] = etag
else:
hosts[hinfo['title']] = hinfo['extensions']

return hosts, etags

def delete_host(self, hostname):
Expand Down

0 comments on commit 4c1b8e6

Please sign in to comment.