/
resource_registry_service.py
167 lines (137 loc) · 7.84 KB
/
resource_registry_service.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python
__author__ = 'Thomas R. Lennan, Michael Meisinger, Stephen Henrie'
__license__ = 'Apache 2.0'
import types
from pyon.core.exception import BadRequest, ServerError
from pyon.ion.resource import ExtendedResourceContainer
from pyon.public import log
from interface.services.coi.iresource_registry_service import BaseResourceRegistryService
# The following decorator is needed because the couchdb client used in the RR backend
# does not catch error conditions related to datastore/view clear properly. Here, we catch
# this type of error (TypeError) and reraise as known exception for the caller to deal with.
def mask_couch_error(fn):
def rr_call(*args, **kwargs):
try:
return fn(*args, **kwargs)
except TypeError as ex:
# HACK HACK
# catch error in couchdb client, log and reraise as ServerError
log.exception("CouchDB access error")
raise ServerError(ex)
return rr_call
class ResourceRegistryService(BaseResourceRegistryService):
"""
Service that manages resources instances and all cross-cutting concerns of
system resources. Uses a datastore instance for resource object persistence.
"""
def on_init(self):
self.resource_registry = self.container.resource_registry
# For easier interactive debugging
self.dss = None
self.ds = self.resource_registry.rr_store
try:
self.dss = self.resource_registry.rr_store.server[self.resource_registry.rr_store.datastore_name]
except Exception:
pass
@mask_couch_error
def create(self, object=None):
ctx = self.get_context()
ion_actor_id = ctx.get('ion-actor-id', None) if ctx else None
return self.resource_registry.create(object=object, actor_id=ion_actor_id)
@mask_couch_error
def read(self, object_id='', rev_id=''):
return self.resource_registry.read(object_id=object_id, rev_id=rev_id)
@mask_couch_error
def update(self, object=None):
return self.resource_registry.update(object=object)
@mask_couch_error
def delete(self, object_id=''):
return self.resource_registry.delete(object_id=object_id)
@mask_couch_error
def retire(self, resource_id=''):
return self.resource_registry.retire(resource_id=resource_id)
@mask_couch_error
def execute_lifecycle_transition(self, resource_id='', transition_event=''):
return self.resource_registry.execute_lifecycle_transition(resource_id=resource_id,
transition_event=transition_event)
@mask_couch_error
def set_lifecycle_state(self, resource_id='', target_lcstate=''):
return self.resource_registry.set_lifecycle_state(resource_id=resource_id, target_lcstate=target_lcstate)
@mask_couch_error
def create_attachment(self, resource_id='', attachment=None):
return self.resource_registry.create_attachment(resource_id=resource_id, attachment=attachment)
@mask_couch_error
def read_attachment(self, attachment_id='', include_content=False):
return self.resource_registry.read_attachment(attachment_id=attachment_id, include_content=include_content)
@mask_couch_error
def delete_attachment(self, attachment_id=''):
return self.resource_registry.delete_attachment(attachment_id=attachment_id)
@mask_couch_error
def find_attachments(self, resource_id='', keyword='', limit=0, descending=False, include_content=False, id_only=True):
return self.resource_registry.find_attachments(
resource_id=resource_id, keyword='', limit=limit,
descending=descending, include_content=include_content,
id_only=id_only)
@mask_couch_error
def create_association(self, subject=None, predicate=None, object=None, assoc_type=None):
return self.resource_registry.create_association(subject=subject, predicate=predicate,
object=object, assoc_type=assoc_type)
@mask_couch_error
def delete_association(self, association=''):
return self.resource_registry.delete_association(association=association)
@mask_couch_error
def read_object(self, subject="", predicate="", object_type="", assoc="", id_only=False):
return self.resource_registry.read_object(subject=subject, predicate=predicate,
object_type=object_type, assoc=assoc, id_only=id_only)
@mask_couch_error
def find_objects(self, subject="", predicate="", object_type="", id_only=False, limit=0, skip=0, descending=False):
return self.resource_registry.find_objects(subject=subject, predicate=predicate,
object_type=object_type, id_only=id_only, limit=limit, skip=skip, descending=descending)
@mask_couch_error
def read_subject(self, subject_type="", predicate="", object="", assoc="", id_only=False):
return self.resource_registry.read_subject(subject_type=subject_type, predicate=predicate,
object=object, assoc=assoc, id_only=id_only)
@mask_couch_error
def find_subjects(self, subject_type="", predicate="", object="", id_only=False, limit=0, skip=0, descending=False):
return self.resource_registry.find_subjects(subject_type=subject_type, predicate=predicate,
object=object, id_only=id_only, limit=limit, skip=skip, descending=descending)
@mask_couch_error
def find_associations(self, subject="", predicate="", object="", assoc_type=None, id_only=False, limit=0, skip=0, descending=False):
return self.resource_registry.find_associations(subject=subject, predicate=predicate,
object=object, assoc_type=assoc_type, id_only=id_only, limit=limit, skip=skip, descending=descending)
@mask_couch_error
def find_objects_mult(self, subjects=[], id_only=False):
return self.resource_registry.find_objects_mult(subjects=subjects, id_only=id_only)
@mask_couch_error
def find_subjects_mult(self, objects=[], id_only=False):
return self.resource_registry.find_subjects_mult(objects=objects, id_only=id_only)
@mask_couch_error
def get_association(self, subject="", predicate="", object="", assoc_type=None, id_only=False):
return self.resource_registry.get_association(subject=subject, predicate=predicate,
object=object, assoc_type=assoc_type, id_only=id_only)
@mask_couch_error
def find_resources(self, restype="", lcstate="", name="", id_only=False):
return self.resource_registry.find_resources(restype=restype, lcstate=lcstate, name=name, id_only=id_only)
@mask_couch_error
def find_resources_ext(self, restype='', lcstate='', name='', keyword='', nested_type='', attr_name='', attr_value='',
alt_id='', alt_id_ns='', limit=0, skip=0, descending=False, id_only=False):
return self.resource_registry.find_resources_ext(restype=restype, lcstate=lcstate, name=name,
keyword=keyword, nested_type=nested_type, attr_name=attr_name, attr_value=attr_value,
alt_id=alt_id, alt_id_ns=alt_id_ns,
limit=limit, skip=skip, descending=descending,
id_only=id_only)
@mask_couch_error
def read_mult(self, object_ids=[]):
return self.resource_registry.read_mult(object_ids)
@mask_couch_error
def get_resource_extension(self, resource_id='', resource_extension='', ext_associations=None, ext_exclude=None, user_id=''):
"""Returns any ExtendedResource object containing additional related information derived from associations
@param resource_id str
@param resource_extension str
@param ext_associations dict
@param ext_exclude list
@retval actor_identity ExtendedResource
@throws BadRequest A parameter is missing
@throws NotFound An object with the specified resource_id does not exist
"""
return self.resource_registry.get_resource_extension(resource_id, resource_extension, ext_associations, ext_exclude)