Skip to content

Commit 94728a4

Browse files
committed
refactoring
1 parent 3f9cd99 commit 94728a4

File tree

4 files changed

+62
-30
lines changed

4 files changed

+62
-30
lines changed

src/sasctl/_services/model_management.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ def list_model_workflow_definition(self):
243243
from .workflow import Workflow
244244
wf = Workflow()
245245

246-
return wf.list_workflow_enableddefinitions()
246+
return wf.list_enabled_definitions()
247247

248248
def list_model_workflow_prompt(self, workflowName):
249249
"""List prompt Workflow Processes Definitions.
@@ -308,6 +308,7 @@ def execute_model_workflow_definition(self, projectName, workflowName, input=Non
308308
"""
309309
from .model_repository import ModelRepository
310310
from .workflow import Workflow
311+
311312
mr = ModelRepository()
312313
wf = Workflow()
313314

@@ -317,18 +318,22 @@ def execute_model_workflow_definition(self, projectName, workflowName, input=Non
317318

318319
#Associations running workflow to model project, note workflow has to be running
319320
# THINK ABOUT: do we do a check on status of the workflow to determine if it is still running before associating?
320-
321-
input={"processName":workflow['name'],"processId":workflow['id'],"objectType":"MM_Project",
322-
"solutionObjectName":projectName,"solutionObjectId":project['id'],
323-
"solutionObjectUri":"/modelRepository/projects/"+project['id'],
324-
"solutionObjectMediaType":"application/vnd.sas.models.project+json"}
321+
322+
input = {"processName": workflow['name'],
323+
"processId": workflow['id'],
324+
"objectType": "MM_Project",
325+
"solutionObjectName": projectName,
326+
"solutionObjectId": project['id'],
327+
"solutionObjectUri": "/modelRepository/projects/" + project['id'],
328+
"solutionObjectMediaType": "application/vnd.sas.models.project+json"}
325329

326330
#Note, you can get a HTTP Error 404: {"errorCode":74052,"message":"The workflow process for id <> cannot be found.
327331
# Associations can only be made to running processes.","details":["correlator:
328332
# e62c5562-2b11-45db-bcb7-933200cb0f0a","traceId: 3118c0fb1eb9702d","path:
329333
# /modelManagement/workflowAssociations"],"links":[],"version":2,"httpStatusCode":404}
330334
# Which is fine and expected like the Visual Experience.
331-
return self.post('/workflowAssociations', json=input,
332-
headers={'Content-Type': 'application/vnd.sas.workflow.object.association+json'})
335+
return self.post('/workflowAssociations',
336+
json=input,
337+
headers={'Content-Type': 'application/vnd.sas.workflow.object.association+json'})
333338

334339

src/sasctl/_services/workflow.py

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,55 @@
66

77
from .service import Service
88

9+
910
class Workflow(Service):
1011
"""The Workflow API provides basic resources for list, prompt details,
1112
and running workflow processes.
1213
"""
1314
_SERVICE_ROOT= '/workflow'
1415

16+
def list_definitions(self,
17+
include_enabled=True,
18+
include_disabled=False):
19+
"""List workflow definitions.
1520
16-
def list_workflow_enableddefinitions(self):
17-
"""List all enabled Workflow Processes Definitions.
21+
Parameters
22+
----------
23+
include_enabled : bool, optional
24+
Include enabled definitions in the results. Defaults to True.
25+
include_disabled : bool, optional
26+
Include disabled definitions in the results. Defaults to False.
1827
1928
Returns
2029
-------
21-
RestObj
22-
The list of workflows
30+
list
31+
The list of definitions.
2332
2433
"""
34+
if include_enabled and include_disabled:
35+
url = '/definitions'
36+
elif include_enabled:
37+
url = '/enabledDefinitions'
38+
elif include_disabled:
39+
url = '/disabledDefinitions'
40+
else:
41+
return []
42+
43+
# Header required to prevent 400 ERROR Bad Request
44+
return self.get(url, headers={"Accept-Language": "en-US"})
45+
46+
def list_enabled_definitions(self):
47+
"""List process definitions that are currently enabled.
48+
49+
Returns
50+
-------
51+
list
52+
The list of definitions.
53+
54+
"""
55+
return self.list_definitions(include_enabled=True,
56+
include_disabled=False)
2557

26-
#Additional header to fix 400 ERROR Bad Request
27-
headers={"Accept-Language": "en-US"}
28-
return self.get('/enabledDefinitions', headers=headers)
29-
3058
def list_workflow_prompt(self, name):
3159
"""List prompt Workflow Processes Definitions.
3260
@@ -42,7 +70,7 @@ def list_workflow_prompt(self, name):
4270
4371
"""
4472

45-
ret = self.__find_specific_workflow(name)
73+
ret = self._find_specific_workflow(name)
4674
if ret is None:
4775
raise ValueError("No Workflow enabled for %s name or id." % name)
4876

@@ -68,8 +96,8 @@ def run_workflow_definition(self, name, input=None):
6896
The executing workflow
6997
7098
"""
71-
72-
workflow = self.__find_specific_workflow(name)
99+
100+
workflow = self._find_specific_workflow(name)
73101
if workflow is None:
74102
raise ValueError("No Workflow enabled for %s name or id." % name)
75103

@@ -80,11 +108,11 @@ def run_workflow_definition(self, name, input=None):
80108
return self.post('/processes?definitionId=' + workflow['id'], json=input,
81109
headers={'Content-Type': 'application/vnd.sas.workflow.variables+json'})
82110

83-
def __find_specific_workflow(self, name):
111+
def _find_specific_workflow(self, name):
84112
# Internal helper method
85113
# Finds a workflow with the name (can be a name or id)
86114
# Returns a dict objects of the workflow
87-
listendef = self.list_workflow_enableddefinitions()
115+
listendef = self.list_enabled_definitions()
88116
for tmp in listendef:
89117
if tmp['name'] == name:
90118
return tmp

tests/unit/test_model_management.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ def test_execute_model_workflow_definition_invalidworkflow():
9090
WORKFLOWS = [{'name': 'Test W', 'id': '12345'},{'name': 'TestW2', 'id': '98765', 'prompts': [{'id': '98765', 'variableName': 'projectId', 'variableType': 'string'}]}]
9191

9292
with mock.patch('sasctl._services.workflow.Workflow'
93-
'.list_workflow_enableddefinitions') as list_workflow_enableddefinitions:
93+
'.list_enabled_definitions') as list_workflow_enableddefinitions:
9494
with mock.patch('sasctl._services.model_repository.ModelRepository'
9595
'.get_project') as get_project:
9696
list_workflow_enableddefinitions.return_value = WORKFLOWS

