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 get_associated_resource_info(self, origin_resource_type="", resource_id="", assoc_resource_type=None): if not origin_resource_type: raise Inconsistent("The parameter origin_resource_type is not set") 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 assoc_resource_type is not None and assoc_resource_type not in getextends(OT.AssociatedResources): raise BadRequest( "The requested resource %s is not extended from %s" % (assoc_resource_type, OT.AssociatedResources) ) resource_data = IonObject(assoc_resource_type) log.debug("get_associated_resource_info: %s", assoc_resource_type) for field in resource_data._schema: deco_value = resource_data.get_decorator_value(field, "ResourceType") if deco_value is not None: # Set the type of the associated resource to be used as the key in dict of associations setattr(resource_data, "resource_type", deco_value) res_list, _ = self._rr.find_resources(restype=deco_value, id_only=False) exclude_lcs_filter_value = resource_data.get_decorator_value(field, "ExcludeLifecycleStates") if exclude_lcs_filter_value is not None and exclude_lcs_filter_value.find(",") > -1: exclude_filter = exclude_lcs_filter_value.split(",") res_list = [res for res in res_list if res.lcstate not in exclude_filter] # Look for ResourceSPOFilter filter and filter above results to exclude other resources already assigned # This filter MUST be a comma separated value of Subject, Predicate, Object res_filter_value = resource_data.get_decorator_value(field, "ResourceSPOFilter") if res_filter_value is not None and res_filter_value.find(",") > -1: assoc_filter = res_filter_value.split(",") res_associations = self._rr.find_associations(predicate=assoc_filter[1], id_only=False) assoc_list = [a for a in res_associations if a.st == assoc_filter[0] and a.ot == assoc_filter[2]] def resource_available(res): # give me a list of associations relevent to the passed in resource rel_assocs = [ a for a in assoc_list if res._id == (a.o if a.st == origin_resource_type else a.s) ] # of those resources, give me the ids of resources whos types match what i am currently building for assocs = [(a.s if a.st == origin_resource_type else a.o) for a in rel_assocs] # return true if there are no associations for the resource in question, or it has an association to # the current resource we are building for return len(assocs) == 0 or resource_id in assocs # Iterate over the list and remove any object which are assigned to other resources and not the target resource final_list = [] final_list.extend([res for res in res_list if resource_available(res)]) setattr(resource_data, field, final_list) else: setattr(resource_data, field, res_list) continue deco_value = resource_data.get_decorator_value(field, "Association") if deco_value is not None: # If the association is related to an existing resource and this is a create then skip if not resource_id and ( resource_data.is_decorator(field, "ResourceSubject") or resource_data.is_decorator(field, "ResourceObject") ): continue resource_sub = resource_id if resource_data.is_decorator(field, "ResourceSubject") else None resource_obj = resource_id if resource_data.is_decorator(field, "ResourceObject") else None assoc_list = self._rr.find_associations( subject=resource_sub, predicate=deco_value, object=resource_obj, id_only=False ) subject_type = resource_data.get_decorator_value(field, "SubjectType") if subject_type is not None: assoc_list = [assoc for assoc in assoc_list if (assoc.st == subject_type)] object_type = resource_data.get_decorator_value(field, "ObjectType") if object_type is not None: assoc_list = [assoc for assoc in assoc_list if (assoc.ot == object_type)] setattr(resource_data, field, assoc_list) continue # set key if we have one key = resource_data.get_class_decorator_value("Key") if key is None: resource_data.key = resource_data.resource_type else: resource_data.key = key return resource_data
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 get_associated_resource_info(self, origin_resource_type="", resource_id="", assoc_resource_type=None): if not origin_resource_type: raise Inconsistent("The parameter origin_resource_type is not set") 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 assoc_resource_type is not None and assoc_resource_type not in getextends(OT.AssociatedResources): raise BadRequest('The requested resource %s is not extended from %s' % (assoc_resource_type, OT.AssociatedResources)) resource_data = IonObject(assoc_resource_type) for field in resource_data._schema: deco_value = resource_data.get_decorator_value(field, 'ResourceType') if deco_value is not None: #Set the type of the associated resource to be used as the key in dict of associations setattr(resource_data, 'resource_type', deco_value) res_list,_ = self._rr.find_resources(restype=deco_value, id_only=False) exclude_lcs_filter_value = resource_data.get_decorator_value(field, 'ExcludeLifecycleStates') if exclude_lcs_filter_value is not None and exclude_lcs_filter_value.find(',') > -1: exclude_filter = exclude_lcs_filter_value.split(',') res_list = [res for res in res_list if res.lcstate not in exclude_filter ] #Look for ResourceSPOFilter filter and filter above results to exclude other resources already assigned #This filter MUST be a comma separated value of Subject, Predicate, Object res_filter_value = resource_data.get_decorator_value(field, 'ResourceSPOFilter') if res_filter_value is not None and res_filter_value.find(',') > -1: assoc_filter = res_filter_value.split(',') res_associations = self._rr.find_associations(predicate=assoc_filter[1], id_only=False) assoc_list = [a for a in res_associations if a.st==assoc_filter[0] and a.ot==assoc_filter[2]] def resource_available(res): for assoc in assoc_list: if assoc.st == origin_resource_type: if assoc.o == res._id: if not resource_id: return False if assoc.s == resource_id: return True else: return False else: if assoc.s == res._id: if not resource_id: return False if assoc.o == resource_id: return True else: return False return True #Iterate over the list and remove any object which are assigned to other resources and not the target resource final_list = [] final_list.extend( [res for res in res_list if resource_available(res)]) setattr(resource_data, field, final_list) else: setattr(resource_data, field, res_list) continue deco_value = resource_data.get_decorator_value(field, 'Association') if deco_value is not None: #If the association is related to an existing resource and this is a create then skip if not resource_id and ( resource_data.is_decorator(field, 'ResourceSubject') or resource_data.is_decorator(field, 'ResourceObject')): continue resource_sub = resource_id if resource_data.is_decorator(field, 'ResourceSubject') else None resource_obj = resource_id if resource_data.is_decorator(field, 'ResourceObject') else None assoc_list = self._rr.find_associations(subject=resource_sub, predicate=deco_value, object=resource_obj, id_only=False) subject_type = resource_data.get_decorator_value(field, 'SubjectType') if subject_type is not None: assoc_list = [assoc for assoc in assoc_list if ( assoc.st == subject_type )] object_type = resource_data.get_decorator_value(field, 'ObjectType') if object_type is not None: assoc_list = [assoc for assoc in assoc_list if ( assoc.ot == object_type )] setattr(resource_data, field, assoc_list) continue return resource_data
def get_associated_resource_info(self, origin_resource_type="", resource_id="", assoc_resource_type=None): if not origin_resource_type: raise Inconsistent("The parameter origin_resource_type is not set") 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 assoc_resource_type is not None and assoc_resource_type not in getextends( OT.AssociatedResources): raise BadRequest( 'The requested resource %s is not extended from %s' % (assoc_resource_type, OT.AssociatedResources)) resource_data = IonObject(assoc_resource_type) log.debug("get_associated_resource_info: %s", assoc_resource_type) for field in resource_data._schema: deco_value = resource_data.get_decorator_value( field, 'ResourceType') if deco_value is not None: #Set the type of the associated resource to be used as the key in dict of associations setattr(resource_data, 'resource_type', deco_value) res_list, _ = self._rr.find_resources(restype=deco_value, id_only=False) exclude_lcs_filter_value = resource_data.get_decorator_value( field, 'ExcludeLifecycleStates') if exclude_lcs_filter_value is not None and exclude_lcs_filter_value.find( ',') > -1: exclude_filter = exclude_lcs_filter_value.split(',') res_list = [ res for res in res_list if res.lcstate not in exclude_filter ] #Look for ResourceSPOFilter filter and filter above results to exclude other resources already assigned #This filter MUST be a comma separated value of Subject, Predicate, Object res_filter_value = resource_data.get_decorator_value( field, 'ResourceSPOFilter') if res_filter_value is not None and res_filter_value.find( ',') > -1: assoc_filter = res_filter_value.split(',') res_associations = self._rr.find_associations( predicate=assoc_filter[1], id_only=False) assoc_list = [ a for a in res_associations if a.st == assoc_filter[0] and a.ot == assoc_filter[2] ] def resource_available(res): # give me a list of associations relevent to the passed in resource rel_assocs = [ a for a in assoc_list if res._id == ( a.o if a.st == origin_resource_type else a.s) ] # of those resources, give me the ids of resources whos types match what i am currently building for assocs = [ (a.s if a.st == origin_resource_type else a.o) for a in rel_assocs ] # return true if there are no associations for the resource in question, or it has an association to # the current resource we are building for return len(assocs) == 0 or resource_id in assocs #Iterate over the list and remove any object which are assigned to other resources and not the target resource final_list = [] final_list.extend( [res for res in res_list if resource_available(res)]) setattr(resource_data, field, final_list) else: setattr(resource_data, field, res_list) continue deco_value = resource_data.get_decorator_value( field, 'Association') if deco_value is not None: #If the association is related to an existing resource and this is a create then skip if not resource_id and ( resource_data.is_decorator(field, 'ResourceSubject') or resource_data.is_decorator(field, 'ResourceObject')): continue resource_sub = resource_id if resource_data.is_decorator( field, 'ResourceSubject') else None resource_obj = resource_id if resource_data.is_decorator( field, 'ResourceObject') else None assoc_list = self._rr.find_associations(subject=resource_sub, predicate=deco_value, object=resource_obj, id_only=False) subject_type = resource_data.get_decorator_value( field, 'SubjectType') if subject_type is not None: assoc_list = [ assoc for assoc in assoc_list if (assoc.st == subject_type) ] object_type = resource_data.get_decorator_value( field, 'ObjectType') if object_type is not None: assoc_list = [ assoc for assoc in assoc_list if (assoc.ot == object_type) ] setattr(resource_data, field, assoc_list) continue # set key if we have one key = resource_data.get_class_decorator_value('Key') if key is None: resource_data.key = resource_data.resource_type else: resource_data.key = key return resource_data