Esempio n. 1
0
    def process_create_entry_work(self, rdict, context, do_load):
        b = BatchQuery()
        # MOSTLY the resource will not exist. So start by calculating the URL and trying to insert the entire record.
        if not do_load:
            url = u"file://{}{}/{}".format(decode_str(context['local_ip']),
                                           decode_str(context['path']),
                                           decode_str(context['entry']))
        else:
            with open(context['fullpath'], 'r') as f:
                blob = Blob.create_from_file(f, rdict['size'])
                if blob:
                    url = "cassandra://{}".format(blob.id)
                else:
                    return None

        try:
            # OK -- try to insert ( create ) the record...
            t1 = time.time()
            resource = Resource.batch(b).create(url=url, **rdict)
            msg = u'Resource {} created --> {}'.format(resource.name,
                                                       time.time() - t1)
            logger.info(msg)
        except ResourceConflictError:
            # If the create fails, the record already exists... so retrieve it...
            t1 = time.time()
            resource = Resource.objects().get(container=context['collection'],
                                              name=rdict['name'])
            msg = u"{} ::: Fetch Object -> {}".format(resource.name,
                                                      time.time() - t1)
            logger.info(msg)

        # if the url is not correct then update
        # TODO: If the URL is a block set that's stored internally, reduce its count so that it can be garbage collected
        # t3 = None
        if resource.url != url:
            t2 = time.time()
            # if url.startswith('cassandra://') : tidy up the stored block count...
            resource.batch(b).update(url=url)
            t3 = time.time()
            msg = u"{} ::: update -> {}".format(resource.name, t3 - t2)
            logger.info(msg)

        # t1 = time.time()
        SearchIndex.reset(resource.id)
        SearchIndex.index(resource, ['name', 'metadata'])

        # msg = "Index Management -> {}".format(time.time() - t1)
        # logger.info(msg)
        b.execute()
Esempio n. 2
0
    def test_create_ok(self):
        coll = Collection.get_root_collection()

        resource = Resource.create(name='test_resource', container=coll.id)
        assert resource
        assert resource.name == 'test_resource'
        assert resource.container == coll.id
Esempio n. 3
0
    def test_permission_ok(self):
        coll = Collection.get_root_collection()
        user = User.create(username="******", password="******", email="*****@*****.**", groups=[], quick=True)
        group = Group.create(name="test_group_resourdce", owner=user.id)
        user.update(groups=[group.id])

        resource = Resource.create(name='new_test_resource', container=coll.id, read_access=[group.id])
        assert resource.user_can(user, "read")
Esempio n. 4
0
    def create(cls, **kwargs):
        """Create a new collection

        We intercept the create call"""
        # TODO: Allow name starting or ending with a space ?
#         container = kwargs.get('container', '/').strip()
#         name = kwargs.get('name').strip()
# 
#         kwargs['name'] = name
#         kwargs['container'] = container

        name = kwargs.get('name')
        container = kwargs.get('container', '/')
        kwargs['container'] = container

        d = datetime.now()
        kwargs['create_ts'] = d
        kwargs['modified_ts'] = d

        if 'metadata' in kwargs:
            kwargs['metadata'] = meta_cdmi_to_cassandra(kwargs['metadata'])

        # Check if parent collection exists
        parent = Collection.find_by_path(container)

        if parent is None:
            raise NoSuchCollectionError(container)

        resource = Resource.find_by_path(merge(container, name))

        if resource is not None:
            raise ResourceConflictError(container)
        collection = Collection.find_by_path(merge(container, name))

        if collection is not None:
            raise CollectionConflictError(container)

        res = super(Collection, cls).create(**kwargs)
        res.mqtt_publish('create')

        return res
Esempio n. 5
0
        def get_object(obj, user):
            """Return the object corresponding to the SearchIndex object"""
            if obj.object_type == 'Collection':
                result_obj = Collection.find_by_id(obj.object_id)
                if not result_obj.user_can(user, "read"):
                    return None

                result_obj = result_obj.to_dict(user)
                result_obj['result_type'] = 'Collection'
                return result_obj
            elif obj.object_type == 'Resource':
                result_obj = Resource.find_by_id(obj.object_id)
                # Check the resource's collection for read permission
                if not result_obj.user_can(user, "read"):
                    return None

                result_obj = result_obj.to_dict(user)
                result_obj['result_type'] = 'Resource'
                return result_obj

            return None
Esempio n. 6
0
    def test_permission_public_ok(self):
        coll = Collection.get_root_collection()
        user = User.create(username="******", password="******", email="*****@*****.**", groups=[], quick=True)

        resource = Resource.create(name='new_test_resource_public', container=coll.id)
        assert resource.user_can(user, "read")
Esempio n. 7
0
 def test_create_dupe(self):
     coll = Collection.get_root_collection()
     resource = Resource.create(name='test_dupe', container=coll.id)
     assert resource
     resource = Resource.create(name='test_dupe', container=coll.id)
Esempio n. 8
0
 def test_create_fail(self):
     resource = Resource.create(name='invalid_resource', container="Wombles!")
Esempio n. 9
0
def is_resource(path):
    """Check if the resource exists"""
    return Resource.find_by_path(path) is not None