mirror of
https://github.com/googleapis/genai-toolbox.git
synced 2026-01-23 06:18:02 -05:00
## Description --- This pull request adds a new tool, cloud-sql-clone-instance, which enables cloning a Cloud SQL instance from the toolbox using the Cloud SQL Admin API. The tool supports both standard cloning and point-in-time recovery (PITR). It also supports specifying preferred zones for cloned instances via the preferredZone and preferredSecondaryZone fields. Key Features: Instance Cloning: The tool allows you to clone a Cloud SQL instance by specifying the source and destination instance names. Point-in-Time Recovery (PITR): By providing a pointInTime timestamp, you can create a clone of an instance as it existed at a specific moment. High Availability Configuration: The preferredZone and preferredSecondaryZone parameters allow you to configure the cloned instance for high availability. Tested: <img width="1182" height="446" alt="Screenshot 2025-11-11 at 12 21 47 PM" src="https://github.com/user-attachments/assets/7f39a5a3-3967-43d0-8041-f1d47b4fbcd9" /> ## 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) - [ ] Make sure to add `!` if this involve a breaking change 🛠️ Fixes #1915 Co-authored-by: prernakakkar-google <158031829+prernakakkar-google@users.noreply.github.com> Co-authored-by: Averi Kitsch <akitsch@google.com>
245 lines
7.0 KiB
Go
245 lines
7.0 KiB
Go
// 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 cloudsql
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"net/url"
|
|
"reflect"
|
|
"regexp"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
"github.com/googleapis/genai-toolbox/internal/testutils"
|
|
"github.com/googleapis/genai-toolbox/tests"
|
|
sqladmin "google.golang.org/api/sqladmin/v1"
|
|
|
|
_ "github.com/googleapis/genai-toolbox/internal/tools/cloudsql/cloudsqlcloneinstance"
|
|
)
|
|
|
|
var (
|
|
cloneInstanceToolKind = "cloud-sql-clone-instance"
|
|
)
|
|
|
|
type cloneInstanceTransport struct {
|
|
transport http.RoundTripper
|
|
url *url.URL
|
|
}
|
|
|
|
func (t *cloneInstanceTransport) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
if strings.HasPrefix(req.URL.String(), "https://sqladmin.googleapis.com") {
|
|
req.URL.Scheme = t.url.Scheme
|
|
req.URL.Host = t.url.Host
|
|
}
|
|
return t.transport.RoundTrip(req)
|
|
}
|
|
|
|
type masterCloneInstanceHandler struct {
|
|
t *testing.T
|
|
}
|
|
|
|
func (h *masterCloneInstanceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
if !strings.Contains(r.UserAgent(), "genai-toolbox/") {
|
|
h.t.Errorf("User-Agent header not found")
|
|
}
|
|
var body sqladmin.InstancesCloneRequest
|
|
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
|
|
h.t.Fatalf("failed to decode request body: %v", err)
|
|
} else {
|
|
h.t.Logf("Received request body: %+v", body)
|
|
}
|
|
|
|
var expectedBody sqladmin.InstancesCloneRequest
|
|
var response any
|
|
var statusCode int
|
|
|
|
switch body.CloneContext.DestinationInstanceName {
|
|
case "cloned-instance":
|
|
expectedBody = sqladmin.InstancesCloneRequest{
|
|
CloneContext: &sqladmin.CloneContext{
|
|
DestinationInstanceName: "cloned-instance",
|
|
},
|
|
}
|
|
response = map[string]any{"name": "op1", "status": "PENDING"}
|
|
statusCode = http.StatusOK
|
|
case "cloned-pitr-instance":
|
|
expectedBody = sqladmin.InstancesCloneRequest{
|
|
CloneContext: &sqladmin.CloneContext{
|
|
DestinationInstanceName: "cloned-pitr-instance",
|
|
PointInTime: "2025-11-04T10:00:00Z",
|
|
},
|
|
}
|
|
response = map[string]any{"name": "op2", "status": "PENDING"}
|
|
statusCode = http.StatusOK
|
|
default:
|
|
http.Error(w, fmt.Sprintf("unhandled destination instance name: %s", body.CloneContext.DestinationInstanceName), http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if diff := cmp.Diff(expectedBody, body); diff != "" {
|
|
h.t.Errorf("unexpected request body (-want +got):\n%s", diff)
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(statusCode)
|
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
func TestCloneInstanceToolEndpoints(t *testing.T) {
|
|
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
|
defer cancel()
|
|
|
|
handler := &masterCloneInstanceHandler{t: t}
|
|
server := httptest.NewServer(handler)
|
|
defer server.Close()
|
|
|
|
serverURL, err := url.Parse(server.URL)
|
|
if err != nil {
|
|
t.Fatalf("failed to parse server URL: %v", err)
|
|
}
|
|
|
|
originalTransport := http.DefaultClient.Transport
|
|
if originalTransport == nil {
|
|
originalTransport = http.DefaultTransport
|
|
}
|
|
http.DefaultClient.Transport = &cloneInstanceTransport{
|
|
transport: originalTransport,
|
|
url: serverURL,
|
|
}
|
|
t.Cleanup(func() {
|
|
http.DefaultClient.Transport = originalTransport
|
|
})
|
|
|
|
var args []string
|
|
toolsFile := getCloneInstanceToolsConfig()
|
|
cmd, cleanup, err := tests.StartCmd(ctx, toolsFile, args...)
|
|
if err != nil {
|
|
t.Fatalf("command initialization returned an error: %s", err)
|
|
}
|
|
defer cleanup()
|
|
|
|
waitCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
|
defer cancel()
|
|
out, err := testutils.WaitForString(waitCtx, regexp.MustCompile(`Server ready to serve`), cmd.Out)
|
|
if err != nil {
|
|
t.Logf("toolbox command logs: \n%s", out)
|
|
t.Fatalf("toolbox didn't start successfully: %s", err)
|
|
}
|
|
|
|
tcs := []struct {
|
|
name string
|
|
toolName string
|
|
body string
|
|
want string
|
|
expectError bool
|
|
errorStatus int
|
|
}{
|
|
{
|
|
name: "successful clone instance",
|
|
toolName: "clone-instance",
|
|
body: `{"project": "p1", "sourceInstanceName": "source-instance", "destinationInstanceName": "cloned-instance"}`,
|
|
want: `{"name":"op1","status":"PENDING"}`,
|
|
},
|
|
{
|
|
name: "successful pitr clone instance",
|
|
toolName: "clone-instance",
|
|
body: `{"project": "p1", "sourceInstanceName": "source-instance", "destinationInstanceName": "cloned-pitr-instance", "pointInTime": "2025-11-04T10:00:00Z"}`,
|
|
want: `{"name":"op2","status":"PENDING"}`,
|
|
},
|
|
{
|
|
name: "missing destination instance name",
|
|
toolName: "clone-instance",
|
|
body: `{"project": "p1", "sourceInstanceName": "source-instance"}`,
|
|
expectError: true,
|
|
errorStatus: http.StatusBadRequest,
|
|
},
|
|
}
|
|
|
|
for _, tc := range tcs {
|
|
tc := tc
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
api := fmt.Sprintf("http://127.0.0.1:5000/api/tool/%s/invoke", tc.toolName)
|
|
req, err := http.NewRequest(http.MethodPost, api, bytes.NewBufferString(tc.body))
|
|
if err != nil {
|
|
t.Fatalf("unable to create request: %s", err)
|
|
}
|
|
req.Header.Add("Content-type", "application/json")
|
|
resp, err := http.DefaultClient.Do(req)
|
|
if err != nil {
|
|
t.Fatalf("unable to send request: %s", err)
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if tc.expectError {
|
|
if resp.StatusCode != tc.errorStatus {
|
|
bodyBytes, _ := io.ReadAll(resp.Body)
|
|
t.Fatalf("expected status %d but got %d: %s", tc.errorStatus, resp.StatusCode, string(bodyBytes))
|
|
}
|
|
return
|
|
}
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
bodyBytes, _ := io.ReadAll(resp.Body)
|
|
t.Fatalf("response status code is not 200, got %d: %s", resp.StatusCode, string(bodyBytes))
|
|
}
|
|
|
|
var result struct {
|
|
Result string `json:"result"`
|
|
}
|
|
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
|
t.Fatalf("failed to decode response: %v", err)
|
|
}
|
|
|
|
var got, want map[string]any
|
|
if err := json.Unmarshal([]byte(result.Result), &got); err != nil {
|
|
t.Fatalf("failed to unmarshal result: %v", err)
|
|
}
|
|
if err := json.Unmarshal([]byte(tc.want), &want); err != nil {
|
|
t.Fatalf("failed to unmarshal want: %v", err)
|
|
}
|
|
|
|
if !reflect.DeepEqual(got, want) {
|
|
t.Fatalf("unexpected result: got %+v, want %+v", got, want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func getCloneInstanceToolsConfig() map[string]any {
|
|
return map[string]any{
|
|
"sources": map[string]any{
|
|
"my-cloud-sql-source": map[string]any{
|
|
"kind": "cloud-sql-admin",
|
|
},
|
|
},
|
|
"tools": map[string]any{
|
|
"clone-instance": map[string]any{
|
|
"kind": cloneInstanceToolKind,
|
|
"source": "my-cloud-sql-source",
|
|
},
|
|
},
|
|
}
|
|
}
|