Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix delay in integration domain addition #139

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 37 additions & 22 deletions src/ipa-tuura/domains/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#

import logging
import threading

from django.http import Http404
from domains.adapters import DomainSerializer
Expand Down Expand Up @@ -38,20 +39,26 @@ def create(self, request, *args, **kwargs):
serializer.is_valid(raise_exception=True)
logger.info(f"domain create {serializer.validated_data}")

try:
add_domain(serializer.validated_data)
except RuntimeError as e:
return Response(str(e), status=status.HTTP_405_METHOD_NOT_ALLOWED)
except Exception as e:
raise e
else:
self.perform_create(serializer)
# Define the background process for handling the domain addition
def process_domain_creation():
try:
add_domain(serializer.validated_data)
except Exception as e:
logger.error(f"Unexpected error during domain creation: {str(e)}")
raise e
else:
self.perform_create(serializer)

# reset the writable interface
ipa = IPA()
ipa._reset_instance()
# reset the writable interface
ipa = IPA()
ipa._reset_instance()

return Response(serializer.data, status=status.HTTP_201_CREATED)
# Run the domain creation logic in a separate thread
thread = threading.Thread(target=process_domain_creation)
thread.start()

# Return the created response
return Response(serializer.validated_data, status=status.HTTP_202_ACCEPTED)

# handles GETs for 1 Domain
def retrieve(self, request, *args, **kwargs):
Expand All @@ -77,13 +84,21 @@ def list(self, request, *args, **kwargs):

# handles DELETEs for 1 Domain
def destroy(self, request, *args, **kwargs):
try:
instance = self.get_object()
serializer = self.get_serializer(instance)
logger.info(f"domain destroy {serializer.data}")
delete_domain(serializer.data)
self.perform_destroy(instance)
except Http404:
pass

return Response(status=status.HTTP_204_NO_CONTENT)

# Define the background process for handling the domain destroy
def process_domain_destroy():
try:
instance = self.get_object()
serializer = self.get_serializer(instance)
logger.info(f"domain destroy {serializer.data}")
delete_domain(serializer.data)
self.perform_destroy(instance)
except Exception as e:
logger.error(f"Unexpected error during domain destroy: {str(e)}")
raise e

# Run the domain destroy logic in a separate thread
thread = threading.Thread(target=process_domain_destroy)
thread.start()

return Response(status=status.HTTP_202_ACCEPTED)
Loading