tests/unit/test_workflow.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,21 @@ def test_list_workflow_prompt_invalidworkflow():
1717
wf = workflow.Workflow()
1818

1919
with mock.patch('sasctl._services.workflow.Workflow'
20-
'.list_workflow_enableddefinitions') as list_workflow_enableddefinitions:
21-
list_workflow_enableddefinitions.return_value = WORKFLOWS
20+
'.list_definitions') as list_definitions:
21+
list_definitions.return_value = WORKFLOWS
2222
with pytest.raises(ValueError):
2323
# Project missing
2424
_ = wf.list_workflow_prompt('bad')
2525

26+
2627
def test_list_workflow_prompt_workflownoprompt():
2728

2829
WORKFLOWS = [{'name': 'Test W', 'id': '12345'},{'name': 'TestW2', 'id': '98765', 'prompts': [{'id': '98765', 'variableName': 'projectId', 'variableType': 'string'}]}]
2930
wf = workflow.Workflow()
3031

3132
with mock.patch('sasctl._services.workflow.Workflow'
32-
'.list_workflow_enableddefinitions') as list_workflow_enableddefinitions:
33-
list_workflow_enableddefinitions.return_value = WORKFLOWS
33+
'.list_definitions') as list_definitions:
34+
list_definitions.return_value = WORKFLOWS
3435

3536
#Testing no prompts on workflow with name
3637
testresult = wf.list_workflow_prompt('Test W')
@@ -43,22 +44,20 @@ def test_list_workflow_prompt_workflownoprompt():
4344
assert testresult is None
4445

4546

46-
4747
def test_list_workflow_prompt_workflowprompt():
4848

4949
WORKFLOWS = [{'name': 'Test W', 'id': '12345'},{'name': 'TestW2', 'id': '98765', 'prompts': [{'id': '98765', 'variableName': 'projectId', 'variableType': 'string'}]}]
5050
wf = workflow.Workflow()
5151

5252
with mock.patch('sasctl._services.workflow.Workflow'
53-
'.list_workflow_enableddefinitions') as list_workflow_enableddefinitions:
54-
list_workflow_enableddefinitions.return_value = WORKFLOWS
53+
'.list_definitions') as list_definitions:
54+
list_definitions.return_value = WORKFLOWS
5555

5656
#Testing workflow with id and prompts
5757
testresult = wf.list_workflow_prompt('TestW2')
5858
print(testresult)
5959
assert testresult is not None
6060

61-
6261
#Testing workflow with id and prompts
6362
testresult = wf.list_workflow_prompt('98765')
6463
print(testresult)

0 commit comments

Comments
 (0)