Skip to content

Commit

Permalink
Refactor for self contained domain logic
Browse files Browse the repository at this point in the history
  • Loading branch information
talanknight committed Oct 9, 2023
1 parent e98b892 commit be119eb
Show file tree
Hide file tree
Showing 48 changed files with 719 additions and 399 deletions.
27 changes: 12 additions & 15 deletions internal/daemon/cache/store.go → internal/cache/db/db.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package cache
package db

import (
"context"
Expand All @@ -19,12 +19,10 @@ var cacheSchema string
// DefaultStoreUrl uses a temp in-memory sqlite database see: https://www.sqlite.org/inmemorydb.html
const DefaultStoreUrl = "file::memory:?_pragma=foreign_keys(1)"

type Store struct {
conn *db.DB
}

func Open(ctx context.Context, opt ...Option) (*Store, error) {
const op = "cache.Open"
// Open creates a database connection. WithUrl is supported, but by default it
// uses an in memory sqlite table. Sqlite is the only supported dbtype.
func Open(ctx context.Context, opt ...Option) (*db.DB, error) {
const op = "db.Open"
opts, err := getOpts(opt...)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
Expand All @@ -36,27 +34,26 @@ func Open(ctx context.Context, opt ...Option) (*Store, error) {
default:
url = DefaultStoreUrl
}
underlying, err := db.Open(ctx, db.Sqlite, url)
conn, err := db.Open(ctx, db.Sqlite, url)
if err != nil {
return nil, errors.Wrap(ctx, err, op)
}
s := &Store{conn: underlying}
s.conn.Debug(opts.withDebug)
conn.Debug(opts.withDebug)

switch {
case opts.withDbType == dbw.Sqlite:
if err := s.createTables(ctx); err != nil {
if err := createTables(ctx, conn); err != nil {
errors.Wrap(ctx, err, op)
}
default:
return nil, errors.New(ctx, errors.InvalidParameter, op, fmt.Sprintf("%q is not a supported cache store type", opts.withDbType))
}
return s, nil
return conn, nil
}

func (s *Store) createTables(ctx context.Context) error {
const op = "cache.(Store).createTables"
rw := db.New(s.conn)
func createTables(ctx context.Context, conn *db.DB) error {
const op = "db.createTables"
rw := db.New(conn)
if _, err := rw.Exec(ctx, cacheSchema, nil); err != nil {
return errors.Wrap(ctx, err, op)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//go:build cgo
// +build cgo

package cache
package db

import (
_ "gorm.io/driver/sqlite"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//go:build !cgo
// +build !cgo

package cache
package db

import (
_ "github.com/glebarez/go-sqlite"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package daemon
package db

import (
"context"

"github.com/hashicorp/boundary/internal/daemon/cache"
"github.com/hashicorp/go-dbw"
)

type options struct {
withDebug bool
withBoundaryTokenReaderFunc cache.BoundaryTokenReaderFn
withDebug bool
withUrl string
withDbType dbw.DbType
}

// Option - how options are passed as args
type Option func(*options) error

func getDefaultOptions() options {
return options{}
return options{
withDbType: dbw.Sqlite,
}
}

func getOpts(opt ...Option) (options, error) {
Expand All @@ -32,18 +33,18 @@ func getOpts(opt ...Option) (options, error) {
return opts, nil
}

// WithDebug provides an optional debug flag.
func WithDebug(_ context.Context, debug bool) Option {
// WithUrls provides optional url
func WithUrl(url string) Option {
return func(o *options) error {
o.withDebug = debug
o.withUrl = url
return nil
}
}

// WithBoundaryTokenReaderFunc provides an option for specifying a BoundaryTokenReaderFn
func WithBoundaryTokenReaderFunc(_ context.Context, fn cache.BoundaryTokenReaderFn) Option {
// WithDebug provides an optional debug flag.
func WithDebug(debug bool) Option {
return func(o *options) error {
o.withBoundaryTokenReaderFunc = fn
o.withDebug = debug
return nil
}
}
Original file line number Diff line number Diff line change
@@ -1,46 +1,40 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package daemon
package db

import (
"context"
"testing"

"github.com/hashicorp/boundary/api/authtokens"
"github.com/hashicorp/boundary/internal/daemon/cache"
"github.com/hashicorp/go-dbw"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_GetOpts(t *testing.T) {
t.Parallel()
ctx := context.Background()

t.Run("default", func(t *testing.T) {
opts, err := getOpts()
require.NoError(t, err)
testOpts := options{}
testOpts := options{
withDbType: dbw.Sqlite,
}
assert.Equal(t, opts, testOpts)
})
t.Run("WithDebug", func(t *testing.T) {
opts, err := getOpts(WithDebug(ctx, true))
t.Run("WithUrl", func(t *testing.T) {
url := "something"
opts, err := getOpts(WithUrl(url))
require.NoError(t, err)
testOpts := getDefaultOptions()
testOpts.withDebug = true
testOpts.withUrl = url
assert.Equal(t, opts, testOpts)
})
t.Run("WithBoundaryTokenReaderFunc", func(t *testing.T) {
var f cache.BoundaryTokenReaderFn = func(ctx context.Context, addr, token string) (*authtokens.AuthToken, error) {
return nil, nil
}
opts, err := getOpts(WithBoundaryTokenReaderFunc(ctx, f))
t.Run("WithDebug", func(t *testing.T) {
opts, err := getOpts(WithDebug(true))
require.NoError(t, err)

assert.NotNil(t, opts.withBoundaryTokenReaderFunc)
opts.withBoundaryTokenReaderFunc = nil

testOpts := getDefaultOptions()
testOpts.withDebug = true
assert.Equal(t, opts, testOpts)
})
}
File renamed without changes.
59 changes: 59 additions & 0 deletions internal/cache/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package cache

import (
"github.com/hashicorp/go-dbw"
)

type options struct {
withUpdateLastAccessedTime bool
withDbType dbw.DbType
withTargetRetrievalFunc TargetRetrievalFunc
withSessionRetrievalFunc SessionRetrievalFunc
}

// Option - how options are passed as args
type Option func(*options) error

func getDefaultOptions() options {
return options{
withDbType: dbw.Sqlite,
}
}

func getOpts(opt ...Option) (options, error) {
opts := getDefaultOptions()

for _, o := range opt {
if err := o(&opts); err != nil {
return opts, err
}
}
return opts, nil
}

// WithUpdateLastAccessedTime provides an option for updating the last access time
func WithUpdateLastAccessedTime(b bool) Option {
return func(o *options) error {
o.withUpdateLastAccessedTime = b
return nil
}
}

// WithTargetRetrievalFunc provides an option for specifying a targetRetrievalFunc
func WithTargetRetrievalFunc(fn TargetRetrievalFunc) Option {
return func(o *options) error {
o.withTargetRetrievalFunc = fn
return nil
}
}

// WithSessionRetrievalFunc provides an option for specifying a sessionRetrievalFunc
func WithSessionRetrievalFunc(fn SessionRetrievalFunc) Option {
return func(o *options) error {
o.withSessionRetrievalFunc = fn
return nil
}
}
59 changes: 59 additions & 0 deletions internal/cache/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1

package cache

import (
"context"
"testing"

"github.com/hashicorp/boundary/api/sessions"
"github.com/hashicorp/boundary/api/targets"
"github.com/hashicorp/go-dbw"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_GetOpts(t *testing.T) {
t.Parallel()

t.Run("default", func(t *testing.T) {
opts, err := getOpts()
require.NoError(t, err)
testOpts := options{
withDbType: dbw.Sqlite,
}
assert.Equal(t, opts, testOpts)
})
t.Run("WithUpdateLastAccessedTime", func(t *testing.T) {
opts, err := getOpts(WithUpdateLastAccessedTime(true))
require.NoError(t, err)
testOpts := getDefaultOptions()
testOpts.withUpdateLastAccessedTime = true
assert.Equal(t, opts, testOpts)
})
t.Run("WithTargetRetrievalFunc", func(t *testing.T) {
var f TargetRetrievalFunc = func(ctx context.Context, keyringstring, tokenName string) ([]*targets.Target, error) { return nil, nil }
opts, err := getOpts(WithTargetRetrievalFunc(f))
require.NoError(t, err)

assert.NotNil(t, opts.withTargetRetrievalFunc)
opts.withTargetRetrievalFunc = nil

testOpts := getDefaultOptions()
assert.Equal(t, opts, testOpts)
})
t.Run("WithSessionRetrievalFunc", func(t *testing.T) {
var f SessionRetrievalFunc = func(ctx context.Context, keyringstring, tokenName string) ([]*sessions.Session, error) {
return nil, nil
}
opts, err := getOpts(WithSessionRetrievalFunc(f))
require.NoError(t, err)

assert.NotNil(t, opts.withSessionRetrievalFunc)
opts.withSessionRetrievalFunc = nil

testOpts := getDefaultOptions()
assert.Equal(t, opts, testOpts)
})
}
Loading

0 comments on commit be119eb

Please sign in to comment.