oc-cdtapi 3.26.2__py3-none-any.whl → 3.28.1__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 CHANGED
@@ -23,7 +23,10 @@ class ForemanAPI(HttpAPI):
23
23
  _error = ForemanAPIError
24
24
  _env_prefix = "FOREMAN"
25
25
 
26
- headers = {"Accept": "version=2,application/json", "Content-Type": "application/json"}
26
+ headers = {
27
+ "Accept": "application/json;version=2",
28
+ "Content-Type": "application/json"
29
+ }
27
30
 
28
31
  def __init__(self, *args, **kwargs):
29
32
  """
@@ -70,7 +73,7 @@ class ForemanAPI(HttpAPI):
70
73
  return self.__foreman_version_major
71
74
 
72
75
  def re(self, req):
73
- if not req.startswith("foreman_puppet"):
76
+ if not req.startswith(("foreman_puppet", "ansible")):
74
77
  return posixpath.join(self.root, "api", req)
75
78
  else:
76
79
  return posixpath.join(self.root, req)
@@ -1200,6 +1203,25 @@ class ForemanAPI(HttpAPI):
1200
1203
  logging.error('Error parsing disk size for [%s]: %s' % (hostname, e))
1201
1204
  return None
1202
1205
 
1206
+ def get_host_memory_mb(self, hostname):
1207
+ """
1208
+ :param hostname: str
1209
+ :return: int
1210
+ """
1211
+ logging.debug('Reached get_host_memory_mb')
1212
+ response = self.get(posixpath.join("hosts", hostname, "vm_compute_attributes"))
1213
+ data = response.json()
1214
+
1215
+ try:
1216
+ memory_mb = data.get("memory_mb")
1217
+ if memory_mb is not None:
1218
+ return int(memory_mb)
1219
+ logging.error('Could not find memory_mb in vm_compute_attributes for [%s]' % hostname)
1220
+ return None
1221
+ except (ValueError, TypeError) as e:
1222
+ logging.error('Error parsing memory_mb for [%s]: %s' % (hostname, e))
1223
+ return None
1224
+
1203
1225
  def get_all_users(self):
1204
1226
  """
1205
1227
  :return: list
@@ -1410,4 +1432,144 @@ class ForemanAPI(HttpAPI):
1410
1432
  return
1411
1433
 
1412
1434
  logging.debug(f"updating {parameter_name}")
