feat(source/cloud-sql-admin): Add User agent and attach sqldmin in cloud-sql-admin source. (#1441)

## Description

---
1. This change introduces a userAgentRoundTripper that correctly
prepends our custom user agent to the existing User-Agent header
2. Moves sqladmin client to source.
3. Updated cloudsql tools for above support.
4. Add test cases to validate User agent.

## 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
- [ ] Ensure the tests and linter pass
- [ ] Code coverage does not decrease (if any source code was changed)
- [x] Appropriate docs were updated (if necessary)
- [ ] Make sure to add `!` if this involve a breaking change

🛠️ Fixes #<issue_number_goes_here>
This commit is contained in:
prernakakkar-google
2025-09-15 16:31:38 +00:00
committed by GitHub
parent 7d384dc28f
commit 56b6574fc2
11 changed files with 118 additions and 97 deletions

View File

@@ -22,7 +22,6 @@ import (
"github.com/googleapis/genai-toolbox/internal/sources"
"github.com/googleapis/genai-toolbox/internal/sources/cloudsqladmin"
"github.com/googleapis/genai-toolbox/internal/tools"
"google.golang.org/api/option"
sqladmin "google.golang.org/api/sqladmin/v1"
)
@@ -135,7 +134,7 @@ func (t Tool) Invoke(ctx context.Context, params tools.ParamValues, accessToken
iamUser, _ := paramsMap["iamUser"].(bool)
user := &sqladmin.User{
user := sqladmin.User{
Name: name,
}
@@ -150,19 +149,12 @@ func (t Tool) Invoke(ctx context.Context, params tools.ParamValues, accessToken
user.Password = password
}
client, err := t.Source.GetClient(ctx, string(accessToken))
service, err := t.Source.GetService(ctx, string(accessToken))
if err != nil {
return nil, err
}
service, err := sqladmin.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
return nil, fmt.Errorf("error creating new sqladmin service: %w", err)
}
service.UserAgent = t.Source.UserAgent
resp, err := service.Users.Insert(project, instance, user).Do()
resp, err := service.Users.Insert(project, instance, &user).Do()
if err != nil {
return nil, fmt.Errorf("error creating user: %w", err)
}