def create_extended_resource_container(self, extended_resource_type, resource_id, computed_resource_type=None, ext_associations=None, ext_exclude=None): if not self.service_provider or not self.resource_registry: raise Inconsistent("This class is not initialized properly") if extended_resource_type not in getextends(OT.ResourceContainer): raise BadRequest('Requested resource %s is not extended from %s' % ( extended_resource_type, OT.ResourceContainer) ) if computed_resource_type and computed_resource_type not in getextends(OT.ComputedAttributes): raise BadRequest('Requested resource %s is not extended from %s' % ( computed_resource_type, OT.ComputedAttributes) ) resource_object = self.resource_registry.read(resource_id) if not resource_object: raise NotFound("Resource %s does not exist" % resource_id) res_container = IonObject(extended_resource_type) res_container._id = resource_object._id res_container.resource = resource_object self.set_container_field_values(res_container, ext_exclude) self.set_computed_attributes(res_container, computed_resource_type, ext_exclude) self.set_extended_associations(res_container, ext_associations, ext_exclude) res_container.ts_created = get_ion_ts() return res_container
def _create_resource(self, restype, name, *args, **kwargs): res_obj = IonObject(restype, dict(name=name, **kwargs)) res_obj_res = self.data_store.create(res_obj, create_unique_resource_id()) res_obj._id = res_obj_res[0] self.resources[name] = res_obj return res_obj_res[0]
def test_get_valid_resource_commitment(self): from pyon.util.containers import get_ion_ts_millis # create ION org and an actor ion_org = IonObject(RT.Org, name='ION') ion_org_id, _ = self.rr.create(ion_org) ion_org._id = ion_org_id actor = IonObject(RT.ActorIdentity, name='actor1') actor_id, _ = self.rr.create(actor) # create an expired commitment in the org ts = get_ion_ts_millis() - 50000 com_obj = IonObject(RT.Commitment, provider=ion_org_id, consumer=actor_id, commitment=True, expiration=ts) com_id, _ = self.rr.create(com_obj) id = self.rr.create_association(ion_org_id, PRED.hasCommitment, com_id) c = get_valid_resource_commitments(ion_org_id, actor_id) #verify that the commitment is not returned self.assertIsNone(c) # create a commitment that has not expired yet ts = get_ion_ts_millis() + 50000 com_obj = IonObject(RT.Commitment, provider=ion_org_id, consumer=actor_id, commitment=True, expiration=ts) com_id, _ = self.rr.create(com_obj) id = self.rr.create_association(ion_org_id, PRED.hasCommitment, com_id) c = get_valid_resource_commitments(ion_org_id, actor_id) #verify that the commitment is returned self.assertIsNotNone(c)
def create_prepare_resource_support(self, resource_id="", prepare_resource_type=None, origin_resource_type=None): if not isinstance(resource_id, types.StringType): raise Inconsistent("The parameter resource_id is not a single resource id string") if not self.service_provider or not self._rr: raise Inconsistent("This class is not initialized properly") if prepare_resource_type is not None and prepare_resource_type not in getextends(OT.ResourcePrepareSupport): raise BadRequest('The requested resource %s is not extended from %s' % (prepare_resource_type, OT.ResourcePrepareSupport)) resource_data = IonObject(prepare_resource_type) # Check to make sure the extended resource decorator raise OriginResourceType matches the type of the resource type origin_resource_decorator = resource_data.get_class_decorator_value('OriginResourceType') if origin_resource_decorator is None and origin_resource_type is None: raise NotFound('OriginResourceType decorator not found in object specification %s', prepare_resource_type) origin_resource_type = origin_resource_type if origin_resource_type is not None else origin_resource_decorator if origin_resource_type is None: raise NotFound('OriginResourceType decorator not found in object specification %s', prepare_resource_type) resource_object = None if resource_id: resource_object = self._rr.read(resource_id) if origin_resource_type != resource_object.type_ and not issubtype(resource_object.type_, origin_resource_type): raise Inconsistent('The OriginResourceType decorator of the requested resource %s(%s) does not match the type of the specified resource id(%s).' % ( prepare_resource_type, origin_resource_type, resource_object.type_)) resource_data._id = resource_object._id else: resource_object = IonObject(origin_resource_type) resource_data.resource = resource_object resource_data.resource_schema = get_object_schema(origin_resource_type) for field in resource_data._schema: deco_value = resource_data.get_decorator_value(field, 'AssociatedResources') assoc_dict = {} if deco_value is not None: if deco_value.find(',') == -1: associated_resources = [deco_value] else: associated_resources = deco_value.split(',') for res in associated_resources: assoc = self.get_associated_resource_info(origin_resource_type, resource_id, res) assoc_dict[assoc.key] = assoc setattr(resource_data, field, assoc_dict) continue return resource_data
def create_extended_resource_container(self, extended_resource_type, resource_id, computed_resource_type=None, ext_associations=None, ext_exclude=None): overall_start_time = time.time() if not isinstance(resource_id, types.StringType): raise Inconsistent("The parameter resource_id is not a single resource id string") if not self.service_provider or not self._rr: raise Inconsistent("This class is not initialized properly") if extended_resource_type not in getextends(OT.ResourceContainer): raise BadRequest('The requested resource %s is not extended from %s' % (extended_resource_type, OT.ResourceContainer)) if computed_resource_type and computed_resource_type not in getextends(OT.ComputedAttributes): raise BadRequest('The requested resource %s is not extended from %s' % (computed_resource_type, OT.ComputedAttributes)) resource_object = self._rr.read(resource_id) if not resource_object: raise NotFound("The Resource %s does not exist" % resource_id) res_container = IonObject(extended_resource_type) # @TODO - replace with object level decorators and raise exceptions if not hasattr(res_container, 'origin_resource_type'): log.error('The requested resource %s does not contain a properly set origin_resource_type field.' , extended_resource_type) #raise Inconsistent('The requested resource %s does not contain a properly set origin_resource_type field.' % extended_resource_type) if hasattr(res_container, 'origin_resource_type') and res_container.origin_resource_type != resource_object.type_\ and not issubtype(resource_object.type_, res_container.origin_resource_type): log.error('The origin_resource_type of the requested resource %s(%s) does not match the type of the specified resource id(%s).' % ( extended_resource_type, res_container.origin_resource_type, resource_object.type_)) #raise Inconsistent('The origin_resource_type of the requested resource %s(%s) does not match the type of the specified resource id(%s).' % (extended_resource_type, res_container.origin_resource_type, resource_object.type_)) res_container._id = resource_object._id res_container.resource = resource_object self.set_container_lcstate_info(res_container) self.set_container_field_values(res_container, ext_exclude) self.set_computed_attributes(res_container, computed_resource_type, ext_exclude) self.set_extended_associations(res_container, ext_associations, ext_exclude) res_container.ts_created = get_ion_ts() overall_stop_time = time.time() log.debug("Time to process extended resource container %s %f secs", extended_resource_type, overall_stop_time - overall_start_time ) return res_container
def test_get_valid_org_commitment(self): from pyon.util.containers import get_ion_ts_millis # create ION org and an actor ion_org = IonObject(RT.Org, name='ION') ion_org_id, _ = self.rr.create(ion_org) ion_org._id = ion_org_id actor = IonObject(RT.ActorIdentity, name='actor1') actor_id, _ = self.rr.create(actor) device = IonObject(RT.TestDevice, name="device1") device_id, _ = self.rr.create(device) # create an expired commitment in the org ts = get_ion_ts_millis() - 50000 com_obj = IonObject(RT.Commitment, provider=ion_org_id, consumer=actor_id, commitment=True, expiration=ts) com_id, _ = self.rr.create(com_obj) self.rr.create_association(ion_org_id, PRED.hasCommitment, com_id) c = get_valid_principal_commitments(ion_org_id, actor_id) # verify that the commitment is not returned self.assertIsNone(c) self.rr.create_association(com_id, PRED.hasTarget, device_id) c = get_valid_resource_commitments(device_id, actor_id) # verify that the commitment is not returned self.assertIsNone(c) # create a commitment that has not expired yet ts = get_ion_ts_millis() + 50000 com_obj = IonObject(RT.Commitment, provider=ion_org_id, consumer=actor_id, commitment=True, expiration=ts) com_id, _ = self.rr.create(com_obj) self.rr.create_association(ion_org_id, PRED.hasCommitment, com_id) c = get_valid_principal_commitments(ion_org_id, actor_id) # verify that the commitment is returned self.assertIsNotNone(c) self.rr.create_association(com_id, PRED.hasTarget, device_id) c = get_valid_resource_commitments(device_id, actor_id) # verify that the commitment is not returned self.assertIsNotNone(c)
def test_crud_list_obj(self): """ crud batch operations using lists of objects where possible """ obj1 = IonObject(RT.InstrumentDevice, name='SBE37IMDevice', description="SBE37IMDevice", serial_number="12345" ) obj2 = IonObject(RT.Observatory, name='mount spaghetti', description='covered with snow') objs = [obj1,obj2] tuples = self.repo.insert('sample', objs) for t in tuples: self.assertTrue(t[0]) self.assertTrue(t[1] is not None) ids = [ tuples[0][1], tuples[1][1], 'howdy' ] tuples = self.repo.read('sample', ids) self.assertTrue(tuples[0][0]) self.assertTrue(tuples[1][0]) self.assertFalse(tuples[2][0]) obj3 = tuples[0][2] obj4 = tuples[1][2] obj3.name = 'no longer SBE' obj4.description = 'no more snow' obj1._id = 'abc123' objs = [ obj3, obj4, obj1 ] tuples = self.repo.update('sample', objs) self.assertTrue(tuples[0][0]) self.assertTrue(tuples[1][0]) self.assertFalse(tuples[2][0]) tuples = self.repo.read('sample', objs) self.assertTrue(tuples[0][0]) self.assertTrue(tuples[1][0]) self.assertFalse(tuples[2][0]) obj5 = tuples[0][2] obj6 = tuples[1][2] for key in ['_id', 'name', 'description', 'serial_number']: self.assertEqual(obj3.__dict__[key],obj5.__dict__[key], msg='objects do not have the same '+key) objs = [ obj4, obj5 ] # 4 has obsolete _rev tuples = self.repo.delete('sample', objs) self.assertFalse(tuples[0][0]) self.assertTrue(tuples[1][0], msg='failed: '+str(tuples[1][2]) +'\nobj: ' + repr(obj5.__dict__)) objs = [ obj5, obj6 ] # 5 is already deleted tuples = self.repo.delete('sample', objs) self.assertFalse(tuples[0][0]) self.assertTrue(tuples[1][0], msg='failed: '+str(tuples[1][2]) +'\nobj: ' + repr(obj5.__dict__))
def test_get_resource_commitment(self): from pyon.util.containers import get_ion_ts # create ION org and an actor ion_org = IonObject(RT.Org, name='ION') ion_org_id, _ = self.rr.create(ion_org) ion_org._id = ion_org_id actor = IonObject(RT.ActorIdentity, name='actor1') actor_id, _ = self.rr.create(actor) # create an expired commitment in the org ts = int(get_ion_ts()) - 50000 com_obj = IonObject(RT.Commitment, provider=ion_org_id, consumer=actor_id, commitment=True, expiration=ts) com_id, _ = self.rr.create(com_obj) id = self.rr.create_association(ion_org_id, PRED.hasCommitment, com_id) c = self.container.governance_controller.get_resource_commitments( actor_id, ion_org_id) #verify that the commitment is not returned self.assertIsNone(c) # create a commitment that has not expired yet ts = int(get_ion_ts()) + 50000 com_obj = IonObject(RT.Commitment, provider=ion_org_id, consumer=actor_id, commitment=True, expiration=ts) com_id, _ = self.rr.create(com_obj) id = self.rr.create_association(ion_org_id, PRED.hasCommitment, com_id) c = self.container.governance_controller.get_resource_commitments( actor_id, ion_org_id) #verify that the commitment is returned self.assertIsNotNone(c)
def create_extended_resource_container(self, extended_resource_type, resource_id, computed_resource_type=None, ext_associations=None, ext_exclude=None): if not self.service_provider or not self.resource_registry: raise Inconsistent("This class is not initialized properly") if extended_resource_type not in getextends(OT.ResourceContainer): raise BadRequest('Requested resource %s is not extended from %s' % (extended_resource_type, OT.ResourceContainer)) if computed_resource_type and computed_resource_type not in getextends( OT.ComputedAttributes): raise BadRequest('Requested resource %s is not extended from %s' % (computed_resource_type, OT.ComputedAttributes)) resource_object = self.resource_registry.read(resource_id) if not resource_object: raise NotFound("Resource %s does not exist" % resource_id) res_container = IonObject(extended_resource_type) res_container._id = resource_object._id res_container.resource = resource_object self.set_container_field_values(res_container, ext_exclude) self.set_computed_attributes(res_container, computed_resource_type, ext_exclude) self.set_extended_associations(res_container, ext_associations, ext_exclude) res_container.ts_created = get_ion_ts() return res_container
def test_get_actor_header(self): #Setup data actor = IonObject(RT.ActorIdentity, name='actor1') actor_id, _ = self.rr.create(actor) ion_org = IonObject(RT.Org, name='ION', org_governance_name='ION') ion_org_id, _ = self.rr.create(ion_org) ion_org._id = ion_org_id manager_role = IonObject(RT.UserRole, name='Org Manager', governance_name=ORG_MANAGER_ROLE, description='Org Manager') manager_role_id = self.add_user_role(ion_org, manager_role) member_role = IonObject(RT.UserRole, name='Org Member', governance_name=ORG_MEMBER_ROLE, description='Org Member') # all actors have a defaul org_member_role actor_roles = find_roles_by_actor(actor_id) self.assertDictEqual(actor_roles, {'ION': [ORG_MEMBER_ROLE]}) actor_header = get_actor_header(actor_id) self.assertDictEqual(actor_header, {'ion-actor-id': actor_id, 'ion-actor-roles': {'ION': [ORG_MEMBER_ROLE]}}) #Add Org Manager Role self.rr.create_association(actor_id, PRED.hasRole, manager_role_id) actor_roles = find_roles_by_actor(actor_id) role_header = get_role_message_headers({'ION': [manager_role, member_role]}) self.assertDictEqual(actor_roles, role_header) org2 = IonObject(RT.Org, name='Org 2', org_governance_name='Second_Org') org2_id, _ = self.rr.create(org2) org2._id = org2_id member2_role = IonObject(RT.UserRole, governance_name=ORG_MEMBER_ROLE, name='Org Member', description='Org Member') member2_role_id = self.add_user_role(org2, member2_role) operator2_role = IonObject(RT.UserRole, governance_name='INSTRUMENT_OPERATOR', name='Instrument Operator', description='Instrument Operator') operator2_role_id = self.add_user_role(org2, operator2_role) self.rr.create_association(actor_id, PRED.hasRole, member2_role_id) self.rr.create_association(actor_id, PRED.hasRole, operator2_role_id) actor_roles = find_roles_by_actor(actor_id) role_header = get_role_message_headers({'ION': [manager_role, member_role], 'Second_Org': [operator2_role, member2_role]}) self.assertEqual(len(actor_roles), 2) self.assertEqual(len(role_header), 2) self.assertIn('Second_Org', actor_roles) self.assertIn('Second_Org', role_header) self.assertEqual(len(actor_roles['Second_Org']), 2) self.assertEqual(len(role_header['Second_Org']), 2) self.assertIn('INSTRUMENT_OPERATOR', actor_roles['Second_Org']) self.assertIn('INSTRUMENT_OPERATOR', role_header['Second_Org']) self.assertIn(ORG_MEMBER_ROLE, actor_roles['Second_Org']) self.assertIn(ORG_MEMBER_ROLE, role_header['Second_Org']) self.assertIn('ION', actor_roles) self.assertIn('ION', role_header) self.assertIn(ORG_MANAGER_ROLE, actor_roles['ION']) self.assertIn(ORG_MEMBER_ROLE, actor_roles['ION']) self.assertIn(ORG_MANAGER_ROLE, role_header['ION']) self.assertIn(ORG_MEMBER_ROLE, role_header['ION']) actor_header = get_actor_header(actor_id) self.assertEqual(actor_header['ion-actor-id'], actor_id) self.assertEqual(actor_header['ion-actor-roles'], actor_roles) #Now make sure we can change the name of the Org and not affect the headers org2 = self.rr.read(org2_id) org2.name = 'Updated Org 2' org2_id, _ = self.rr.update(org2) actor_roles = find_roles_by_actor(actor_id) self.assertEqual(len(actor_roles), 2) self.assertEqual(len(role_header), 2) self.assertIn('Second_Org', actor_roles) self.assertIn('Second_Org', role_header) self.assertEqual(len(actor_roles['Second_Org']), 2) self.assertEqual(len(role_header['Second_Org']), 2) self.assertIn('INSTRUMENT_OPERATOR', actor_roles['Second_Org']) self.assertIn('INSTRUMENT_OPERATOR', role_header['Second_Org']) self.assertIn(ORG_MEMBER_ROLE, actor_roles['Second_Org']) self.assertIn(ORG_MEMBER_ROLE, role_header['Second_Org']) self.assertIn('ION', actor_roles) self.assertIn('ION', role_header) self.assertIn(ORG_MANAGER_ROLE, actor_roles['ION']) self.assertIn(ORG_MEMBER_ROLE, actor_roles['ION']) self.assertIn(ORG_MANAGER_ROLE, role_header['ION']) self.assertIn(ORG_MEMBER_ROLE, role_header['ION']) actor_header = get_actor_header(actor_id) self.assertEqual(actor_header['ion-actor-id'], actor_id) self.assertEqual(actor_header['ion-actor-roles'], actor_roles)
def test_get_actor_header(self): #Setup data actor = IonObject(RT.ActorIdentity, name='actor1') actor_id, _ = self.rr.create(actor) ion_org = IonObject(RT.Org, name='ION', org_governance_name='ION') ion_org_id, _ = self.rr.create(ion_org) ion_org._id = ion_org_id manager_role = IonObject(RT.UserRole, name='Org Manager', governance_name=MODERATOR_ROLE, description='Org Manager') manager_role_id = self.add_org_role(ion_org, manager_role) member_role = IonObject(RT.UserRole, name='Org Member', governance_name=MEMBER_ROLE, description='Org Member') # all actors have a defaul MEMBER_ROLE actor_roles = find_roles_by_actor(actor_id) self.assertDictEqual(actor_roles, {'ION': [MEMBER_ROLE]}) actor_header = get_actor_header(actor_id) self.assertDictEqual(actor_header, { 'ion-actor-id': actor_id, 'ion-actor-roles': { 'ION': [MEMBER_ROLE] } }) #Add Org Manager Role self.rr.create_association(actor_id, PRED.hasRole, manager_role_id) actor_roles = find_roles_by_actor(actor_id) role_header = get_role_message_headers( {'ION': [manager_role, member_role]}) self.assertDictEqual(actor_roles, role_header) org2 = IonObject(RT.Org, name='Org 2', org_governance_name='Second_Org') org2_id, _ = self.rr.create(org2) org2._id = org2_id member2_role = IonObject(RT.UserRole, governance_name=MEMBER_ROLE, name='Org Member', description='Org Member') member2_role_id = self.add_org_role(org2, member2_role) operator2_role = IonObject(RT.UserRole, governance_name='OPERATOR', name='Instrument Operator', description='Instrument Operator') operator2_role_id = self.add_org_role(org2, operator2_role) self.rr.create_association(actor_id, PRED.hasRole, member2_role_id) self.rr.create_association(actor_id, PRED.hasRole, operator2_role_id) actor_roles = find_roles_by_actor(actor_id) role_header = get_role_message_headers({ 'ION': [manager_role, member_role], 'Second_Org': [operator2_role, member2_role] }) self.assertEqual(len(actor_roles), 2) self.assertEqual(len(role_header), 2) self.assertIn('Second_Org', actor_roles) self.assertIn('Second_Org', role_header) self.assertEqual(len(actor_roles['Second_Org']), 2) self.assertEqual(len(role_header['Second_Org']), 2) self.assertIn('OPERATOR', actor_roles['Second_Org']) self.assertIn('OPERATOR', role_header['Second_Org']) self.assertIn(MEMBER_ROLE, actor_roles['Second_Org']) self.assertIn(MEMBER_ROLE, role_header['Second_Org']) self.assertIn('ION', actor_roles) self.assertIn('ION', role_header) self.assertIn(MODERATOR_ROLE, actor_roles['ION']) self.assertIn(MEMBER_ROLE, actor_roles['ION']) self.assertIn(MODERATOR_ROLE, role_header['ION']) self.assertIn(MEMBER_ROLE, role_header['ION']) actor_header = get_actor_header(actor_id) self.assertEqual(actor_header['ion-actor-id'], actor_id) self.assertEqual(actor_header['ion-actor-roles'], actor_roles) #Now make sure we can change the name of the Org and not affect the headers org2 = self.rr.read(org2_id) org2.name = 'Updated Org 2' org2_id, _ = self.rr.update(org2) actor_roles = find_roles_by_actor(actor_id) self.assertEqual(len(actor_roles), 2) self.assertEqual(len(role_header), 2) self.assertIn('Second_Org', actor_roles) self.assertIn('Second_Org', role_header) self.assertEqual(len(actor_roles['Second_Org']), 2) self.assertEqual(len(role_header['Second_Org']), 2) self.assertIn('OPERATOR', actor_roles['Second_Org']) self.assertIn('OPERATOR', role_header['Second_Org']) self.assertIn(MEMBER_ROLE, actor_roles['Second_Org']) self.assertIn(MEMBER_ROLE, role_header['Second_Org']) self.assertIn('ION', actor_roles) self.assertIn('ION', role_header) self.assertIn(MODERATOR_ROLE, actor_roles['ION']) self.assertIn(MEMBER_ROLE, actor_roles['ION']) self.assertIn(MODERATOR_ROLE, role_header['ION']) self.assertIn(MEMBER_ROLE, role_header['ION']) actor_header = get_actor_header(actor_id) self.assertEqual(actor_header['ion-actor-id'], actor_id) self.assertEqual(actor_header['ion-actor-roles'], actor_roles)
def create_extended_resource_container(self, extended_resource_type, resource_id, computed_resource_type=None, ext_associations=None, ext_exclude=None, **kwargs): """ Returns an extended resource container for a given resource_id. """ overall_start_time = time.time() self.ctx = None # Clear the context in case this instance gets reused if not isinstance(resource_id, types.StringType): raise Inconsistent( "The parameter resource_id is not a single resource id string") if not self.service_provider or not self._rr: raise Inconsistent("This class is not initialized properly") if extended_resource_type not in getextends(OT.ResourceContainer): raise BadRequest( 'The requested resource %s is not extended from %s' % (extended_resource_type, OT.ResourceContainer)) if computed_resource_type and computed_resource_type not in getextends( OT.ComputedAttributes): raise BadRequest( 'The requested resource %s is not extended from %s' % (computed_resource_type, OT.ComputedAttributes)) resource_object = self._rr.read(resource_id) if not resource_object: raise NotFound("The Resource %s does not exist" % resource_id) res_container = IonObject(extended_resource_type) # @TODO - replace with object level decorators and raise exceptions if not hasattr(res_container, 'origin_resource_type'): log.error( 'The requested resource %s does not contain a properly set origin_resource_type field.', extended_resource_type) #raise Inconsistent('The requested resource %s does not contain a properly set origin_resource_type field.' % extended_resource_type) if hasattr(res_container, 'origin_resource_type') and res_container.origin_resource_type != resource_object.type_\ and not issubtype(resource_object.type_, res_container.origin_resource_type): log.error( 'The origin_resource_type of the requested resource %s(%s) does not match the type of the specified resource id(%s).' % (extended_resource_type, res_container.origin_resource_type, resource_object.type_)) #raise Inconsistent('The origin_resource_type of the requested resource %s(%s) does not match the type of the specified resource id(%s).' % (extended_resource_type, res_container.origin_resource_type, resource_object.type_)) res_container._id = resource_object._id res_container.resource = resource_object # Initialize context object field and load resource associations self._prepare_context(resource_object._id) # Fill lcstate related resource container fields self.set_container_lcstate_info(res_container) # Fill resource container fields self.set_container_field_values(res_container, ext_exclude, **kwargs) # Fill computed attributes self.set_computed_attributes(res_container, computed_resource_type, ext_exclude, **kwargs) # Fill additional associations self.set_extended_associations(res_container, ext_associations, ext_exclude) res_container.ts_created = get_ion_ts() overall_stop_time = time.time() log.debug("Time to process extended resource container %s %f secs", extended_resource_type, overall_stop_time - overall_start_time) #log.info("ResourceContainer: %s" % res_container) return res_container
def test_get_actor_header(self): #Setup data actor = IonObject(RT.ActorIdentity, name='actor1') actor_id,_ = self.rr.create(actor) ion_org = IonObject(RT.Org, name='ION') ion_org_id,_ = self.rr.create(ion_org) ion_org._id = ion_org_id manager_role = IonObject(RT.UserRole, name=ORG_MANAGER_ROLE,label='Org Manager', description='Org Manager') manager_role_id = self.add_user_role(ion_org, manager_role) member_role = IonObject(RT.UserRole, name=ORG_MEMBER_ROLE,label='Org Member', description='Org Member') member_role_id = self.add_user_role(ion_org, member_role) actor_roles = self.container.governance_controller.find_roles_by_actor(actor_id) self.assertDictEqual(actor_roles, {'ION': [ORG_MEMBER_ROLE]}) actor_header = self.container.governance_controller.get_actor_header(actor_id) self.assertDictEqual(actor_header, {'ion-actor-id': actor_id, 'ion-actor-roles': {'ION': [ORG_MEMBER_ROLE]}}) #Add Org Manager Role aid = self.rr.create_association(actor_id, PRED.hasRole, manager_role_id) actor_roles = self.container.governance_controller.find_roles_by_actor(actor_id) self.assertDictEqual(actor_roles, {'ION': [ORG_MANAGER_ROLE, ORG_MEMBER_ROLE]}) org2 = IonObject(RT.Org, name='Org2') org2_id,_ = self.rr.create(org2) org2._id = org2_id manager2_role = IonObject(RT.UserRole, name=ORG_MANAGER_ROLE,label='Org Manager', description='Org Manager') manager2_role_id = self.add_user_role(org2, manager_role) member2_role = IonObject(RT.UserRole, name=ORG_MEMBER_ROLE,label='Org Member', description='Org Member') member2_role_id = self.add_user_role(org2, member2_role) operator2_role = IonObject(RT.UserRole, name='INSTRUMENT_OPERATOR',label='Instrument Operator', description='Instrument Operator') operator2_role_id = self.add_user_role(org2, operator2_role) aid = self.rr.create_association(actor_id, PRED.hasRole, member2_role_id) aid = self.rr.create_association(actor_id, PRED.hasRole, operator2_role_id) actor_roles = self.container.governance_controller.find_roles_by_actor(actor_id) self.assertEqual(len(actor_roles), 2) self.assertIn('Org2', actor_roles) self.assertEqual(len(actor_roles['Org2']), 2) self.assertIn('INSTRUMENT_OPERATOR', actor_roles['Org2']) self.assertIn(ORG_MEMBER_ROLE, actor_roles['Org2']) self.assertIn('ION', actor_roles) self.assertIn(ORG_MANAGER_ROLE, actor_roles['ION']) self.assertIn(ORG_MEMBER_ROLE, actor_roles['ION']) actor_header = self.container.governance_controller.get_actor_header(actor_id) self.assertEqual(actor_header['ion-actor-id'], actor_id) self.assertEqual(actor_header['ion-actor-roles'], actor_roles)
def create_extended_resource_container( self, extended_resource_type, resource_id, computed_resource_type=None, ext_associations=None, ext_exclude=None, **kwargs ): """ Returns an extended resource container for a given resource_id. """ overall_start_time = time.time() self.ctx = None # Clear the context in case this instance gets reused if not isinstance(resource_id, types.StringType): raise Inconsistent("The parameter resource_id is not a single resource id string") if not self.service_provider or not self._rr: raise Inconsistent("This class is not initialized properly") if extended_resource_type not in getextends(OT.ResourceContainer): raise BadRequest( "The requested resource %s is not extended from %s" % (extended_resource_type, OT.ResourceContainer) ) if computed_resource_type and computed_resource_type not in getextends(OT.BaseComputedAttributes): raise BadRequest( "The requested resource %s is not extended from %s" % (computed_resource_type, OT.BaseComputedAttributes) ) resource_object = self._rr.read(resource_id) if not resource_object: raise NotFound("The Resource %s does not exist" % resource_id) res_container = IonObject(extended_resource_type) # Check to make sure the extended resource decorator raise OriginResourceType matches the type of the resource type originResourceType = res_container.get_class_decorator_value("OriginResourceType") if originResourceType is None: log.error( "The requested extended resource %s does not contain an OriginResourceType decorator.", extended_resource_type, ) elif originResourceType != resource_object.type_ and not issubtype(resource_object.type_, originResourceType): raise Inconsistent( "The OriginResourceType decorator of the requested resource %s(%s) does not match the type of the specified resource id(%s)." % (extended_resource_type, originResourceType, resource_object.type_) ) res_container._id = resource_object._id res_container.resource = resource_object # Initialize context object field and load resource associations self._prepare_context(resource_object._id) # Fill lcstate related resource container fields self.set_container_lcstate_info(res_container) # Fill resource container info; currently only type_version self.set_res_container_info(res_container) # Fill resource container fields self.set_container_field_values(res_container, ext_exclude, **kwargs) # Fill computed attributes self.set_computed_attributes(res_container, computed_resource_type, ext_exclude, **kwargs) # Fill additional associations self.set_extended_associations(res_container, ext_associations, ext_exclude) res_container.ts_created = get_ion_ts() overall_stop_time = time.time() log.debug( "Time to process extended resource container %s %f secs", extended_resource_type, overall_stop_time - overall_start_time, ) # log.info("ResourceContainer: %s" % res_container) return res_container
def test_event_persist(self): events = [{'_id': '778dcc0811bd4b518ffd1ef873f3f457', 'base_types': ['Event'], 'description': 'Event to deliver the status of instrument.', 'origin': 'instrument_1', 'origin_type': 'PlatformDevice', 'status': 1, 'sub_type': 'input_voltage', 'time_stamps': [2.0, 2.0], 'ts_created': '1364121284585', 'type_': 'DeviceStatusEvent', 'valid_values': [-100, 100], 'values': [110.0, 111.0]}, {'_id': 'b40731684e41418082e1727f3cf61026', 'base_types': ['Event'], 'description': 'Event to deliver the status of instrument.', 'origin': 'instrument_1', 'origin_type': 'PlatformDevice', 'status': 1, 'sub_type': 'input_voltage', 'time_stamps': [2.0, 2.0], 'ts_created': '1364121284609', 'type_': 'DeviceStatusEvent', 'valid_values': [-100, 100], 'values': [110.0, 111.0]}] dsm = DatastoreManager() ds = dsm.get_datastore("events", "EVENTS") ds.delete_datastore() ds.create_datastore() event_repo = EventRepository(dsm) event1_dict = events[0].copy() event1_dict.pop("_id") event1_type = event1_dict.pop("type_") event1 = IonObject(event1_type, **event1_dict) event_repo.put_event(event1) events_r = event_repo.find_events(origin=event1_dict["origin"]) self.assertEquals(len(events_r), 1) event1_read = events_r[0][2] self.assertEquals(event1_read.time_stamps, event1_dict["time_stamps"]) event2_dict = events[1].copy() event2_id = event2_dict.pop("_id") event2_type = event2_dict.pop("type_") event2_obj = IonObject(event2_type, **event2_dict) event2_obj._id = event2_id event_repo.put_event(event2_obj) event1_dict = events[0].copy() event1_id = event1_dict.pop("_id") event1_type = event1_dict.pop("type_") event1_obj = IonObject(event1_type, **event1_dict) event1_obj._id = event1_id event2_dict = events[1].copy() event2_id = event2_dict.pop("_id") event2_type = event2_dict.pop("type_") event2_obj = IonObject(event2_type, **event2_dict) event2_obj._id = event2_id event_repo.put_events([event1_obj, event2_obj]) events_r = event_repo.find_events(event_type='DeviceStatusEvent') self.assertEquals(len(events_r), 3)
def test_event_persist(self): events = [{ '_id': '778dcc0811bd4b518ffd1ef873f3f457', 'base_types': ['Event', 'ResourceEvent'], 'description': 'Event to deliver the status of instrument.', 'origin': 'instrument_1', 'origin_type': 'PlatformDevice', 'sub_type': 'input_voltage', 'ts_created': '1364121284585', 'type_': 'ResourceLifecycleEvent' }, { '_id': 'b40731684e41418082e1727f3cf61026', 'base_types': ['Event', 'ResourceEvent'], 'description': 'Event to deliver the status of instrument.', 'origin': 'instrument_1', 'origin_type': 'PlatformDevice', 'sub_type': 'input_voltage', 'ts_created': '1364121284609', 'type_': 'ResourceModifiedEvent' }] dsm = DatastoreManager() ds = dsm.get_datastore(DataStore.DS_EVENTS, DataStore.DS_PROFILE.EVENTS) ds.delete_datastore() ds.create_datastore() event_repo = EventRepository(dsm) # Store one event without ID event1_dict = events[0].copy() event1_dict.pop("_id") event1_type = event1_dict.pop("type_") event1 = IonObject(event1_type, **event1_dict) event_repo.put_event(event1) events_r = event_repo.find_events(origin=event1_dict["origin"]) self.assertEquals(len(events_r), 1) event1_read = events_r[0][2] # Store one event with ID event2_dict = events[1].copy() event2_id = event2_dict.pop("_id") event2_type = event2_dict.pop("type_") event2_obj = IonObject(event2_type, **event2_dict) event2_obj._id = event2_id event_repo.put_event(event2_obj) # Store multiple new events with ID set and unset, non-existing event1_dict = events[0].copy() event1_id = event1_dict.pop("_id") event1_type = event1_dict.pop("type_") event1_obj = IonObject(event1_type, **event1_dict) event1_obj._id = create_unique_event_id() event2_dict = events[1].copy() event2_id = event2_dict.pop("_id") event2_type = event2_dict.pop("type_") event2_obj = IonObject(event2_type, **event2_dict) event_repo.put_events([event1_obj, event2_obj]) events_r = event_repo.find_events(event_type='ResourceModifiedEvent') self.assertEquals(len(events_r), 2)
def _create_resource(self, restype, name, *args, **kwargs): res_obj = IonObject(restype, dict(name=name, **kwargs)) res_obj_res = self.data_store.create(res_obj) res_obj._id = res_obj_res[0] self.resources[name] = res_obj return res_obj_res[0]
def create_prepare_resource_support(self, resource_id="", prepare_resource_type=None, origin_resource_type=None): if not isinstance(resource_id, types.StringType): raise Inconsistent( "The parameter resource_id is not a single resource id string") if not self.service_provider or not self._rr: raise Inconsistent("This class is not initialized properly") if prepare_resource_type is not None and prepare_resource_type not in getextends( OT.ResourcePrepareSupport): raise BadRequest( 'The requested resource %s is not extended from %s' % (prepare_resource_type, OT.ResourcePrepareSupport)) resource_data = IonObject(prepare_resource_type) # Check to make sure the extended resource decorator raise OriginResourceType matches the type of the resource type origin_resource_decorator = resource_data.get_class_decorator_value( 'OriginResourceType') if origin_resource_decorator is None and origin_resource_type is None: raise NotFound( 'OriginResourceType decorator not found in object specification %s', prepare_resource_type) origin_resource_type = origin_resource_type if origin_resource_type is not None else origin_resource_decorator if origin_resource_type is None: raise NotFound( 'OriginResourceType decorator not found in object specification %s', prepare_resource_type) resource_object = None if resource_id: resource_object = self._rr.read(resource_id) if origin_resource_type != resource_object.type_ and not issubtype( resource_object.type_, origin_resource_type): raise Inconsistent( 'The OriginResourceType decorator of the requested resource %s(%s) does not match the type of the specified resource id(%s).' % (prepare_resource_type, origin_resource_type, resource_object.type_)) resource_data._id = resource_object._id else: resource_object = IonObject(origin_resource_type) resource_data.resource = resource_object resource_data.resource_schema = get_object_schema(origin_resource_type) for field in resource_data._schema: deco_value = resource_data.get_decorator_value( field, 'AssociatedResources') assoc_dict = {} if deco_value is not None: if deco_value.find(',') == -1: associated_resources = [deco_value] else: associated_resources = deco_value.split(',') for res in associated_resources: assoc = self.get_associated_resource_info( origin_resource_type, resource_id, res) assoc_dict[assoc.key] = assoc setattr(resource_data, field, assoc_dict) continue return resource_data