Example #1
0
def _get_collection_info(cursor, username, customer_id, collection_name):
    """
    get basic information about the collection
    See Ticket #51 Implement GET JSON for a collection
    """
    log = logging.getLogger("_get_collection_info")
    log.debug("_list_collection(cursor, {0}, {1}".format(
        customer_id, collection_name))
    row = _list_collection(cursor, customer_id, collection_name)
    if row is None:
        collection_dict = {
            "success": False,
            "error_message": "No such collection"
        }
        return httplib.NOT_FOUND, collection_dict

    default_collection_name = compute_default_collection_name(username)

    name, versioning, raw_access_control, raw_creation_time = row
    if raw_access_control is None:
        access_control = None
    else:
        access_control = json.loads(raw_access_control)
    collection_dict = {
        "success": True,
        "name": name,
        "default_collection": name == default_collection_name,
        "versioning": versioning,
        "access_control": access_control,
        "creation-time": http_timestamp_str(raw_creation_time)
    }
    return httplib.OK, collection_dict
def _get_collection_info(cursor, username, customer_id, collection_name):
    """
    get basic information about the collection
    See Ticket #51 Implement GET JSON for a collection
    """
    log = logging.getLogger("_get_collection_info")
    log.debug("_list_collection(cursor, {0}, {1}".format(customer_id, 
                                                         collection_name))
    row = _list_collection(cursor, customer_id, collection_name)
    if row is None:
        collection_dict = {"success"       : False, 
                           "error_message" : "No such collection"}
        return httplib.NOT_FOUND, collection_dict

    default_collection_name = compute_default_collection_name(username)

    name, versioning, raw_access_control, raw_creation_time = row
    if raw_access_control is None:
        access_control = None
    else:
        access_control = json.loads(raw_access_control)
    collection_dict = {"success" : True,
                       "name" : name, 
                       "default_collection" : name == default_collection_name,
                       "versioning" : versioning, 
                       "access_control" : access_control,
                       "creation-time" : http_timestamp_str(raw_creation_time)}
    return httplib.OK, collection_dict
Example #3
0
def _create_default_collection(connection, username, versioning):
    """
    create the customer's default collection, based on username
    """
    collection_name = compute_default_collection_name(username)
    return _create_collection(connection, username, collection_name,
                              versioning)
Example #4
0
def _create_default_collection(connection, username, versioning):
    """
    create the customer's default collection, based on username
    """
    collection_name = compute_default_collection_name(username)
    return _create_collection(connection, 
                              username, 
                              collection_name, 
                              versioning)
Example #5
0
    def dispatch_request(self, username):
        log = logging.getLogger("ListCollectionsView")
        try:
            user_request_id = \
                flask.request.headers['x-nimbus-io-user-request-id']
        except KeyError:
            user_request_id = str(uuid.uuid4())
            log.warn("user_request_id = {0}, " \
                     "no x-nimbus-io-user-request-id " \
                     "header".format(user_request_id))
        log.info("user_request_id = {0}, " \
                 "user_name = {1}".format(user_request_id,
                                          username))

        with GetConnection(self.connection_pool) as connection:

            customer_key_lookup = \
                CustomerKeyConnectionLookup(self.memcached_client,
                                            connection)
            customer_id = authenticate(customer_key_lookup, username,
                                       flask.request)
            if customer_id is None:
                log.info("user_request_id = {0}, unauthorized".format(
                    user_request_id))
                flask.abort(httplib.UNAUTHORIZED)

            try:
                raw_collection_list = _list_collections(
                    connection, customer_id)
            except Exception:
                log.exception("user_request_id = {0}".format(user_request_id))
                raise

        # ticket #50 When listing collections for a user, show whether a
        # collection is a default collection.
        default_collection_name = compute_default_collection_name(username)

        collection_list = list()
        for raw_entry in raw_collection_list:
            name, versioning, raw_access_control, raw_creation_time = raw_entry
            if raw_access_control is None:
                access_control = None
            else:
                access_control = json.loads(raw_access_control)
            entry = {
                "name": name,
                "default_collection": name == default_collection_name,
                "versioning": versioning,
                "access_control": access_control,
                "creation-time": http_timestamp_str(raw_creation_time)
            }
            collection_list.append(entry)

        log.info("user_request_id = {0}, found {1} collections".format(
            user_request_id, len(collection_list)))

        # 2012-08-16 dougfort Ticket #29 - format json for debuging
        data = json.dumps(collection_list, sort_keys=True, indent=4)

        # 2012-08-16 dougfort Ticket #28 - set content_type
        response = flask.Response(data,
                                  status=httplib.OK,
                                  content_type="application/json")
        response.headers["content-length"] = str(len(data))
        return response
