Commit c0aa2174 authored by Antony Chazapis's avatar Antony Chazapis
Browse files

Merge API and backend container functions

parent d7d10b84
......@@ -12,9 +12,14 @@ try:
except:
from pithos.api.util import parse_http_date_safe
from pithos.api.faults import Fault, NotModified, BadRequest, Unauthorized, LengthRequired, PreconditionFailed, RangeNotSatisfiable, UnprocessableEntity
from pithos.api.faults import Fault, NotModified, BadRequest, Unauthorized, ItemNotFound, LengthRequired, PreconditionFailed, RangeNotSatisfiable, UnprocessableEntity
from pithos.api.util import get_object_meta, get_range, api_method
from settings import PROJECT_PATH
from os import path
STORAGE_PATH = path.join(PROJECT_PATH, 'data')
from pithos.backends.dummy import BackEnd
from pithos.backends.dummy_debug import *
import logging
......@@ -36,7 +41,7 @@ def authenticate(request):
response = HttpResponse(status = 204)
response['X-Auth-Token'] = 'eaaafd18-0fed-4b3a-81b4-663c99ec1cbb'
# TODO: Do we support redirections?
# TODO: Must support X-Storage-Url to be compatible.
response['X-Storage-Url'] = 'http://127.0.0.1:8000/v1/asdf'
return response
......@@ -80,12 +85,15 @@ def object_demux(request, v_account, v_container, v_object):
def account_meta(request, v_account):
# Normal Response Codes: 204
# Error Response Codes: serviceUnavailable (503),
# itemNotFound (404),
# unauthorized (401),
# badRequest (400)
info = get_account_meta(request.user)
be = BackEnd(STORAGE_PATH)
try:
info = be.get_account_meta(request.user)
except NameError:
info = {'count': 0, 'bytes': 0}
response = HttpResponse(status = 204)
response['X-Account-Container-Count'] = info['count']
response['X-Account-Bytes-Used'] = info['bytes']
......@@ -95,6 +103,7 @@ def account_meta(request, v_account):
def container_list(request, v_account):
# Normal Response Codes: 200, 204
# Error Response Codes: serviceUnavailable (503),
# itemNotFound (404),
# unauthorized (401),
# badRequest (400)
......@@ -104,19 +113,28 @@ def container_list(request, v_account):
try:
limit = int(limit)
except ValueError:
limit = None
limit = 10000
containers = list_containers(request.user, marker, limit)
be = BackEnd(STORAGE_PATH)
try:
containers = be.list_containers(request.user, marker, limit)
except NameError:
containers = []
if len(containers) == 0:
return HttpResponse(status = 204)
if request.serialization == 'text':
return HttpResponse('\n'.join(containers), status = 200)
# TODO: Do this with a backend parameter?
try:
containers = [be.get_container_meta(request.user, x) for x in containers]
except NameError:
raise ItemNotFound()
if request.serialization == 'xml':
data = render_to_string('containers.xml', {'account': request.user, 'containers': containers})
elif request.serialization == 'json':
data = json.dumps(containers)
else:
data = '\n'.join(x['name'] for x in containers)
data = json.dumps(containers)
return HttpResponse(data, status = 200)
@api_method('HEAD')
......@@ -127,7 +145,11 @@ def container_meta(request, v_account, v_container):
# unauthorized (401),
# badRequest (400)
info = get_container_meta(request.user, v_container)
be = BackEnd(STORAGE_PATH)
try:
info = be.get_container_meta(request.user, v_container)
except NameError:
raise ItemNotFound()
response = HttpResponse(status = 204)
response['X-Container-Object-Count'] = info['count']
......@@ -142,9 +164,11 @@ def container_create(request, v_account, v_container):
# unauthorized (401),
# badRequest (400)
if create_container(request.user, v_container):
be = BackEnd(STORAGE_PATH)
try:
be.create_container(request.user, v_container)
return HttpResponse(status = 201)
else:
except NameError:
return HttpResponse(status = 202)
@api_method('DELETE')
......@@ -155,13 +179,24 @@ def container_delete(request, v_account, v_container):
# unauthorized (401),
# badRequest (400)
object_count, bytes_count = get_container_meta(request.user, v_container)
if object_count > 0:
be = BackEnd(STORAGE_PATH)
try:
info = be.get_container_meta(request.user, v_container)
except NameError:
raise ItemNotFound()
if info['count'] > 0:
return HttpResponse(status = 409)
delete_container(request.user, v_container)
# TODO: Handle both exceptions.
try:
be.delete_container(request.user, v_container)
except:
raise ItemNotFound()
return HttpResponse(status = 204)
# --- UP TO HERE ---
@api_method('GET', format_allowed = True)
def object_list(request, v_account, v_container):
# Normal Response Codes: 200, 204
......
......@@ -4,6 +4,9 @@ conn = cloudfiles.get_connection('jsmith', '1234567890', authurl = 'http://127.0
print 'Authenticated. Token: %s' % conn.token
print 'Container count: %d Total bytes: %d' % conn.get_info()
container = 'asdf'
conn.create_container(container)
containers = conn.get_all_containers()
print 'Found: %d containers' % len(containers)
for container in containers:
......
......@@ -7,7 +7,14 @@ import types
class BackEnd:
def __init__(self, basepath, log_file='backend.out', log_level=logging.DEBUG):
self.basepath = basepath
logging.basicConfig(filename=log_file,level=log_level,)
self.logger = logging.getLogger(__name__)
self.logger.setLevel(log_level)
formatter = logging.Formatter('[%(levelname)s] %(message)s')
handler = logging.FileHandler(log_file)
handler.setFormatter(formatter)
self.logger.addHandler(handler)
if not os.path.exists(basepath):
os.makedirs(basepath)
db = os.path.join(basepath, 'db')
......@@ -23,9 +30,8 @@ class BackEnd:
"""
returns a dictionary with the container metadata
"""
logging.info("get_account_meta: %s", account)
self.logger.debug("get_account_meta: %s", account)
fullname = os.path.join(self.basepath, account)
print fullname
if not os.path.exists(fullname):
raise NameError('Account does not exist')
contents = os.listdir(fullname)
......@@ -36,10 +42,10 @@ class BackEnd:
def create_container(self, account, name):
"""
creates a new container with the given name
if it doesn't exists under the basepath
if it doesn't exist under the basepath
"""
logging.info("create_container: %s %s", account, name)
fullname = os.path.join(self.basepath, account, name)
self.logger.debug("create_container: %s %s", account, name)
fullname = os.path.join(self.basepath, account, name)
if not os.path.exists(fullname):
os.makedirs(fullname)
else:
......@@ -49,11 +55,10 @@ class BackEnd:
def delete_container(self, account, name):
"""
deletes the container with the given name
if it exists under the basepath
and it's empty
if it exists under the basepath and is empty
"""
logging.debug("delete_container: %s %s", account, name)
fullname = os.path.join(self.basepath, account, name)
self.logger.debug("delete_container: %s %s", account, name)
fullname = os.path.join(self.basepath, account, name)
if not os.path.exists(fullname):
raise NameError('Container does not exist')
if os.listdir(fullname):
......@@ -66,6 +71,7 @@ class BackEnd:
"""
returns a dictionary with the container metadata
"""
self.logger.debug("get_container_meta: %s %s", account, name)
fullname = os.path.join(self.basepath, account, name)
if not os.path.exists(fullname):
raise NameError('Container does not exist')
......@@ -76,12 +82,14 @@ class BackEnd:
def list_containers(self, account, marker = None, limit = 10000):
"""
returns a list of at most limit (default = 10000) account containers
starting from the next item after marker
if optinal parameter marker is provided
or the 1st item otherwise
returns a list of at most limit (default = 10000) containers
starting from the next item after the optional marker
"""
containers = os.listdir(os.path.join(self.basepath, account))
self.logger.debug("list_containers: %s %s %s", account, marker, limit)
fullname = os.path.join(self.basepath, account)
if not os.path.exists(fullname):
raise NameError('Account does not exist')
containers = os.listdir(fullname)
start = 0
if marker:
try:
......@@ -90,11 +98,13 @@ class BackEnd:
pass
return containers[start:limit]
# --- UP TO HERE ---
def list_objects(self, account, container, prefix='', delimiter=None, marker = None, limit = 10000):
"""
returns a list of the objects existing under a specific account container
"""
logging.info("list_objects: %s %s %s %s %s %s", account, container, prefix, delimiter, marker, limit)
self.logger.info("list_objects: %s %s %s %s %s %s", account, container, prefix, delimiter, marker, limit)
dir = os.path.join(self.basepath, account, container)
if not os.path.exists(dir):
raise NameError('Container does not exist')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment