Service instantiation #31

Merged
rixx merged 37 commits from 24-service-instantiation into main 2025-04-04 10:57:29 +00:00
3 changed files with 21 additions and 12 deletions
Showing only changes of commit 555462a99e - Show all commits

View file

@ -1,7 +1,6 @@
import rules import rules
import urlman import urlman
from django.conf import settings from django.conf import settings
from django.core.validators import RegexValidator
from django.db import models from django.db import models
from django.utils.functional import cached_property from django.utils.functional import cached_property
from django.utils.text import slugify from django.utils.text import slugify
@ -10,6 +9,7 @@ from django_scopes import ScopedManager, scopes_disabled
from servala.core import rules as perms from servala.core import rules as perms
from servala.core.models.mixins import ServalaModelMixin from servala.core.models.mixins import ServalaModelMixin
from servala.core.validators import kubernetes_name_validator
class Organization(ServalaModelMixin, models.Model): class Organization(ServalaModelMixin, models.Model):
@ -21,16 +21,7 @@ class Organization(ServalaModelMixin, models.Model):
help_text=_( help_text=_(
"This namespace will be used for all Kubernetes resources. Cannot be changed after creation." "This namespace will be used for all Kubernetes resources. Cannot be changed after creation."
), ),
validators=[ validators=[kubernetes_name_validator],
RegexValidator(
regex=r"^[a-z0-9]([-a-z0-9]*[a-z0-9])?$",
message=_(
'Namespace must consist of lowercase alphanumeric characters or "-", '
"must start and end with an alphanumeric character."
),
code="invalid_namespace",
)
],
) )
billing_entity = models.ForeignKey( billing_entity = models.ForeignKey(

View file

@ -9,6 +9,7 @@ from kubernetes import config
from kubernetes.client.rest import ApiException from kubernetes.client.rest import ApiException
from servala.core.models.mixins import ServalaModelMixin from servala.core.models.mixins import ServalaModelMixin
from servala.core.validators import kubernetes_name_validator
class ServiceCategory(ServalaModelMixin, models.Model): class ServiceCategory(ServalaModelMixin, models.Model):
@ -16,7 +17,9 @@ class ServiceCategory(ServalaModelMixin, models.Model):
Categories for services, e.g. "Databases", "Storage", "Compute". Categories for services, e.g. "Databases", "Storage", "Compute".
""" """
name = models.CharField(max_length=100, verbose_name=_("Name")) name = models.CharField(
max_length=100, verbose_name=_("Name"), validators=[kubernetes_name_validator]
)
description = models.TextField(blank=True, verbose_name=_("Description")) description = models.TextField(blank=True, verbose_name=_("Description"))
logo = models.ImageField( logo = models.ImageField(
upload_to="public/service_categories", upload_to="public/service_categories",
@ -446,3 +449,6 @@ class ServiceInstance(ServalaModelMixin, models.Model):
class Meta: class Meta:
verbose_name = _("Service instance") verbose_name = _("Service instance")
verbose_name_plural = _("Service instances") verbose_name_plural = _("Service instances")
# Names are unique per de-facto namespace, which is defined by the
# Organization + ServiceDefinition (group, version) + the ControlPlane.
unique_together = [("name", "organization", "context")]

View file

@ -0,0 +1,12 @@
from django.core.validators import RegexValidator
from django.utils.translation import gettext_lazy as _
# Kubernetes resource name validator - follows the same rules as namespace names
kubernetes_name_validator = RegexValidator(
regex=r"^[a-z0-9]([-a-z0-9]*[a-z0-9])?$",
message=_(
'Name must consist of lowercase alphanumeric characters or "-", '
"must start and end with an alphanumeric character."
),
code="invalid_kubernetes_name",
)