From 286592aee964d413d10db5fdd67dd8c7d06d2005 Mon Sep 17 00:00:00 2001 From: RefaelBeker7 Date: Sun, 30 Jun 2024 15:12:19 +0300 Subject: [PATCH] fix run_as job and sub_folder in folder objs --- setup.py | 2 +- src/ctm_python_client/__init__.py | 2 +- src/ctm_python_client/core/workflow.py | 18 +++++--- tests/test_aapi.py | 59 ++++++++++++++++++++++++++ tests/test_sanity.py | 2 +- 5 files changed, 74 insertions(+), 9 deletions(-) diff --git a/setup.py b/setup.py index b12f7b0..400215c 100644 --- a/setup.py +++ b/setup.py @@ -8,7 +8,7 @@ name="ctm-python-client", packages=find_packages(where="src"), package_dir={"": "src"}, - version="2.3.0", + version="2.3.1", description="Python Workflows for Control-M", long_description=long_description, long_description_content_type='text/markdown', diff --git a/src/ctm_python_client/__init__.py b/src/ctm_python_client/__init__.py index 08030bb..1ddd3d7 100644 --- a/src/ctm_python_client/__init__.py +++ b/src/ctm_python_client/__init__.py @@ -1,2 +1,2 @@ -__version__ = '2.3.0' +__version__ = '2.3.1' __author__ = 'BMC Software' \ No newline at end of file diff --git a/src/ctm_python_client/core/workflow.py b/src/ctm_python_client/core/workflow.py index 07c1744..d96c7c1 100644 --- a/src/ctm_python_client/core/workflow.py +++ b/src/ctm_python_client/core/workflow.py @@ -89,21 +89,27 @@ def clear_all(self): self._connections = {} def _apply_defaults_for_type(self, obj: typing.Any, type_: str): - for field in attrs.fields(WorkflowDefaults): - if type_ in field.metadata.get('applyon') \ - and self.defaults.__getattribute__(field.name): - if obj.__getattribute__(field.name) is None: - obj.__setattr__( - field.name, self.defaults.__getattribute__(field.name)) + if not obj: + return + else: + for field in attrs.fields(WorkflowDefaults): + if type_ in field.metadata.get('applyon') \ + and self.defaults.__getattribute__(field.name): + if obj.__getattribute__(field.name) is None: + obj.__setattr__( + field.name, self.defaults.__getattribute__(field.name)) def _apply_defaults_for_job(self, obj: Job): self._apply_defaults_for_type(obj, 'Job') def _apply_defaults_for_folder(self, obj: Folder): self._apply_defaults_for_type(obj, 'Folder') + list(map(self._apply_defaults_for_SubFolder, obj.sub_folder_list)) + list(map(self._apply_defaults_for_job, obj.job_list)) def _apply_defaults_for_SimpleFolder(self, obj: SimpleFolder): self._apply_defaults_for_type(obj, 'SimpleFolder') + list(map(self._apply_defaults_for_job, obj.job_list)) def _apply_defaults_for_SubFolder(self, obj: SubFolder): self._apply_defaults_for_type(obj, 'SubFolder') diff --git a/tests/test_aapi.py b/tests/test_aapi.py index 25904db..ffbf57f 100644 --- a/tests/test_aapi.py +++ b/tests/test_aapi.py @@ -90,3 +90,62 @@ def test_events(): assert any(isinstance(obj, aapi.AddEvents) and any(event.event == 'add2' for event in obj.events) for obj in job.add_events_list) assert json.loads(job.dumps_aapi()) + +def test_job_in_folder_run_as(): + workflow = Workflow(Environment.create_workbench("refael"), WorkflowDefaults(run_as='workbench'), skip_initial_authentication=True) + job_command = aapi.JobCommand('TestJob', command='echo Hello') + + sla_job = aapi.JobSLAManagement('ForecastSLA', + service_name='ForecastSLA', + service_priority='1', + job_runs_deviations_tolerance='1', + complete_in=aapi.JobSLAManagement.CompleteIn(time='00:15'), + complete_by=aapi.JobSLAManagement.CompleteBy( + time='21:40', days='3'), + average_run_time_tolerance=aapi.JobSLAManagement.AverageRunTimeTolerance( + average_run_time='15', + units=aapi.JobSLAManagement.AverageRunTimeTolerance.Units.Minutes + )) + + subfolder = aapi.SubFolder('SubFolder') + folder = aapi.Folder("TestFolder", + sub_folder_list=[subfolder], + job_list=[sla_job, job_command] + ) + workflow.add(folder) + # {"TestFolder": {"Type": "Folder", "ForecastSLA": {"Type": "Job:SLAManagement", "RunAs": "workbench", "ServiceName": "ForecastSLA", "ServicePriority": "1", "JobRunsDeviationsTolerance": "1", "AverageRunTimeTolerance": {"AverageRunTime": "15", "Units": "Minutes"}, "CompleteBy": {"Time": "21:40", "Days": "3"}, "CompleteIn": {"Time": "00:15"}}, "TestJob": {"Type": "Job:Command", "RunAs": "workbench", "Command": "echo Hello"}, "RunAs": "workbench", "SubFolder": {"Type": "SubFolder", "RunAs": "workbench"}}} + o = json.loads(''' + { + "Type": "Folder", + "ForecastSLA": { + "Type": "Job:SLAManagement", + "RunAs": "workbench", + "ServiceName": "ForecastSLA", + "ServicePriority": "1", + "JobRunsDeviationsTolerance": "1", + "AverageRunTimeTolerance": { + "AverageRunTime": "15", + "Units": "Minutes" + }, + "CompleteBy": { + "Time": "21:40", + "Days": "3" + }, + "CompleteIn": { + "Time": "00:15" + } + }, + "TestJob": { + "Type": "Job:Command", + "RunAs": "workbench", + "Command": "echo Hello" + }, + "RunAs": "workbench", + "SubFolder": { + "Type": "SubFolder", + "RunAs": "workbench" + } + } + ''') + assert workflow.get("TestFolder").as_aapi_dict() == o + diff --git a/tests/test_sanity.py b/tests/test_sanity.py index f982410..15cc4f1 100644 --- a/tests/test_sanity.py +++ b/tests/test_sanity.py @@ -2,6 +2,6 @@ def test_version_author(): import ctm_python_client assert ctm_python_client.__author__ == 'BMC Software' - assert ctm_python_client.__version__ == '2.3.0' + assert ctm_python_client.__version__ == '2.3.1'