-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathbases.py
More file actions
140 lines (119 loc) · 4.95 KB
/
bases.py
File metadata and controls
140 lines (119 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import enum
import json
import random
import typing
import attrs
from ctm_python_client.core.comm import Environment
from ctm_python_client.core.monitoring import RunMonitor
class AAPIJob:
pass
class AAPIObject:
def as_aapi_dict(self, ignore_event_type=True):
# Import inside the method to prevent circular import issues
from aapi.ifbase import IfCompletionStatus
res = {}
# Get type if it exists
if '_type' in attrs.fields_dict(self.__class__):
type_value = self._type
should_ignore_type = ignore_event_type and type_value.startswith(('Event', 'Condition'))
if not should_ignore_type:
res['Type'] = type_value
# Process remaining (non-_type) fields
if attrs.has(self):
for field in attrs.fields(self.__class__):
if field.name == '_type': # type already handled
continue
value = self.__getattribute__(field.name)
aapi_repr = field.metadata.get("_aapi_repr_")
if value in [None, [], {}]:
continue
if aapi_repr:
if "_type_aapi_" in field.metadata:
if "_hide_type_" in field.metadata:
res.pop("Type", None)
continue
# Handle different value types
if "as_aapi_dict" in dir(value):
res[aapi_repr] = value.as_aapi_dict()
elif isinstance(value, list):
res[aapi_repr] = [
o.as_aapi_dict() if "as_aapi_dict" in dir(o) else o
for o in value
]
elif isinstance(value, enum.Enum):
res[aapi_repr] = value.value
else:
res[aapi_repr] = value
elif field.metadata.get("_abstract_aapi_container_"):
for obj in value:
obj_ignore_type = ignore_event_type
if isinstance(self, IfCompletionStatus):
obj_ignore_type = False
res[obj.object_name] = obj.as_aapi_dict(ignore_event_type=obj_ignore_type)
else:
res["attributes_valid"] = False
return res
def dumps_aapi(self, indent=None):
return json.dumps(self.as_aapi_dict(), indent=indent)
def dump_aapi(self, f, indent=None):
return json.dump(self.as_aapi_dict(), f, indent=indent)
def as_dict(self):
return attrs.asdict(self)
def run_on_demand(
self,
environment: Environment,
skip_initial_authentication: bool = False,
inpath: str = f"run_on_demand{random.randint(100,999)}",
controlm_server: str = None,
run_as: str = None,
host: str = None,
application: str = None,
sub_application: str = None,
skip_login: bool = False,
file_path: str = None,
delete_afterwards: bool = True,
open_in_browser: str = None,
) -> RunMonitor:
# Import circular dependency
from aapi import Folder
from ctm_python_client.core.workflow import Workflow, WorkflowDefaults
if (hasattr(self, "_type") and "Job" in self._type) or (
hasattr(self, "job_list")
and self.job_list is not None
and len(self.job_list) > 0
):
try:
on_demand_workflow = Workflow(
environment,
WorkflowDefaults(
controlm_server=controlm_server,
run_as=run_as,
host=host,
application=application,
sub_application=sub_application,
),
skip_initial_authentication=skip_initial_authentication,
)
if isinstance(self, Folder):
on_demand_workflow.add(self)
else:
on_demand_workflow.add(self, inpath=inpath)
return on_demand_workflow.run_on_demand(
skip_login=skip_login,
file_path=file_path,
delete_afterwards=delete_afterwards,
open_in_browser=open_in_browser,
)
except Exception as e:
if hasattr(e, "body"):
errors = [
err.get("message", "") + " " + err.get("item", "")
for err in json.loads(e.body)["errors"]
]
raise RuntimeError(f"AAPI request failed: {', '.join(errors)}")
else:
raise e
finally:
on_demand_workflow.clear_all()
else:
raise Exception("Run is not allowed for json without jobs")