oc-cdtapi 3.22.1__py3-none-any.whl → 3.24.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 CHANGED
@@ -2,6 +2,8 @@ import json
2
2
  import logging
3
3
  import posixpath
4
4
  import re
5
+ import time
6
+ from time import sleep
5
7
 
6
8
  from oc_cdtapi.API import HttpAPI, HttpAPIError
7
9
  from collections import namedtuple, defaultdict
@@ -71,7 +73,7 @@ class ForemanAPI(HttpAPI):
71
73
  return posixpath.join(self.root, "api", req)
72
74
  else:
73
75
  return posixpath.join(self.root, req)
74
-
76
+
75
77
  def get_host_by_owner(self, owner):
76
78
  """
77
79
  wrapper for api v1/v2
@@ -84,7 +86,7 @@ class ForemanAPI(HttpAPI):
84
86
  elif self.apiversion == 2:
85
87
  logging.debug('Passing to get_host_by_owner_v2')
86
88
  return self.get_host_by_owner_v2(owner)
87
-
89
+
88
90
  def get_host_by_owner_v2(self, owner):
89
91
  logging.debug('Reached get_host_by_owner_v2')
90
92
  logging.debug('owner = [%s]' % owner)
@@ -95,7 +97,7 @@ class ForemanAPI(HttpAPI):
95
97
  response = self.get('hosts', params=params).json()
96
98
  results = response.get('results')
97
99
  return results
98
-
100
+
99
101
  def get_environment(self, env_name):
100
102
  """
101
103
  wrapper for api v1/v2
@@ -243,7 +245,7 @@ class ForemanAPI(HttpAPI):
243
245
  deploy_on, custom_json):
244
246
  """
245
247
  Creates a host using the default parameters or the ones from an external json
246
- note that create_vm in engine actually sends db_task instead of hostname and custom_json,
248
+ note that create_vm in engine actually sends db_task instead of hostname and custom_json,
247
249
  other parms are ignored
248
250
  """
249
251
  logging.debug('Reached create_host_v1')
@@ -780,7 +782,7 @@ class ForemanAPI(HttpAPI):
780
782
  logging.debug('Reached is_host_powered_on')
781
783
  response = self.get(posixpath.join("hosts", hostname, "power")).json()
782
784
  return response['state'] == 'on'
783
-
785
+
784
786
  def host_power(self, hostname, action):
785
787
  """
786
788
  wrapper for api v1/v2
@@ -875,7 +877,7 @@ class ForemanAPI(HttpAPI):
875
877
  for hostgroup in hostgroups:
876
878
  if hostgroup.get("name") == hostgroup_name:
877
879
  return hostgroup.get ('id')
878
-
880
+
879
881
  logging.debug("Hostgroup [%s] not found, returning None" % hostgroup_name)
880
882
  return None
881
883
 
@@ -980,7 +982,7 @@ class ForemanAPI(HttpAPI):
980
982
  def set_host_expiry_v1(self, hostname, expiry):
981
983
  """
982
984
  Attempts to set host expiry date
983
- :param hostname: full hostname
985
+ :param hostname: full hostname
984
986
  :param expiry: expiry date in format yyyy-mm-dd
985
987
  """
986
988
  logging.debug('Reached set_host_expiry_v1')
@@ -992,7 +994,7 @@ class ForemanAPI(HttpAPI):
992
994
  def set_host_expiry_v2(self, hostname, expiry):
993
995
  """
994
996
  Attempts to set host expiry date
995
- :param hostname: full hostname
997
+ :param hostname: full hostname
996
998
  :param expiry: expiry date in format yyyy-mm-dd
