Skip to content

Commit

Permalink
update spanner usage for new version of spanner api libs
Browse files Browse the repository at this point in the history
  • Loading branch information
ecordell committed Mar 22, 2023
1 parent e58747e commit 48d95d4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 18 deletions.
29 changes: 13 additions & 16 deletions e2e/databases/spanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ import (
"path/filepath"
"time"

database "cloud.google.com/go/spanner/admin/database/apiv1"
instances "cloud.google.com/go/spanner/admin/instance/apiv1"
dbadmin "cloud.google.com/go/spanner/admin/database/apiv1"
database "cloud.google.com/go/spanner/admin/database/apiv1/databasepb"
instanceadmin "cloud.google.com/go/spanner/admin/instance/apiv1"
instance "cloud.google.com/go/spanner/admin/instance/apiv1/instancepb"
"github.com/go-logr/logr"
"github.com/nightlyone/lockfile"

//revive:disable:dot-imports convention is dot-import
. "github.com/onsi/gomega"
"google.golang.org/api/option"
adminpb "google.golang.org/genproto/googleapis/spanner/admin/database/v1"
"google.golang.org/genproto/googleapis/spanner/admin/instance/v1"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"k8s.io/apimachinery/pkg/api/meta"
Expand Down Expand Up @@ -52,13 +51,12 @@ func (p *SpannerProvider) New(ctx context.Context) *LogicalDatabase {

Eventually(func(g Gomega) {
logicalDBName = names.SimpleNameGenerator.GenerateName("sp")
p.execDatabase(ctx, g, func(client *database.DatabaseAdminClient) {
op, err := client.CreateDatabase(ctx, &adminpb.CreateDatabaseRequest{
p.execDatabase(ctx, g, func(client *dbadmin.DatabaseAdminClient) {
op, err := client.CreateDatabase(ctx, &database.CreateDatabaseRequest{
Parent: "projects/fake-project-id/instances/fake-instance",
CreateStatement: "CREATE DATABASE `" + logicalDBName + "`",
})
g.Expect(err).To(Succeed())

_, err = op.Wait(ctx)
g.Expect(err).To(Succeed())
})
Expand All @@ -77,18 +75,18 @@ func (p *SpannerProvider) Cleanup(ctx context.Context, db *LogicalDatabase) {
// TODO: figure out how to cleanup a spanner emulator db
}

func (p *SpannerProvider) execInstance(ctx context.Context, g Gomega, cmd func(client *instances.InstanceAdminClient)) {
func (p *SpannerProvider) execInstance(ctx context.Context, g Gomega, cmd func(client *instanceadmin.InstanceAdminClient)) {
ctx, cancel := context.WithTimeout(ctx, 500*time.Second)
defer cancel()
ports := e2eutil.PortForward(Default, p.namespace, "spanner-0", []string{":9010"}, ctx.Done())
g.Expect(len(ports)).To(Equal(1))

Expect(os.Setenv("SPANNER_EMULATOR_HOST", fmt.Sprintf("localhost:%d", ports[0].Local))).To(Succeed())

var instancesClient *instances.InstanceAdminClient
var instancesClient *instanceadmin.InstanceAdminClient
g.Eventually(func() error {
var err error
instancesClient, err = instances.NewInstanceAdminClient(ctx,
instancesClient, err = instanceadmin.NewInstanceAdminClient(ctx,
option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
option.WithoutAuthentication(),
option.WithEndpoint(fmt.Sprintf("localhost:%d", ports[0].Local)))
Expand All @@ -99,13 +97,12 @@ func (p *SpannerProvider) execInstance(ctx context.Context, g Gomega, cmd func(c
cmd(instancesClient)
}

func (p *SpannerProvider) execDatabase(ctx context.Context, g Gomega, cmd func(client *database.DatabaseAdminClient)) {
func (p *SpannerProvider) execDatabase(ctx context.Context, g Gomega, cmd func(client *dbadmin.DatabaseAdminClient)) {
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
defer cancel()
ports := e2eutil.PortForward(Default, p.namespace, "spanner-0", []string{":9010"}, ctx.Done())
g.Expect(len(ports)).To(Equal(1))

adminClient, err := database.NewDatabaseAdminClient(ctx,
adminClient, err := dbadmin.NewDatabaseAdminClient(ctx,
option.WithGRPCDialOption(grpc.WithTransportCredentials(insecure.NewCredentials())),
option.WithoutAuthentication(),
option.WithEndpoint(fmt.Sprintf("localhost:%d", ports[0].Local)))
Expand Down Expand Up @@ -141,7 +138,7 @@ func (p *SpannerProvider) ensureDatabase(ctx context.Context) {
CreateFromManifests(ctx, p.namespace, "spanner", p.restConfig, p.mapper)

Eventually(func(g Gomega) {
p.execInstance(ctx, g, func(client *instances.InstanceAdminClient) {
p.execInstance(ctx, g, func(client *instanceadmin.InstanceAdminClient) {
createInstanceOp, err := client.CreateInstance(ctx, &instance.CreateInstanceRequest{
Parent: "projects/fake-project-id",
InstanceId: "fake-instance",
Expand Down Expand Up @@ -173,7 +170,7 @@ func (p *SpannerProvider) running(ctx context.Context) error {
}

var err error
p.execInstance(ctx, Default, func(client *instances.InstanceAdminClient) {
p.execInstance(ctx, Default, func(client *instanceadmin.InstanceAdminClient) {
var i *instance.Instance
i, err = client.GetInstance(ctx, &instance.GetInstanceRequest{
Name: "projects/fake-project-id/instances/fake-instance",
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ require (
go.uber.org/zap v1.24.0
golang.org/x/exp v0.0.0-20220823124025-807a23277127
google.golang.org/api v0.106.0
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f
google.golang.org/grpc v1.51.0
k8s.io/api v0.26.3
k8s.io/apiextensions-apiserver v0.26.3
Expand Down Expand Up @@ -158,6 +157,7 @@ require (
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.5.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
2 changes: 1 addition & 1 deletion tools/generate-update-graph/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
panic(err)
}

if err := os.WriteFile(os.Args[1], yamlBytes, 0o666); err != nil {
if err := os.WriteFile(os.Args[1], yamlBytes, 0o600); err != nil {
panic(err)
}
}
Expand Down

0 comments on commit 48d95d4

Please sign in to comment.