Skip to content
Snippets Groups Projects
Commit 229abb75 authored by Thomas Schneider's avatar Thomas Schneider
Browse files

Initial import

parents
No related branches found
No related tags found
No related merge requests found
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib
# Test binary, built with `go test -c`
*.test
# Output of the go coverage tool, specifically when used with LiteIDE
*.out
# Dependency directories (remove the comment below to include it)
# vendor/
# Go workspace file
go.work
go.mod 0 → 100644
package rwthdns
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
)
const ApiBase = "https://noc-portal.rz.rwth-aachen.de/dns-admin/api/v1/"
type Dnssec struct {
ZoneSigningKey struct {
CreatedAt time.Time `json:"created_at"`
} `json:"zone_signing_key"`
KeySigningKey struct {
CreatedAt time.Time `json:"created_at"`
} `json:"key_signing_key"`
}
// type dnssec Dnssec
// func (d *Dnssec) UnmarshalJSON(data []byte) error {
// var b bool
// err := json.Unmarshal(data, &b)
// if err == nil {
// if b {
// return &json.UnmarshalTypeError{
// Value: "true",
// Type: reflect.TypeOf(d),
// }
// }
// d = nil
// return nil
// }
// if _, ok := err.(*json.UnmarshalTypeError); !ok {
// return err
// }
// var ds dnssec
// err = json.Unmarshal(data, &ds)
// if err != nil {
// return err
// }
// if d != nil {
// *d = Dnssec(ds)
// } else {
// tmp := Dnssec(ds)
// d = &tmp
// }
// return nil
// }
type Zone struct {
Id int `json:"id"`
ZoneName string `json:"zone_name"`
Status string `json:"status"`
UpdatedAt time.Time `json:"updated_at"`
LastDeploy time.Time `json:"last_deploy"`
Dnssec interface{} `json:"dnssec"`
}
type Record struct {
Id int `json:"id"`
ZoneId int `json:"zone_id"`
Type string `json:"type"`
Content string `json:"content"`
Status string `json:"status"`
UpdatedAt time.Time `json:"updated_at"`
Editable bool `json:"editable"`
}
type Client struct {
ApiToken string
Client *http.Client
}
func (c *Client) do(method string, endpoint string, params url.Values) (*http.Response, error) {
if c.Client == nil {
c.Client = http.DefaultClient
}
u, err := url.Parse(ApiBase + endpoint)
if err != nil {
return nil, err
}
u.RawQuery = params.Encode()
r, err := http.NewRequest(method, u.String(), nil)
if err != nil {
return nil, err
}
r.Header.Set("PRIVATE-TOKEN", c.ApiToken)
res, err := c.Client.Do(r)
return res, err
}
func (c *Client) ListZones(search *string) ([]Zone, error) {
q := url.Values{}
if search != nil {
q.Set("search", url.QueryEscape(*search))
}
res, err := c.do("GET", "list_zones", q)
if err != nil {
return nil, err
}
dec := json.NewDecoder(res.Body)
var zones []Zone
err = dec.Decode(&zones)
return zones, err
}
func (c *Client) ListRecords(zone *int, search *string) ([]Record, error) {
q := url.Values{}
if zone != nil {
q.Add("zone_id", url.QueryEscape(fmt.Sprintf("%d", *zone)))
}
if search != nil {
q.Add("search", url.QueryEscape(*search))
}
res, err := c.do("GET", "list_records", q)
if err != nil {
return nil, err
}
dec := json.NewDecoder(res.Body)
var records []Record
err = dec.Decode(&records)
return records, err
}
func (c *Client) DeployZone(zone int) (Zone, error) {
q := url.Values{}
q.Set("zone_id", url.QueryEscape(fmt.Sprintf("%d", zone)))
res, err := c.do("POST", "deploy_zone", q)
if err != nil {
return Zone{}, err
}
dec := json.NewDecoder(res.Body)
var z Zone
err = dec.Decode(&z)
return z, err
}
func (c *Client) CreateRecord(content string) (Record, error) {
q := url.Values{}
q.Set("record_content", url.QueryEscape(content))
res, err := c.do("POST", "create_record", q)
if err != nil {
return Record{}, err
}
dec := json.NewDecoder(res.Body)
var r Record
err = dec.Decode(&r)
return r, err
}
func (c *Client) UpdateRecord(record int, content string) (Record, error) {
q := url.Values{}
q.Set("record_id", url.QueryEscape(fmt.Sprintf("%d", record)))
q.Set("record_content", url.QueryEscape(content))
res, err := c.do("POST", "update_record", q)
if err != nil {
return Record{}, err
}
dec := json.NewDecoder(res.Body)
var r Record
err = dec.Decode(&r)
return r, err
}
func (c *Client) DestroyRecord(record int) (Record, error) {
q := url.Values{}
q.Set("record_id", url.QueryEscape(fmt.Sprintf("%d", record)))
res, err := c.do("POST", "destroy_record", q)
if err != nil {
return Record{}, err
}
dec := json.NewDecoder(res.Body)
var r Record
err = dec.Decode(&r)
return r, err
}
func (c *Client) RestoreRecord(record int) (Record, error) {
q := url.Values{}
q.Set("record_id", url.QueryEscape(fmt.Sprintf("%d", record)))
res, err := c.do("POST", "restore_record", q)
if err != nil {
return Record{}, err
}
dec := json.NewDecoder(res.Body)
var r Record
err = dec.Decode(&r)
return r, err
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment