forked from uncloud/uncloud
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:
parent
739bd72526
commit
a72bc142a6
3 changed files with 43 additions and 26 deletions
|
@ -1,15 +1,22 @@
|
||||||
import os
|
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
import uncloud.secrets as secrets
|
||||||
|
|
||||||
|
|
||||||
|
from xmlrpc.client import ServerProxy as RPCClient
|
||||||
|
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from xmlrpc.client import ServerProxy as RPCClient
|
|
||||||
|
|
||||||
from xmltodict import parse
|
from xmltodict import parse
|
||||||
|
from ungleich_common.ldap.ldap_manager import LdapManager
|
||||||
|
|
||||||
from opennebula.models import VM as VMModel
|
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):
|
class Command(BaseCommand):
|
||||||
|
@ -19,30 +26,39 @@ class Command(BaseCommand):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def handle(self, *args, **options):
|
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(
|
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:
|
if success:
|
||||||
vms = json.loads(json.dumps(parse(response)))['VM_POOL']['VM']
|
vms = json.loads(json.dumps(parse(response)))['VM_POOL']['VM']
|
||||||
unknown_user_with_email = set()
|
unknown_user_with_email = set()
|
||||||
for i, vm in enumerate(vms):
|
|
||||||
|
for vm in vms:
|
||||||
vm_id = vm['ID']
|
vm_id = vm['ID']
|
||||||
vm_owner_email = vm['UNAME']
|
vm_owner_email = vm['UNAME']
|
||||||
|
|
||||||
try:
|
user = find_user_based_on_email(users, vm_owner_email)
|
||||||
user = get_user_model().objects.get(email=vm_owner_email)
|
if not user:
|
||||||
except get_user_model().DoesNotExist:
|
|
||||||
unknown_user_with_email.add(vm_owner_email)
|
unknown_user_with_email.add(vm_owner_email)
|
||||||
continue
|
else:
|
||||||
# user = get_user_model().objects.create_user(username=vm_owner)
|
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(
|
VMModel.objects.update_or_create(
|
||||||
defaults= { 'data': vm,
|
defaults={'data': vm, 'owner': user_in_db}, vmid=vm_id
|
||||||
'owner': user },
|
|
||||||
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:
|
else:
|
||||||
print(response)
|
print(response)
|
||||||
print(uncloud.secrets.OPENNEBULA_USER_PASS)
|
print(secrets.OPENNEBULA_USER_PASS)
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from rest_framework import viewsets, permissions
|
from rest_framework import viewsets, permissions
|
||||||
from rest_framework.response import Response
|
from rest_framework.response import Response
|
||||||
|
from django.shortcuts import get_object_or_404
|
||||||
|
|
||||||
from .models import VM
|
from .models import VM
|
||||||
from .serializers import VMSerializer, OpenNebulaVMSerializer
|
from .serializers import VMSerializer, OpenNebulaVMSerializer
|
||||||
|
@ -11,17 +12,16 @@ class RawVMViewSet(viewsets.ModelViewSet):
|
||||||
permission_classes = [permissions.IsAdminUser]
|
permission_classes = [permissions.IsAdminUser]
|
||||||
|
|
||||||
|
|
||||||
class VMViewSet(viewsets.ModelViewSet):
|
class VMViewSet(viewsets.ViewSet):
|
||||||
permission_classes = [permissions.IsAuthenticated]
|
permission_classes = [permissions.IsAuthenticated]
|
||||||
serializer_class = OpenNebulaVMSerializer
|
|
||||||
|
|
||||||
def get_queryset(self):
|
|
||||||
return VM.objects.filter(owner=self.request.user)
|
|
||||||
|
|
||||||
def list(self, request):
|
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)
|
return Response(serializer.data)
|
||||||
|
|
||||||
def retrieve(self, request, pk=None):
|
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)
|
return Response(serializer.data)
|
||||||
|
|
|
@ -3,3 +3,4 @@ djangorestframework
|
||||||
django-auth-ldap
|
django-auth-ldap
|
||||||
stripe
|
stripe
|
||||||
xmltodict
|
xmltodict
|
||||||
|
git+https://code.ungleich.ch/ahmedbilal/ungleich-common/#egg=ungleich-common-ldap&subdirectory=ldap
|
||||||
|
|
Loading…
Reference in a new issue