-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathinfomaniak.py
More file actions
186 lines (153 loc) · 5.78 KB
/
infomaniak.py
File metadata and controls
186 lines (153 loc) · 5.78 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#! /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__ = '2025042001'
from . import url
BASE_URL = 'https://api.infomaniak.com'
def get_events(token, insecure=False, no_proxy=False, timeout=8):
"""
Get all Infomaniak Events.
This function queries the Infomaniak API to retrieve all available events. Requires an
OAuth Bearer token for authentication.
### Parameters
- **token** (`str`):
OAuth2 Bearer token used for authentication.
- **insecure** (`bool`, optional):
Disable SSL verification. Default is `False`.
- **no_proxy** (`bool`, optional):
Ignore proxy settings. Default is `False`.
- **timeout** (`int`, optional):
Timeout for the request in seconds. Default is 8.
### Returns
- **tuple** (`bool`, `dict` or `str`):
- If successful, returns `(True, events dictionary)`.
- If failed, returns `(False, error message)`.
### Notes
- API documentation: https://developer.infomaniak.com/docs/api/get/2/events
### Example
>>> success, events = get_events(token)
>>> if success:
>>> print(events)
"""
uri = f'{BASE_URL}/2/events?locale=en'
headers = {'Authorization': f'Bearer {token}'}
success, events = url.fetch_json(
uri,
header=headers,
insecure=insecure,
no_proxy=no_proxy,
timeout=timeout,
)
if not success:
return success, events
if not events:
return False, f'There was no result from {uri}.'
if events.get('result') != 'success':
return False, events.get('error', {}).get('description', 'Unknown error')
return True, events
def get_swiss_backup_products(
account_id, token, insecure=False, no_proxy=False, timeout=8
):
"""
Get all Infomaniak Swiss Backup products.
This function queries the Infomaniak API to retrieve all Swiss Backup products for a given
account ID. Requires an OAuth Bearer token for authentication.
### Parameters
- **account_id** (`str`):
ID of the Infomaniak account.
- **token** (`str`):
OAuth2 Bearer token used for authentication.
- **insecure** (`bool`, optional):
Disable SSL verification. Default is `False`.
- **no_proxy** (`bool`, optional):
Ignore proxy settings. Default is `False`.
- **timeout** (`int`, optional):
Timeout for the request in seconds. Default is 8.
### Returns
- **tuple** (`bool`, `dict` or `str`):
- If successful, returns `(True, products dictionary)`.
- If failed, returns `(False, error message)`.
### Notes
- API documentation: https://developer.infomaniak.com/docs/api/get/1/swiss_backups
### Example
>>> success, products = get_swiss_backup_products(account_id, token)
>>> if success:
>>> print(products)
"""
uri = f'{BASE_URL}/1/swiss_backups?account_id={account_id}'
headers = {'Authorization': f'Bearer {token}'}
success, products = url.fetch_json(
uri,
header=headers,
insecure=insecure,
no_proxy=no_proxy,
timeout=timeout,
)
if not success:
return success, products
if not products:
return False, f'There was no result from {uri}.'
if products.get('result') != 'success':
return False, products.get('error', {}).get('description', 'Unknown error')
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.
This function retrieves all devices (slots) for every Swiss Backup product under a specific
Infomaniak account.
### Parameters
- **account_id** (`str`):
ID of the Infomaniak account.
- **token** (`str`):
OAuth2 Bearer token used for authentication.
- **insecure** (`bool`, optional):
Disable SSL verification. Default is `False`.
- **no_proxy** (`bool`, optional):
Ignore proxy settings. Default is `False`.
- **timeout** (`int`, optional):
Timeout for the request in seconds. Default is 8.
### Returns
- **tuple** (`bool`, `list` or `str`):
- If successful, returns `(True, list of slots with additional info)`.
- If failed, returns `(False, error message)`.
### Notes
- API documentation:
https://developer.infomaniak.com/docs/api/get/1/swiss_backups/{swiss_backup_id}/slots/{slot_id}
### Example
>>> success, slots = get_swiss_backup_slots(account_id, token)
>>> if success:
>>> print(slots)
"""
success, products = get_swiss_backup_products(account_id, token)
if not success:
return success, products
slots = []
for product in products.get('data', []):
uri = f'{BASE_URL}/1/swiss_backups/{product.get("id")}/slots'
headers = {'Authorization': f'Bearer {token}'}
success, slot = url.fetch_json(
uri,
header=headers,
insecure=insecure,
no_proxy=no_proxy,
timeout=timeout,
)
if not success:
return success, slot
if not slot:
return False, f'There was no result from {uri}.'
if slot.get('result') != 'success':
return False, slot.get('error', {}).get('description', 'Unknown error')
slot['product_customer_name'] = product.get('customer_name')
slot['product_tags'] = product.get('tags')
slots.append(slot)
return True, slots