def _finalize_uirefs(self):
     # Create real resource IDs
     for obj in self.ui_objs.values():
         #oid = self.uiid_prefix + obj.uirefid
         oid = create_unique_resource_id()
         obj._id = oid
         self.ui_obj_by_id[oid] = obj
Exemple #2
0
 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]
Exemple #3
0
    def create(self, object=None, actor_id=None):
        if object is None:
            raise BadRequest("Object not present")
        if not isinstance(object, IonObjectBase):
            raise BadRequest("Object is not an IonObject")
        if not is_resource(object):
            raise BadRequest("Object is not a Resource")

        lcsm = get_restype_lcsm(object._get_type())
        object.lcstate = lcsm.initial_state if lcsm else "DEPLOYED_AVAILABLE"
        cur_time = get_ion_ts()
        object.ts_created = cur_time
        object.ts_updated = cur_time
        new_res_id = create_unique_resource_id()
        res = self.rr_store.create(object, new_res_id)
        res_id, rev = res

        if actor_id and actor_id != 'anonymous':
            log.debug("Associate resource_id=%s with owner=%s" % (res_id, actor_id))
            self.rr_store.create_association(res_id, PRED.hasOwner, actor_id)

        self.event_pub.publish_event(event_type="ResourceModifiedEvent",
                                     origin=res_id, origin_type=object._get_type(),
                                     sub_type="CREATE",
                                     mod_type=ResourceModificationType.CREATE)

        return res
Exemple #4
0
    def create_mult(self, res_list, actor_id=None):
        """Creates a list of resources from objects. Objects may have _id in it to predetermine their ID.
        Returns a list of 2-tuples (resource_id, rev)"""
        cur_time = get_ion_ts()
        id_list = []
        for resobj in res_list:
            lcsm = get_restype_lcsm(resobj.type_)
            resobj.lcstate = lcsm.initial_state if lcsm else LCS.DEPLOYED
            resobj.availability = lcsm.initial_availability if lcsm else AS.AVAILABLE
            resobj.ts_created = cur_time
            resobj.ts_updated = cur_time
            id_list.append(resobj._id if "_id" in resobj else create_unique_resource_id())

        res = self.rr_store.create_mult(res_list, id_list, allow_ids=True)
        rid_list = [(rid, rrv) for success, rid, rrv in res]

        # Associations with owners
        if actor_id and actor_id != 'anonymous':
            assoc_list = []
            for resobj, (rid, rrv) in zip(res_list, rid_list):
                resobj._id = rid
                assoc_list.append((resobj, PRED.hasOwner, actor_id))
            self.create_association_mult(assoc_list)

        # Publish events
        for resobj, (rid, rrv) in zip(res_list, rid_list):
            self.event_pub.publish_event(event_type="ResourceModifiedEvent",
                origin=rid, origin_type=resobj.type_,
                mod_type=ResourceModificationType.CREATE)

        return rid_list
Exemple #5
0
 def _finalize_uirefs(self):
     # Create real resource IDs
     for obj in self.ui_objs.values():
         #oid = self.uiid_prefix + obj.uirefid
         oid = create_unique_resource_id()
         obj._id = oid
         self.ui_obj_by_id[oid] = obj
    def create_mult(self, res_list, lcstate=None):
        cur_time = get_ion_ts()
        for resobj in res_list:
            resobj['lcstate'] = lcstate or "DEPLOYED_AVAILABLE"
            resobj['ts_created'] = cur_time
            resobj['ts_updated'] = cur_time

        id_list = [create_unique_resource_id() for i in xrange(len(res_list))]
        res = self.datastore.create_doc_mult(res_list, id_list)
        res_list = [(rid,rrv) for success,rid,rrv in res]

        return res_list
    def create_mult(self, res_list, lcstate=None):
        cur_time = get_ion_ts()
        for resobj in res_list:
            resobj['lcstate'] = lcstate or "DEPLOYED_AVAILABLE"
            resobj['ts_created'] = cur_time
            resobj['ts_updated'] = cur_time

        id_list = [create_unique_resource_id() for i in xrange(len(res_list))]
        res = self.datastore.create_doc_mult(res_list, id_list)
        res_list = [(rid, rrv) for success, rid, rrv in res]

        return res_list
