Skip to content

Commit

Permalink
chore: add namespace to connection string
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Nov 17, 2023
1 parent bbc1666 commit d60d60f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 22 deletions.
44 changes: 28 additions & 16 deletions context/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,38 @@ var (
)

// extractConnectionNameType extracts the name and connection type from a connection
// string formatted as "connection://<type>/<name>".
func extractConnectionNameType(connectionString string) (name string, connectionType string, found bool) {
// string formatted as "connection://<type>/<namespace>/<name>".
func extractConnectionNameType(connectionString string) (name, namespace, connectionType string, found bool) {
prefix := "connection://"

if !strings.HasPrefix(connectionString, prefix) {
return
}

connectionString = strings.TrimPrefix(connectionString, prefix)
parts := strings.SplitN(connectionString, "/", 2)
if len(parts) != 2 {
parts := strings.Split(connectionString, "/")
if len(parts) > 3 || len(parts) < 1 {
return
}

if parts[0] == "" || parts[1] == "" {
return
}

return parts[1], parts[0], true
if len(parts) == 3 {
name, namespace, connectionType = parts[2], parts[1], parts[0]
return name, namespace, connectionType, true
} else if len(parts) == 2 {
name, connectionType = parts[1], parts[0]
return name, "", connectionType, true
}

return
}

// HydrateConnectionByURL retrieves a connection from the given connection string.
// The connection string is expected to be in one of the following forms:
// - connection://<type>/<name> or
// - connection://<type>/<name> or connection://<type>/<namespace>/<name>
// - the UUID of the connection.
func HydrateConnectionByURL(ctx Context, connectionString string) (*models.Connection, error) {
if connectionString == "" {
Expand Down Expand Up @@ -72,7 +80,7 @@ func IsValidConnectionURL(connectionString string) bool {
if _, err := uuid.Parse(connectionString); err == nil {
return true
}
_, _, found := extractConnectionNameType(connectionString)
_, _, _, found := extractConnectionNameType(connectionString)
return found
}

Expand All @@ -87,24 +95,28 @@ func FindConnectionByURL(ctx Context, connectionString string) (*models.Connecti
return &connection, nil
}

name, connectionType, found := extractConnectionNameType(connectionString)
name, namespace, connectionType, found := extractConnectionNameType(connectionString)
if !found {
return nil, nil
}

connection, err := FindConnection(ctx, connectionType, name)
connection, err := FindConnection(ctx, connectionType, name, namespace)
if err != nil {
return nil, fmt.Errorf("failed to find connection (type=%s, name=%s): %w", connectionType, name, err)
return nil, fmt.Errorf("failed to find connection (type=%s, name=%s, namespace=%s): %w", connectionType, name, namespace, err)
}

return connection, nil
}

// FindConnection returns the connection with the given type and name
func FindConnection(ctx Context, connectionType, name string) (*models.Connection, error) {
func FindConnection(ctx Context, connectionType, name, namespace string) (*models.Connection, error) {
var connection models.Connection

err := ctx.DB().Where("type = ? AND name = ?", connectionType, name).First(&connection).Error
if namespace == "" {
namespace = ctx.GetNamespace()
}

err := ctx.DB().Where("type = ? AND name = ? AND namespace = ?", connectionType, name, namespace).First(&connection).Error
if err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
Expand All @@ -116,12 +128,12 @@ func FindConnection(ctx Context, connectionType, name string) (*models.Connectio
return &connection, nil
}

func (ctx Context) GetConnection(connectionType string, name string) (*models.Connection, error) {
return GetConnection(ctx, connectionType, name)
func (ctx Context) GetConnection(connectionType, name, namespace string) (*models.Connection, error) {
return GetConnection(ctx, connectionType, name, namespace)
}

func GetConnection(ctx Context, connectionType string, name string) (*models.Connection, error) {
connection, err := FindConnection(ctx, connectionType, name)
func GetConnection(ctx Context, connectionType, name, namespace string) (*models.Connection, error) {
connection, err := FindConnection(ctx, connectionType, name, namespace)
if err != nil {
return nil, err
}
Expand Down
22 changes: 19 additions & 3 deletions context/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,37 @@ func TestGetConnectionNameType(t *testing.T) {
connection string
Expect struct {
name string
namespace string
connectionType string
found bool
}
}{
{
name: "valid connection string",
connection: "connection://db/mission_control",
connection: "connection://db/default/mission_control",
Expect: struct {
name string
namespace string
connectionType string
found bool
}{
name: "mission_control",
namespace: "default",
connectionType: "db",
found: true,
},
},
{
name: "valid connection string | name has /",
connection: "connection://db/mission_control//",
connection: "connection://db/default/mission_control//",
Expect: struct {
name string
namespace string
connectionType string
found bool
}{
name: "mission_control//",
namespace: "default",
connectionType: "db",
found: true,
},
Expand All @@ -43,10 +48,12 @@ func TestGetConnectionNameType(t *testing.T) {
connection: "connection:///type-only",
Expect: struct {
name string
namespace string
connectionType string
found bool
}{
name: "",
namespace: "",
connectionType: "",
found: false,
},
Expand All @@ -56,10 +63,12 @@ func TestGetConnectionNameType(t *testing.T) {
connection: "invalid-connection-string",
Expect: struct {
name string
namespace string
connectionType string
found bool
}{
name: "",
namespace: "",
connectionType: "",
found: false,
},
Expand All @@ -69,10 +78,12 @@ func TestGetConnectionNameType(t *testing.T) {
connection: "",
Expect: struct {
name string
namespace string
connectionType string
found bool
}{
name: "",
namespace: "",
connectionType: "",
found: false,
},
Expand All @@ -82,10 +93,12 @@ func TestGetConnectionNameType(t *testing.T) {
connection: "connection://type-only",
Expect: struct {
name string
namespace string
connectionType string
found bool
}{
name: "",
namespace: "",
connectionType: "",
found: false,
},
Expand All @@ -94,10 +107,13 @@ func TestGetConnectionNameType(t *testing.T) {

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
name, connectionType, found := extractConnectionNameType(tc.connection)
name, namespace, connectionType, found := extractConnectionNameType(tc.connection)
if name != tc.Expect.name {
t.Errorf("g.Expected name %q, but got %q", tc.Expect.name, name)
}
if namespace != tc.Expect.namespace {
t.Errorf("g.Expected namespace %q, but got %q", tc.Expect.namespace, namespace)
}
if connectionType != tc.Expect.connectionType {
t.Errorf("g.Expected connection type %q, but got %q", tc.Expect.connectionType, connectionType)
}
Expand Down
5 changes: 3 additions & 2 deletions schema/connections.hcl
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ table "connections" {
type = text
}
column "namespace" {
null = true
null = false
type = text
default = "default"
}
column "type" {
null = false
Expand Down Expand Up @@ -68,7 +69,7 @@ table "connections" {
primary_key {
columns = [column.id]
}
index "connections_name_type_key" {
index "connections_type_name_namespace_key" {
unique = true
columns = [column.type, column.name, column.namespace]
}
Expand Down
2 changes: 1 addition & 1 deletion tests/connection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var _ = Describe("Connection", Ordered, func() {
var connection *models.Connection
var err error
It("should be retrieved successfully", func() {
connection, err = testutils.DefaultContext.GetConnection("test", "test")
connection, err = testutils.DefaultContext.GetConnection("test", "test", "default")
Expect(err).ToNot(HaveOccurred())
})

Expand Down

0 comments on commit d60d60f

Please sign in to comment.