1413
- self.put(posixpath.join("hosts", hostname, "parameters", parameter_name), headers=self.headers, json=payload)
1435
+ self.put(posixpath.join("hosts", hostname, "parameters", parameter_name), headers=self.headers, json=payload)
1436
+
1437
+ def get_host_ansible_roles(self, hostname):
1438
+ """
1439
+ Get ansible role from host.
1440
+ :param hostname: str
1441
+ """
1442
+ logging.debug('Reached get_host_ansible_roles')
1443
+
1444
+ response = self.get(posixpath.join("hosts", hostname, "ansible_roles"), headers=self.headers)
1445
+ roles = response.json()
1446
+
1447
+ return roles
1448
+
1449
+ def get_ansible_role(self, roles=None):
1450
+ """
1451
+ Get ansible role id by its name.
1452
+ :param role_name: str
1453
+ """
1454
+ logging.debug('Reached get_ansible_role')
1455
+
1456
+ if not roles:
1457
+ params = {'per_page': 'all'}
1458
+ response = self.get(posixpath.join("ansible", "api", "ansible_roles"), params=params, headers=self.headers).json()
1459
+
1460
+ logging.debug(f"About to return {response.get('subtotal')} roles")
1461
+ return response.get("results")
1462
+
1463
+ params = {'search': None}
1464
+
1465
+ if isinstance(roles, (str, int)):
1466
+ roles = [roles]
1467
+
1468
+ query = []
1469
+ for role in roles:
1470
+ if isinstance(role, str):
1471
+ query.append(f"name={role}")
1472
+ elif isinstance(role, int):
1473
+ query.append(f"id={role}")
1474
+ else:
1475
+ raise ForemanAPIError(f"Invalid role type: {type(role)}")
1476
+
1477
+ params["search"] = " or ".join(query)
1478
+
1479
+ logging.debug(f"Search param is {params.get('search')}")
1480
+ response = self.get(posixpath.join("ansible", "api", "ansible_roles"), params=params, headers=self.headers).json()
1481
+
1482
+ logging.debug(f"About to return {response.get('subtotal')} roles")
1483
+ return response.get("results")
1484
+
1485
+ def assign_ansible_roles(self, hostname, roles):
1486
+ """
1487
+ Assign an ansible roles and override value by given role_id and kwargs to specific hostname.
1488
+ :param hostname: str
1489
+ :param roles: list/str/int
1490
+ """
1491
+ logging.debug('Reached assign_ansible_roles')
1492
+ logging.debug(f'Hostname: [{hostname}]')
1493
+ logging.debug(f'Roles: [{roles}]')
1494
+
1495
+ role_ids = []
1496
+ role_names = []
1497
+
1498
+ if isinstance(roles, (str, int)):
1499
+ roles = [roles]
1500
+
1501
+ roles = self.get_ansible_role(roles)
1502
+ for role in roles:
1503
+ role_ids.append(role.get("id"))
1504
+ role_names.append(role.get("name"))
1505
+
1506
+ payload = {
1507
+ "ansible_role_ids": role_ids
1508
+ }
1509
+
1510
+ # Assign ansible roles
1511
+ logging.debug(f'About to set roles {role_names}')
1512
+ self.post(posixpath.join("hosts", hostname, "assign_ansible_roles"), headers=self.headers, json=payload)
1513
+
1514
+ def assign_ansible_roles_and_override(self, hostname, roles):
1515
+ """
1516
+ Assign an ansible roles and override value by given role_id and kwargs to specific hostname.
1517
+ :param hostname: str
1518
+ :param roles: dict
1519
+ """
1520
+ logging.debug('Reached assign_ansible_roles_and_override')
1521
+ logging.debug(f'Hostname: [{hostname}]')
1522
+ logging.debug(f'Roles: [{roles}]')
1523
+
1524
+ role_ids = []
1525
+ updated_dict = {}
1526
+
1527
+ if not isinstance(roles, dict):
1528
+ raise ForemanAPIError(code=400, text="Input must be in dict")
1529
+
1530
+ temp_roles = []
1531
+ for role, variable in roles.items():
1532
+ temp_roles.append(role)
1533
+
1534
+ new_roles = self.get_ansible_role(temp_roles)
1535
+ for new_role in new_roles:
1536
+ role_ids.append(new_role.get("id"))
1537
+ if not roles.get(new_role.get("id")):
1538
+ continue
1539
+
1540
+ roles[new_role.get("name")] = roles.pop(new_role.get("id"))
1541
+
1542
+ payload = {
1543
+ "ansible_role_ids": role_ids
1544
+ }
1545
+
1546
+ for key, values in roles.items():
1547
+ params = {"search": f"ansible_role={key}", "per_page": "all"}
1548
+
1549
+ variables = self.get(posixpath.join("ansible", "api", "ansible_variables"), params=params).json()["results"]
1550
+ valid_params = {v["parameter"]: v["id"] for v in variables}
1551
+
1552
+ missing = [p for p in values.keys() if p not in valid_params]
1553
+ if missing:
1554
+ raise ForemanAPIError(code=400, text=f"Role '{key}' missing variables: {', '.join(missing)}")
1555
+
1556
+ for param_name, param_value in values.items():
1557
+ variable_id = valid_params[param_name]
1558
+ updated_dict[f"{variable_id}-{param_name}"] = param_value
1559
+
1560
+ # Assign ansible roles
1561
+ logging.debug(f'About to set roles {roles}')
1562
+ self.post(posixpath.join("hosts", hostname, "assign_ansible_roles"), headers=self.headers, json=payload)
1563
+
1564
+ for key, value in updated_dict.items():
1565
+ payload = {
1566
+ "ansible_variable_id": key,
1567
+ "override_value": {
1568
+ "match": f"fqdn={hostname}",
1569
+ "value": value
1570
+ }
1571
+ }
1572
+ logging.debug(f'About to override ansible variables {key} with value {value}')
1573
+
1574
+ self.post(posixpath.join("ansible", "api", "ansible_override_values"), headers=self.headers, json=payload)
1575
+ sleep(1)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: oc-cdtapi
3
- Version: 3.26.2
3
+ Version: 3.28.1
4
4
  Summary: Custom Development python API libraries
5
5
  License: Apache2.0
6
6
  Requires-Python: >=3.6
@@ -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=blS0E2fyF2cgdnIT5itTrLExWRODBRzaOhAG35QnKN0,51393
6
+ oc_cdtapi/ForemanAPI.py,sha256=0GsC9s9uGoYgMbsgBW5aDEGoGLGeRPJJSTAWhbelhVY,57135
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.26.2.data/scripts/nexus.py,sha256=4teqZ_KtCSrwHDJVgA7lkreteod4Xt5XJFZNbwb7E6E,6858
16
- oc_cdtapi-3.26.2.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
17
- oc_cdtapi-3.26.2.dist-info/METADATA,sha256=633OzwA9vkGtpD5kA4ebTHVzrLL12isuJXUb2gSWxgc,504
18
- oc_cdtapi-3.26.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
19
- oc_cdtapi-3.26.2.dist-info/top_level.txt,sha256=d4-5-D-0CSeSXYuLCP7-nIFCpjkfmJr-Y_muzds8iVU,10
20
- oc_cdtapi-3.26.2.dist-info/RECORD,,
15
+ oc_cdtapi-3.28.1.data/scripts/nexus.py,sha256=4teqZ_KtCSrwHDJVgA7lkreteod4Xt5XJFZNbwb7E6E,6858
16
+ oc_cdtapi-3.28.1.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
17
+ oc_cdtapi-3.28.1.dist-info/METADATA,sha256=0VzwXmwuzjqdH9LNfO08rOSM9yoc5aQn5PkbNPznHK8,504
18
+ oc_cdtapi-3.28.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
19
+ oc_cdtapi-3.28.1.dist-info/top_level.txt,sha256=d4-5-D-0CSeSXYuLCP7-nIFCpjkfmJr-Y_muzds8iVU,10
20
+ oc_cdtapi-3.28.1.dist-info/RECORD,,