Package dbaas-go provides Go SDK to work with Selectel DBaaS
The Go library documentation is available at go.dev.
You can use this library to work with the following objects of the Selectel Managed Databases Service:
- acl
- available extension
- configuration parameter
- database
- datastore
- datastore type
- extension
- flavor
- grant
- logical replication slots
- prometheus metrics tokens
- topic
- user
You can install dbaas-go
package via go get
command:
go get github.com/selectel/dbaas-go
To 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 currently has the following API endpoint:
You can also retrieve all available API endpoints from the Identity catalog.
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)
}