diff --git a/uncloud/opennebula/management/commands/syncvm.py b/uncloud/opennebula/management/commands/syncvm.py index f5f80b1..00108f0 100644 --- a/uncloud/opennebula/management/commands/syncvm.py +++ b/uncloud/opennebula/management/commands/syncvm.py @@ -1,15 +1,22 @@ -import os import json +import uncloud.secrets as secrets + + +from xmlrpc.client import ServerProxy as RPCClient + from django.core.management.base import BaseCommand from django.contrib.auth import get_user_model -from xmlrpc.client import ServerProxy as RPCClient - from xmltodict import parse +from ungleich_common.ldap.ldap_manager import LdapManager from opennebula.models import VM as VMModel -import uncloud.secrets + +def find_user_based_on_email(users, email): + for user in users: + if email in user.mail.values: + return user class Command(BaseCommand): @@ -19,30 +26,39 @@ class Command(BaseCommand): pass def handle(self, *args, **options): - with RPCClient(uncloud.secrets.OPENNEBULA_URL) as rpc_client: + ldap_server_uri = secrets.LDAP_SERVER_URI.split(',')[0] + ldap_manager = LdapManager( + server=ldap_server_uri, + admin_dn=secrets.LDAP_ADMIN_DN, + admin_password=secrets.LDAP_ADMIN_PASSWORD, + ) + users = ldap_manager.get('') # Get all users + + with RPCClient(secrets.OPENNEBULA_URL) as rpc_client: success, response, *_ = rpc_client.one.vmpool.infoextended( - uncloud.secrets.OPENNEBULA_USER_PASS, -2, -1, -1, -1 + secrets.OPENNEBULA_USER_PASS, -2, -1, -1, -1 ) if success: vms = json.loads(json.dumps(parse(response)))['VM_POOL']['VM'] unknown_user_with_email = set() - for i, vm in enumerate(vms): + + for vm in vms: vm_id = vm['ID'] vm_owner_email = vm['UNAME'] - try: - user = get_user_model().objects.get(email=vm_owner_email) - except get_user_model().DoesNotExist: + user = find_user_based_on_email(users, vm_owner_email) + if not user: unknown_user_with_email.add(vm_owner_email) - continue - # user = get_user_model().objects.create_user(username=vm_owner) + else: + try: + user_in_db = get_user_model().objects.get(email=vm_owner_email) + except get_user_model().DoesNotExist: + user_in_db = get_user_model().objects.create_user(username=user.uid, email=vm_owner_email) - VMModel.objects.update_or_create( - defaults= { 'data': vm, - 'owner': user }, - vmid=vm_id - ) - print('User with email but no username:', unknown_user_with_email) + VMModel.objects.update_or_create( + defaults={'data': vm, 'owner': user_in_db}, vmid=vm_id + ) + print('User with email but not found in ldap:', unknown_user_with_email) else: print(response) - print(uncloud.secrets.OPENNEBULA_USER_PASS) + print(secrets.OPENNEBULA_USER_PASS) diff --git a/uncloud/opennebula/views.py b/uncloud/opennebula/views.py index 5498928..66269c7 100644 --- a/uncloud/opennebula/views.py +++ b/uncloud/opennebula/views.py @@ -1,5 +1,6 @@ from rest_framework import viewsets, permissions from rest_framework.response import Response +from django.shortcuts import get_object_or_404 from .models import VM from .serializers import VMSerializer, OpenNebulaVMSerializer @@ -11,17 +12,16 @@ class RawVMViewSet(viewsets.ModelViewSet): permission_classes = [permissions.IsAdminUser] -class VMViewSet(viewsets.ModelViewSet): +class VMViewSet(viewsets.ViewSet): permission_classes = [permissions.IsAuthenticated] - serializer_class = OpenNebulaVMSerializer - - def get_queryset(self): - return VM.objects.filter(owner=self.request.user) def list(self, request): - serializer = OpenNebulaVMSerializer(self.queryset, many=True, context={'request': request}) + queryset = VM.objects.filter(owner=request.user) + serializer = OpenNebulaVMSerializer(queryset, many=True, context={'request': request}) return Response(serializer.data) def retrieve(self, request, pk=None): - serializer = OpenNebulaVMSerializer(self.queryset) + queryset = VM.objects.filter(owner=request.user) + user = get_object_or_404(queryset, pk=pk) + serializer = OpenNebulaVMSerializer(queryset) return Response(serializer.data) diff --git a/uncloud/requirements.txt b/uncloud/requirements.txt index 11ab309..e79f479 100644 --- a/uncloud/requirements.txt +++ b/uncloud/requirements.txt @@ -3,3 +3,4 @@ djangorestframework django-auth-ldap stripe xmltodict +git+https://code.ungleich.ch/ahmedbilal/ungleich-common/#egg=ungleich-common-ldap&subdirectory=ldap