-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(noncloud-mysql): add source and tool
- Loading branch information
Showing
5 changed files
with
176 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package mysql | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"fmt" | ||
|
||
_ "github.com/go-sql-driver/mysql" | ||
"github.com/googleapis/genai-toolbox/internal/sources" | ||
"go.opentelemetry.io/otel/trace" | ||
) | ||
|
||
const SourceKind string = "mysql" | ||
|
||
// validate interface | ||
var _ sources.SourceConfig = Config{} | ||
|
||
type Config struct { | ||
Name string `yaml:"name"` | ||
Kind string `yaml:"kind"` | ||
Host string `yaml:"host"` | ||
Port string `yaml:"port"` | ||
User string `yaml:"user"` | ||
Password string `yaml:"password"` | ||
Database string `yaml:"database"` | ||
} | ||
|
||
func (r Config) SourceConfigKind() string { | ||
return SourceKind | ||
} | ||
|
||
func (r Config) Initialize(ctx context.Context, tracer trace.Tracer) (sources.Source, error) { | ||
pool, err := initMySQLConnectionPool(ctx, tracer, r.Name, r.Host, r.Port, r.User, r.Password, r.Database) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to create pool: %w", err) | ||
} | ||
|
||
err = pool.PingContext(context.Background()) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to connect successfully: %w", err) | ||
} | ||
|
||
s := &Source{ | ||
Name: r.Name, | ||
Kind: SourceKind, | ||
Pool: pool, | ||
} | ||
return s, nil | ||
} | ||
|
||
var _ sources.Source = &Source{} | ||
|
||
type Source struct { | ||
Name string `yaml:"name"` | ||
Kind string `yaml:"kind"` | ||
Pool *sql.DB | ||
} | ||
|
||
func (s *Source) SourceKind() string { | ||
return SourceKind | ||
} | ||
|
||
func (s *Source) MySQLPool() *sql.DB { | ||
return s.Pool | ||
} | ||
|
||
func initMySQLConnectionPool(ctx context.Context, tracer trace.Tracer, name, host, port, user, pass, dbname string) (*sql.DB, error) { | ||
//nolint:all // Reassigned ctx | ||
ctx, span := sources.InitConnectionSpan(ctx, tracer, SourceKind, name) | ||
defer span.End() | ||
|
||
// Configure the driver to connect to the database | ||
dsn := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?parseTime=true", user, pass, host, port, dbname) | ||
|
||
// Interact with the driver directly as you normally would | ||
pool, err := sql.Open("mysql", dsn) | ||
if err != nil { | ||
return nil, fmt.Errorf("sql.Open: %w", err) | ||
} | ||
return pool, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright 2025 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package mysql_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"github.com/googleapis/genai-toolbox/internal/server" | ||
"github.com/googleapis/genai-toolbox/internal/sources/mysql" | ||
"github.com/googleapis/genai-toolbox/internal/testutils" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func TestParseFromYamlCloudSQLMySQL(t *testing.T) { | ||
tcs := []struct { | ||
desc string | ||
in string | ||
want server.SourceConfigs | ||
}{ | ||
{ | ||
desc: "basic example", | ||
in: ` | ||
sources: | ||
my-mysql-instance: | ||
kind: mysql | ||
host: 0.0.0.0 | ||
port: my-host | ||
database: my_db | ||
`, | ||
want: server.SourceConfigs{ | ||
"my-mysql-instance": mysql.Config{ | ||
Name: "my-mysql-instance", | ||
Kind: mysql.SourceKind, | ||
Host: "0.0.0.0", | ||
Port: "my-host", | ||
Database: "my_db", | ||
}, | ||
}, | ||
}, | ||
} | ||
for _, tc := range tcs { | ||
t.Run(tc.desc, func(t *testing.T) { | ||
got := struct { | ||
Sources server.SourceConfigs `yaml:"sources"` | ||
}{} | ||
// Parse contents | ||
err := yaml.Unmarshal(testutils.FormatYaml(tc.in), &got) | ||
if err != nil { | ||
t.Fatalf("unable to unmarshal: %s", err) | ||
} | ||
if !cmp.Equal(tc.want, got.Sources) { | ||
t.Fatalf("incorrect parse: want %v, got %v", tc.want, got.Sources) | ||
} | ||
}) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters