Skip to content

Commit

Permalink
Merge pull request #2 from meliuz/remove-namespace-routes
Browse files Browse the repository at this point in the history
feat: Permite delete route
  • Loading branch information
adilson-cruz authored Oct 4, 2023
2 parents 768b170 + 4172434 commit a4c653f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
5 changes: 5 additions & 0 deletions terrareg/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,8 @@ class DuplicateNamespaceDisplayNameError(TerraregError):
"""A namespace already exists with this display name"""

pass

class NamespaceNotEmptyError(TerraregError):
"""Namespace cannot be deleted due to containing modules"""

pass
14 changes: 13 additions & 1 deletion terrareg/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import terrareg.audit_action
from terrareg.errors import (
DuplicateNamespaceDisplayNameError, InvalidModuleNameError, InvalidModuleProviderNameError, InvalidNamespaceDisplayNameError, InvalidUserGroupNameError,
InvalidVersionError, NamespaceAlreadyExistsError, NoModuleVersionAvailableError,
InvalidVersionError, NamespaceAlreadyExistsError, NamespaceNotEmptyError, NoModuleVersionAvailableError,
InvalidGitTagFormatError, InvalidNamespaceNameError, ReindexingExistingModuleVersionsIsProhibitedError, RepositoryUrlContainsInvalidPortError, RepositoryUrlContainsInvalidTemplateError,
RepositoryUrlDoesNotContainValidSchemeError,
RepositoryUrlContainsInvalidSchemeError,
Expand Down Expand Up @@ -983,6 +983,18 @@ def get_all_modules(self):
for module in modules
]

def delete(self):
"""Delete namespace"""
# Check for any modules in the namespace
if self.get_all_modules():
raise NamespaceNotEmptyError("Namespace cannot be deleted as it contains modules")

# Delete namespace
db = Database.get()
delete = sqlalchemy.delete(db.namespace).where(db.namespace.c.id==self.pk)
with db.get_connection() as conn:
conn.execute(delete)

def create_data_directory(self):
"""Create data directory and data directories of parents."""
# Check if data directory exists
Expand Down
21 changes: 21 additions & 0 deletions terrareg/server/api/terrareg_namespace_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class ApiTerraregNamespaceDetails(ErrorCatchingResource):
method_decorators = {
# Limit post methods to users with FULL namespace permissions
"post": [terrareg.auth_wrapper.auth_wrapper('check_namespace_access',
terrareg.user_group_namespace_permission_type.UserGroupNamespacePermissionType.FULL,
request_kwarg_map={'namespace': 'namespace'})],
"delete": [terrareg.auth_wrapper.auth_wrapper('check_namespace_access',
terrareg.user_group_namespace_permission_type.UserGroupNamespacePermissionType.FULL,
request_kwarg_map={'namespace': 'namespace'})]
}
Expand Down Expand Up @@ -47,3 +50,21 @@ def _post(self, namespace):
"view_href": namespace.get_view_url(),
"display_name": namespace.display_name
}

def _delete(self, namespace):
"""
Delete namespace
JSON body:
* csrf_token - CSRF token required for sessioned-based authentication
"""
namespace = terrareg.models.Namespace.get(namespace)
if namespace is None:
return self._get_404_response()

csrf_token = request.json.get('csrf_token')

terrareg.csrf.check_csrf_token(csrf_token)

namespace.delete()

return {}, 200

0 comments on commit a4c653f

Please # to comment.