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

API to return 409 in case of existing group/namespace #572

Merged
merged 1 commit into from Nov 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES/152.bugfix
@@ -0,0 +1 @@
API returns 409 in case of existing group with same name.
11 changes: 11 additions & 0 deletions galaxy_ng/app/api/ui/viewsets/group.py
@@ -1,10 +1,12 @@
from django.db import transaction
from django_filters import filters
from django_filters.rest_framework import filterset, DjangoFilterBackend

from pulpcore.app import viewsets
from galaxy_ng.app.access_control import access_policy

from galaxy_ng.app.api.base import LocalSettingsMixin
from galaxy_ng.app.exceptions import ConflictError
from django.contrib.auth.models import Group


Expand All @@ -27,6 +29,15 @@ class GroupViewSet(LocalSettingsMixin, viewsets.GroupViewSet):
filterset_class = GroupFilter
permission_classes = [access_policy.GroupAccessPolicy]

@transaction.atomic
def create(self, request, *args, **kwargs):
name = request.data['name']
if Group.objects.filter(name=name).exists():
raise ConflictError(
detail={'name': f'A group named {name} already exists.'}
)
return super().create(request, *args, **kwargs)


class GroupModelPermissionViewSet(LocalSettingsMixin, viewsets.GroupModelPermissionViewSet):
permission_classes = [access_policy.GroupAccessPolicy]
Expand Down
25 changes: 14 additions & 11 deletions galaxy_ng/app/api/v3/viewsets/namespace.py
@@ -1,7 +1,6 @@
import logging

from django.db import transaction
from django.db.utils import IntegrityError
from django_filters import filters
from django_filters.rest_framework import filterset, DjangoFilterBackend
from pulp_ansible.app.models import AnsibleRepository, AnsibleDistribution
Expand Down Expand Up @@ -60,16 +59,20 @@ def get_serializer_class(self):
@transaction.atomic
def create(self, request, *args, **kwargs):
"""Override to also create inbound pulp repository and distribution."""

try:
name = INBOUND_REPO_NAME_FORMAT.format(namespace_name=request.data['name'])
repo = AnsibleRepository.objects.create(name=name)
AnsibleDistribution.objects.create(name=name, base_path=name, repository=repo)
return super().create(request, *args, **kwargs)
except IntegrityError as e:
raise ConflictError(detail={
'name': 'There is a conflict error: {}'.format(str(e))
})
name = request.data['name']
if models.Namespace.objects.filter(name=name).exists():
raise ConflictError(
detail={'name': f'A namespace named {name} already exists.'}
)

inbound_name = INBOUND_REPO_NAME_FORMAT.format(namespace_name=name)
repo = AnsibleRepository.objects.create(name=inbound_name)
AnsibleDistribution.objects.create(
name=inbound_name,
base_path=inbound_name,
repository=repo
)
return super().create(request, *args, **kwargs)

@transaction.atomic
def destroy(self, request, *args, **kwargs):
Expand Down
20 changes: 20 additions & 0 deletions galaxy_ng/tests/unit/api/test_api_v3_namespace_viewsets.py
Expand Up @@ -189,3 +189,23 @@ def test_delete_namespace_no_error_if_no_repo_exist(self):
with self.settings(GALAXY_DEPLOYMENT_MODE=self.deployment_mode):
response = self.client.delete(ns_detail_url)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

def test_conflict_error_if_already_exists(self):
"""Ensure API returns 409-Conflict in case of existing namespace."""
ns3_name = "unittestnamespace3"
self._create_namespace(ns3_name, groups=[self.pe_group])
ns_list_url = reverse('galaxy:api:v3:namespaces-list')

self.client.force_authenticate(user=self.admin_user)

with self.settings(GALAXY_DEPLOYMENT_MODE=self.deployment_mode):
post_data = {
"name": ns3_name,
"company": "A company name",
"email": "email@companyname.com",
"description": "A testing namespace",
"groups": [{"name": "system:partner-engineers"}],
"object_permissions": ["change_namespace", "upload_to_namespace"]
}
response = self.client.post(ns_list_url, post_data, format='json')
self.assertEqual(response.status_code, status.HTTP_409_CONFLICT)