Skip to content

Commit

Permalink
feat(datastore): Adding dynamic routing header (googleapis#8364)
Browse files Browse the repository at this point in the history
  • Loading branch information
bhshkh authored Sep 26, 2023
1 parent 8729aa0 commit d235a42
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
11 changes: 8 additions & 3 deletions datastore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package datastore
import (
"context"
"fmt"
"net/url"
"time"

"cloud.google.com/go/datastore/internal"
Expand All @@ -43,14 +44,18 @@ type datastoreClient struct {
}

func newDatastoreClient(conn grpc.ClientConnInterface, projectID, databaseID string) pb.DatastoreClient {
resourcePrefixValue := "projects/" + projectID
if databaseID != "" {
resourcePrefixValue += "/databases/" + databaseID
resourcePrefixValue := "projects/" + url.QueryEscape(projectID)
reqParamsHeaderValue := "project_id=" + url.QueryEscape(projectID)

if databaseID != DefaultDatabaseID && databaseID != "" {
resourcePrefixValue += "/databases/" + url.QueryEscape(databaseID)
reqParamsHeaderValue += "&database_id=" + url.QueryEscape(databaseID)
}
return &datastoreClient{
c: pb.NewDatastoreClient(conn),
md: metadata.Pairs(
resourcePrefixHeader, resourcePrefixValue,
reqParamsHeader, reqParamsHeaderValue,
"x-goog-api-client", fmt.Sprintf("gl-go/%s gccl/%s grpc/", version.Go(), internal.Version)),
}
}
Expand Down
3 changes: 3 additions & 0 deletions datastore/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ const DetectProjectID = "*detect-project-id*"
// the resource being operated on.
const resourcePrefixHeader = "google-cloud-resource-prefix"

// reqParamsHeader is routing header required to access named databases
const reqParamsHeader = "x-goog-request-params"

// DefaultDatabaseID is ID of the default database denoted by an empty string
const DefaultDatabaseID = ""

Expand Down
25 changes: 25 additions & 0 deletions datastore/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"flag"
"fmt"
"log"
"net/url"
"os"
"reflect"
"sort"
Expand Down Expand Up @@ -68,6 +69,10 @@ var (
return newClient(ctx, t, nil)
}
testParams map[string]interface{}

// xGoogReqParamsHeaderChecker is a HeaderChecker that ensures that the "x-goog-request-params"
// header is present on outgoing metadata.
xGoogReqParamsHeaderChecker *testutil.HeaderChecker
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -121,6 +126,24 @@ func testMain(m *testing.M) int {
for _, databaseID := range databaseIDs {
log.Printf("Setting up tests to run on databaseID: %q\n", databaseID)
testParams["databaseID"] = databaseID
xGoogReqParamsHeaderChecker = &testutil.HeaderChecker{
Key: reqParamsHeader,
ValuesValidator: func(values ...string) error {
if len(values) == 0 {
return fmt.Errorf("missing values")
}
wantValue := fmt.Sprintf("project_id=%s", url.QueryEscape(testutil.ProjID()))
if databaseID != DefaultDatabaseID && databaseID != "" {
wantValue = fmt.Sprintf("%s&database_id=%s", wantValue, url.QueryEscape(databaseID))
}
for _, gotValue := range values {
if gotValue != wantValue {
return fmt.Errorf("got %s, want %s", gotValue, wantValue)
}
}
return nil
},
}
status := m.Run()
if status != 0 {
return status
Expand Down Expand Up @@ -153,6 +176,7 @@ func initReplay() {
OnFailure: t.Fatalf,
Checkers: []*testutil.HeaderChecker{
testutil.XGoogClientHeaderChecker,
xGoogReqParamsHeaderChecker,
},
}

Expand All @@ -179,6 +203,7 @@ func newClient(ctx context.Context, t *testing.T, dialOpts []grpc.DialOption) *C
OnFailure: t.Fatalf,
Checkers: []*testutil.HeaderChecker{
testutil.XGoogClientHeaderChecker,
xGoogReqParamsHeaderChecker,
},
}
opts := append(grpcHeadersEnforcer.CallOptions(), option.WithTokenSource(ts))
Expand Down

0 comments on commit d235a42

Please sign in to comment.