initial commit
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
package netbox
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type APIRequestClient struct {
|
||||
Client *http.Client
|
||||
NetboxURL *url.URL
|
||||
Token string
|
||||
UserAgent string
|
||||
}
|
||||
|
||||
type APIResultModel interface {
|
||||
Record | Zone
|
||||
}
|
||||
|
||||
type APIManyResponse[T APIResultModel] struct {
|
||||
Count int `json:"count"`
|
||||
Next string `json:"next"`
|
||||
Previous string `json:"previous"`
|
||||
Results []T `json:"results"`
|
||||
}
|
||||
|
||||
func doGet(
|
||||
requestClient *APIRequestClient,
|
||||
url string,
|
||||
) (*http.Response, error) {
|
||||
request, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
request.Header.Set(
|
||||
"Authorization",
|
||||
fmt.Sprintf("Token %s", requestClient.Token),
|
||||
)
|
||||
|
||||
request.Header.Set("User-Agent", requestClient.UserAgent)
|
||||
|
||||
return requestClient.Client.Do(request)
|
||||
}
|
||||
|
||||
func responseError(response *http.Response) error {
|
||||
if response.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf(
|
||||
"request error [%d] %q",
|
||||
response.StatusCode,
|
||||
response.Status,
|
||||
)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func get[T APIResultModel](
|
||||
requestClient *APIRequestClient,
|
||||
url string,
|
||||
) (T, error) {
|
||||
var out T
|
||||
response, err := doGet(requestClient, url)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
if err := responseError(response); err != nil {
|
||||
return out, err
|
||||
}
|
||||
decoder := json.NewDecoder(response.Body)
|
||||
if err := decoder.Decode(&out); err != nil {
|
||||
return out, fmt.Errorf("could not unmarshal response: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func getMany[T APIResultModel](
|
||||
requestClient *APIRequestClient,
|
||||
url string,
|
||||
) ([]T, error) {
|
||||
nextUrl := url
|
||||
var out []T
|
||||
|
||||
for nextUrl != "" {
|
||||
response, err := doGet(requestClient, nextUrl)
|
||||
if err != nil {
|
||||
return out, err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
if err := responseError(response); err != nil {
|
||||
return out, err
|
||||
}
|
||||
|
||||
var apiResponse APIManyResponse[T]
|
||||
decoder := json.NewDecoder(response.Body)
|
||||
if err := decoder.Decode(&apiResponse); err != nil {
|
||||
return out, fmt.Errorf("could not unmarshal response: %w", err)
|
||||
}
|
||||
|
||||
if out == nil {
|
||||
out = make([]T, 0, apiResponse.Count)
|
||||
}
|
||||
out = append(out, apiResponse.Results...)
|
||||
|
||||
nextUrl = apiResponse.Next
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package netbox
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Record struct {
|
||||
Type string `json:"type"`
|
||||
Value string `json:"value"`
|
||||
TTL *uint32 `json:"ttl"`
|
||||
Zone Zone `json:"zone"`
|
||||
FQDN string `json:"fqdn"`
|
||||
}
|
||||
|
||||
type RecordQuery struct {
|
||||
FQDN string
|
||||
Name string
|
||||
Type []string
|
||||
Zone *Zone
|
||||
}
|
||||
|
||||
func (recordQuery *RecordQuery) Encode() string {
|
||||
out := url.Values{}
|
||||
|
||||
if recordQuery.FQDN != "" {
|
||||
out.Set("fqdn", recordQuery.FQDN)
|
||||
}
|
||||
|
||||
if recordQuery.Name != "" {
|
||||
out.Set("name", recordQuery.Name)
|
||||
}
|
||||
|
||||
if len(recordQuery.Type) != 0 {
|
||||
for _, t := range recordQuery.Type {
|
||||
out.Add("type", t)
|
||||
}
|
||||
}
|
||||
|
||||
if recordQuery.Zone != nil {
|
||||
out.Set("zone_id", strconv.Itoa(recordQuery.Zone.ID))
|
||||
}
|
||||
|
||||
return out.Encode()
|
||||
}
|
||||
|
||||
func urlRecords(netboxurl *url.URL) *url.URL {
|
||||
return netboxurl.JoinPath("records", "/")
|
||||
}
|
||||
|
||||
func GetRecordsQuery(
|
||||
requestClient *APIRequestClient,
|
||||
query *RecordQuery,
|
||||
) ([]Record, error) {
|
||||
requestUrl := urlRecords(requestClient.NetboxURL)
|
||||
requestUrl.RawQuery = query.Encode()
|
||||
records, err := getMany[Record](requestClient, requestUrl.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if query.Zone != nil {
|
||||
for k, record := range records {
|
||||
if record.TTL == nil {
|
||||
records[k].TTL = &query.Zone.DefaultTTL
|
||||
}
|
||||
}
|
||||
} else {
|
||||
resolvedRecords, err := resolveRecordTTLs(requestClient, records)
|
||||
if err != nil {
|
||||
return records, err
|
||||
}
|
||||
records = resolvedRecords
|
||||
}
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func resolveRecordTTLs(
|
||||
requestClient *APIRequestClient,
|
||||
records []Record,
|
||||
) ([]Record, error) {
|
||||
zoneTTL := make(map[int]uint32)
|
||||
for k, record := range records {
|
||||
if record.TTL != nil {
|
||||
continue
|
||||
}
|
||||
if ttl, ok := zoneTTL[record.Zone.ID]; ok {
|
||||
records[k].TTL = &ttl
|
||||
continue
|
||||
}
|
||||
zoneUrl := urlZoneID(requestClient.NetboxURL, record.Zone.ID)
|
||||
zone, err := get[Zone](requestClient, zoneUrl.String())
|
||||
if err != nil {
|
||||
return records, err
|
||||
}
|
||||
zoneTTL[zone.ID] = zone.DefaultTTL
|
||||
records[k].TTL = &zone.DefaultTTL
|
||||
}
|
||||
return records, nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package netbox
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
type Zone struct {
|
||||
DefaultTTL uint32 `json:"default_ttl"`
|
||||
ID int `json:"id"`
|
||||
Name string `json:"name"`
|
||||
NameServers []SOAMName `json:"nameservers"`
|
||||
}
|
||||
|
||||
type SOAMName struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
func urlZones(netboxurl *url.URL) *url.URL {
|
||||
return netboxurl.JoinPath("zones", "/")
|
||||
}
|
||||
|
||||
func urlZoneID(netboxurl *url.URL, id int) *url.URL {
|
||||
return netboxurl.JoinPath("zones", "/", strconv.Itoa(id), "/")
|
||||
}
|
||||
|
||||
func GetZones(requestClient *APIRequestClient) ([]Zone, error) {
|
||||
requestUrl := urlZones(requestClient.NetboxURL)
|
||||
zones, err := getMany[Zone](requestClient, requestUrl.String())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return zones, nil
|
||||
}
|
||||
Reference in New Issue
Block a user