Service instantiation #31

Merged
rixx merged 37 commits from 24-service-instantiation into main 2025-04-04 10:57:29 +00:00
2 changed files with 17 additions and 9 deletions
Showing only changes of commit fb5a6e9a42 - Show all commits

View file

@ -3,7 +3,7 @@ import urlman
from django.conf import settings from django.conf import settings
from django.core.cache import cache from django.core.cache import cache
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.db import models from django.db import IntegrityError, models
from django.utils.functional import cached_property from django.utils.functional import cached_property
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from encrypted_fields.fields import EncryptedJSONField from encrypted_fields.fields import EncryptedJSONField
@ -492,6 +492,19 @@ class ServiceInstance(ServalaModelMixin, models.Model):
def create_instance(cls, name, organization, context, created_by, spec_data): def create_instance(cls, name, organization, context, created_by, spec_data):
# Ensure the namespace exists # Ensure the namespace exists
context.control_plane.get_or_create_namespace(organization.namespace) context.control_plane.get_or_create_namespace(organization.namespace)
try:
instance = cls.objects.create(
name=name,
organization=organization,
created_by=created_by,
context=context,
)
except IntegrityError:
raise ValidationError(
_(
"An instance with this name already exists in this organization. Please choose a different name."
)
)
group = context.service_definition.api_definition["group"] group = context.service_definition.api_definition["group"]
version = context.service_definition.api_definition["version"] version = context.service_definition.api_definition["version"]
@ -521,10 +534,4 @@ class ServiceInstance(ServalaModelMixin, models.Model):
plural=plural, plural=plural,
body=create_data, body=create_data,
) )
return instance
return cls.objects.create(
name=name,
organization=organization,
created_by=created_by,
context=context,
)

View file

@ -135,7 +135,8 @@ class ServiceOfferingDetailView(OrganizationViewMixin, HtmxViewMixin, DetailView
) )
return redirect(service_instance.urls.base) return redirect(service_instance.urls.base)
except Exception as e: except Exception as e:
messages.error(self.request, str(e)) error_message = getattr(e, "message", None) or str(e)
messages.error(self.request, error_message)
# If the form is not valid or if the service creation failed, we render it again # If the form is not valid or if the service creation failed, we render it again
context["service_form"] = form context["service_form"] = form