Git Product home page Git Product logo

domains-go's Introduction

domains-go: Go SDK for Selectel Domains API

Go.dev reference Go Report Card Build Status Coverage Status

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

domains-go's People

Contributors

dstdfx avatar dchudik avatar archirk avatar kolsean avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.