Skip to content

Commit

Permalink
Merge pull request #67 from controlm/behavior-of-events
Browse files Browse the repository at this point in the history
Events behavior - #29
  • Loading branch information
rbeker-BMC authored Mar 4, 2024
2 parents c18222c + be0387c commit 8f0837d
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 13 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
name="ctm-python-client",
packages=find_packages(where="src"),
package_dir={"": "src"},
version="2.2.0",
version="2.2.1",
description="Python Workflows for Control-M",
long_description=long_description,
long_description_content_type='text/markdown',
Expand Down
3 changes: 2 additions & 1 deletion src/aapi/addevents.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ class AddEvents(AAPIObject):

def __attrs_post_init__(self):
if self.object_name == attrs.fields_dict(self.__class__)['object_name'].default:
self.object_name = f'{self.object_name}_{random.choices(string.ascii_letters + string.digits, k=8)}'
random_attr = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
self.object_name = f'{self.object_name}_{random_attr}'
3 changes: 2 additions & 1 deletion src/aapi/deleteevents.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ class DeleteEvents(AAPIObject):

def __attrs_post_init__(self):
if self.object_name == attrs.fields_dict(self.__class__)['object_name'].default:
self.object_name = f'{self.object_name}_{random.choices(string.ascii_letters + string.digits, k=8)}'
random_attr = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
self.object_name = f'{self.object_name}_{random_attr}'
3 changes: 2 additions & 1 deletion src/aapi/waitforevents.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ class WaitForEvents(AAPIObject):

def __attrs_post_init__(self):
if self.object_name == attrs.fields_dict(self.__class__)['object_name'].default:
self.object_name = f'{self.object_name}_{random.choices(string.ascii_letters + string.digits, k=8)}'
random_attr = ''.join(random.choices(string.ascii_letters + string.digits, k=8))
self.object_name = f'{self.object_name}_{random_attr}'
2 changes: 1 addition & 1 deletion src/ctm_python_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = '2.2.0'
__version__ = '2.2.1'
__author__ = 'BMC Software'
6 changes: 3 additions & 3 deletions src/ctm_python_client/core/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,19 +208,19 @@ def connect(self, srcpath: str, destpath: str, *, inpath: str = None):
# support backward compatibility
if not obj_src.add_events_list:
obj_src.add_events_list.append(
AddEvents('events_to_add', events=[]))
AddEvents(events=[]))
obj_src.add_events_list[-1].events.append(
ConditionOutAdd(event=event_name))

if not obj_dest.wait_for_events_list:
obj_dest.wait_for_events_list.append(
WaitForEvents('events_to_wait', events=[]))
WaitForEvents(events=[]))
obj_dest.wait_for_events_list[-1].events.append(
ConditionIn(event=event_name))

if not obj_dest.delete_events_list:
obj_dest.delete_events_list.append(
DeleteEvents('events_to_delete', events=[]))
DeleteEvents(events=[]))
obj_dest.delete_events_list[-1].events.append(
ConditionOutDelete(event=event_name))

Expand Down
54 changes: 50 additions & 4 deletions tests/test_aapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,58 @@ def test_json_generation():

def test_copy_workflow():
workflow = Workflow(Environment.create_workbench("refael"), WorkflowDefaults(run_as='workbench'), skip_initial_authentication=True)
workflow.add(aapi.JobCommand('MyFirstJob', command='ls -lt', run_as='workbench'), inpath='MyFirstFolder')
workflow.add(aapi.JobCommand('TestJob', command='ls -lt', run_as='workbench'), inpath='TestFolder')

newworkflow = BaseWorkflow()
newworkflow.copy_from(workflow=workflow)

assert newworkflow.get("MyFirstFolder").dumps_aapi(indent=2) == workflow.get("MyFirstFolder").dumps_aapi(indent=2)
assert newworkflow.get("TestFolder").dumps_aapi(indent=2) == workflow.get("TestFolder").dumps_aapi(indent=2)

o = json.loads('{\n "Type": "Folder",\n "MyFirstJob": {\n "Type": "Job:Command",\n "RunAs": "workbench",\n "Command": "ls -lt"\n },\n "RunAs": "workbench"\n}')
assert newworkflow.get("MyFirstFolder").as_aapi_dict() == o
o = json.loads('{\n "Type": "Folder",\n "TestJob": {\n "Type": "Job:Command",\n "RunAs": "workbench",\n "Command": "ls -lt"\n },\n "RunAs": "workbench"\n}')
assert newworkflow.get("TestFolder").as_aapi_dict() == o

def test_connecting_jobs():
workflow = Workflow(Environment.create_workbench("refael"), WorkflowDefaults(run_as='workbench'), skip_initial_authentication=True)
firstjob = workflow.add(
aapi.JobCommand('JobToConnect1', command="first"),
inpath='TestFolder'
)

secondjob = workflow.add(
aapi.JobCommand('JobToConnect2', command="second"),
inpath='TestFolder'
)
workflow.connect(firstjob, secondjob)

def test_chaining_jobs():
workflow = Workflow(Environment.create_workbench("refael"), WorkflowDefaults(run_as='workbench'), skip_initial_authentication=True)
workflow.chain(
[
aapi.JobCommand('FirstTestJob', "First"),
aapi.JobCommand('SecondTestJob', "Second"),
aapi.JobCommand('ThirdTestJob', "Third", run_as_dummy=True),
],
inpath='ChainedJobs'
)

def test_events():
job = aapi.JobCommand('Job1', command='echo Hello')

waitForEventList = aapi.WaitForEvents([aapi.Event(event="wait1"), aapi.Event(event="wait2", date=aapi.Event.Date.AnyDate)])
job.event_list.append(waitForEventList)

deleteEventListObject = aapi.DeleteEvents([aapi.EventOutDelete(event="delete")])
job.event_list.append(deleteEventListObject)

addEventListObject1 = aapi.AddEvents([aapi.EventOutAdd(event="add1")])
job.event_list.append(addEventListObject1)

addEventListObject2 = aapi.AddEvents([aapi.EventOutAdd(event="add2",date=aapi.Event.Date.NoDate)])
job.add_events_list.append(addEventListObject2)

assert any(isinstance(obj, aapi.WaitForEvents) and any(event.event == 'wait2' for event in obj.events) for obj in job.event_list)
assert any(isinstance(obj, aapi.DeleteEvents) and any(event.event == 'delete' for event in obj.events) for obj in job.event_list)
assert any(isinstance(obj, aapi.AddEvents) and any(event.event == 'add1' for event in obj.events) for obj in job.event_list)
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())
2 changes: 1 addition & 1 deletion tests/test_sanity.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.2.0'
assert ctm_python_client.__version__ == '2.2.1'


0 comments on commit 8f0837d

Please sign in to comment.