Package dbaas-go provides Go SDK to work with Selectel DBaaS
The Go library documentation is available at go.dev.
The SDK provides access to Selectel Managed Databases Service resources through DBaaS API v1 and v2 clients.
The v1 client provides access to the existing DBaaS API v1 resources, including:
- acls
- available extensions
- configuration parameters
- databases
- datastores
- datastore types
- extensions
- flavors
- grants
- logical replication slots
- prometheus metrics tokens
- topics
- users
The v2 client currently provides:
- datastore types
- flavors
- ClickHouse datastores
- ClickHouse node groups
- ClickHouse shard groups
- ClickHouse datastore configuration parameters
You can install dbaas-go package via go get command:
go get github.com/selectel/dbaas-goTo work with the Selectel Managed Databases Service API you first need to:
- Create a Selectel account: registration page.
- Create a project in Selectel Cloud Platform projects.
- Retrieve a token for your project via API or go-selvpcclient.
Selectel Managed Databases Service API endpoints are available for different regions.
Specify the API version in the endpoint according to the client you use by appending /v1 or /v2.
For example:
https://ru-2.dbaas.selcloud.ru/v1 # DBaaS API v1
https://ru-2.dbaas.selcloud.ru/v2 # DBaaS API v2
The currently available DBaaS API endpoints are:
You can also retrieve all available API endpoints from the Identity catalog.
You can use Godoc to view methods and signatures
go install golang.org/x/tools/cmd/godoc@latest
export PATH="$PATH:$HOME/go/bin/"
godoc -http=localhost:6060View http://localhost:6060/pkg/github.com/selectel/dbaas-go/
package main
import (
"context"
"log"
"fmt"
"github.com/gophercloud/gophercloud"
"github.com/gophercloud/gophercloud/openstack"
"github.com/gophercloud/gophercloud/openstack/networking/v2/subnets"
"github.com/selectel/dbaas-go"
)
func main() {
// Token to work with Selectel Cloud project.
token := "TOKEN"
// DBaaS endpoint to work with.
endpoint := "https://ru-2.dbaas.selcloud.ru/v1"
openstackEndpoint := "https://api.selvpc.ru/identity/v3/"
openstackRegion := "ru-2"
// Initialize the DBaaS v1 client.
dbaasClient, err := dbaas.NewDBAASClient(token, endpoint)
if err != nil {
log.Fatal(err)
}
// Prepare empty context.
ctx := context.Background()
// Get available datastore types.
datastoreTypes, err := dbaasClient.DatastoreTypes(ctx)
if err != nil {
log.Fatal(err)
}
// Auth options for openstack to get all subnets.
devopts := gophercloud.AuthOptions{
IdentityEndpoint: openstackEndpoint,
TokenID: token,
}
provider, err := openstack.AuthenticatedClient(devopts)
if err != nil {
log.Fatal(err)
}
// Create a new network client.
networkClient, err := openstack.NewNetworkV2(provider, gophercloud.EndpointOpts{Region: openstackRegion})
if err != nil {
log.Fatal(err)
}
// Get a list of available subnets.
listOpts := subnets.ListOpts{
IPVersion: 4,
}
allPages, err := subnets.List(networkClient, listOpts).AllPages()
if err != nil {
log.Fatal(err)
}
allSubnets, err := subnets.ExtractSubnets(allPages)
if err != nil {
log.Fatal(err)
}
// Create options for a new datastore.
datastoreCreateOpts := dbaas.DatastoreCreateOpts{
Name: "go_cluster",
TypeID: datastoreTypes[0].ID,
NodeCount: 1,
SubnetID: allSubnets[0].ID,
Flavor: &dbaas.Flavor{Vcpus: 2, RAM: 4096, Disk: 32},
}
// Create a new datastore.
newDatastore, err := dbaasClient.CreateDatastore(ctx, datastoreCreateOpts)
if err != nil {
log.Fatal(err)
}
// Print datastores fields.
fmt.Printf("Created datastore: %+v\n", newDatastore)
}package main
import (
"context"
"fmt"
"log"
dbaas "github.com/selectel/dbaas-go/v2"
"github.com/selectel/dbaas-go/v2/clickhouse"
"github.com/selectel/dbaas-go/v2/common"
)
func main() {
// Token to work with Selectel Cloud project.
token := "TOKEN"
// DBaaS v2 endpoint to work with.
endpoint := "https://ru-2.dbaas.selcloud.ru/v2"
// Initialize the DBaaS v2 client.
client, err := dbaas.NewAPI(token, endpoint)
if err != nil {
log.Fatal(err)
}
// Prepare empty context.
ctx := context.Background()
// Create a new ClickHouse datastore.
shardOneWeight := 100
shardOneHasPublicIPs := false
datastore, err := client.ClickHouse.CreateDatastore(
ctx,
clickhouse.DatastoreCreateRequest{
Name: "go_cluster",
TypeID: "DATASTORE_TYPE_ID",
SubnetID: "SUBNET_ID",
Password: "PASSWORD",
NodeGroups: []clickhouse.NodeGroupCreateRequest{
{
Name: "shard1",
Role: clickhouse.NodeGroupRoleData,
NodeCount: 1,
Flavor: clickhouse.FlavorForNodeGroupRequest{
Type: common.FlavorTypeFIXED,
ID: "FLAVOR_ID",
DiskType: common.FlavorDiskLocal,
},
Weight: &shardOneWeight,
HasPublicIPs: &shardOneHasPublicIPs,
},
},
},
)
if err != nil {
log.Fatal(err)
}
// Print datastore fields.
fmt.Printf("Created datastore: %+v\n", datastore)
}The TypeID, SubnetID and Flavor.ID values must be replaced with IDs available in your Selectel project.