-
Notifications
You must be signed in to change notification settings - Fork 7
/
infomaniak.py
96 lines (83 loc) · 3.19 KB
/
infomaniak.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#! /usr/bin/env python3
# -*- coding: utf-8; py-indent-offset: 4 -*-
#
# Author: Linuxfabrik GmbH, Zurich, Switzerland
# Contact: info (at) linuxfabrik (dot) ch
# https://www.linuxfabrik.ch/
# License: The Unlicense, see LICENSE file.
# https://github.com/Linuxfabrik/monitoring-plugins/blob/main/CONTRIBUTING.rst
"""Provides functions using the Infomanik REST-API.
"""
__author__ = 'Linuxfabrik GmbH, Zurich/Switzerland'
__version__ = '2024031401'
from . import url
BASE_URL = 'https://api.infomaniak.com'
def get_events(token, insecure=False, no_proxy=False, timeout=8):
"""Get all Infomaniak Events.
https://developer.infomaniak.com/docs/api/get/2/events
"""
uri = '{}/2/events?locale=en'.format(BASE_URL)
success, events = url.fetch_json(
uri,
header={'Authorization':'Bearer {}'.format(token)},
insecure=insecure,
no_proxy=no_proxy,
timeout=timeout,
)
if not success:
return (success, events)
if not events:
return (False, 'There was no result from {}.'.format(uri))
if events.get('result') != 'success':
return (False, events.get('error').get('description'))
return (True, events)
def get_swiss_backup_products(account_id, token, insecure=False, no_proxy=False, timeout=8):
"""Get all Infomaniak Swiss Backup products.
https://developer.infomaniak.com/docs/api/get/1/swiss_backups
"""
uri = '{}/1/swiss_backups?account_id={}'.format(BASE_URL, account_id)
success, products = url.fetch_json(
uri,
header={'Authorization':'Bearer {}'.format(token)},
insecure=insecure,
no_proxy=no_proxy,
timeout=timeout,
)
if not success:
return (success, products)
if not products:
return (False, 'There was no result from {}.'.format(uri))
if products.get('result') != 'success':
return (False, products.get('error').get('description'))
return (True, products)
def get_swiss_backup_slots(account_id, token, insecure=False, no_proxy=False, timeout=8):
"""Get all devices / slots for each Infomaniak Swiss Backup product.
https://developer.infomaniak.com/docs/api/get/1/swiss_backups/%7Bswiss_backup_id%7D/slots/%7Bslot_id%7D
"""
success, products = get_swiss_backup_products(account_id, token)
if not success:
return (success, products)
slots = []
for product in products.get('data', {}):
uri = '{}/1/swiss_backups/{}/slots'.format(
BASE_URL,
product.get('id'),
)
success, slot = url.fetch_json(
uri,
header={'Authorization':'Bearer {}'.format(token)},
insecure=insecure,
no_proxy=no_proxy,
timeout=timeout,
)
if not success:
return (success, slot)
if not slot:
return (False, 'There was no result from {}.'.format(uri))
if slot.get('result') != 'success':
return (False, slot.get('error').get('description'))
# append some information from the parent, too:
slot['product_customer_name'] = product.get('customer_name')
slot['product_tags'] = product.get('tags')
slots.append(slot)
return (True, slots)