oc-cdtapi 3.22.1__py3-none-any.whl → 3.23.4__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
@@ -1244,3 +1246,97 @@ 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 get_job_template_id(self, template_name):
1251
+ """
1252
+ Get template ID by name. Cache results to avoid repeated API calls.
1253
+ :param template_name: str
1254
+ :return template_id: int
1255
+ """
1256
+ logging.debug('Reached get_job_template_id')
1257
+ if not hasattr(self, '_template_cache'):
1258
+ self._template_cache = {}
1259
+
1260
+ if template_name in self._template_cache:
1261
+ return self._template_cache[template_name]
1262
+
1263
+ response = self.get(posixpath.join("job_templates"), params={'per_page': 'all'})
1264
+ templates = response.json()["results"]
1265
+
1266
+ for job in templates:
1267
+ self._template_cache[job["name"]] = job["id"]
1268
+
1269
+ template_id = self._template_cache.get(template_name)
1270
+ if not template_id:
1271
+ raise ForemanAPIError(f"Job template '{template_name}' not found")
1272
+
1273
+ logging.debug(f"Template id for [{template_name}] is [{template_id}]")
1274
+ return template_id
1275
+
1276
+ def send_job_invocation(self, task_name, vm_name, **inputs):
1277
+ """
1278
+ Send a job invocation for a specific task and VM.
1279
+ :param task_name: str
1280
+ :param vm_name: str
1281
+ :param **inputs: kwargs
1282
+ :return job_id
1283
+ """
1284
+ logging.debug('Reached send_job_invocation')
1285
+ task_configs = {
1286
+ "resize_partition": {
1287
+ "template_name": "Run \"cdt-resize-partition\" role CDT",
1288
+ "description": "Partition Resizing"
1289
+ }
1290
+ }
1291
+
1292
+ config = task_configs.get(task_name)
1293
+ if not config:
1294
+ raise ForemanAPIError(f"Unknown task: {task_name}")
1295
+
1296
+ logging.debug(f"config for [{task_name}] is [{config}]")
1297
+
1298
+ template_id = self.get_job_template_id(config["template_name"])
1299
+
1300
+ payload = {
1301
+ "job_invocation": {
1302
+ "job_template_id": template_id,
1303
+ "targeting_type": "static_query",
1304
+ "search_query": f'name = "{vm_name}"',
1305
+ "description_format": config["description"],
1306
+ "inputs": inputs,
1307
+ "ansible": {
1308
+ "tags": "",
1309
+ "tags_flag": "include"
1310
+ }
1311
+ }
1312
+ }
1313
+
1314
+ logging.debug(f"About to send job invocations with following request [{payload}]")
1315
+ response = self.post(
1316
+ posixpath.join("job_invocations"),
1317
+ headers=self.headers,
1318
+ json=payload
1319
+ )
1320
+
1321
+ return response.json()["id"]
1322
+
1323
+ def is_job_invocation_success(self, job_id, timeout=300, poll_interval=5):
1324
+ """
1325
+ Get a job status by given id.
1326
+ :param job_id: str
1327
+ :param timeout: int
1328
+ :param poll_interval: int
1329
+ :return status: bool
1330
+ """
1331
+ start_time = time.time()
1332
+ while True:
1333
+ if time.time() - start_time > timeout:
1334
+ raise ForemanAPIError(f"Job {job_id} timed out after {timeout} seconds")
1335
+ response = self.get(posixpath.join("job_invocations", str(job_id)))
1336
+ is_pending = bool(response.json().get("pending", False))
1337
+ if not is_pending:
1338
+ break
1339
+ logging.debug(f"job still pending, sleeping for {poll_interval} second")
1340
+ sleep(poll_interval)
1341
+
1342
+ 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.23.4
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=oL_OdUgRL_7z9te83lmLarInDaKWW1_rjwXaWTZajIY,48401
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.23.4.data/scripts/nexus.py,sha256=4teqZ_KtCSrwHDJVgA7lkreteod4Xt5XJFZNbwb7E6E,6858
16
+ oc_cdtapi-3.23.4.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
17
+ oc_cdtapi-3.23.4.dist-info/METADATA,sha256=vm9AmaXHNBtw1DznVmDF9PLEfIDC1xn0bNrxE-mV0uU,504
18
+ oc_cdtapi-3.23.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
19
+ oc_cdtapi-3.23.4.dist-info/top_level.txt,sha256=d4-5-D-0CSeSXYuLCP7-nIFCpjkfmJr-Y_muzds8iVU,10
20
+ oc_cdtapi-3.23.4.dist-info/RECORD,,