Exemple #8
0
    def _create_bulk_resource(self, res_obj, res_alias=None):
        if not hasattr(res_obj, "_id"):
            res_obj._id = create_unique_resource_id()
        ts = get_ion_ts()
        if hasattr(res_obj, "ts_created") and not res_obj.ts_created:
            res_obj.ts_created = ts
        if hasattr(res_obj, "ts_updated") and not res_obj.ts_updated:
            res_obj.ts_updated = ts

        res_id = res_obj._id
        self.bulk_resources[res_id] = res_obj
        if res_alias:
            self._register_id(res_alias, res_id, res_obj)
        return res_id
Exemple #9
0
    def _create_bulk_resource(self, res_obj, res_alias=None):
        if not hasattr(res_obj, "_id"):
            res_obj._id = create_unique_resource_id()
        ts = get_ion_ts()
        if hasattr(res_obj, "ts_created") and not res_obj.ts_created:
            res_obj.ts_created = ts
        if hasattr(res_obj, "ts_updated") and not res_obj.ts_updated:
            res_obj.ts_updated = ts

        res_id = res_obj._id
        self.bulk_resources[res_id] = res_obj
        if res_alias:
            self._register_id(res_alias, res_id, res_obj)
        return res_id
    def create(self, object=None, actor_id=None, lcstate=None):
        if object is None:
            raise BadRequest("Object not present")
        if not "type_" in object:
            raise BadRequest("Object is not an IonObject")
        cur_time = get_ion_ts()
        object['lcstate'] =  lcstate or "DEPLOYED_AVAILABLE"
        object['ts_created'] = cur_time
        object['ts_updated'] = cur_time
        new_res_id = create_unique_resource_id()
        res_id, rev = self.datastore.create_doc(object, new_res_id)

        if actor_id and actor_id != 'anonymous':
            self.create_association(res_id, "hasOwner", actor_id)

        return res
    def create(self, object=None, actor_id=None, lcstate=None):
        if object is None:
            raise BadRequest("Object not present")
        if not "type_" in object:
            raise BadRequest("Object is not an IonObject")
        cur_time = get_ion_ts()
        object['lcstate'] = lcstate or "DEPLOYED_AVAILABLE"
        object['ts_created'] = cur_time
        object['ts_updated'] = cur_time
        new_res_id = create_unique_resource_id()
        res_id, rev = self.datastore.create_doc(object, new_res_id)

        if actor_id and actor_id != 'anonymous':
            self.create_association(res_id, "hasOwner", actor_id)

        return res
Exemple #12
0
    def create(self, object=None, actor_id=None, object_id=None, attachments=None):
        """
        Accepts object that is to be stored in the data store and tags them with additional data
        (timestamp and such) If actor_id is provided, creates hasOwner association with objects.
        If attachments are provided
        (in dict(att1=dict(data=xyz), att2=dict(data=aaa, content_type='text/plain') form)
        they get attached to the object.
        Returns a tuple containing object and revision identifiers.
        """
        if object is None:
            raise BadRequest("Object not present")
        if not isinstance(object, IonObjectBase):
            raise BadRequest("Object is not an IonObject")
        if not is_resource(object):
            raise BadRequest("Object is not a Resource")
        if "_id" in object:
            raise BadRequest("Object must not contain _id")
        if "_rev" in object:
            raise BadRequest("Object must not contain _rev")


        lcsm = get_restype_lcsm(object._get_type())
        object.lcstate = lcsm.initial_state if lcsm else LCS.DEPLOYED
        object.availability = lcsm.initial_availability if lcsm else AS.AVAILABLE
        cur_time = get_ion_ts()
        object.ts_created = cur_time
        object.ts_updated = cur_time
        if object_id is None:
            new_res_id = create_unique_resource_id()
        else:
            new_res_id = object_id
        res = self.rr_store.create(object, new_res_id, attachments=attachments)
        res_id, rev = res

        if actor_id and actor_id != 'anonymous':
            log.debug("Associate resource_id=%s with owner=%s", res_id, actor_id)
            self.create_association(res_id, PRED.hasOwner, actor_id)

        if self.container.has_capability(self.container.CCAP.EVENT_PUBLISHER):
            self.event_pub.publish_event(event_type="ResourceModifiedEvent",
                                     origin=res_id, origin_type=object._get_type(),
                                     sub_type="CREATE",
                                     mod_type=ResourceModificationType.CREATE)

        return res
