Skip to content

selectel/dbaas-go

Folders and files

NameName
Last commit message
Last commit date
Jan 15, 2024
May 17, 2021
Jan 12, 2024
May 17, 2021
May 17, 2021
Jan 24, 2024
Jan 12, 2024
Jan 12, 2024
Jan 12, 2024
Jan 12, 2024
Jan 18, 2024
Jan 18, 2024
Jan 12, 2024
Jan 12, 2024
Feb 12, 2024
Feb 12, 2024
Jan 12, 2024
Jan 12, 2024
Jan 12, 2024
May 17, 2021
Sep 19, 2022
Jan 12, 2024
Jan 12, 2024
Jan 18, 2024
Jan 18, 2024
Mar 12, 2024
Mar 11, 2024
Jan 15, 2024
Jan 15, 2024
Jan 12, 2024
Jan 12, 2024
Jan 12, 2024
Jan 12, 2024
Feb 12, 2024
Jan 12, 2024
Jan 12, 2024
Jan 12, 2024
Jan 12, 2024
Jan 12, 2024
Jan 12, 2024

Repository files navigation

dbaas-go: Go SDK for Selectel DBaaS

Go.dev reference Go Report Card

Package dbaas-go provides Go SDK to work with Selectel DBaaS

Documentation

The Go library documentation is available at go.dev.

What this library is capable of

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

Getting started

Instalation

You can install dbaas-go package via go get command:

go get github.com/selectel/dbaas-go

Authentication

To work with the Selectel Managed Databases Service API you first need to:

Endpoints

Selectel Managed Databases Service currently has the following API endpoint:

URL Region
https://ru-1.dbaas.selcloud.ru/v1 ru-1
https://ru-2.dbaas.selcloud.ru/v1 ru-2
https://ru-3.dbaas.selcloud.ru/v1 ru-3
https://ru-7.dbaas.selcloud.ru/v1 ru-7
https://ru-8.dbaas.selcloud.ru/v1 ru-8
https://ru-9.dbaas.selcloud.ru/v1 ru-9
https://uz-1.dbaas.selcloud.ru/v1 uz-1
https://kz-1.dbaas.selcloud.ru/v1 kz-1

You can also retrieve all available API endpoints from the Identity catalog.

Usage example

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)
}