From b2d93df44d8a994d5f3d12234625015587942a6a Mon Sep 17 00:00:00 2001 From: Haoming Meng Date: Fri, 8 Mar 2024 17:20:42 +0000 Subject: [PATCH 1/2] Add handler to delete a namespace by id --- registry/registry.go | 2 +- registry/registry_db.go | 6 +++++- registry/registry_ui.go | 28 ++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/registry/registry.go b/registry/registry.go index a3a8f785a..9baf01e0d 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -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) diff --git a/registry/registry_db.go b/registry/registry_db.go index 5a2c6bacc..de5ed5f6a 100644 --- a/registry/registry_db.go +++ b/registry/registry_db.go @@ -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 } diff --git a/registry/registry_ui.go b/registry/registry_ui.go index 221db2464..f1f87d319 100644 --- a/registry/registry_ui.go +++ b/registry/registry_ui.go @@ -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{} @@ -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) From 53112f3cfe7419f5232a60483e2486e222bd6d71 Mon Sep 17 00:00:00 2001 From: Haoming Meng Date: Fri, 8 Mar 2024 18:55:54 +0000 Subject: [PATCH 2/2] Add swagger doc --- .../app/api/docs/pelican-swagger.yaml | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/web_ui/frontend/app/api/docs/pelican-swagger.yaml b/web_ui/frontend/app/api/docs/pelican-swagger.yaml index 60b6ce8b3..870a95673 100644 --- a/web_ui/frontend/app/api/docs/pelican-swagger.yaml +++ b/web_ui/frontend/app/api/docs/pelican-swagger.yaml @@ -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: