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 support for registry admin to delete a namespace registration #915

Merged
merged 2 commits into from
Mar 20, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ func deleteNamespaceHandler(ctx *gin.Context) {
}

// If we get to this point in the code, we've passed all the security checks and we're ready to delete
err = deleteNamespace(prefix)
err = deleteNamespaceByPrefix(prefix)
if err != nil {
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "server encountered an error deleting namespace from database"})
log.Errorf("Failed to delete namespace from database: %v", err)
Expand Down
6 changes: 5 additions & 1 deletion registry/registry_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,11 @@ func updateNamespaceStatusById(id int, status RegistrationStatus, approverId str
return db.Model(ns).Where("id = ?", id).Update("admin_metadata", string(adminMetadataByte)).Error
}

func deleteNamespace(prefix string) error {
func deleteNamespaceByID(id int) error {
return db.Delete(&Namespace{}, id).Error
}

func deleteNamespaceByPrefix(prefix string) error {
// GORM by default uses transaction for write operations
return db.Where("prefix = ?", prefix).Delete(&Namespace{}).Error
}
Expand Down
28 changes: 28 additions & 0 deletions registry/registry_ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,33 @@ func getNamespaceJWKS(ctx *gin.Context) {
ctx.Data(200, "application/json", jsonData)
}

func deleteNamespace(ctx *gin.Context) {
idStr := ctx.Param("id")
id, err := strconv.Atoi(idStr)
if err != nil || id <= 0 {
// Handle the error if id is not a valid integer
ctx.JSON(http.StatusBadRequest, gin.H{"error": "Invalid ID format. ID must a non-zero integer"})
return
}
exists, err := namespaceExistsById(id)
if err != nil {
log.Error("Error checking if namespace exists: ", err)
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error checking if namespace exists"})
return
}
if !exists {
log.Errorf("Namespace not found for id: %d", id)
ctx.JSON(http.StatusNotFound, gin.H{"error": "Namespace not found"})
return
}
err = deleteNamespaceByID(id)
if err != nil {
log.Errorf("Error deleting the namespace: %v", err)
ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Error deleting the namespace"})
}
ctx.JSON(http.StatusOK, gin.H{"msg": "success"})
}

func listInstitutions(ctx *gin.Context) {
// When Registry.Institutions is set
institutions := []Institution{}
Expand Down Expand Up @@ -762,6 +789,7 @@ func RegisterRegistryWebAPI(router *gin.RouterGroup) error {
registryWebAPI.PUT("/namespaces/:id", web_ui.AuthHandler, func(ctx *gin.Context) {
createUpdateNamespace(ctx, true)
})
registryWebAPI.DELETE("/namespaces/:id", web_ui.AuthHandler, web_ui.AdminAuthHandler, deleteNamespace)
registryWebAPI.GET("/namespaces/:id/pubkey", getNamespaceJWKS)
registryWebAPI.PATCH("/namespaces/:id/approve", web_ui.AuthHandler, web_ui.AdminAuthHandler, func(ctx *gin.Context) {
updateNamespaceStatus(ctx, Approved)
Expand Down
38 changes: 38 additions & 0 deletions web_ui/frontend/app/api/docs/pelican-swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,44 @@ paths:
schema:
type: object
$ref: "#/definitions/ErrorModel"
delete:
summary: Delete a namespace registration by ID
description: "`Authentication Required` `Admin`

Only admin users have privilege to this action.
"
tags:
- "registry_ui"
produces:
- "application/json"
parameters:
- name: id
in: path
description: ID of the namespace to update
required: true
type: integer
responses:
"200":
description: OK
schema:
type: object
$ref: "#/definitions/SuccessModel"
"400":
description: Invalid namespace ID
schema:
type: object
$ref: "#/definitions/ErrorModel"
"403":
description: The user does not have previlege to update the namespace
schema:
type: object
$ref: "#/definitions/ErrorModel"
"500":
description: Internal server error
schema:
type: object
$ref: "#/definitions/ErrorModel"

/registry_ui/namespaces/{id}/pubkey:
get:
tags:
Expand Down
Loading