997
999
  """
998
1000
  logging.debug('Reached set_host_expiry_v2')
@@ -1222,14 +1224,14 @@ class ForemanAPI(HttpAPI):
1222
1224
  logging.debug('Reached set_host_owner')
1223
1225
  owner_id = self.get_owner(owner)
1224
1226
  owner_type = 'User'
1225
-
1227
+
1226
1228
  if not owner_id:
1227
1229
  owner_id = self.get_usergroup_id(owner)
1228
1230
  owner_type = 'Usergroup'
1229
-
1231
+
1230
1232
  if not owner_id:
1231
1233
  raise ForemanAPIError(f"The owner [{owner}] is not found")
1232
-
1234
+
1233
1235
  self.update_host(hostname, {"host": {"owner_id": owner_id, "owner_type": owner_type}})
1234
1236
 
1235
1237
 
@@ -1244,3 +1246,111 @@ class ForemanAPI(HttpAPI):
1244
1246
  pl['host'] = {}
1245
1247
  pl['host']['owner_id'] = owner_id
1246
1248
  self.update_host(hostname, pl)
1249
+
1250
+ def set_backup_policy(self, hostname, backup_policy):
1251
+ """
1252
+ Change backup policy
1253
+ :param hostname: str
1254
+ :param backup_policy: str
1255
+ """
1256
+ logging.debug('Reached set_backup_policy')
1257
+ payload = {
1258
+ "parameter": {
1259
+ "value": backup_policy
1260
+ }
1261
+ }
1262
+ self.update_host(hostname=hostname, payload=payload)
1263
+
1264
+ def get_job_template_id(self, template_name):
1265
+ """
1266
+ Get template ID by name. Cache results to avoid repeated API calls.
1267
+ :param template_name: str
1268
+ :return template_id: int
1269
+ """
1270
+ logging.debug('Reached get_job_template_id')
1271
+ if not hasattr(self, '_template_cache'):
1272
+ self._template_cache = {}
1273
+
1274
+ if template_name in self._template_cache:
1275
+ return self._template_cache[template_name]
1276
+
1277
+ response = self.get(posixpath.join("job_templates"), params={'per_page': 'all'})
1278
+ templates = response.json()["results"]
1279
+
1280
+ for job in templates:
1281
+ self._template_cache[job["name"]] = job["id"]
1282
+
1283
+ template_id = self._template_cache.get(template_name)
1284
+ if not template_id:
1285
+ raise ForemanAPIError(f"Job template '{template_name}' not found")
1286
+
1287
+ logging.debug(f"Template id for [{template_name}] is [{template_id}]")
1288
+ return template_id
1289
+
1290
+ def send_job_invocation(self, task_name, vm_name, **inputs):
1291
+ """
1292
+ Send a job invocation for a specific task and VM.
1293
+ :param task_name: str
1294
+ :param vm_name: str
1295
+ :param **inputs: kwargs
1296
+ :return job_id
1297
+ """
1298
+ logging.debug('Reached send_job_invocation')
1299
+ task_configs = {
1300
+ "resize_partition": {
1301
+ "template_name": "Run \"cdt-resize-partition\" role CDT",
1302
+ "description": "Partition Resizing"
1303
+ }
1304
+ }
1305
+
1306
+ config = task_configs.get(task_name)
1307
+ if not config:
1308
+ raise ForemanAPIError(f"Unknown task: {task_name}")
1309
+
1310
+ logging.debug(f"config for [{task_name}] is [{config}]")
1311
+
1312
+ template_id = self.get_job_template_id(config["template_name"])
1313
+
1314
+ payload = {
1315
+ "job_invocation": {
1316
+ "job_template_id": template_id,
1317
+ "targeting_type": "static_query",
1318
+ "search_query": f'name = "{vm_name}"',
1319
+ "description_format": config["description"],
1320
+ "inputs": inputs,
1321
+ "ansible": {
1322
+ "tags": "",
1323
+ "tags_flag": "include"
1324
+ }
1325
+ }
1326
+ }
1327
+
1328
+ logging.debug(f"About to send job invocations with following request [{payload}]")
1329
+ response = self.post(
1330
+ posixpath.join("job_invocations"),
1331
+ headers=self.headers,
1332
+ json=payload
1333
+ )
1334
+
1335
+ return response.json()["id"]
1336
+
1337
+ def is_job_invocation_success(self, job_id, timeout=300, poll_interval=5):
1338
+ """
1339
+ Get a job status by given id.
1340
+ :param job_id: str
1341
+ :param timeout: int
1342
+ :param poll_interval: int
1343
+ :return status: bool
1344
+ """
1345
+ start_time = time.time()
1346
+ while True:
1347
+ if time.time() - start_time > timeout:
1348
+ raise ForemanAPIError(f"Job {job_id} timed out after {timeout} seconds")
1349
+ response = self.get(posixpath.join("job_invocations", str(job_id)))
1350
+ is_pending = bool(response.json().get("pending", False))
1351
+ if not is_pending:
1352
+ break
1353
+ logging.debug(f"job still pending, sleeping for {poll_interval} second")
1354
+ sleep(poll_interval)
1355
+
1356
+ return bool(response.json().get("succeeded", False))
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: oc-cdtapi
3
- Version: 3.22.1
3
+ Version: 3.24.2
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=tdD7PXGPeUq7wgwI8D3QkILB-pOICbQCQMyE9BXZhNE,45046
6
+ oc_cdtapi/ForemanAPI.py,sha256=I8AIOkJk8_HQ_YJclhePUUn_FNRbTkKG00CXmZtuQdk,48727
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.22.1.data/scripts/nexus.py,sha256=4teqZ_KtCSrwHDJVgA7lkreteod4Xt5XJFZNbwb7E6E,6858
16
- oc_cdtapi-3.22.1.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
17
- oc_cdtapi-3.22.1.dist-info/METADATA,sha256=JbARpF0q8pRtbrbo0NedtjD-21ywiWteCpn8cxeHDt8,504
18
- oc_cdtapi-3.22.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
19
- oc_cdtapi-3.22.1.dist-info/top_level.txt,sha256=d4-5-D-0CSeSXYuLCP7-nIFCpjkfmJr-Y_muzds8iVU,10
20
- oc_cdtapi-3.22.1.dist-info/RECORD,,
15
+ oc_cdtapi-3.24.2.data/scripts/nexus.py,sha256=4teqZ_KtCSrwHDJVgA7lkreteod4Xt5XJFZNbwb7E6E,6858
16
+ oc_cdtapi-3.24.2.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
17
+ oc_cdtapi-3.24.2.dist-info/METADATA,sha256=i8t_cC5famMxrEnhwIX_qR40f7w9NomCxNxCzHvmz_E,504
18
+ oc_cdtapi-3.24.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
19
+ oc_cdtapi-3.24.2.dist-info/top_level.txt,sha256=d4-5-D-0CSeSXYuLCP7-nIFCpjkfmJr-Y_muzds8iVU,10
20
+ oc_cdtapi-3.24.2.dist-info/RECORD,,