Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

firestore, datastore: make DetectProjectID work with the emulator #2598

Merged
merged 2 commits into from Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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