domains-go: Go SDK for Selectel Domains API
Package domains-go provides Go SDK to work with the Selectel Domains API.
Contents
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 Domains API:
Getting started
Installation
You can install needed domains-go
packages via go get
command:
go get github.com/selectel/domains-go
Authentication
To work with the Selectel Domains API you first need to:
❗️IMPORTANT❗️
Selectel Token
and Keystone Project Token
are different tokens!
Above we mentioned how to get keystone project token, how to obtain selectel token read here
Usage example
package main
import (
"context"
"fmt"
"log"
"net/http"
v2 "github.com/selectel/domains-go/pkg/v2"
)
func main() {
// Keystone project token. Read more in authorization.
token := "gAAAAABeVNzu-..."
// Domains API V2 endpoint to work with
endpoint := "https://api.selectel.ru/domains/v2"
httpClient := &http.Client{}
userAgent := "domains-go-v2"
hdrs := http.Header{}
hdrs.Add("X-Auth-Token", token)
hdrs.Add("User-Agent", userAgent)
// Initialize the Domains API V2 client
client := v2.NewClient(endpoint, httpClient, hdrs)
createZoneOpts := &v2.Zone{
Name: "domains-go-v2.ru.",
}
// Create zone
selectelCreatedZone, err := client.CreateZone(context.Background(), createZoneOpts)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Created zone: %+v\n", selectelCreatedZone)
listZonesOpts := &map[string]string{}
// List zones
selectelZones, err := client.ListZones(context.Background(), listZonesOpts)
if err != nil {
log.Fatal(err)
}
for _, zone := range selectelZones.GetItems() {
fmt.Printf("%+v\n", zone)
}
createRrsetOpts := &v2.RRSet{
Name: "txt.domains-go-v2.ru.",
Type: v2.TXT,
TTL: 60,
Records: []v2.RecordItem{
// Only for TXT Rrset escaping quotes
{Content: "\"Hello world!\""},
},
}
// Create rrset type TXT
selectelCreatedRrset, err := client.CreateRRSet(context.Background(), selectelCreatedZone.ID, createRrsetOpts)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Created rrset: %+v\n", selectelCreatedRrset)
}
Current version vs Legacy version
Current version is github.com/selectel/domains-go/pkg/v2
Legacy version is github.com/selectel/domains-go/pkg/v1
They are not compatible. They utilize different API and created zones live on different authoritative servers.
Zone created in v2 API with current version is entirely new zone, and not available via v1 api and vice versa.
If you are going to create new zone, we strongly recommend to use github.com/selectel/domains-go/pkg/v2
.
If you have zones in v1, you still can manage them with github.com/selectel/domains-go/pkg/v1
.
Legacy version following objects of the Selectel Domains API v1:
Current version following objects of the Selectel Domains API v2:
Usage legacy example
❗️IMPORTANT❗️
We are not recommending using this example.
package main
import (
"context"
"fmt"
"log"
v1 "github.com/selectel/domains-go/pkg/v1"
"github.com/selectel/domains-go/pkg/v1/domain"
"github.com/selectel/domains-go/pkg/v1/record"
)
func main() {
// Token to work with Selectel Cloud project
token := "gAAAAABeVNzu-..."
// Domains API V1 endpoint to work with
endpoint := "https://api.selectel.ru/domains/v1"
// Initialize the Domains API V1 client
client := v1.NewDomainsClientV1(token, endpoint)
createDomainOpts := &domain.CreateOpts{
Name: "testdomain.xyz",
}
// Create domain
selectelDomain, _, err := domain.Create(context.Background(), client, createDomainOpts)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Created domain: %+v\n", selectelDomain)
// List domains
selectelDomains, _, err := domain.List(context.Background(), client)
if err != nil {
log.Fatal(err)
}
for _, d := range selectelDomains {
fmt.Printf("%+v\n", d)
}
createRecordOpts := &record.CreateOpts{
Name: "share.testdomain.xyz",
Type: record.TypeCNAME,
TTL: 60,
Content: "origin.example.com",
}
// Create domain record
domainRecord, _, err := record.Create(context.Background(), client, selectelDomain.ID, createRecordOpts)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Created record: %+v\n", domainRecord)
}