/
operation.py
109 lines (85 loc) · 3.36 KB
/
operation.py
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
from . import stack
from oslo_versionedobjects import base
from oslo_versionedobjects import fields
class SubObject(fields.FieldType):
def __init__(self, obj_names, **kwargs):
self._obj_names = obj_names
super(SubObject, self).__init__(**kwargs)
def coerce(self, obj, attr, value):
try:
obj_name = value.obj_name()
except AttributeError:
obj_name = ""
if obj_name not in self._obj_names:
raise ValueError(_('An object of type %(types)s is required '
'in field %(attr)s') %
{'type': ",".join(self._obj_names), 'attr': attr})
return value
@staticmethod
def to_primitive(obj, attr, value):
return value.obj_to_primitive()
@staticmethod
def from_primitive(obj, attr, value):
# FIXME(danms): Avoid circular import from base.py
from oslo_versionedobjects import base as obj_base
# NOTE (ndipanov): If they already got hydrated by the serializer, just
# pass them back unchanged
if isinstance(value, obj_base.VersionedObject):
return value
return obj_base.VersionedObject.obj_from_primitive(value, obj._context)
def describe(self):
return "Object<%s>" % ",".join(self._obj_names)
def stringify(self, value):
if 'uuid' in value.fields:
ident = '(%s)' % (value.obj_attr_is_set('uuid') and value.uuid or
'UNKNOWN')
elif 'id' in value.fields:
ident = '(%s)' % (value.obj_attr_is_set('id') and value.id or
'UNKNOWN')
else:
ident = ''
return '%s%s' % (",".join(self._obj_names), ident)
# This inheritance is a hack so the annoying isinstance check
# in obj_reset_changes() processes our custom field :-(
# Really the Field class should have a obj_reset_changes
# method so VersionedObject doesn't have to hardcode this
# list of types
class ListOfSubObjectsField(fields.ListOfObjectsField):
def __init__(self, objtypes, **kwargs):
self.AUTO_TYPE = fields.List(SubObject(objtypes))
super(ListOfSubObjectsField, self).__init__(objtypes[0], **kwargs)
class Operation(base.VersionedObject):
# Version 1.0: Initial version
VERSION = "1.0"
OBJ_PROJECT_NAMESPACE = "devsupport"
fields = {
'start': fields.DateTimeField(nullable=True),
'finish': fields.DateTimeField(nullable=True),
'callstack': fields.ObjectField('StackTrace',
nullable=True),
'exception': fields.ObjectField('ExceptionTrace'),
'children': ListOfSubObjectsField(
['CommandExecute',
'DatabaseRequest',
'EntryPoint',
'Group',
'HTTPRequest',
'HTTPDispatch',
'MessagingCall',
'MessagingCast',
'MessagingDispatch',
'MethodCall',
'ThreadSpawn',
'ThreadExecute'],
nullable=True)
}
obj_relationships = {
'callstack': [('1.0', '1.0')],
'exception': [('1.0', '1.0')],
'children': [('1.0', '1.0')],
}
def obj_load_attr(self, attrname):
if attrname == "children":
self.children = []
return
super(Operation, self).obj_load_attr(attrname)