-
Notifications
You must be signed in to change notification settings - Fork 4
/
ForemanAPIClient.py
executable file
·410 lines (351 loc) · 13.3 KB
/
ForemanAPIClient.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This file contains the ForemanAPIClient and
APILevelNotSupportedExceptrion classes
"""
import logging
import requests
import json
import socket
from requests.packages.urllib3.exceptions import InsecureRequestWarning
LOGGER = logging.getLogger('ForemanAPIClient')
class APILevelNotSupportedException(Exception):
"""
Dummy class for unsupported API levels
.. class:: APILevelNotSupportedException
"""
pass
class ForemanAPIClient:
"""
Class for communicating with the Foreman API.
.. class:: ForemanAPIClient
"""
API_MIN = 2
"""
int: Minimum supported API version. You really don't want to use v1.
"""
HEADERS = {'User-Agent': 'katprep (https://github.com/stdevel/katprep)'}
"""
dict: Default headers set for every HTTP request
"""
HOSTNAME = ""
"""
str: Foreman API hostname
"""
URL = ""
"""
str: Foreman API base URL
"""
SESSION = None
"""
Session: HTTP session to Foreman host
"""
VERIFY = True
"""
bool: Boolean whether force SSL verification
"""
def __init__(self, hostname, username, password, verify=True, prefix=""):
"""
Constructor, creating the class. It requires specifying a
hostname, username and password to access the API. After
initialization, a connected is established.
:param hostname: Foreman host
:type hostname: str
:param username: API username
:type username: str
:param password: corresponding password
:type password: str
:param verify: force SSL verification
:type verify: bool
:param prefix: API prefix (e.g. /katello)
:type prefix: str
"""
#disable SSL warning outputs
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
#set connection information
self.HOSTNAME = self.validate_hostname(hostname)
self.USERNAME = username
self.PASSWORD = password
self.VERIFY = verify
self.URL = "https://{0}{1}/api/v2".format(self.HOSTNAME, prefix)
#start session and check API version if Foreman API
self.__connect()
if prefix == "":
self.validate_api_support()
def __connect(self):
"""This function establishes a connection to Foreman."""
global SESSION
self.SESSION = requests.Session()
self.SESSION.auth = (self.USERNAME, self.PASSWORD)
#TODO: find a nicer way to displaying _all_ the hits...
def __api_request(self, method, sub_url, payload="", hits=1337, page=1):
"""
Sends a HTTP request to the Foreman API. This function requires
a valid HTTP method and a sub-URL (such as /hosts). Optionally,
you can also specify payload (for POST, DELETE, PUT) and hits/page
and a page number (when retrieving data using GET).
There are also alias functions available.
:param method: HTTP request method (GET, POST, DELETE, PUT)
:type method: str
:param sub_url: relative path within the API tree (e.g. /hosts)
:type sub_url: str
:param payload: payload for POST/PUT requests
:type payload: str
:param hits: numbers of hits/page for GET requests (must be set sadly)
:type hits: int
:param page: number of page/results to display (must be set sadly)
:type page: int
.. todo:: Find a nicer way to display all hits, we shouldn't use 1337 hits/page
.. seealso:: api_get()
.. seealso:: api_post()
.. seealso:: api_put()
.. seealso:: api_delete()
"""
#send request to API
try:
if method.lower() not in ["get", "post", "delete", "put"]:
#going home
raise ValueError("Illegal method '{}' specified".format(method))
#setting headers
my_headers = self.HEADERS
if method.lower() != "get":
#add special headers for non-GETs
my_headers["Content-Type"] = "application/json"
my_headers["Accept"] = "application/json,version=2"
#send request
if method.lower() == "put":
#PUT
result = self.SESSION.put(
"{}{}".format(self.URL, sub_url),
data=payload, headers=my_headers, verify=self.VERIFY
)
elif method.lower() == "delete":
#DELETE
result = self.SESSION.delete(
"{}{}".format(self.URL, sub_url),
data=payload, headers=my_headers, verify=self.VERIFY
)
elif method.lower() == "post":
#POST
result = self.SESSION.post(
"{}{}".format(self.URL, sub_url),
data=payload, headers=my_headers, verify=self.VERIFY
)
else:
#GET
result = self.SESSION.get(
"{}{}?per_page={}&page={}".format(
self.URL, sub_url, hits, page),
headers=self.HEADERS, verify=self.VERIFY
)
if "unable to authenticate" in result.text.lower():
raise ValueError("Unable to authenticate")
if result.status_code not in [200, 201, 202]:
raise ValueError("{}: HTTP operation not successful {}".format(
result.status_code, result.text))
else:
#return result
if method.lower() == "get":
return result.text
else:
return True
except ValueError as err:
LOGGER.error(err)
pass
#Aliases
def api_get(self, sub_url, hits=1337, page=1):
"""
Sends a GET request to the Foreman API. This function requires a
sub-URL (such as /hosts) and - optionally - hits/page and page
definitons.
:param sub_url: relative path within the API tree (e.g. /hosts)
:type sub_url: str
:param hits: numbers of hits/page for GET requests (must be set sadly)
:type hits: int
:param page: number of page/results to display (must be set sadly)
:type page: int
"""
return self.__api_request("get", sub_url, "", hits, page)
def api_post(self, sub_url, payload):
"""
Sends a POST request to the Foreman API. This function requires a
sub-URL (such as /hosts/1) and payload data.
:param sub_url: relative path within the API tree (e.g. /hosts)
:type sub_url: str
:param payload: payload for POST/PUT requests
:type payload: str
"""
return self.__api_request("post", sub_url, payload)
def api_delete(self, sub_url, payload):
"""
Sends a DELETE request to the Foreman API. This function requires a
sub-URL (such as /hosts/2) and payload data.
:param sub_url: relative path within the API tree (e.g. /hosts)
:type sub_url: str
:param payload: payload for POST/PUT requests
:type payload: str
"""
return self.__api_request("delete", sub_url, payload)
def api_put(self, sub_url, payload):
"""
Sends a PUT request to the Foreman API. This function requires a
sub-URL (such as /hosts/3) and payload data.
:param sub_url: relative path within the API tree (e.g. /hosts)
:type sub_url: str
:param payload: payload for POST/PUT requests
:type payload: str
"""
return self.__api_request("put", sub_url, payload)
def validate_api_support(self):
"""
Checks whether the API version on the Foreman server is supported.
Using older version than API v2 is not recommended. In this case, an
exception will be thrown.
"""
try:
#get api version
result_obj = json.loads(
self.api_get("/status")
)
LOGGER.debug("API version {} found.".format(
result_obj["api_version"]))
if result_obj["api_version"] != self.API_MIN:
raise APILevelNotSupportedException(
"Your API version ({}) does not support the required calls."
"You'll need API version {} - stop using historic"
" software!".format(result_obj["api_version"], self.API_MIN)
)
except ValueError as err:
LOGGER.error(err)
@staticmethod
def validate_hostname(hostname):
"""
Validates that the Foreman API uses a FQDN as hostname.
Also looks up the "real" hostname if "localhost" is specified.
Otherwise, the picky Foreman API won't connect.
:param hostname: the hostname to validate
:type hostname: str
"""
if hostname == "localhost":
#get real hostname
hostname = socket.gethostname()
else:
#convert to FQDN if possible:
fqdn = socket.gethostbyaddr(hostname)
if "." in fqdn[0]:
hostname = fqdn[0]
return hostname
def get_url(self):
"""Returns the configured URL of the object instance"""
return self.URL
def get_name_by_id(self, object_id, api_object):
"""
Returns a Foreman object's name by its ID.
param object_id: Foreman object ID
type object_id: int
param api_object: Foreman object type (e.g. host, environment)
type api_object: str
"""
valid_objects = [
"hostgroup", "location", "organization", "environment",
"host", "user"
]
try:
if api_object.lower() not in valid_objects:
#invalid type
raise ValueError(
"Unable to lookup name by invalid field"
" type '{}'".format(api_object)
)
else:
#get ID by name
result_obj = json.loads(
self.api_get("/{}s/{}".format(api_object, object_id))
)
if result_obj["id"] == object_id:
LOGGER.debug(
"I think I found {} #{}...".format(
api_object, object_id
)
)
if api_object.lower() == "user":
return "{} {}".format(
result_obj["firstname"], result_obj["lastname"]
)
else:
return result_obj["name"]
except ValueError as err:
LOGGER.error(err)
def get_id_by_name(self, name, api_object):
"""
Returns a Foreman object's internal ID by its name.
:param name: Foreman object name
:type name: str
:param api_object: Foreman object type (e.g. host, environment)
:type api_object: str
"""
valid_objects = [
"hostgroup", "location", "organization", "environment",
"host"
]
try:
if api_object.lower() not in valid_objects:
#invalid type
raise ValueError(
"Unable to lookup name by invalid field"
" type '{}'".format(api_object)
)
else:
#get ID by name
result_obj = json.loads(
self.api_get("/{}s".format(api_object))
)
#TODO: nicer way than looping? numpy? Direct URL?
for entry in result_obj["results"]:
if entry["name"].lower() == name.lower():
LOGGER.debug(
"{} {} seems to have ID #{}".format(
api_object, name, entry["id"]
)
)
return entry["id"]
except ValueError as err:
LOGGER.error(err)
def get_hostparam_id_by_name(self, host, param_name):
"""
Returns a Foreman host parameter's internal ID by its name.
:param host: Foreman host object ID
:type host: int
:param param_name: host parameter name
:type param_name: str
"""
try:
result_obj = json.loads(
self.api_get("/hosts/{}/parameters".format(host))
)
#TODO: nicer way than looping? numpy?
#TODO allow/return multiple IDs to reduce overhead?
for entry in result_obj["results"]:
if entry["name"].lower() == param_name.lower():
LOGGER.debug(
"Found relevant parameter '{}' with ID #{}".format(
entry["name"], entry["id"]
)
)
return entry["id"]
except ValueError as err:
LOGGER.error(err)
def get_host_params(self, host):
"""
Returns all parameters for a particular host.
:param host: Forenam host name
:type host: str
"""
try:
result_obj = json.loads(
self.api_get("/hosts/{}/parameters".format(host))
)
return result_obj["results"]
except ValueError as err:
LOGGER.error(err)