Exemple #13
0
    def create(self,
               object=None,
               actor_id=None,
               object_id=None,
               attachments=None):
        """
        Accepts object that is to be stored in the data store and tags them with additional data
        (timestamp and such) If actor_id is provided, creates hasOwner association with objects.
        If attachments are provided
        (in dict(att1=dict(data=xyz), att2=dict(data=aaa, content_type='text/plain') form)
        they get attached to the object.
        Returns a tuple containing object and revision identifiers.
        """
        if object is None:
            raise BadRequest("Object not present")
        if not isinstance(object, IonObjectBase):
            raise BadRequest("Object is not an IonObject")
        if not is_resource(object):
            raise BadRequest("Object is not a Resource")

        lcsm = get_restype_lcsm(object._get_type())
        object.lcstate = lcsm.initial_state if lcsm else "DEPLOYED_AVAILABLE"
        cur_time = get_ion_ts()
        object.ts_created = cur_time
        object.ts_updated = cur_time
        if object_id is None:
            new_res_id = create_unique_resource_id()
        else:
            new_res_id = object_id
        res = self.rr_store.create(object, new_res_id, attachments=attachments)
        res_id, rev = res

        if actor_id and actor_id != 'anonymous':
            log.debug("Associate resource_id=%s with owner=%s" %
                      (res_id, actor_id))
            self.rr_store.create_association(res_id, PRED.hasOwner, actor_id)

        self.event_pub.publish_event(event_type="ResourceModifiedEvent",
                                     origin=res_id,
                                     origin_type=object._get_type(),
                                     sub_type="CREATE",
                                     mod_type=ResourceModificationType.CREATE)

        return res
Exemple #14
0
    def _create_mult(self, res_list):
        cur_time = get_ion_ts()
        for resobj in res_list:
            lcsm = get_restype_lcsm(resobj._get_type())
            resobj.lcstate = lcsm.initial_state if lcsm else "DEPLOYED_AVAILABLE"
            resobj.ts_created = cur_time
            resobj.ts_updated = cur_time

        id_list = [create_unique_resource_id() for i in xrange(len(res_list))]
        res = self.rr_store.create_mult(res_list, id_list)
        res_list = [(rid, rrv) for success, rid, rrv in res]

        # TODO: Publish events (skipped, because this is inefficent one by one for a large list
#        for rid,rrv in res_list:
#            self.event_pub.publish_event(event_type="ResourceModifiedEvent",
#                origin=res_id, origin_type=object._get_type(),
#                mod_type=ResourceModificationType.CREATE)

        return res_list
Exemple #15
0
    def _create_mult(self, res_list):
        cur_time = get_ion_ts()
        for resobj in res_list:
            lcsm = get_restype_lcsm(resobj._get_type())
            resobj.lcstate = lcsm.initial_state if lcsm else "DEPLOYED_AVAILABLE"
            resobj.ts_created = cur_time
            resobj.ts_updated = cur_time

        id_list = [create_unique_resource_id() for i in xrange(len(res_list))]
        res = self.rr_store.create_mult(res_list, id_list)
        res_list = [(rid,rrv) for success,rid,rrv in res]

        # TODO: Publish events (skipped, because this is inefficent one by one for a large list
#        for rid,rrv in res_list:
#            self.event_pub.publish_event(event_type="ResourceModifiedEvent",
#                origin=res_id, origin_type=object._get_type(),
#                mod_type=ResourceModificationType.CREATE)

        return res_list
Exemple #16
0
    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
Exemple #17
0
    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
Exemple #18
0
    def create_mult(self, res_list):
        cur_time = get_ion_ts()
        id_list = []
        for resobj in res_list:
            lcsm = get_restype_lcsm(resobj._get_type())
            resobj.lcstate = lcsm.initial_state if lcsm else LCS.DEPLOYED
            resobj.availability = lcsm.initial_availability if lcsm else AS.AVAILABLE
            resobj.ts_created = cur_time
            resobj.ts_updated = cur_time
            id_list.append(resobj._id if "_id" in resobj else create_unique_resource_id())

        res = self.rr_store.create_mult(res_list, id_list, allow_ids=True)
        res_list = [(rid, rrv) for success, rid, rrv in res]

        # TODO: Associations with owners

        # TODO: Publish events (skipped, because this is inefficient one by one for a large list
#        for rid,rrv in res_list:
#            self.event_pub.publish_event(event_type="ResourceModifiedEvent",
#                origin=res_id, origin_type=object._get_type(),
#                mod_type=ResourceModificationType.CREATE)

        return res_list
Exemple #19
0
 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]