Skip to content
Snippets Groups Projects
Select Git revision
  • 1444f0daaf605716132ff8b165e783d2030f561f
  • main default protected
  • debug
3 results

main.go

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    rwthdns.go 4.21 KiB
    package rwthdns
    
    import (
    	"encoding/json"
    	"fmt"
    	"net/http"
    	"net/url"
    	"strings"
    	"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
    	}
    
    	r, err := http.NewRequest(method, ApiBase+endpoint,
    		strings.NewReader(params.Encode()))
    	if err != nil {
    		return nil, err
    	}
    	r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
    	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", *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", fmt.Sprintf("%d", *zone))
    	}
    	if search != nil {
    		q.Add("search", *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", 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", 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", fmt.Sprintf("%d", record))
    	q.Set("record_content", 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", fmt.Sprintf("%d", record))
    
    	res, err := c.do("DELETE", "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", 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
    }