Fixed issues in opennebula/views.py + syncvm now behaves correctly and print users which are not in ldap as per their email address

This commit is contained in:
ahmadbilalkhalid 2020-02-25 11:50:49 +05:00
parent 739bd72526
commit a72bc142a6
3 changed files with 43 additions and 26 deletions

View file

@ -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
defaults={'data': vm, 'owner': user_in_db}, vmid=vm_id
)
print('User with email but no username:', unknown_user_with_email)
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)

View file

@ -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)

View file

@ -3,3 +3,4 @@ djangorestframework
django-auth-ldap
stripe
xmltodict
git+https://code.ungleich.ch/ahmedbilal/ungleich-common/#egg=ungleich-common-ldap&subdirectory=ldap