diff --git a/registry/registry.go b/registry/registry.go index e95bae623..2582f9c83 100644 --- a/registry/registry.go +++ b/registry/registry.go @@ -267,7 +267,7 @@ func keySignChallengeCommit(ctx *gin.Context, data *registrationData) (bool, map log.Debug("Registering namespace ", data.Prefix) // Check if prefix exists before doing anything else - exists, err := namespaceExists(data.Prefix) + exists, err := namespaceExistsByPrefix(data.Prefix) if err != nil { log.Errorf("Failed to check if namespace already exists: %v", err) return false, nil, errors.Wrap(err, "Server encountered an error checking if namespace already exists") @@ -605,7 +605,7 @@ func deleteNamespaceHandler(ctx *gin.Context) { } // Check if prefix exists before trying to delete it - exists, err := namespaceExists(prefix) + exists, err := namespaceExistsByPrefix(prefix) if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": "server encountered an error checking if namespace already exists"}) log.Errorf("Failed to check if the namespace already exists: %v", err) @@ -737,7 +737,7 @@ func wildcardHandler(ctx *gin.Context) { } else if strings.HasSuffix(path, "/.well-known/openid-configuration") { // Check that the namespace exists before constructing config JSON prefix := strings.TrimSuffix(path, "/.well-known/openid-configuration") - exists, err := namespaceExists(prefix) + exists, err := namespaceExistsByPrefix(prefix) if err != nil { ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Server encountered an error while checking if the prefix exists"}) log.Errorf("Error while checking for existence of prefix %s: %v", prefix, err) diff --git a/registry/registry_db.go b/registry/registry_db.go index de5ed5f6a..9f1a7d425 100644 --- a/registry/registry_db.go +++ b/registry/registry_db.go @@ -168,7 +168,7 @@ func createTopologyTable() error { } // Check if a namespace exists in either Topology or Pelican registry -func namespaceExists(prefix string) (bool, error) { +func namespaceExistsByPrefix(prefix string) (bool, error) { var count int64 err := db.Model(&Namespace{}).Where("prefix = ?", prefix).Count(&count).Error @@ -244,17 +244,6 @@ func namespaceExistsById(id int) (bool, error) { } } -func namespaceExistsByPrefix(prefix string) (bool, error) { - var namespaces []Namespace - result := db.Where("prefix = ?", prefix).Limit(1).Find(&namespaces) - - if result.Error != nil { - return false, result.Error - } else { - return result.RowsAffected > 0, nil - } -} - func namespaceBelongsToUserId(id int, userId string) (bool, error) { var result Namespace err := db.First(&result, "id = ?", id).Error diff --git a/registry/registry_db_test.go b/registry/registry_db_test.go index af6a1edce..521de598b 100644 --- a/registry/registry_db_test.go +++ b/registry/registry_db_test.go @@ -159,26 +159,6 @@ var ( } ) -func TestNamespaceExistsByPrefix(t *testing.T) { - setupMockRegistryDB(t) - defer teardownMockNamespaceDB(t) - - t.Run("return-false-for-prefix-dne", func(t *testing.T) { - found, err := namespaceExistsByPrefix("/non-existed-namespace") - require.NoError(t, err) - assert.False(t, found) - }) - - t.Run("return-true-for-existing-ns", func(t *testing.T) { - resetNamespaceDB(t) - err := insertMockDBData([]Namespace{{Prefix: "/foo"}}) - require.NoError(t, err) - found, err := namespaceExistsByPrefix("/foo") - require.NoError(t, err) - assert.True(t, found) - }) -} - func TestGetNamespaceById(t *testing.T) { setupMockRegistryDB(t) defer teardownMockNamespaceDB(t) @@ -785,12 +765,12 @@ func TestRegistryTopology(t *testing.T) { require.NoError(t, err) // Check that topology namespace exists - exists, err := namespaceExists("/topo/foo") + exists, err := namespaceExistsByPrefix("/topo/foo") require.NoError(t, err) require.True(t, exists) // Check that topology namespace exists - exists, err = namespaceExists("/topo/bar") + exists, err = namespaceExistsByPrefix("/topo/bar") require.NoError(t, err) require.True(t, exists) @@ -806,12 +786,12 @@ func TestRegistryTopology(t *testing.T) { require.NoError(t, err) // Check that the regular namespace exists - exists, err = namespaceExists("/regular/foo") + exists, err = namespaceExistsByPrefix("/regular/foo") require.NoError(t, err) require.True(t, exists) // Check that a bad namespace doesn't exist - exists, err = namespaceExists("/bad/namespace") + exists, err = namespaceExistsByPrefix("/bad/namespace") require.NoError(t, err) require.False(t, exists) @@ -830,22 +810,22 @@ func TestRegistryTopology(t *testing.T) { require.NoError(t, err) // Check that /topo/foo still exists - exists, err = namespaceExists("/topo/foo") + exists, err = namespaceExistsByPrefix("/topo/foo") require.NoError(t, err) require.True(t, exists) // And that /topo/baz was added - exists, err = namespaceExists("/topo/baz") + exists, err = namespaceExistsByPrefix("/topo/baz") require.NoError(t, err) require.True(t, exists) // Check that /topo/bar is gone - exists, err = namespaceExists("/topo/bar") + exists, err = namespaceExistsByPrefix("/topo/bar") require.NoError(t, err) require.False(t, exists) // Finally, check that /regular/foo survived - exists, err = namespaceExists("/regular/foo") + exists, err = namespaceExistsByPrefix("/regular/foo") require.NoError(t, err) require.True(t, exists) diff --git a/registry/registry_ui.go b/registry/registry_ui.go index f1f87d319..8fda952bc 100644 --- a/registry/registry_ui.go +++ b/registry/registry_ui.go @@ -444,7 +444,7 @@ func createUpdateNamespace(ctx *gin.Context, isUpdate bool) { if !isUpdate { // Check if prefix exists before doing anything else. Skip check if it's update operation - exists, err := namespaceExists(ns.Prefix) + exists, err := namespaceExistsByPrefix(ns.Prefix) if err != nil { log.Errorf("Failed to check if namespace already exists: %v", err) ctx.JSON(http.StatusInternalServerError, gin.H{"error": "Server encountered an error checking if namespace already exists"})