cloudmysql

package
v0.0.0-...-1538fac Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 28, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package cloudmysql provides connections to managed MySQL Cloud SQL instances.

Example
package main

import (
	"context"

	"github.com/Lioric/go-cloud/gcp"
	"github.com/Lioric/go-cloud/mysql/cloudmysql"
	"golang.org/x/oauth2"
	"golang.org/x/oauth2/google"
)

func main() {
	ctx := context.Background()
	creds, err := google.FindDefaultCredentials(ctx, "https://www.googleapis.com/auth/cloud-platform")
	if err != nil {
		panic(err)
	}
	authClient := gcp.HTTPClient{Client: *oauth2.NewClient(ctx, creds.TokenSource)}
	db, err := cloudmysql.Open(ctx, cloudmysql.NewCertSource(&authClient), &cloudmysql.Params{
		// Replace these with your actual settings.
		ProjectID: "example-project",
		Region:    "us-central1",
		Instance:  "my-instance01",
		User:      "myrole",
		Database:  "test",
	})
	if err != nil {
		panic(err)
	}

	// Use database in your program.
	db.Exec("CREATE TABLE foo (bar INT);")
}
Output:

Index

Examples

Constants

This section is empty.

Variables

CertSourceSet is a Wire provider set that binds a Cloud SQL proxy certificate source from an GCP-authenticated HTTP client.

Functions

func NewCertSource

func NewCertSource(c *gcp.HTTPClient) *certs.RemoteCertSource

NewCertSource creates a local certificate source that uses the given HTTP client. The client is assumed to make authenticated requests.

func Open

func Open(ctx context.Context, certSource proxy.CertSource, params *Params) (*sql.DB, error)

Open opens a Cloud SQL database.

Types

type Params

type Params struct {
	ProjectID string
	Region    string
	Instance  string
	User      string
	Password  string // may be empty, see https://cloud.google.com/sql/docs/sql-proxy#user
	Database  string
}

Params specifies how to connect to a Cloud SQL database.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL