Skip to content

Commit

Permalink
Add missing parts to create an instance
Browse files Browse the repository at this point in the history
  • Loading branch information
lestrrat committed Nov 24, 2022
1 parent ee2d29e commit 810a79f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
30 changes: 23 additions & 7 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ const (
// The zero value should not be used. Always use the value
// returned from driver.New
type Driver struct {
mu *sync.RWMutex
cond *sync.Cond
dsn string
config *Config
ready bool
setupError error
onClose []func() error
mu *sync.RWMutex
cond *sync.Cond
dsn string
config *Config
ready bool
setupError error
onClose []func() error
instanceConfig string
}

func New(dsn string) (*Driver, error) {
Expand All @@ -59,6 +60,7 @@ func New(dsn string) (*Driver, error) {
func (d *Driver) Run(ctx context.Context, options ...Option) <-chan error {
dropDatabase := true
useEmulator := true
instanceConfig := ""
for _, option := range options {
switch option.Ident() {
case identDropDatabase{}:
Expand All @@ -67,9 +69,13 @@ func (d *Driver) Run(ctx context.Context, options ...Option) <-chan error {
ctx = context.WithValue(ctx, identDDLDirectory{}, option.Value().(string))
case identUseEmulator{}:
useEmulator = option.Value().(bool)
case identInstanceConfig{}:
instanceConfig = option.Value().(string)
}
}

d.instanceConfig = instanceConfig

defer d.cond.Broadcast()

// channel to notify readiness to the user
Expand Down Expand Up @@ -214,9 +220,19 @@ func (d *Driver) createSpannerInstance(ctx context.Context) error {
return fmt.Errorf(`unexpected error while retrieving instance: %w`, err)
}

if d.instanceConfig == "" {
return fmt.Errorf(`value for InstanceConfig must be specified via driver.WithInstanceCnfig() to create an instance`)
}

if _, err := instanceAdminClient.CreateInstance(ctx, &instancepb.CreateInstanceRequest{
Parent: projectMarker + d.config.Project,
InstanceId: d.config.Instance,
Instance: &instancepb.Instance{
Name: projectMarker + d.config.Project + instanceMarker + d.config.Instance,
Config: d.instanceConfig,
DisplayName: d.config.Instance,
NodeCount: 1,
},
}); err != nil {
return fmt.Errorf(`failed to create instance %q: %w`, name, err)
}
Expand Down
8 changes: 7 additions & 1 deletion driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package driver_test

import (
"context"
"os"
"testing"
"time"

Expand All @@ -22,7 +23,12 @@ func TestDriver(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

exited := d.Run(ctx)
var options []driver.Option
if v := os.Getenv(`SPANNER_EMULATOR_DRIVER_TEST_INSTANCE_CONFIG`); v != "" {
options = append(options, driver.WithInstanceConfig(v))
}

exited := d.Run(ctx, options...)

require.NoError(t, d.Ready(ctx), `driver should start successfully`)

Expand Down
5 changes: 5 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type Option = option.Interface
type identDropDatabase struct{}
type identDDLDirectory struct{}
type identUseEmulator struct{}
type identInstanceConfig struct{}

func WithDropDatabase(v bool) Option {
return option.New(identDropDatabase{}, v)
Expand All @@ -19,3 +20,7 @@ func WithDDLDirectory(dir string) Option {
func WithUseEmulator(v bool) Option {
return option.New(identUseEmulator{}, v)
}

func WithInstanceConfig(s string) Option {
return option.New(identInstanceConfig{}, s)
}

0 comments on commit 810a79f

Please sign in to comment.