def acquire_resource(self, org_id='', user_id='', resource_id=''): """Acquire the specified resource for a specified user withing the specified Org. Once shared, the resource is committed to the user. Throws a NotFound exception if none of the ids are found. @param org_id str @param user_id str @param resource_id str @retval success bool @throws NotFound object with specified id does not exist """ param_objects = self._validate_parameters(org_id=org_id, user_id=user_id, resource_id=resource_id) commitment = IonObject(RT.ResourceCommitment, name='', org_id=org_id, user_id=user_id, resource_id=resource_id, description='Resource Commitment') commitment_id, commitment_rev = self.clients.resource_registry.create( commitment) commitment._id = commitment_id commitment._rev = commitment_rev self.clients.resource_registry.create_association( user_id, PRED.hasCommitment, commitment) self.clients.resource_registry.create_association( resource_id, PRED.hasCommitment, commitment) return True
def load_mock_associations(self, assoc_list): for assoc_entry in assoc_list: sid = assoc_entry[0] oid = assoc_entry[2] st = self.res_objs[sid]._get_type() ot = self.res_objs[oid]._get_type() ass_obj = IonObject('Association', s=sid, st=st, o=oid, ot=ot, p=assoc_entry[1], ts=get_ion_ts()) ass_obj._id = "%s_%s_%s" % (sid, assoc_entry[1], oid) self.associations.append(ass_obj) self.container_mock.resource_registry.find_associations = Mock() def side_effect(subject=None, predicate=None, obj=None, **kwargs): if predicate: assocs = [ assoc for assoc in self.associations if assoc.p == predicate ] else: assocs = self.associations return assocs self.container_mock.resource_registry.find_associations.side_effect = side_effect
def test_createDataProduct_and_DataProducer_with_id_BadRequest(self): # setup self.resource_registry.find_resources.return_value = ([], 'do not care') self.resource_registry.create.return_value = ('SOME_RR_ID1', 'Version_1') self.data_acquisition_management.create_data_producer.side_effect = BadRequest("Create cannot create document with ID: ") # Data Product dpt_obj = IonObject(RT.DataProduct, name='DPT_X', description='some new data product') # Data Producer dpr_obj = IonObject(RT.DataProducer, name='DP_X', description='some new data producer') dpr_obj._id = "SOME_OTHER_RR_ID" # test call with self.assertRaises(BadRequest) as cm: dp_id = self.data_product_management_service.create_data_product(dpt_obj, dpr_obj) # check results self.resource_registry.find_resources.assert_called_once_with(RT.DataProduct, None, dpt_obj.name, True) self.resource_registry.create.assert_called_once_with(dpt_obj) self.data_acquisition_management.create_data_producer.assert_called_once_with(dpr_obj) ex = cm.exception self.assertEqual(ex.message, "Create cannot create document with ID: ")
def create_resource_commitment(self, org_id="", actor_id="", resource_id="", exclusive=False, expiration=0): """Creates a Commitment for the specified resource for a specified actor within the specified Org. Once shared, the resource is committed to the actor. """ org_obj = self._validate_resource_id("org_id", org_id, RT.Org, optional=True) actor_obj = self._validate_resource_id("actor_id", actor_id, RT.ActorIdentity) resource_obj = self._validate_resource_id("resource_id", resource_id) if org_id: # Check that resource is shared in Org? pass res_commitment = IonObject(OT.ResourceCommitment, resource_id=resource_id, exclusive=exclusive) commitment = IonObject(RT.Commitment, name="", provider=org_id, consumer=actor_id, commitment=res_commitment, description="Resource Commitment", expiration=str(expiration)) commitment._id, commitment._rev = self.rr.create(commitment) # Creating associations to all related objects self.rr.create_association(actor_id, PRED.hasCommitment, commitment._id) self.rr.create_association(commitment._id, PRED.hasTarget, resource_id) if org_id: self.rr.create_association(org_id, PRED.hasCommitment, commitment._id) self.event_pub.publish_event(event_type=OT.ResourceCommitmentCreatedEvent, origin=org_id, origin_type="Org", sub_type=resource_obj.type_, description="The resource has been committed by the Org", resource_id=resource_id, org_name=org_obj.name, commitment_id=commitment._id, commitment_type=commitment.commitment.type_) return commitment._id
def _convert_negotiations_to_requests(self, negotiations=None, user_info_id='', org_id=''): assert isinstance(negotiations, list) orgs,_ = self.clients.resource_registry.find_resources(restype=RT.Org) ret_list = [] for neg in negotiations: request = IonObject(OT.OrgUserNegotiationRequest, ts_updated=neg.ts_updated, negotiation_id=neg._id, negotiation_type=NegotiationTypeEnum._str_map[neg.negotiation_type], negotiation_status=NegotiationStatusEnum._str_map[neg.negotiation_status], originator=ProposalOriginatorEnum._str_map[neg.proposals[-1].originator], request_type=neg.proposals[-1].type_, description=neg.description, reason=neg.reason, user_id=user_info_id) # since this is a proxy for the Negotiation object, simulate its id to help the UI deal with it request._id = neg._id org_request = [ o for o in orgs if o._id == neg.proposals[-1].provider ] if org_request: request.org_id = org_request[0]._id request.name = org_request[0].name ret_list.append(request) return ret_list
def load_mock_events(self, event_list): is_first = len(self.events) == 0 for cnt, event_entry in enumerate(event_list): origin = event_entry.get('o', None) origin_type = event_entry.get('ot', None) sub_type = event_entry.get('st', None) attr = event_entry.get('attr', {}) evt_obj = IonObject(event_entry['et'], origin=origin, origin_type=origin_type, sub_type=sub_type, ts_created=get_ion_ts(), **attr) evt_obj._id = str(cnt) self.events.append(evt_obj) if is_first: self.container_mock.event_repository.find_events = Mock() def side_effect(event_type=None, **kwargs): return [(evt._id, None, evt) for evt in reversed(self.events) if evt.type_ == event_type] self.container_mock.event_repository.find_events.side_effect = side_effect
def _create_association(self, subject=None, predicate=None, obj=None, support_bulk=False): """ Create an association between two IonObjects with a given predicate. Supports bulk mode """ if self.bulk and support_bulk: if not subject or not predicate or not obj: raise BadRequest("Association must have all elements set: %s/%s/%s" % (subject, predicate, obj)) if isinstance(subject, basestring): subject = self._get_resource_obj(subject) if "_id" not in subject: raise BadRequest("Subject id not available") subject_id = subject._id st = subject.type_ if isinstance(obj, basestring): obj = self._get_resource_obj(obj) if "_id" not in obj: raise BadRequest("Object id not available") object_id = obj._id ot = obj.type_ assoc_id = create_unique_association_id() assoc_obj = IonObject("Association", s=subject_id, st=st, p=predicate, o=object_id, ot=ot, ts=get_ion_ts()) assoc_obj._id = assoc_id self.bulk_associations[assoc_id] = assoc_obj return assoc_id, '1-norev' else: return self.rr.create_association(subject, predicate, obj)
def acquire_resource(self, sap=None): """Creates a Commitment Resource for the specified resource for a specified user withing the specified Org as defined in the proposal. Once shared, the resource is committed to the user. Throws a NotFound exception if none of the ids are found. @param proposal AcquireResourceProposal @retval commitment_id str @throws NotFound object with specified id does not exist """ param_objects = self._validate_parameters(org_id=sap.provider, user_id=sap.consumer, resource_id=sap.resource) if sap.type_ == OT.AcquireResourceExclusiveProposal: exclusive = True else: exclusive = False res_commitment = IonObject(OT.ResourceCommitment, resource_id=sap.resource, exclusive=exclusive) commitment = IonObject(RT.Commitment, name='', provider=sap.provider, consumer=sap.consumer, commitment=res_commitment, description='Resource Commitment', expiration=sap.expiration) commitment_id, commitment_rev = self.clients.resource_registry.create(commitment) commitment._id = commitment_id commitment._rev = commitment_rev #Creating associations to all objects self.clients.resource_registry.create_association(sap.provider, PRED.hasCommitment, commitment_id) self.clients.resource_registry.create_association(sap.consumer, PRED.hasCommitment, commitment_id) self.clients.resource_registry.create_association(sap.resource, PRED.hasCommitment, commitment_id) self.clients.resource_registry.create_association(sap.negotiation_id, PRED.hasContract, commitment_id) #TODO - publish some kind of event for creating a commitment return commitment_id
def create_resource_commitment(self, org_id="", actor_id="", resource_id="", exclusive=False, expiration=0): """Creates a Commitment for the specified resource for a specified actor within the specified Org. Once shared, the resource is committed to the actor. """ org_obj = self._validate_resource_id("org_id", org_id, RT.Org, optional=True) actor_obj = self._validate_resource_id("actor_id", actor_id, RT.ActorIdentity) resource_obj = self._validate_resource_id("resource_id", resource_id) if org_id: # Check that resource is shared in Org? pass res_commitment = IonObject(OT.ResourceCommitment, resource_id=resource_id, exclusive=exclusive) commitment = IonObject(RT.Commitment, name="", provider=org_id, consumer=actor_id, commitment=res_commitment, description="Resource Commitment", expiration=str(expiration)) commitment._id, commitment._rev = self.rr.create(commitment) # Creating associations to all related objects self.rr.create_association(actor_id, PRED.hasCommitment, commitment._id) self.rr.create_association(commitment._id, PRED.hasTarget, resource_id) if org_id: self.rr.create_association(org_id, PRED.hasCommitment, commitment._id) self.event_pub.publish_event( event_type=OT.ResourceCommitmentCreatedEvent, origin=org_id, origin_type="Org", sub_type=resource_obj.type_, description="The resource has been committed by the Org", resource_id=resource_id, org_name=org_obj.name, commitment_id=commitment._id, commitment_type=commitment.commitment.type_) return commitment._id
def load_mock_associations(self, assoc_list): for assoc_entry in assoc_list: sid = assoc_entry[0] oid = assoc_entry[2] st = self.res_objs[sid]._get_type() ot = self.res_objs[oid]._get_type() ass_obj = IonObject('Association', s=sid, st=st, o=oid, ot=ot, p=assoc_entry[1], ts=get_ion_ts()) ass_obj._id = "%s_%s_%s" % (sid, assoc_entry[1], oid) self.associations.append(ass_obj) self.container_mock.resource_registry.find_associations = Mock() def side_effect(subject=None, predicate=None, obj=None, **kwargs): if predicate: assocs = [assoc for assoc in self.associations if assoc.p == predicate] else: assocs = self.associations return assocs self.container_mock.resource_registry.find_associations.side_effect = side_effect
def load_mock_resources(self, res_list): for res_entry in res_list: name = res_entry.get('name', 'NO_NAME') lcstate = res_entry.get('lcstate', 'DEPLOYED_AVAILABLE') attr = res_entry.get('attr', {}) res_id = create_unique_resource_id() res_id = res_entry.get('_id', res_id) res_obj = IonObject(res_entry['rt'], name=name, **attr) res_obj._id = res_id res_obj.lcstate = lcstate res_obj.ts_created = get_ion_ts() res_obj.ts_updated = res_obj.ts_created self.res_objs[res_id] = res_obj self.res_id_list.append(res_id) self.container_mock.resource_registry.read_mult = Mock() def side_effect(res_id_list): return [self.res_objs[res_id] for res_id in res_id_list] self.container_mock.resource_registry.read_mult.side_effect = side_effect
def process_new_resource(restype): try: restype = str(restype) res = IonObject(restype) res._id = "NEW" for k,v in request.args.iteritems(): if '.' in k: key = None obj = res attrs = k.split('.') while len(attrs): key = attrs.pop(0) if not len(attrs): if hasattr(obj,key): setattr(obj,key,v) break if hasattr(obj,key): obj = getattr(obj,key) else: break elif hasattr(res,k): setattr(res,k,v) fragments = [ build_standard_menu(), "<h1>Create New %s</h1>" % (build_type_link(restype)), "<form name='edit' action='/cmd/update?rid=NEW&restype=%s' method='post'>" % restype, ] fragments.extend(build_editable_resource(res, is_new=True)) fragments.append("<p><input type='reset'/> <input type='submit' value='Create'/></p>") fragments.append("</form>") fragments.append("<p>%s</p>" % build_link("Back to List Page", "/list/%s" % restype)), content = "\n".join(fragments) return build_page(content) except NotFound: return flask.redirect("/") except Exception as e: return build_error_page(traceback.format_exc())
def acquire_resource(self, org_id='', user_id='', resource_id=''): """Acquire the specified resource for a specified user withing the specified Org. Once shared, the resource is committed to the user. Throws a NotFound exception if none of the ids are found. @param org_id str @param user_id str @param resource_id str @retval success bool @throws NotFound object with specified id does not exist """ param_objects = self._validate_parameters(org_id=org_id, user_id=user_id, resource_id=resource_id) commitment = IonObject(RT.ResourceCommitment, name='', org_id=org_id, user_id=user_id, resource_id=resource_id, description='Resource Commitment') commitment_id, commitment_rev = self.clients.resource_registry.create(commitment) commitment._id = commitment_id commitment._rev = commitment_rev self.clients.resource_registry.create_association(user_id, PRED.hasCommitment, commitment) self.clients.resource_registry.create_association(resource_id, PRED.hasCommitment, commitment) return True
def load_mock_resources(self, res_list): for res_entry in res_list: name = res_entry.get('name', 'NO_NAME') lcstate = res_entry.get('lcstate', 'DEPLOYED') lcav = res_entry.get('availability', 'AVAILABLE') attr = res_entry.get('attr', {}) res_id = create_unique_resource_id() res_id = res_entry.get('_id', res_id) res_obj = IonObject(res_entry['rt'], name=name, **attr) res_obj._id = res_id res_obj.lcstate = lcstate res_obj.availability = lcav res_obj.ts_created = get_ion_ts() res_obj.ts_updated = res_obj.ts_created self.res_objs[res_id] = res_obj self.res_id_list.append(res_id) self.container_mock.resource_registry.read_mult = Mock() def side_effect(res_id_list): return [self.res_objs[res_id] for res_id in res_id_list] self.container_mock.resource_registry.read_mult.side_effect = side_effect
def _convert_negotiations_to_requests(self, negotiations=None, user_info_id='', org_id=''): assert isinstance(negotiations, list) orgs, _ = self.clients.resource_registry.find_resources(restype=RT.Org) ret_list = [] for neg in negotiations: request = IonObject( OT.OrgUserNegotiationRequest, ts_updated=neg.ts_updated, negotiation_id=neg._id, negotiation_type=NegotiationTypeEnum._str_map[ neg.negotiation_type], negotiation_status=NegotiationStatusEnum._str_map[ neg.negotiation_status], originator=ProposalOriginatorEnum._str_map[ neg.proposals[-1].originator], request_type=neg.proposals[-1].type_, description=neg.description, reason=neg.reason, user_id=user_info_id) # since this is a proxy for the Negotiation object, simulate its id to help the UI deal with it request._id = neg._id org_request = [ o for o in orgs if o._id == neg.proposals[-1].provider ] if org_request: request.org_id = org_request[0]._id request.name = org_request[0].name ret_list.append(request) return ret_list
def process_new_resource(restype): try: restype = str(restype) res = IonObject(restype) res._id = "NEW" fragments = [ build_standard_menu(), "<h1>Create New %s</h1>" % (build_type_link(restype)), "<form name='edit' action='/cmd/update?rid=NEW&restype=%s' method='post'>" % restype, ] fragments.extend(build_editable_resource(res, is_new=True)) fragments.append("<p><input type='reset'/> <input type='submit' value='Create'/></p>") fragments.append("</form>") fragments.append("<p>%s</p>" % build_link("Back to List Page", "/list/%s" % restype)), content = "\n".join(fragments) return build_page(content) except NotFound: return flask.redirect("/") except Exception, e: return build_error_page(traceback.format_exc())
def create_resource_commitment(self, org_id='', actor_id='', resource_id='', exclusive=False, expiration=0): """Creates a Commitment Resource for the specified resource for a specified actor withing the specified Org. Once shared, the resource is committed to the actor. Throws a NotFound exception if none of the ids are found. @param org_id str @param actor_id str @param resource_id str @param exclusive bool @param expiration int @retval commitment_id str @throws NotFound object with specified id does not exist """ param_objects = self._validate_parameters(org_id=org_id, actor_id=actor_id, resource_id=resource_id) org = param_objects['org'] actor = param_objects['actor'] resource = param_objects['resource'] res_commitment = IonObject(OT.ResourceCommitment, resource_id=resource_id, exclusive=exclusive) commitment = IonObject(RT.Commitment, name='', provider=org_id, consumer=actor_id, commitment=res_commitment, description='Resource Commitment', expiration=str(expiration)) commitment_id, commitment_rev = self.clients.resource_registry.create(commitment) commitment._id = commitment_id commitment._rev = commitment_rev #Creating associations to all related objects self.clients.resource_registry.create_association(org_id, PRED.hasCommitment, commitment_id) self.clients.resource_registry.create_association(actor_id, PRED.hasCommitment, commitment_id) self.clients.resource_registry.create_association(resource_id, PRED.hasCommitment, commitment_id) self.event_pub.publish_event(event_type=OT.ResourceCommitmentCreatedEvent, origin=org_id, origin_type='Org', sub_type=resource.type_, description='The resource has been committed by the Org', resource_id=resource_id, org_name=org.name, commitment_id=commitment._id, commitment_type=commitment.commitment.type_) return commitment_id