Example #6
0
    def dispatch_request(self, username, collection_name):
        log = logging.getLogger("DeleteCollectionView")
        try:
            user_request_id = \
                flask.request.headers['x-nimbus-io-user-request-id']
        except KeyError:
            user_request_id = str(uuid.uuid4())
            log.warn("user_request_id = {0}, " \
                     "no x-nimbus-io-user-request-id " \
                     "header".format(user_request_id))
        log.info("user_request_id = {0}, " \
                 "user_name = {1}, " \
                 "collection_name = {2}".format(user_request_id,
                                                username,
                                                collection_name))

        assert flask.request.method == "DELETE" or \
            (flask.request.method == "POST" \
             and flask.request.args["action"] == "delete"), \
                (user_request_id, flask.request.method, flask.request.args, )

        with GetConnection(self.connection_pool) as connection:

            customer_key_lookup = \
                CustomerKeyConnectionLookup(self.memcached_client,
                                            connection)
            customer_id = authenticate(customer_key_lookup, username,
                                       flask.request)
            if customer_id is None:
                log.info("user_request_id = {0}, " \
                         "unauthroized".format(user_request_id))
                flask.abort(httplib.UNAUTHORIZED)

            # you can't delete your default collection
            default_collection_name = compute_default_collection_name(username)
            if collection_name == default_collection_name:
                log.warn("user_request_id = {0}, " \
                         "attempt to delete default collection {1}".format(
                         user_request_id, default_collection_name))
                flask.abort(httplib.METHOD_NOT_ALLOWED)

            # TODO: can't delete a collection that contains keys

            cursor = connection.cursor()
            try:
                deleted = _delete_collection(cursor, user_request_id,
                                             customer_id, collection_name)
            except Exception:
                log.exception("user_request_id = {0}".format(user_request_id))
                cursor.close()
                connection.rollback()
                raise

            cursor.close()

            # Ticket #39 collection manager allows authenticated users to set
            # versioning property on collections they don't own
            if not deleted:
                log.error("user_request_id = {0}, " \
                          "forbidden".format(user_request_id))
                connection.rollback()
                flask.abort(httplib.FORBIDDEN)

            connection.commit()

        log.info("user_request_id = {0}, "\
                 "collection {1} deleted".format(user_request_id,
                                                 collection_name))

        # Ticket #33 Make Nimbus.io API responses consistently JSON
        collection_dict = {"success": True}
        data = json.dumps(collection_dict, sort_keys=True, indent=4)

        response = flask.Response(data,
                                  status=httplib.OK,
                                  content_type="application/json")
        response.headers["content-length"] = str(len(data))
        return response
    def dispatch_request(self, username):
        log = logging.getLogger("ListCollectionsView")
        try:
            user_request_id = \
                flask.request.headers['x-nimbus-io-user-request-id']
        except KeyError:
            user_request_id = str(uuid.uuid4())
            log.warn("user_request_id = {0}, " \
                     "no x-nimbus-io-user-request-id " \
                     "header".format(user_request_id))
        log.info("user_request_id = {0}, " \
                 "user_name = {1}".format(user_request_id, 
                                          username))

        with GetConnection(self.connection_pool) as connection:

            customer_key_lookup = \
                CustomerKeyConnectionLookup(self.memcached_client,
                                            connection)
            customer_id = authenticate(customer_key_lookup,
                                       username,
                                       flask.request)
            if customer_id is None:
                log.info("user_request_id = {0}, unauthorized".format(
                         user_request_id))
                flask.abort(httplib.UNAUTHORIZED)

            try:
                raw_collection_list = _list_collections(connection, customer_id)
            except Exception:
                log.exception("user_request_id = {0}".format(user_request_id))
                raise

        # ticket #50 When listing collections for a user, show whether a
        # collection is a default collection.
        default_collection_name = compute_default_collection_name(username)

        collection_list = list()
        for raw_entry in raw_collection_list:
            name, versioning, raw_access_control, raw_creation_time = raw_entry
            if raw_access_control is None:
                access_control = None
            else:
                access_control = json.loads(raw_access_control)
            entry = {"name" : name, 
                     "default_collection" : name == default_collection_name,
                     "versioning" : versioning, 
                     "access_control" : access_control,
                     "creation-time" : http_timestamp_str(raw_creation_time)}
            collection_list.append(entry)

        log.info("user_request_id = {0}, found {1} collections".format(
                 user_request_id, len(collection_list)))

        # 2012-08-16 dougfort Ticket #29 - format json for debuging
        data = json.dumps(collection_list, sort_keys=True, indent=4) 

        # 2012-08-16 dougfort Ticket #28 - set content_type
        response = flask.Response(data, 
                                  status=httplib.OK,
                                  content_type="application/json")
        response.headers["content-length"] = str(len(data))
        return response
    def dispatch_request(self, username, collection_name):
        log = logging.getLogger("DeleteCollectionView")
        try:
            user_request_id = \
                flask.request.headers['x-nimbus-io-user-request-id']
        except KeyError:
            user_request_id = str(uuid.uuid4())
            log.warn("user_request_id = {0}, " \
                     "no x-nimbus-io-user-request-id " \
                     "header".format(user_request_id))
        log.info("user_request_id = {0}, " \
                 "user_name = {1}, " \
                 "collection_name = {2}".format(user_request_id,
                                                username, 
                                                collection_name))

        assert flask.request.method == "DELETE" or \
            (flask.request.method == "POST" \
             and flask.request.args["action"] == "delete"), \
                (user_request_id, flask.request.method, flask.request.args, )

        with GetConnection(self.connection_pool) as connection:

            customer_key_lookup = \
                CustomerKeyConnectionLookup(self.memcached_client,
                                            connection)
            customer_id = authenticate(customer_key_lookup,
                                       username,
                                       flask.request)
            if customer_id is None:
                log.info("user_request_id = {0}, " \
                         "unauthroized".format(user_request_id))
                flask.abort(httplib.UNAUTHORIZED)

            # you can't delete your default collection
            default_collection_name = compute_default_collection_name(username)
            if collection_name == default_collection_name:
                log.warn("user_request_id = {0}, " \
                         "attempt to delete default collection {1}".format(
                         user_request_id, default_collection_name))
                flask.abort(httplib.METHOD_NOT_ALLOWED)

            # TODO: can't delete a collection that contains keys

            cursor = connection.cursor()
            try:
                deleted = _delete_collection(cursor, 
                                             user_request_id,
                                             customer_id, 
                                             collection_name)
            except Exception:
                log.exception("user_request_id = {0}".format(user_request_id))
                cursor.close()
                connection.rollback()
                raise

            cursor.close()

            # Ticket #39 collection manager allows authenticated users to set 
            # versioning property on collections they don't own
            if not deleted:
                log.error("user_request_id = {0}, " \
                          "forbidden".format(user_request_id))
                connection.rollback()
                flask.abort(httplib.FORBIDDEN)

            connection.commit()

        log.info("user_request_id = {0}, "\
                 "collection {1} deleted".format(user_request_id,
                                                 collection_name))

        # Ticket #33 Make Nimbus.io API responses consistently JSON
        collection_dict = {"success" : True}
        data = json.dumps(collection_dict, sort_keys=True, indent=4) 

        response = flask.Response(data, 
                                  status=httplib.OK,
                                  content_type="application/json")
        response.headers["content-length"] = str(len(data))
        return response