oc-cdtapi 3.26.1__py3-none-any.whl → 3.27.2__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.
- oc_cdtapi/ForemanAPI.py +163 -19
- {oc_cdtapi-3.26.1.dist-info → oc_cdtapi-3.27.2.dist-info}/METADATA +1 -1
- {oc_cdtapi-3.26.1.dist-info → oc_cdtapi-3.27.2.dist-info}/RECORD +7 -7
- {oc_cdtapi-3.26.1.data → oc_cdtapi-3.27.2.data}/scripts/nexus.py +0 -0
- {oc_cdtapi-3.26.1.dist-info → oc_cdtapi-3.27.2.dist-info}/WHEEL +0 -0
- {oc_cdtapi-3.26.1.dist-info → oc_cdtapi-3.27.2.dist-info}/licenses/LICENSE +0 -0
- {oc_cdtapi-3.26.1.dist-info → oc_cdtapi-3.27.2.dist-info}/top_level.txt +0 -0
oc_cdtapi/ForemanAPI.py
CHANGED
|
@@ -11,7 +11,8 @@ from datetime import datetime, timedelta
|
|
|
11
11
|
from packaging import version
|
|
12
12
|
|
|
13
13
|
class ForemanAPIError(HttpAPIError):
|
|
14
|
-
|
|
14
|
+
def __str__(self):
|
|
15
|
+
return self.text
|
|
15
16
|
|
|
16
17
|
|
|
17
18
|
class ForemanAPI(HttpAPI):
|
|
@@ -22,7 +23,10 @@ class ForemanAPI(HttpAPI):
|
|
|
22
23
|
_error = ForemanAPIError
|
|
23
24
|
_env_prefix = "FOREMAN"
|
|
24
25
|
|
|
25
|
-
headers = {
|
|
26
|
+
headers = {
|
|
27
|
+
"Accept": "application/json;version=2",
|
|
28
|
+
"Content-Type": "application/json"
|
|
29
|
+
}
|
|
26
30
|
|
|
27
31
|
def __init__(self, *args, **kwargs):
|
|
28
32
|
"""
|
|
@@ -69,7 +73,7 @@ class ForemanAPI(HttpAPI):
|
|
|
69
73
|
return self.__foreman_version_major
|
|
70
74
|
|
|
71
75
|
def re(self, req):
|
|
72
|
-
if not req.startswith("foreman_puppet"):
|
|
76
|
+
if not req.startswith(("foreman_puppet", "ansible")):
|
|
73
77
|
return posixpath.join(self.root, "api", req)
|
|
74
78
|
else:
|
|
75
79
|
return posixpath.join(self.root, req)
|
|
@@ -285,10 +289,10 @@ class ForemanAPI(HttpAPI):
|
|
|
285
289
|
default_params.update(custom)
|
|
286
290
|
|
|
287
291
|
if not default_params["is_owned_by"]:
|
|
288
|
-
raise ForemanAPIError("The owner id is not specified")
|
|
292
|
+
raise ForemanAPIError(code=400, text="The owner id is not specified")
|
|
289
293
|
|
|
290
294
|
if not default_params["name"]:
|
|
291
|
-
raise ForemanAPIError("The hostname is not specified")
|
|
295
|
+
raise ForemanAPIError(code=400, text="The hostname is not specified")
|
|
292
296
|
|
|
293
297
|
logging.debug("ForemanAPI is about to send the following payload:")
|
|
294
298
|
logging.debug(default_params)
|
|
@@ -343,10 +347,10 @@ class ForemanAPI(HttpAPI):
|
|
|
343
347
|
default_params.update(custom)
|
|
344
348
|
|
|
345
349
|
if not default_params["is_owned_by"]:
|
|
346
|
-
raise ForemanAPIError("The owner id is not specified")
|
|
350
|
+
raise ForemanAPIError(code=400, text="The owner id is not specified")
|
|
347
351
|
|
|
348
352
|
if not default_params["name"]:
|
|
349
|
-
raise ForemanAPIError("The hostname is not specified")
|
|
353
|
+
raise ForemanAPIError(code=400, text="The hostname is not specified")
|
|
350
354
|
|
|
351
355
|
if not default_params.get('hostgroup_id'):
|
|
352
356
|
hostgroup = self.get_hostgroup_id('stands')
|
|
@@ -628,7 +632,7 @@ class ForemanAPI(HttpAPI):
|
|
|
628
632
|
"""
|
|
629
633
|
logging.debug('Reached override_smart_class_v1')
|
|
630
634
|
request = self.post(posixpath.join("smart_class_parameters", str(scid),
|
|
631
|
-
|
|
635
|
+
"override_values"), headers=self.headers, data=params)
|
|
632
636
|
|
|
633
637
|
def override_smart_class_v2(self, scid, params):
|
|
634
638
|
"""
|
|
@@ -699,7 +703,8 @@ class ForemanAPI(HttpAPI):
|
|
|
699
703
|
"""
|
|
700
704
|
logging.debug('Reached add_puppet_class_to_host_v2')
|
|
701
705
|
logging.debug('Params to be sent: %s' % params)
|
|
702
|
-
response = self.post(posixpath.join('foreman_puppet', 'api', 'hosts', hostname, 'puppetclass_ids'),
|
|
706
|
+
response = self.post(posixpath.join('foreman_puppet', 'api', 'hosts', hostname, 'puppetclass_ids'),
|
|
707
|
+
headers=self.headers, data=params)
|
|
703
708
|
|
|
704
709
|
def get_subnets(self):
|
|
705
710
|
"""
|
|
@@ -804,7 +809,7 @@ class ForemanAPI(HttpAPI):
|
|
|
804
809
|
actions = ["start", "stop"]
|
|
805
810
|
|
|
806
811
|
if action not in actions:
|
|
807
|
-
raise ForemanAPIError(
|
|
812
|
+
raise ForemanAPIError(code=500, text="Incorrect power action was provided")
|
|
808
813
|
|
|
809
814
|
params = json.dumps({"power_action": action})
|
|
810
815
|
request = self.put(posixpath.join("hosts", hostname, "power"), headers=self.headers, data=params)
|
|
@@ -816,7 +821,7 @@ class ForemanAPI(HttpAPI):
|
|
|
816
821
|
logging.debug('Reached host_power_v2')
|
|
817
822
|
actions = ["on", "off"]
|
|
818
823
|
if action not in actions:
|
|
819
|
-
raise ForemanAPIError(
|
|
824
|
+
raise ForemanAPIError(code=500, text="Incorrect power action was provided")
|
|
820
825
|
params = json.dumps({"power_action": action})
|
|
821
826
|
request = self.put(posixpath.join('hosts', hostname, "power"), headers=self.headers, data=params)
|
|
822
827
|
if not request.status_code == 200:
|
|
@@ -876,7 +881,7 @@ class ForemanAPI(HttpAPI):
|
|
|
876
881
|
|
|
877
882
|
for hostgroup in hostgroups:
|
|
878
883
|
if hostgroup.get("name") == hostgroup_name:
|
|
879
|
-
return hostgroup.get
|
|
884
|
+
return hostgroup.get('id')
|
|
880
885
|
|
|
881
886
|
logging.debug("Hostgroup [%s] not found, returning None" % hostgroup_name)
|
|
882
887
|
return None
|
|
@@ -1061,7 +1066,7 @@ class ForemanAPI(HttpAPI):
|
|
|
1061
1066
|
"""
|
|
1062
1067
|
logging.debug('Reached get_flavor_id_v1')
|
|
1063
1068
|
flavors_list = self.get(posixpath.join("compute_resources", str(compute_resource_id),
|
|
1064
|
-
|
|
1069
|
+
"available_flavors")).json()["results"]
|
|
1065
1070
|
|
|
1066
1071
|
try:
|
|
1067
1072
|
flavor_id = next(flavor["id"] for flavor in flavors_list if flavor["name"] == flavor_name)
|
|
@@ -1230,11 +1235,10 @@ class ForemanAPI(HttpAPI):
|
|
|
1230
1235
|
owner_type = 'Usergroup'
|
|
1231
1236
|
|
|
1232
1237
|
if not owner_id:
|
|
1233
|
-
raise ForemanAPIError(f"The owner [{owner}] is not found")
|
|
1238
|
+
raise ForemanAPIError(code=404, text=f"The owner [{owner}] is not found")
|
|
1234
1239
|
|
|
1235
1240
|
self.update_host(hostname, {"host": {"owner_id": owner_id, "owner_type": owner_type}})
|
|
1236
1241
|
|
|
1237
|
-
|
|
1238
1242
|
def set_host_owner_id(self, hostname, owner_id):
|
|
1239
1243
|
"""
|
|
1240
1244
|
Change host owner_id
|
|
@@ -1282,7 +1286,7 @@ class ForemanAPI(HttpAPI):
|
|
|
1282
1286
|
|
|
1283
1287
|
template_id = self._template_cache.get(template_name)
|
|
1284
1288
|
if not template_id:
|
|
1285
|
-
raise ForemanAPIError(f"Job template '{template_name}' not found")
|
|
1289
|
+
raise ForemanAPIError(code=404, text=f"Job template '{template_name}' not found")
|
|
1286
1290
|
|
|
1287
1291
|
logging.debug(f"Template id for [{template_name}] is [{template_id}]")
|
|
1288
1292
|
return template_id
|
|
@@ -1305,7 +1309,7 @@ class ForemanAPI(HttpAPI):
|
|
|
1305
1309
|
|
|
1306
1310
|
config = task_configs.get(task_name)
|
|
1307
1311
|
if not config:
|
|
1308
|
-
raise ForemanAPIError(f"Unknown task: {task_name}")
|
|
1312
|
+
raise ForemanAPIError(code=404, text=f"Unknown task: {task_name}")
|
|
1309
1313
|
|
|
1310
1314
|
logging.debug(f"config for [{task_name}] is [{config}]")
|
|
1311
1315
|
|
|
@@ -1345,7 +1349,7 @@ class ForemanAPI(HttpAPI):
|
|
|
1345
1349
|
start_time = time.time()
|
|
1346
1350
|
while True:
|
|
1347
1351
|
if time.time() - start_time > timeout:
|
|
1348
|
-
raise ForemanAPIError(f"Job {job_id} timed out after {timeout} seconds")
|
|
1352
|
+
raise ForemanAPIError(code=500, text=f"Job {job_id} timed out after {timeout} seconds")
|
|
1349
1353
|
response = self.get(posixpath.join("job_invocations", str(job_id)))
|
|
1350
1354
|
is_pending = bool(response.json().get("pending", False))
|
|
1351
1355
|
if not is_pending:
|
|
@@ -1409,4 +1413,144 @@ class ForemanAPI(HttpAPI):
|
|
|
1409
1413
|
return
|
|
1410
1414
|
|
|
1411
1415
|
logging.debug(f"updating {parameter_name}")
|
|
1412
|
-
self.put(posixpath.join("hosts", hostname, "parameters", parameter_name), headers=self.headers, json=payload)
|
|
1416
|
+
self.put(posixpath.join("hosts", hostname, "parameters", parameter_name), headers=self.headers, json=payload)
|
|
1417
|
+
|
|
1418
|
+
def get_host_ansible_roles(self, hostname):
|
|
1419
|
+
"""
|
|
1420
|
+
Get ansible role from host.
|
|
1421
|
+
:param hostname: str
|
|
1422
|
+
"""
|
|
1423
|
+
logging.debug('Reached get_host_ansible_roles')
|
|
1424
|
+
|
|
1425
|
+
response = self.get(posixpath.join("hosts", hostname, "ansible_roles"), headers=self.headers)
|
|
1426
|
+
roles = response.json()
|
|
1427
|
+
|
|
1428
|
+
return roles
|
|
1429
|
+
|
|
1430
|
+
def get_ansible_role(self, roles=None):
|
|
1431
|
+
"""
|
|
1432
|
+
Get ansible role id by its name.
|
|
1433
|
+
:param role_name: str
|
|
1434
|
+
"""
|
|
1435
|
+
logging.debug('Reached get_ansible_role')
|
|
1436
|
+
|
|
1437
|
+
if not roles:
|
|
1438
|
+
params = {'per_page': 'all'}
|
|
1439
|
+
response = self.get(posixpath.join("ansible", "api", "ansible_roles"), params=params, headers=self.headers).json()
|
|
1440
|
+
|
|
1441
|
+
logging.debug(f"About to return {response.get('subtotal')} roles")
|
|
1442
|
+
return response.get("results")
|
|
1443
|
+
|
|
1444
|
+
params = {'search': None}
|
|
1445
|
+
|
|
1446
|
+
if isinstance(roles, (str, int)):
|
|
1447
|
+
roles = [roles]
|
|
1448
|
+
|
|
1449
|
+
query = []
|
|
1450
|
+
for role in roles:
|
|
1451
|
+
if isinstance(role, str):
|
|
1452
|
+
query.append(f"name={role}")
|
|
1453
|
+
elif isinstance(role, int):
|
|
1454
|
+
query.append(f"id={role}")
|
|
1455
|
+
else:
|
|
1456
|
+
raise ForemanAPIError(f"Invalid role type: {type(role)}")
|
|
1457
|
+
|
|
1458
|
+
params["search"] = " or ".join(query)
|
|
1459
|
+
|
|
1460
|
+
logging.debug(f"Search param is {params.get('search')}")
|
|
1461
|
+
response = self.get(posixpath.join("ansible", "api", "ansible_roles"), params=params, headers=self.headers).json()
|
|
1462
|
+
|
|
1463
|
+
logging.debug(f"About to return {response.get('subtotal')} roles")
|
|
1464
|
+
return response.get("results")
|
|
1465
|
+
|
|
1466
|
+
def assign_ansible_roles(self, hostname, roles):
|
|
1467
|
+
"""
|
|
1468
|
+
Assign an ansible roles and override value by given role_id and kwargs to specific hostname.
|
|
1469
|
+
:param hostname: str
|
|
1470
|
+
:param roles: list/str/int
|
|
1471
|
+
"""
|
|
1472
|
+
logging.debug('Reached assign_ansible_roles')
|
|
1473
|
+
logging.debug(f'Hostname: [{hostname}]')
|
|
1474
|
+
logging.debug(f'Roles: [{roles}]')
|
|
1475
|
+
|
|
1476
|
+
role_ids = []
|
|
1477
|
+
role_names = []
|
|
1478
|
+
|
|
1479
|
+
if isinstance(roles, (str, int)):
|
|
1480
|
+
roles = [roles]
|
|
1481
|
+
|
|
1482
|
+
roles = self.get_ansible_role(roles)
|
|
1483
|
+
for role in roles:
|
|
1484
|
+
role_ids.append(role.get("id"))
|
|
1485
|
+
role_names.append(role.get("name"))
|
|
1486
|
+
|
|
1487
|
+
payload = {
|
|
1488
|
+
"ansible_role_ids": role_ids
|
|
1489
|
+
}
|
|
1490
|
+
|
|
1491
|
+
# Assign ansible roles
|
|
1492
|
+
logging.debug(f'About to set roles {role_names}')
|
|
1493
|
+
self.post(posixpath.join("hosts", hostname, "assign_ansible_roles"), headers=self.headers, json=payload)
|
|
1494
|
+
|
|
1495
|
+
def assign_ansible_roles_and_override(self, hostname, roles):
|
|
1496
|
+
"""
|
|
1497
|
+
Assign an ansible roles and override value by given role_id and kwargs to specific hostname.
|
|
1498
|
+
:param hostname: str
|
|
1499
|
+
:param roles: dict
|
|
1500
|
+
"""
|
|
1501
|
+
logging.debug('Reached assign_ansible_roles_and_override')
|
|
1502
|
+
logging.debug(f'Hostname: [{hostname}]')
|
|
1503
|
+
logging.debug(f'Roles: [{roles}]')
|
|
1504
|
+
|
|
1505
|
+
role_ids = []
|
|
1506
|
+
updated_dict = {}
|
|
1507
|
+
|
|
1508
|
+
if not isinstance(roles, dict):
|
|
1509
|
+
raise ForemanAPIError(code=400, text="Input must be in dict")
|
|
1510
|
+
|
|
1511
|
+
temp_roles = []
|
|
1512
|
+
for role, variable in roles.items():
|
|
1513
|
+
temp_roles.append(role)
|
|
1514
|
+
|
|
1515
|
+
new_roles = self.get_ansible_role(temp_roles)
|
|
1516
|
+
for new_role in new_roles:
|
|
1517
|
+
role_ids.append(new_role.get("id"))
|
|
1518
|
+
if not roles.get(new_role.get("id")):
|
|
1519
|
+
continue
|
|
1520
|
+
|
|
1521
|
+
roles[new_role.get("name")] = roles.pop(new_role.get("id"))
|
|
1522
|
+
|
|
1523
|
+
payload = {
|
|
1524
|
+
"ansible_role_ids": role_ids
|
|
1525
|
+
}
|
|
1526
|
+
|
|
1527
|
+
for key, values in roles.items():
|
|
1528
|
+
params = {"search": f"ansible_role={key}", "per_page": "all"}
|
|
1529
|
+
|
|
1530
|
+
variables = self.get(posixpath.join("ansible", "api", "ansible_variables"), params=params).json()["results"]
|
|
1531
|
+
valid_params = {v["parameter"]: v["id"] for v in variables}
|
|
1532
|
+
|
|
1533
|
+
missing = [p for p in values.keys() if p not in valid_params]
|
|
1534
|
+
if missing:
|
|
1535
|
+
raise ForemanAPIError(code=400, text=f"Role '{key}' missing variables: {', '.join(missing)}")
|
|
1536
|
+
|
|
1537
|
+
for param_name, param_value in values.items():
|
|
1538
|
+
variable_id = valid_params[param_name]
|
|
1539
|
+
updated_dict[f"{variable_id}-{param_name}"] = param_value
|
|
1540
|
+
|
|
1541
|
+
# Assign ansible roles
|
|
1542
|
+
logging.debug(f'About to set roles {roles}')
|
|
1543
|
+
self.post(posixpath.join("hosts", hostname, "assign_ansible_roles"), headers=self.headers, json=payload)
|
|
1544
|
+
|
|
1545
|
+
for key, value in updated_dict.items():
|
|
1546
|
+
payload = {
|
|
1547
|
+
"ansible_variable_id": key,
|
|
1548
|
+
"override_value": {
|
|
1549
|
+
"match": f"fqdn={hostname}",
|
|
1550
|
+
"value": value
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
logging.debug(f'About to override ansible variables {key} with value {value}')
|
|
1554
|
+
|
|
1555
|
+
self.post(posixpath.join("ansible", "api", "ansible_override_values"), headers=self.headers, json=payload)
|
|
1556
|
+
sleep(1)
|
|
@@ -3,7 +3,7 @@ oc_cdtapi/Dbsm2API.py,sha256=WDBst1dCAmDVU9d9Ogzwp2nkjtKZ4thU2xG4sAPI_Nk,9963
|
|
|
3
3
|
oc_cdtapi/DevPIAPI.py,sha256=9WND9ld66eHmC5qoLJq3KDYSoO-pP69UqOQsGZNLZYg,1835
|
|
4
4
|
oc_cdtapi/DmsAPI.py,sha256=eNFdwQLhCbPvHB5SUtP4QcZZtSdjkgt_Cxn3oQ3iJ5s,15605
|
|
5
5
|
oc_cdtapi/DmsGetverAPI.py,sha256=ZPU4HlF59fngKu5mSFhtss3rlBuduffDOSm_y3XWrxk,15556
|
|
6
|
-
oc_cdtapi/ForemanAPI.py,sha256=
|
|
6
|
+
oc_cdtapi/ForemanAPI.py,sha256=Qj20vsX1HNj_N0B4Y6kmah7vx4b_xdgiq97uJA3zQiY,56435
|
|
7
7
|
oc_cdtapi/JenkinsAPI.py,sha256=lZ8pe3a4eb_6h53JE7QLuzOSlu7Sqatc9PQwWhio9Vg,15748
|
|
8
8
|
oc_cdtapi/NexusAPI.py,sha256=uU12GtHvKlWorFaPAnFcQ5AGEc94MZ5SdmfM2Pw3F7A,26122
|
|
9
9
|
oc_cdtapi/PgAPI.py,sha256=URSz7qu-Ir7AOj0jI3ucTXn2PM-nC96nmPZI746OLjA,14356
|
|
@@ -12,9 +12,9 @@ oc_cdtapi/RundeckAPI.py,sha256=O3LmcFaHSz8UqeUyIHTTEMJncDD191Utd-iZaeJay2s,24243
|
|
|
12
12
|
oc_cdtapi/TestServer.py,sha256=HV97UWg2IK4gOYAp9yaMdwFUWsw9v66MxyZdI3qQctA,2715
|
|
13
13
|
oc_cdtapi/VaultAPI.py,sha256=P-x_PsWe_S0mGUKTCmR1KhUjdfs7GmyaltjGQcnWj_s,2967
|
|
14
14
|
oc_cdtapi/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
15
|
-
oc_cdtapi-3.
|
|
16
|
-
oc_cdtapi-3.
|
|
17
|
-
oc_cdtapi-3.
|
|
18
|
-
oc_cdtapi-3.
|
|
19
|
-
oc_cdtapi-3.
|
|
20
|
-
oc_cdtapi-3.
|
|
15
|
+
oc_cdtapi-3.27.2.data/scripts/nexus.py,sha256=4teqZ_KtCSrwHDJVgA7lkreteod4Xt5XJFZNbwb7E6E,6858
|
|
16
|
+
oc_cdtapi-3.27.2.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
17
|
+
oc_cdtapi-3.27.2.dist-info/METADATA,sha256=dNcbqIpG8PQlNEElLXIsaomDuk0X82Q8tuUwJ5U-Xjw,504
|
|
18
|
+
oc_cdtapi-3.27.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
19
|
+
oc_cdtapi-3.27.2.dist-info/top_level.txt,sha256=d4-5-D-0CSeSXYuLCP7-nIFCpjkfmJr-Y_muzds8iVU,10
|
|
20
|
+
oc_cdtapi-3.27.2.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|