feat(tools/firestore-get-rules)!: Add databaseId to the firestore source and firestore-get-rules tool (#1505)

## Description

---
Added `DatabaseId` to the Firestore Source and Firestore-Get-Rules tool
for fetching the granular per-database rules instead of rules for the
default database.

## PR Checklist

---
> Thank you for opening a Pull Request! Before submitting your PR, there
are a
> few things you can do to make sure it goes smoothly:

- [x] Make sure you reviewed

[CONTRIBUTING.md](https://github.com/googleapis/genai-toolbox/blob/main/CONTRIBUTING.md)
- [x] Make sure to open an issue as a

[bug/issue](https://github.com/googleapis/genai-toolbox/issues/new/choose)
before writing your code! That way we can discuss the change, evaluate
  designs, and agree on the general idea
- [x] Ensure the tests and linter pass
- [x] Code coverage does not decrease (if any source code was changed)
- [x] Appropriate docs were updated (if necessary)
- [x] Make sure to add `!` if this involve a breaking change

---------

Co-authored-by: Averi Kitsch <akitsch@google.com>
This commit is contained in:
trehanshakuntG
2025-09-19 04:22:30 +05:30
committed by GitHub
parent 9bb04c42df
commit 7450482bb2
2 changed files with 11 additions and 2 deletions

View File

@@ -78,6 +78,7 @@ func (r Config) Initialize(ctx context.Context, tracer trace.Tracer) (sources.So
Client: client,
RulesClient: rulesClient,
ProjectId: r.Project,
DatabaseId: r.Database,
}
return s, nil
}
@@ -91,6 +92,7 @@ type Source struct {
Client *firestore.Client
RulesClient *firebaserules.Service
ProjectId string `yaml:"projectId"`
DatabaseId string `yaml:"databaseId"`
}
func (s *Source) SourceKind() string {
@@ -110,6 +112,10 @@ func (s *Source) GetProjectId() string {
return s.ProjectId
}
func (s *Source) GetDatabaseId() string {
return s.DatabaseId
}
func initFirestoreConnection(
ctx context.Context,
tracer trace.Tracer,

View File

@@ -44,6 +44,7 @@ func newConfig(ctx context.Context, name string, decoder *yaml.Decoder) (tools.T
type compatibleSource interface {
FirebaseRulesClient() *firebaserules.Service
GetProjectId() string
GetDatabaseId() string
}
// validate compatible sources are still compatible
@@ -96,6 +97,7 @@ func (cfg Config) Initialize(srcs map[string]sources.Source) (tools.Tool, error)
AuthRequired: cfg.AuthRequired,
RulesClient: s.FirebaseRulesClient(),
ProjectId: s.GetProjectId(),
DatabaseId: s.GetDatabaseId(),
manifest: tools.Manifest{Description: cfg.Description, Parameters: parameters.Manifest(), AuthRequired: cfg.AuthRequired},
mcpManifest: mcpManifest,
}
@@ -113,20 +115,21 @@ type Tool struct {
RulesClient *firebaserules.Service
ProjectId string
DatabaseId string
manifest tools.Manifest
mcpManifest tools.McpManifest
}
func (t Tool) Invoke(ctx context.Context, params tools.ParamValues, accessToken tools.AccessToken) (any, error) {
// Get the latest release for Firestore
releaseName := fmt.Sprintf("projects/%s/releases/cloud.firestore", t.ProjectId)
releaseName := fmt.Sprintf("projects/%s/releases/cloud.firestore/%s", t.ProjectId, t.DatabaseId)
release, err := t.RulesClient.Projects.Releases.Get(releaseName).Context(ctx).Do()
if err != nil {
return nil, fmt.Errorf("failed to get latest Firestore release: %w", err)
}
if release.RulesetName == "" {
return nil, fmt.Errorf("no active Firestore rules were found in project '%s'", t.ProjectId)
return nil, fmt.Errorf("no active Firestore rules were found in project '%s' and database '%s'", t.ProjectId, t.DatabaseId)
}
// Get the ruleset content