Skip to content

Commit

Permalink
firestore, datastore: implement DetectProjectID for emulator
Browse files Browse the repository at this point in the history
We can use any project ID when the emulator is being used.

Try to look detect a real-looking one, and fallback to a dummy ID.

Fixes #1751.
  • Loading branch information
broady committed Jul 14, 2020
1 parent fb37918 commit 04bac27
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
28 changes: 20 additions & 8 deletions datastore/datastore.go
Expand Up @@ -75,6 +75,12 @@ func NewClient(ctx context.Context, projectID string, opts ...option.ClientOptio
option.WithoutAuthentication(),
option.WithGRPCDialOption(grpc.WithInsecure()),
}
if projectID == DetectProjectID {
projectID, _ = detectProjectID(ctx, opts...)
if projectID == "" {
projectID = "dummy-emulator-datastore-project"
}
}
} else {
o = []option.ClientOption{
option.WithEndpoint(prodAddr),
Expand All @@ -96,16 +102,11 @@ func NewClient(ctx context.Context, projectID string, opts ...option.ClientOptio
o = append(o, opts...)

if projectID == DetectProjectID {
creds, err := transport.Creds(ctx, o...)
detected, err := detectProjectID(ctx, opts...)
if err != nil {
return nil, fmt.Errorf("fetching creds: %v", err)
return nil, err
}

if creds.ProjectID == "" {
return nil, errors.New("datastore: see the docs on DetectProjectID")
}

projectID = creds.ProjectID
projectID = detected
}

if projectID == "" {
Expand All @@ -122,6 +123,17 @@ func NewClient(ctx context.Context, projectID string, opts ...option.ClientOptio
}, nil
}

func detectProjectID(ctx context.Context, opts ...option.ClientOption) (string, error) {
creds, err := transport.Creds(ctx, opts...)
if err != nil {
return "", fmt.Errorf("fetching creds: %v", err)
}
if creds.ProjectID == "" {
return "", errors.New("datastore: see the docs on DetectProjectID")
}
return creds.ProjectID, nil
}

var (
// ErrInvalidEntityType is returned when functions like Get or Next are
// passed a dst or src argument of invalid type.
Expand Down
25 changes: 19 additions & 6 deletions firestore/client.go
Expand Up @@ -68,18 +68,21 @@ func NewClient(ctx context.Context, projectID string, opts ...option.ClientOptio
return nil, fmt.Errorf("firestore: dialing address from env var FIRESTORE_EMULATOR_HOST: %s", err)
}
o = []option.ClientOption{option.WithGRPCConn(conn)}
if projectID == DetectProjectID {
projectID, _ = detectProjectID(ctx, opts...)
if projectID == "" {
projectID = "dummy-emulator-firestore-project"
}
}
}
o = append(o, opts...)

if projectID == DetectProjectID {
creds, err := transport.Creds(ctx, o...)
detected, err := detectProjectID(ctx, o...)
if err != nil {
return nil, fmt.Errorf("fetching creds: %v", err)
}
if creds.ProjectID == "" {
return nil, errors.New("firestore: see the docs on DetectProjectID")
return nil, err
}
projectID = creds.ProjectID
projectID = detected
}

vc, err := vkit.NewClient(ctx, o...)
Expand All @@ -93,7 +96,17 @@ func NewClient(ctx context.Context, projectID string, opts ...option.ClientOptio
databaseID: "(default)", // always "(default)", for now
}
return c, nil
}

func detectProjectID(ctx context.Context, opts ...option.ClientOption) (string, error) {
creds, err := transport.Creds(ctx, opts...)
if err != nil {
return "", fmt.Errorf("fetching creds: %v", err)
}
if creds.ProjectID == "" {
return "", errors.New("firestore: see the docs on DetectProjectID")
}
return creds.ProjectID, nil
}

// Close closes any resources held by the client.
Expand Down

0 comments on commit 04bac27

Please sign in to comment.