Skip to content

Commit

Permalink
Edit container storage policy
Browse files Browse the repository at this point in the history
  • Loading branch information
JoanBoronat committed Nov 16, 2017
1 parent cf3a6b8 commit c207b33
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
2 changes: 1 addition & 1 deletion api/swift_api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@

# Containers
url(r'^(?P<project_id>[^/]+)/containers/?$', views.containers_list),
url(r'^(?P<project_id>[^/]+)/container/policy?$', views.containers_list),
url(r'^(?P<project_id>[^/]+)/(?P<container_name>[^/]+)/policy?$', views.update_container),

]
40 changes: 36 additions & 4 deletions api/swift_api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -754,13 +754,45 @@ def containers_list(request, project_id):
if containers[c_id]['name'] in ('.dependency', '.storlet'):
del containers[c_id]

return JSONResponse(containers, status=status.HTTP_200_OK)
return JSONResponse(containers, status=status.HTTP_200_OK)
return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)


@csrf_exempt
def update_container(request, project_id, container_name):

if request.method == 'PUT':
data = JSONParser().parse(request)
sp = JSONParser().parse(request)
token = get_token_connection(request)
url = settings.SWIFT_URL + "/AUTH_" + project_id

headers, obj_list = swift_client.get_container(url, token, container_name)
headers['x-storage-policy'] = sp

path_container = settings.SWIFT_CFG_TMP_DIR + "/" + container_name
os.mkdir(path_container)

#TODO
for obj in obj_list:
file = open(path_container + "/" + obj["name"], "w")
obj_headers, obj_body = swift_client.get_object(url, token, container_name, obj["name"])
file.write(obj_body)
obj["headers"] = obj_headers
swift_client.delete_object(url, token, container_name, obj["name"])

swift_client.delete_container(url, token, container_name)
swift_client.put_container(url, token, container_name, headers)

for obj in obj_list:
obj_body = open(path_container + "/" + obj["name"], "r")
swift_response = {}
swift_client.put_object(url, token, container_name, obj["name"],
obj_body, None,
None, None, obj['content_type'],
obj["headers"], None, None, None, swift_response)
os.remove(path_container + "/" + obj)

os.rmdir(path_container)

return JSONResponse("Container Policy updated correctly", status=status.HTTP_201_CREATED)

return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)

0 comments on commit c207b33

Please sign in to comment.