def get_data_process_extension(self, data_process_id='', ext_associations=None, ext_exclude=None, user_id=''): #Returns an DataProcessDefinition Extension object containing additional related information if not data_process_id: raise BadRequest( "The data_process_definition_id parameter is empty") extended_resource_handler = ExtendedResourceContainer(self) extended_data_process = extended_resource_handler.create_extended_resource_container( extended_resource_type=OT.DataProcessExtension, resource_id=data_process_id, computed_resource_type=OT.DataProcessComputedAttributes, ext_associations=ext_associations, ext_exclude=ext_exclude, user_id=user_id) #Loop through any attachments and remove the actual content since we don't need # to send it to the front end this way #TODO - see if there is a better way to do this in the extended resource frame work. if hasattr(extended_data_process, 'attachments'): for att in extended_data_process.attachments: if hasattr(att, 'content'): delattr(att, 'content') return extended_data_process
def get_user_info_extension(self, user_info_id='', user_id=''): """Returns an UserInfoExtension object containing additional related information @param user_info_id str @retval user_info UserInfoExtension @throws BadRequest A parameter is missing @throws NotFound An object with the specified actor_id does not exist """ if not user_info_id: raise BadRequest("The user_info_id parameter is empty") extended_resource_handler = ExtendedResourceContainer(self) extended_user = extended_resource_handler.create_extended_resource_container( extended_resource_type=OT.UserInfoExtension, resource_id=user_info_id, user_id=user_id) #If the org_id is not provided then skip looking for Org related roles. if extended_user: #Did not setup a dependency to org_management service to avoid a potential circular bootstrap issue # since this method should never be called until the system is fully running try: org_client = OrgManagementServiceProcessClient(process=self) roles = org_client.find_all_roles_by_user(extended_user.actor_identity._id) extended_user.roles = list() for org_name in roles: for role in roles[org_name]: flattened_role = copy.copy(role.__dict__) del flattened_role['type_'] #Have to do this to appease the message validators for ION objects flattened_role['org_name'] = org_name #Nothing like forcing a value into the dict to appease the UI code extended_user.roles.append(flattened_role) except Exception, e: raise NotFound('Could not retrieve UserRoles for User Info id: %s - %s' % (user_info_id, e.message))
def get_data_process_definition_extension(self, data_process_definition_id='', ext_associations=None, ext_exclude=None, user_id=''): #Returns an DataProcessDefinition Extension object containing additional related information if not data_process_definition_id: raise BadRequest( "The data_process_definition_id parameter is empty") extended_resource_handler = ExtendedResourceContainer(self) extended_data_process_definition = extended_resource_handler.create_extended_resource_container( extended_resource_type=OT.DataProcessDefinitionExtension, resource_id=data_process_definition_id, computed_resource_type=OT.DataProcessDefinitionComputedAttributes, ext_associations=ext_associations, ext_exclude=ext_exclude, user_id=user_id) #Loop through any attachments and remove the actual content since we don't need # to send it to the front end this way #TODO - see if there is a better way to do this in the extended resource frame work. if hasattr(extended_data_process_definition, 'attachments'): for att in extended_data_process_definition.attachments: if hasattr(att, 'content'): delattr(att, 'content') data_process_ids = set() for dp in extended_data_process_definition.data_processes: data_process_ids.add(dp._id) #create the list of output data_products from the data_processes that is aligned products_map = {} objects, associations = self.clients.resource_registry.find_objects_mult( subjects=list(data_process_ids), id_only=False) for obj, assoc in zip(objects, associations): # if this is a hasOutputProduct association... if assoc.p == PRED.hasOutputProduct: #collect the set of output products from each data process in a map if not products_map.has_key(assoc.s): products_map[assoc.s] = [assoc.o] else: products_map[assoc.s].append(assoc.o) #now set up the final list to align extended_data_process_definition.data_products = [] for dp in extended_data_process_definition.data_processes: extended_data_process_definition.data_products.append( products_map[dp._id]) return extended_data_process_definition
def prepare_user_info_support(self, user_info_id=''): """ Returns the object containing the data to create/update a user info resource """ # TODO: Precondition or check: only the actor identity associated with this user or an ORG_MANAGER/ION_MANAGER # can edit this user info resource extended_resource_handler = ExtendedResourceContainer(self) resource_data = extended_resource_handler.create_prepare_resource_support( user_info_id, OT.UserInfoPrepareSupport) return resource_data
def get_resource_extension(self, resource_id='', resource_extension='', ext_associations=None, ext_exclude=None): """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 """ if not resource_id: raise BadRequest("The resource_id parameter is empty") if not resource_extension: raise BadRequest("The extended_resource parameter not set") extended_resource_handler = ExtendedResourceContainer(self, self) #Handle differently if the resource_id parameter is a list of ids if resource_id.find('[') > -1: res_input = eval(resource_id) extended_resource_list = extended_resource_handler.create_extended_resource_container_list( resource_extension, res_input, computed_resource_type=None, origin_resource_type=None, ext_associations=ext_associations, ext_exclude=ext_exclude) return extended_resource_list extended_resource = extended_resource_handler.create_extended_resource_container( resource_extension, resource_id, computed_resource_type=None, ext_associations=ext_associations, ext_exclude=ext_exclude) return extended_resource
def xtest_create_extended_resource_container(self): mock_clients = self._create_service_mock('resource_registry') self.clients = mock_clients self.container = Mock() extended_resource_handler = ExtendedResourceContainer( self, mock_clients.resource_registry) instrument_device = Mock() instrument_device._id = '123' instrument_device.name = "MyInstrument" instrument_device.type_ = RT.TestInstrument instrument_device.lcstate = LCS.DRAFT instrument_device.availability = AS.PRIVATE instrument_device2 = Mock() instrument_device2._id = '456' instrument_device2.name = "MyInstrument2" instrument_device2.type_ = RT.TestInstrument actor_identity = Mock() actor_identity._id = '111' actor_identity.name = "Foo" actor_identity.type_ = RT.ActorIdentity actor_identity = Mock() actor_identity._id = '1112' actor_identity.name = "Foo2" actor_identity.type_ = RT.ActorIdentity user_info = Mock() user_info._id = '444' user_info.name = "John Doe" user_info.email = "*****@*****.**" user_info.phone = "555-555-5555" user_info.variables = [{ "name": "subscribeToMailingList", "value": "False" }] user_info2 = Mock() user_info2._id = '445' user_info2.name = "aka Evil Twin" user_info2.email = "*****@*****.**" user_info2.phone = "555-555-5555" user_info2.variables = [{ "name": "subscribeToMailingList", "value": "False" }] # ActorIdentity to UserInfo association actor_identity_to_info_association = Mock() actor_identity_to_info_association._id = '555' actor_identity_to_info_association.s = "111" actor_identity_to_info_association.st = RT.ActorIdentity actor_identity_to_info_association.p = PRED.hasInfo actor_identity_to_info_association.o = "444" actor_identity_to_info_association.ot = RT.UserInfo # ActorIdentity to UserInfo association actor_identity_to_info_association2 = Mock() actor_identity_to_info_association2._id = '556' actor_identity_to_info_association2.s = "1112" actor_identity_to_info_association2.st = RT.ActorIdentity actor_identity_to_info_association2.p = PRED.hasInfo actor_identity_to_info_association2.o = "445" actor_identity_to_info_association2.ot = RT.UserInfo # ActorIdentity to Instrument Device association Instrument_device_to_actor_identity_association = Mock() Instrument_device_to_actor_identity_association._id = '666' Instrument_device_to_actor_identity_association.s = "123" Instrument_device_to_actor_identity_association.st = RT.TestInstrument Instrument_device_to_actor_identity_association.p = PRED.hasOwner Instrument_device_to_actor_identity_association.o = "111" Instrument_device_to_actor_identity_association.ot = RT.ActorIdentity # ActorIdentity to Instrument Device association Instrument_device_to_actor_identity_association2 = Mock() Instrument_device_to_actor_identity_association2._id = '667' Instrument_device_to_actor_identity_association2.s = "456" Instrument_device_to_actor_identity_association2.st = RT.TestInstrument Instrument_device_to_actor_identity_association2.p = PRED.hasOwner Instrument_device_to_actor_identity_association2.o = "111" Instrument_device_to_actor_identity_association2.ot = RT.ActorIdentity with self.assertRaises(BadRequest) as cm: extended_user = extended_resource_handler.create_extended_resource_container( RT.ActorIdentity, '111') self.assertIn( 'The requested resource ActorIdentity is not extended from ResourceContainer', cm.exception.message) mock_clients.resource_registry.read.return_value = instrument_device mock_clients.resource_registry.find_objects.return_value = ([ actor_identity ], [Instrument_device_to_actor_identity_association]) mock_clients.resource_registry.find_subjects.return_value = (None, None) mock_clients.resource_registry.find_associations.return_value = [ actor_identity_to_info_association, Instrument_device_to_actor_identity_association ] mock_clients.resource_registry.read_mult.return_value = [user_info] extended_res = extended_resource_handler.create_extended_resource_container( OT.TestExtendedResource, '123') self.assertEquals(extended_res.resource, instrument_device) self.assertEquals(len(extended_res.owners), 2) self.assertEquals(extended_res.resource_object.type_, RT.SystemResource) self.assertEquals(extended_res.remote_resource_object.type_, RT.TestInstrument) self.assertEquals(extended_res.resource_object.name, 'TestSystem_Resource') self.assertEquals(extended_res.owner_count, 2) self.assertEquals(extended_res.single_owner.name, user_info.name) self.assertEquals(len(extended_res.lcstate_transitions), 6) self.assertEquals( set(extended_res.lcstate_transitions.keys()), set(['develop', 'deploy', 'retire', 'plan', 'integrate', 'delete'])) self.assertEquals(len(extended_res.availability_transitions), 2) self.assertEquals(set(extended_res.availability_transitions.keys()), set(['enable', 'announce'])) extended_res = extended_resource_handler.create_extended_resource_container( OT.TestExtendedResourceDevice, '123') self.assertEquals(extended_res.resource, instrument_device) self.assertEquals(len(extended_res.owners), 2) with self.assertRaises(Inconsistent) as cm: extended_res = extended_resource_handler.create_extended_resource_container( OT.TestExtendedResourceBad, '123') #Test adding extra paramaters to methods extended_res = extended_resource_handler.create_extended_resource_container( OT.TestExtendedResource, '123', resource_name='AltSystem_Resource') self.assertEquals(extended_res.resource, instrument_device) self.assertEquals(len(extended_res.owners), 2) self.assertEquals(extended_res.resource_object.type_, RT.SystemResource) self.assertEquals(extended_res.remote_resource_object.type_, RT.TestInstrument) self.assertEquals(extended_res.resource_object.name, 'AltSystem_Resource') #Test field exclusion extended_res = extended_resource_handler.create_extended_resource_container( OT.TestExtendedResource, '123', ext_exclude=['owners']) self.assertEquals(extended_res.resource, instrument_device) self.assertEquals(len(extended_res.owners), 0) self.assertEquals(extended_res.resource_object.type_, RT.SystemResource) self.assertEquals(extended_res.remote_resource_object.type_, RT.TestInstrument) #Test the list of ids interface extended_res_list = extended_resource_handler.create_extended_resource_container_list( OT.TestExtendedResource, ['123', '456']) self.assertEqual(len(extended_res_list), 2) self.assertEquals(extended_res_list[0].resource, instrument_device) self.assertEquals(len(extended_res_list[0].owners), 2) self.assertEquals(extended_res_list[0].resource_object.type_, RT.SystemResource) self.assertEquals(extended_res.remote_resource_object.type_, RT.TestInstrument) #Test create_prepare_update_resource prepare_create = extended_resource_handler.create_prepare_resource_support( prepare_resource_type=OT.TestPrepareUpdateResource) self.assertEqual(prepare_create.type_, OT.TestPrepareUpdateResource) self.assertEqual(prepare_create._id, '') prepare_update = extended_resource_handler.create_prepare_resource_support( resource_id='123', prepare_resource_type=OT.TestPrepareUpdateResource) self.assertEqual(prepare_update.type_, OT.TestPrepareUpdateResource) self.assertEqual(prepare_update._id, '123')
def get_user_info_extension(self, user_info_id='', org_id=''): """Returns an UserInfoExtension object containing additional related information @param user_info_id str @param org_id str - An optional org id that the user is interested in filtering against. @retval user_info UserInfoExtension @throws BadRequest A parameter is missing @throws NotFound An object with the specified actor_id does not exist """ if not user_info_id: raise BadRequest("The user_info_id parameter is empty") #THis is a hack to get the UI going. It would be preferable to get the actor id from the extended resource #container below, but their would need to be a guarantee of order of field processing in order #to ensure that the actor identity has been found BEFORE the negotiation methods are called - and probably #some elegant way to indicate the field and sub field; ie actor_identity._id actors, _ = self.clients.resource_registry.find_subjects( subject_type=RT.ActorIdentity, predicate=PRED.hasInfo, object=user_info_id, id_only=True) actor_id = actors[0] if len(actors) > 0 else '' extended_resource_handler = ExtendedResourceContainer(self) extended_user = extended_resource_handler.create_extended_resource_container( extended_resource_type=OT.UserInfoExtension, resource_id=user_info_id, computed_resource_type=OT.ComputedAttributes, user_id=user_info_id, org_id=org_id, actor_id=actor_id) #If the org_id is not provided then skip looking for Org related roles. if extended_user: #Did not setup a dependency to org_management service to avoid a potential circular bootstrap issue # since this method should never be called until the system is fully running try: org_client = OrgManagementServiceProcessClient(process=self) roles = org_client.find_all_roles_by_user( extended_user.actor_identity._id) extended_user.roles = list() for org_name in roles: for role in roles[org_name]: flattened_role = copy.copy(role.__dict__) del flattened_role[ 'type_'] #Have to do this to appease the message validators for ION objects flattened_role[ 'org_name'] = org_name #Nothing like forcing a value into the dict to appease the UI code extended_user.roles.append(flattened_role) except Exception, e: raise NotFound( 'Could not retrieve UserRoles for User Info id: %s - %s' % (user_info_id, e.message)) #filter notification requests that are retired extended_user.subscriptions = [ nr for nr in extended_user.subscriptions if nr.temporal_bounds.end_datetime == '' ] #filter owned resources that are retired nr_removed = [] for rsrc in extended_user.owned_resources: #remove all the Notifications if rsrc.type_ != OT.NotificationRequest: nr_removed.append(rsrc) extended_user.owned_resources = [ rsrc for rsrc in nr_removed if rsrc.lcstate != 'DELETED' ] #now append the active NotificationRequests extended_user.owned_resources.extend(extended_user.subscriptions)
def test_create_extended_resource_container(self): mock_clients = self._create_service_mock('resource_registry') self.clients = mock_clients extended_resource_handler = ExtendedResourceContainer(self, mock_clients.resource_registry) instrument_device = Mock() instrument_device._id = '123' instrument_device.name = "MyInstrument" instrument_device.type_ = RT.InstrumentDevice actor_identity = Mock() actor_identity._id = '111' actor_identity.name = "Foo" actor_identity.type_ = RT.ActorIdentity user_info = Mock() user_info.name = "John Doe" user_info.email = "*****@*****.**" user_info.phone = "555-555-5555" user_info.variables = [{"name": "subscribeToMailingList", "value": "False"}] # ActorIdentity to UserInfo association actor_identity_to_info_association = Mock() actor_identity_to_info_association._id = '555' actor_identity_to_info_association.s = "111" actor_identity_to_info_association.st = RT.ActorIdentity actor_identity_to_info_association.p = PRED.hasInfo actor_identity_to_info_association.o = "444" actor_identity_to_info_association.ot = RT.UserInfo # ActorIdentity to UserInfo association Instrument_device_to_actor_identity_association = Mock() Instrument_device_to_actor_identity_association._id = '666' Instrument_device_to_actor_identity_association.s = "123" Instrument_device_to_actor_identity_association.st = RT.InstumentDevice Instrument_device_to_actor_identity_association.p = PRED.hasOwner Instrument_device_to_actor_identity_association.o = "111" Instrument_device_to_actor_identity_association.ot = RT.ActorIdentity with self.assertRaises(BadRequest) as cm: extended_user = extended_resource_handler.create_extended_resource_container(RT.ActorIdentity, '111') self.assertIn( 'Requested resource ActorIdentity is not extended from ResourceContainer',cm.exception.message) obj = IonObject(OT.TestExtendedResource) list_objs = ['123', '456', '789'] extended_resource_handler.set_field_associations(obj, 'policies', list_objs) extended_resource_handler.set_field_associations(obj, 'policy_count', list_objs) extended_resource_handler.set_field_associations(obj, 'resource_object', list_objs) self.assertEquals(obj.policies, list_objs) self.assertEquals(obj.policy_count, 3) self.assertEquals(obj.resource_object, '123') mock_clients.resource_registry.read.return_value = instrument_device mock_clients.resource_registry.find_objects.return_value = ([actor_identity], [Instrument_device_to_actor_identity_association]) extended_res = extended_resource_handler.create_extended_resource_container(OT.TestExtendedResource, '123') self.assertEquals(extended_res.resource, instrument_device) self.assertEquals(len(extended_res.owners),1) #Test field exclusion extended_res = extended_resource_handler.create_extended_resource_container(OT.TestExtendedResource, '123',ext_exclude=['owners']) self.assertEquals(extended_res.resource, instrument_device) self.assertEquals(len(extended_res.owners),0)
def test_create_extended_resource_container(self): mock_clients = self._create_service_mock('resource_registry') self.clients = mock_clients self.container = Mock() extended_resource_handler = ExtendedResourceContainer( self, mock_clients.resource_registry) instrument_device = Mock() instrument_device._id = '123' instrument_device.name = "MyInstrument" instrument_device.type_ = RT.InstrumentDevice instrument_device2 = Mock() instrument_device2._id = '456' instrument_device2.name = "MyInstrument2" instrument_device2.type_ = RT.InstrumentDevice actor_identity = Mock() actor_identity._id = '111' actor_identity.name = "Foo" actor_identity.type_ = RT.ActorIdentity user_info = Mock() user_info._id = '444' user_info.name = "John Doe" user_info.email = "*****@*****.**" user_info.phone = "555-555-5555" user_info.variables = [{ "name": "subscribeToMailingList", "value": "False" }] user_info2 = Mock() user_info2._id = '445' user_info2.name = "aka Evil Twin" user_info2.email = "*****@*****.**" user_info2.phone = "555-555-5555" user_info2.variables = [{ "name": "subscribeToMailingList", "value": "False" }] # ActorIdentity to UserInfo association actor_identity_to_info_association = Mock() actor_identity_to_info_association._id = '555' actor_identity_to_info_association.s = "111" actor_identity_to_info_association.st = RT.ActorIdentity actor_identity_to_info_association.p = PRED.hasInfo actor_identity_to_info_association.o = "444" actor_identity_to_info_association.ot = RT.UserInfo # ActorIdentity to UserInfo association actor_identity_to_info_association2 = Mock() actor_identity_to_info_association2._id = '556' actor_identity_to_info_association2.s = "111" actor_identity_to_info_association2.st = RT.ActorIdentity actor_identity_to_info_association2.p = PRED.hasInfo actor_identity_to_info_association2.o = "445" actor_identity_to_info_association2.ot = RT.UserInfo # ActorIdentity to Instrument Device association Instrument_device_to_actor_identity_association = Mock() Instrument_device_to_actor_identity_association._id = '666' Instrument_device_to_actor_identity_association.s = "123" Instrument_device_to_actor_identity_association.st = RT.InstumentDevice Instrument_device_to_actor_identity_association.p = PRED.hasOwner Instrument_device_to_actor_identity_association.o = "111" Instrument_device_to_actor_identity_association.ot = RT.ActorIdentity # ActorIdentity to Instrument Device association Instrument_device_to_actor_identity_association2 = Mock() Instrument_device_to_actor_identity_association2._id = '667' Instrument_device_to_actor_identity_association2.s = "456" Instrument_device_to_actor_identity_association2.st = RT.InstumentDevice Instrument_device_to_actor_identity_association2.p = PRED.hasOwner Instrument_device_to_actor_identity_association2.o = "111" Instrument_device_to_actor_identity_association2.ot = RT.ActorIdentity with self.assertRaises(BadRequest) as cm: extended_user = extended_resource_handler.create_extended_resource_container( RT.ActorIdentity, '111') self.assertIn( 'The requested resource ActorIdentity is not extended from ResourceContainer', cm.exception.message) mock_clients.resource_registry.read.return_value = instrument_device mock_clients.resource_registry.find_objects.return_value = ([ actor_identity ], [Instrument_device_to_actor_identity_association]) mock_clients.resource_registry.find_subjects.return_value = (None, None) mock_clients.resource_registry.find_associations.return_value = [ actor_identity_to_info_association, Instrument_device_to_actor_identity_association ] mock_clients.resource_registry.read_mult.return_value = [user_info] extended_res = extended_resource_handler.create_extended_resource_container( OT.TestExtendedResource, '123') self.assertEquals(extended_res.resource, instrument_device) self.assertEquals(len(extended_res.owners), 1) self.assertEquals(extended_res.resource_object.type_, RT.SystemResource) self.assertEquals(extended_res.remote_resource_object.type_, RT.InstrumentDevice) self.assertEquals(extended_res.resource_object.name, 'TestSystem_Resource') #Test adding extra paramaters to methods extended_res = extended_resource_handler.create_extended_resource_container( OT.TestExtendedResource, '123', resource_name='AltSystem_Resource') self.assertEquals(extended_res.resource, instrument_device) self.assertEquals(len(extended_res.owners), 1) self.assertEquals(extended_res.resource_object.type_, RT.SystemResource) self.assertEquals(extended_res.remote_resource_object.type_, RT.InstrumentDevice) self.assertEquals(extended_res.resource_object.name, 'AltSystem_Resource') #Test field exclusion extended_res = extended_resource_handler.create_extended_resource_container( OT.TestExtendedResource, '123', ext_exclude=['owners']) self.assertEquals(extended_res.resource, instrument_device) self.assertEquals(len(extended_res.owners), 0) self.assertEquals(extended_res.resource_object.type_, RT.SystemResource) self.assertEquals(extended_res.remote_resource_object.type_, RT.InstrumentDevice) #Test the list of ids interface extended_res_list = extended_resource_handler.create_extended_resource_container_list( OT.TestExtendedResource, ['123', '456']) self.assertEqual(len(extended_res_list), 2) self.assertEquals(extended_res_list[0].resource, instrument_device) self.assertEquals(len(extended_res_list[0].owners), 1) self.assertEquals(extended_res_list[0].resource_object.type_, RT.SystemResource) self.assertEquals(extended_res.remote_resource_object.type_, RT.InstrumentDevice)