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

Use go-logr for more thorough logging

parent 460b62d2
No related branches found
No related tags found
No related merge requests found
Pipeline #4182 passed
...@@ -8,6 +8,8 @@ import ( ...@@ -8,6 +8,8 @@ import (
"net/url" "net/url"
"strings" "strings"
"time" "time"
"github.com/go-logr/logr"
) )
const ApiBase = "https://noc-portal.rz.rwth-aachen.de/dns-admin/api/v1/" const ApiBase = "https://noc-portal.rz.rwth-aachen.de/dns-admin/api/v1/"
...@@ -76,16 +78,29 @@ type Record struct { ...@@ -76,16 +78,29 @@ type Record struct {
type Client struct { type Client struct {
ApiToken string ApiToken string
Client *http.Client Client *http.Client
Log *logr.Logger
}
func (c *Client) log() logr.Logger {
if c.Log != nil {
return c.Log.WithName("rwthdns")
}
return logr.Discard()
} }
func (c *Client) do(method string, endpoint string, params url.Values) (*http.Response, error) { func (c *Client) do(method string, endpoint string, params url.Values) (*http.Response, error) {
logger := c.log().WithValues("function", "do")
if c.Client == nil { if c.Client == nil {
c.Client = http.DefaultClient c.Client = http.DefaultClient
} }
logger.V(6).Info("arguments", "method", method, "endpoint", endpoint, "params", params)
r, err := http.NewRequest(method, ApiBase+endpoint, r, err := http.NewRequest(method, ApiBase+endpoint,
strings.NewReader(params.Encode())) strings.NewReader(params.Encode()))
if err != nil { if err != nil {
logger.Error(err, "NewRequest", "method", method, "url", ApiBase+endpoint,
"params", params.Encode())
return nil, err return nil, err
} }
r.Header.Set("Content-Type", "application/x-www-form-urlencoded") r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
...@@ -96,6 +111,8 @@ func (c *Client) do(method string, endpoint string, params url.Values) (*http.Re ...@@ -96,6 +111,8 @@ func (c *Client) do(method string, endpoint string, params url.Values) (*http.Re
} }
func (c *Client) ListZones(search *string) ([]Zone, error) { func (c *Client) ListZones(search *string) ([]Zone, error) {
logger := c.log().WithValues("function", "ListZones")
logger.V(6).Info("arguments", "search", search)
q := url.Values{} q := url.Values{}
if search != nil { if search != nil {
q.Set("search", *search) q.Set("search", *search)
...@@ -103,16 +120,32 @@ func (c *Client) ListZones(search *string) ([]Zone, error) { ...@@ -103,16 +120,32 @@ func (c *Client) ListZones(search *string) ([]Zone, error) {
res, err := c.do("GET", "list_zones", q) res, err := c.do("GET", "list_zones", q)
if err != nil { if err != nil {
logger.Error(err, "(*Client).do()",
"method", "GET", "endpoint", "list_zones", "params", q)
return nil, err return nil, err
} }
dec := json.NewDecoder(res.Body) b, err := io.ReadAll(res.Body)
if err != nil {
logger.Error(err, "io.ReadAll(res.Body)")
return nil, err
}
bs := string(b)
logger.V(6).Info("response", "content", bs, "response", responseInfo(res))
dec := json.NewDecoder(strings.NewReader(bs))
var zones []Zone var zones []Zone
err = dec.Decode(&zones) err = dec.Decode(&zones)
if err != nil {
logger.Error(err, "json.Decoder.Decode()", "zones", zones, "content", bs)
err = fmt.Errorf("Decode: %w (%s)", err, bs)
}
return zones, err return zones, err
} }
func (c *Client) ListRecords(zone *int, search *string) ([]Record, error) { func (c *Client) ListRecords(zone *int, search *string) ([]Record, error) {
logger := c.log().WithValues("function", "ListRecords")
logger.V(6).Info("arguments", "zone", zone, "search", search)
q := url.Values{} q := url.Values{}
if zone != nil { if zone != nil {
q.Add("zone_id", fmt.Sprintf("%d", *zone)) q.Add("zone_id", fmt.Sprintf("%d", *zone))
...@@ -123,50 +156,76 @@ func (c *Client) ListRecords(zone *int, search *string) ([]Record, error) { ...@@ -123,50 +156,76 @@ func (c *Client) ListRecords(zone *int, search *string) ([]Record, error) {
res, err := c.do("GET", "list_records", q) res, err := c.do("GET", "list_records", q)
if err != nil { if err != nil {
logger.Error(err, "(*Client).do()",
"method", "GET", "endpoint", "list_records", "params", q)
return nil, err return nil, err
} }
dec := json.NewDecoder(res.Body) b, err := io.ReadAll(res.Body)
if err != nil {
logger.Error(err, "io.ReadAll(res.Body)")
return nil, err
}
bs := string(b)
logger.V(6).Info("response", "content", bs, "response", responseInfo(res))
dec := json.NewDecoder(strings.NewReader(bs))
var records []Record var records []Record
err = dec.Decode(&records) err = dec.Decode(&records)
if err != nil {
logger.Error(err, "json.Decoder.Decode()", "records", records, "content", bs)
err = fmt.Errorf("Decode: %w (%s)", err, bs)
}
return records, err return records, err
} }
func (c *Client) DeployZone(zone int) (Zone, error) { func (c *Client) DeployZone(zone int) (Zone, error) {
logger := c.log().WithValues("function", "DeployZone")
logger.V(6).Info("arguments", "zone", zone)
q := url.Values{} q := url.Values{}
q.Set("zone_id", fmt.Sprintf("%d", zone)) q.Set("zone_id", fmt.Sprintf("%d", zone))
res, err := c.do("POST", "deploy_zone", q) res, err := c.do("POST", "deploy_zone", q)
if err != nil { if err != nil {
logger.Error(err, "(*Client).do()",
"method", "POST", "endpoint", "deploy_zone", "params", q)
return Zone{}, err return Zone{}, err
} }
b, err := io.ReadAll(res.Body) b, err := io.ReadAll(res.Body)
if err != nil { if err != nil {
logger.Error(err, "io.ReadAll(res.Body)")
return Zone{}, err return Zone{}, err
} }
bs := string(b) bs := string(b)
logger.V(6).Info("response", "content", bs, "response", responseInfo(res))
dec := json.NewDecoder(strings.NewReader(bs)) dec := json.NewDecoder(strings.NewReader(bs))
var z Zone var z Zone
err = dec.Decode(&z) err = dec.Decode(&z)
if err != nil { if err != nil {
logger.Error(err, "json.Decoder.Decode()", "zone", z, "content", bs)
err = fmt.Errorf("Decode: %w (%s)", err, bs) err = fmt.Errorf("Decode: %w (%s)", err, bs)
} }
return z, err return z, err
} }
func (c *Client) CreateRecord(content string) (Record, error) { func (c *Client) CreateRecord(content string) (Record, error) {
logger := c.log().WithValues("function", "CreateRecord")
logger.V(6).Info("arguments", "content", content)
q := url.Values{} q := url.Values{}
q.Set("record_content", content) q.Set("record_content", content)
res, err := c.do("POST", "create_record", q) res, err := c.do("POST", "create_record", q)
if err != nil { if err != nil {
logger.Error(err, "(*Client).do()",
"method", "POST", "endpoint", "create_record", "params", q)
return Record{}, err return Record{}, err
} }
b, err := io.ReadAll(res.Body) b, err := io.ReadAll(res.Body)
if err != nil { if err != nil {
logger.Error(err, "io.ReadAll(res.Body)")
return Record{}, err return Record{}, err
} }
bs := string(b) bs := string(b)
...@@ -175,53 +234,111 @@ func (c *Client) CreateRecord(content string) (Record, error) { ...@@ -175,53 +234,111 @@ func (c *Client) CreateRecord(content string) (Record, error) {
var r Record var r Record
err = dec.Decode(&r) err = dec.Decode(&r)
if err != nil { if err != nil {
logger.Error(err, "json.Decoder.Decode()", "record", r, "content", bs)
err = fmt.Errorf("Decode: %w (%s)", err, bs) err = fmt.Errorf("Decode: %w (%s)", err, bs)
} }
return r, err return r, err
} }
func (c *Client) UpdateRecord(record int, content string) (Record, error) { func (c *Client) UpdateRecord(record int, content string) (Record, error) {
logger := c.log().WithValues("function", "UpdateRecord")
logger.V(6).Info("arguments", "record", record, "content", content)
q := url.Values{} q := url.Values{}
q.Set("record_id", fmt.Sprintf("%d", record)) q.Set("record_id", fmt.Sprintf("%d", record))
q.Set("record_content", content) q.Set("record_content", content)
res, err := c.do("POST", "update_record", q) res, err := c.do("POST", "update_record", q)
if err != nil { if err != nil {
logger.Error(err, "(*Client).do()",
"method", "POST", "endpoint", "update_record", "params", q)
return Record{}, err
}
b, err := io.ReadAll(res.Body)
if err != nil {
logger.Error(err, "io.ReadAll(res.Body)")
return Record{}, err return Record{}, err
} }
bs := string(b)
logger.V(6).Info("response", "content", bs, "response", responseInfo(res))
dec := json.NewDecoder(res.Body) dec := json.NewDecoder(strings.NewReader(bs))
var r Record var r Record
err = dec.Decode(&r) err = dec.Decode(&r)
if err != nil {
logger.Error(err, "json.Decoder.Decode()", "record", r, "content", bs)
err = fmt.Errorf("Decode: %w (%s)", err, bs)
}
return r, err return r, err
} }
func (c *Client) DestroyRecord(record int) (Record, error) { func (c *Client) DestroyRecord(record int) (Record, error) {
logger := c.log().WithValues("function", "DestroyRecord")
logger.V(6).Info("arguments", "record", record)
q := url.Values{} q := url.Values{}
q.Set("record_id", fmt.Sprintf("%d", record)) q.Set("record_id", fmt.Sprintf("%d", record))
res, err := c.do("DELETE", "destroy_record", q) res, err := c.do("DELETE", "destroy_record", q)
if err != nil { if err != nil {
logger.Error(err, "(*Client).do()",
"method", "DELETE", "endpoint", "destroy_record", "params", q)
return Record{}, err return Record{}, err
} }
dec := json.NewDecoder(res.Body) b, err := io.ReadAll(res.Body)
if err != nil {
logger.Error(err, "io.ReadAll(res.Body)")
return Record{}, err
}
bs := string(b)
logger.V(6).Info("response", "content", bs, "response", responseInfo(res))
dec := json.NewDecoder(strings.NewReader(bs))
var r Record var r Record
err = dec.Decode(&r) err = dec.Decode(&r)
if err != nil {
logger.Error(err, "json.Decoder.Decode()", "record", r, "content", bs)
err = fmt.Errorf("Decode: %w (%s)", err, bs)
}
return r, err return r, err
} }
func (c *Client) RestoreRecord(record int) (Record, error) { func (c *Client) RestoreRecord(record int) (Record, error) {
logger := c.log().WithValues("function", "RestoreRecord")
logger.V(6).Info("arguments", "record", record)
q := url.Values{} q := url.Values{}
q.Set("record_id", fmt.Sprintf("%d", record)) q.Set("record_id", fmt.Sprintf("%d", record))
res, err := c.do("POST", "restore_record", q) res, err := c.do("POST", "restore_record", q)
if err != nil { if err != nil {
logger.Error(err, "(*Client).do()",
"method", "POST", "endpoint", "restore_record", "params", q)
return Record{}, err return Record{}, err
} }
dec := json.NewDecoder(res.Body) b, err := io.ReadAll(res.Body)
if err != nil {
logger.Error(err, "io.ReadAll(res.Body)")
return Record{}, err
}
bs := string(b)
logger.V(6).Info("response", "content", bs, "response", responseInfo(res))
dec := json.NewDecoder(strings.NewReader(bs))
var r Record var r Record
err = dec.Decode(&r) err = dec.Decode(&r)
if err != nil {
logger.Error(err, "json.Decoder.Decode()", "record", r, "content", bs)
err = fmt.Errorf("Decode: %w (%s)", err, bs)
}
return r, err return r, err
} }
func responseInfo(res *http.Response) map[string]interface{} {
r := make(map[string]interface{})
r["status"] = res.Status
r["proto"] = res.Proto
r["headers"] = res.Header
return r
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment