Skip to content

Commit

Permalink
Get containers and users
Browse files Browse the repository at this point in the history
  • Loading branch information
JosepSampe committed Oct 30, 2017
1 parent 1212c66 commit 04eb6f8
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 37 deletions.
4 changes: 2 additions & 2 deletions api/api/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ def get_swift_url_and_token(project_name):
admin_passwd, auth_version="3")


def get_admin_role_user_ids():
keystone_client = get_keystone_admin_auth()
def get_admin_role_user_ids(keystone_client):
# keystone_client = get_keystone_admin_auth()
roles = keystone_client.roles.list()
for role in roles:
if role.name == 'admin':
Expand Down
3 changes: 1 addition & 2 deletions api/api/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@
KEYSTONE_URL = 'http://localhost:5000/v3'

# Swift
SWIFT_URL = 'http://localhost:8080/'
SWIFT_API_VERSION = 'v1'
SWIFT_URL = 'http://localhost:8080/v1'

# Redis
REDIS_HOST = 'localhost'
Expand Down
14 changes: 7 additions & 7 deletions api/filters/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,9 +266,9 @@ def filter_deploy(request, filter_id, project_id, container=None, swift_object=N

# TODO: Try to improve this part
if container and swift_object:
target = project_id + "/" + container + "/" + swift_object
target = os.path.join(project_id, container, swift_object)
elif container:
target = project_id + "/" + container
target = os.path.join(project_id, container)
else:
target = project_id

Expand Down Expand Up @@ -421,7 +421,7 @@ def dependency_deploy(request, dependency_id, project_id):
content_length = None
response = dict()
token = get_token_connection(request)
url = settings.SWIFT_URL + settings.SWIFT_API_VERSION + "/AUTH_" + project_id
url = settings.SWIFT_URL + "/AUTH_" + project_id
swift_client.put_object(url, token, 'dependency', dependency["name"], dependency_file, content_length,
None, None, "application/octet-stream", metadata, None, None, None, response)
except ClientException:
Expand Down Expand Up @@ -477,7 +477,7 @@ def dependency_undeploy(request, dependency_id, project_id):

try:
token = get_token_connection(request)
url = settings.SWIFT_URL + settings.SWIFT_API_VERSION + "/AUTH_" + project_id
url = settings.SWIFT_URL + "/AUTH_" + project_id
swift_response = dict()
swift_client.delete_object(url, token, 'dependency', dependency["name"], None, None, None, None, swift_response)

Expand Down Expand Up @@ -513,7 +513,7 @@ def set_filter(r, target, filter_data, parameters, token):
projects_crystal_enabled = r.lrange('projects_crystal_enabled', 0, -1)
for project_id in projects_crystal_enabled:
swift_response = dict()
url = settings.SWIFT_URL + settings.SWIFT_API_VERSION + "/AUTH_" + project_id
url = settings.SWIFT_URL + "/AUTH_" + project_id
storlet_file = open(filter_data["path"], 'r')
swift_client.put_object(url, token, "storlet",
filter_data["filter_name"],
Expand All @@ -522,7 +522,7 @@ def set_filter(r, target, filter_data, parameters, token):
metadata, None, None, None, swift_response)
else:
swift_response = dict()
url = settings.SWIFT_URL + settings.SWIFT_API_VERSION + "/AUTH_" + project_id
url = settings.SWIFT_URL + "/AUTH_" + project_id
storlet_file = open(filter_data["path"], 'r')
swift_client.put_object(url, token, "storlet",
filter_data["filter_name"],
Expand Down Expand Up @@ -567,7 +567,7 @@ def unset_filter(r, target, filter_data, token):
try:
project_id = target.split('/', 3)[0]
swift_response = dict()
url = settings.SWIFT_URL + settings.SWIFT_API_VERSION + "/AUTH_" + project_id
url = settings.SWIFT_URL + "/AUTH_" + project_id
swift_client.delete_object(url, token, "storlet", filter_data["filter_name"], None, None, None, None, swift_response)
except ClientException as e:
print swift_response + str(e)
Expand Down
2 changes: 1 addition & 1 deletion api/projects/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
# Crystal Projects
url(r'^$', views.projects),
url(r'^(?P<project_id>\w+)/?$', views.projects),

# Crystal Project Users
url(r'^(?P<project_id>\w+)/users/?$', views.project_users_list),

Expand Down
43 changes: 30 additions & 13 deletions api/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def projects(request, project_id=None):
try:
# Set Manager as admin of the Crystal Project
keystone_client = get_keystone_admin_auth()
admin_role_id, reseller_admin_role_id, admin_user_id = get_admin_role_user_ids()
admin_role_id, reseller_admin_role_id, admin_user_id = get_admin_role_user_ids(keystone_client)
keystone_client.roles.grant(role=admin_role_id, user=admin_user_id, project=project_id)
keystone_client.roles.grant(role=reseller_admin_role_id, user=admin_user_id, project=project_id)

Expand Down Expand Up @@ -88,7 +88,7 @@ def projects(request, project_id=None):

# Delete Manager as admin of the Crystal Project
keystone_client = get_keystone_admin_auth()
admin_role_id, reseller_admin_role_id, admin_user_id = get_admin_role_user_ids()
admin_role_id, reseller_admin_role_id, admin_user_id = get_admin_role_user_ids(keystone_client)
try:
keystone_client.roles.revoke(role=admin_role_id, user=admin_user_id, project=project_id)
keystone_client.roles.revoke(role=reseller_admin_role_id, user=admin_user_id, project=project_id)
Expand Down Expand Up @@ -197,7 +197,6 @@ def undeploy_docker_image(node, node_data, project_id, r):
raise SSHException('An error occurred creating the Docker image in: '+node)



#
# Crystal Projects groups
#
Expand Down Expand Up @@ -293,15 +292,33 @@ def projects_groups_detail(request, group_id, project_id):
status=status.HTTP_204_NO_CONTENT)
return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)


@csrf_exempt
def project_users_list(request, project_id):

try:
r = get_redis_connection()
except RedisError:
return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR)

# TODO
return JSONResponse([], status=status.HTTP_200_OK)


if request.method == 'GET':
keystone_client = get_keystone_admin_auth()
users = keystone_client.users.list()
roles = keystone_client.roles.list()

for role in roles:
if role.name == 'user':
user_role_id = role.id
break
role_assignments = keystone_client.role_assignments.list()

valid_users = list()
for ra in role_assignments:
if 'project' in ra.scope and ra.scope['project']['id'] == project_id \
and ra.role['id'] == user_role_id:
valid_users.append(ra.user['id'])

user_list = list()
for user in users:
if user.id in valid_users:
user_data = {}
user_data['id'] = user.id
user_data['name'] = user.name
user_list.append(user_data)

return JSONResponse(user_list, status=status.HTTP_200_OK)
return JSONResponse('Method ' + str(request.method) + ' not allowed.', status=status.HTTP_405_METHOD_NOT_ALLOWED)
3 changes: 1 addition & 2 deletions api/swift/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
url(r'^regions/(?P<region_id>[^/]+)/?$', views.region_detail),
url(r'^zones/?$', views.zones),
url(r'^zones/(?P<zone_id>[^/]+)/?$', views.zone_detail),

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


]
21 changes: 11 additions & 10 deletions api/swift/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
from rest_framework import status
from rest_framework.exceptions import ParseError
from rest_framework.parsers import JSONParser
from swiftclient import client as swift_client
from operator import itemgetter
import json
import logging
import requests
import paramiko
from api.common import JSONResponse, get_redis_connection, to_json_bools
from api.common import JSONResponse, get_redis_connection, to_json_bools, get_token_connection
from api.exceptions import FileSynchronizationException


Expand Down Expand Up @@ -542,14 +543,14 @@ def zone_detail(request, zone_id):
# Containers
@csrf_exempt
def containers_list(request, project_id):

try:
r = get_redis_connection()
except RedisError:
return JSONResponse('Error connecting with DB', status=status.HTTP_500_INTERNAL_SERVER_ERROR)
if request.method == 'GET':
token = get_token_connection(request)
url = settings.SWIFT_URL + "/AUTH_" + project_id

# TODO
return JSONResponse([], status=status.HTTP_200_OK)
_, containers = swift_client.get_account(url, token)
for c_id in reversed(range(len(containers))):
if containers[c_id]['name'] in ('dependency', 'storlet'):
del containers[c_id]



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

0 comments on commit 04eb6f8

Please sign in to comment.