Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Wholey authored Feb 19, 2022
1 parent ae71b38 commit 58b0e37
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 13 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ require (
github.com/PuerkitoBio/goquery v1.8.0
github.com/hashicorp/terraform-plugin-docs v0.5.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
github.com/ryanwholey/go-pihole v0.0.2
github.com/ryanwholey/go-pihole v0.0.3
github.com/stretchr/testify v1.7.0
)
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320/go.mod h1:EiZBM
github.com/hashicorp/go-getter v1.5.3 h1:NF5+zOlQegim+w/EUhSLh6QhXHmZMEeHLQzllkQ3ROU=
github.com/hashicorp/go-getter v1.5.3/go.mod h1:BrrV/1clo8cCYu6mxvboYg+KutTiFnXjMEgDD8+i7ZI=
github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd/go.mod h1:9bjs9uLqI8l75knNv3lV1kA55veR+WUPSiKIWcQHudI=
github.com/hashicorp/go-hclog v0.9.2/go.mod h1:5CU+agLiy3J7N7QjHK5d05KxGsuXiQLrjA0H7acj2lQ=
github.com/hashicorp/go-hclog v0.14.1/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
github.com/hashicorp/go-hclog v0.16.1 h1:IVQwpTGNRRIHafnTs2dQLIk4ENtneRIEEJWOVDqz99o=
github.com/hashicorp/go-hclog v0.16.1/go.mod h1:whpDNt7SSdeAju8AWKIWsul05p54N/39EeqMAyrmvFQ=
Expand All @@ -185,6 +186,8 @@ github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9
github.com/hashicorp/go-plugin v1.3.0/go.mod h1:F9eH4LrE/ZsRdbwhfjs9k9HoDUwAHnYtXdgmf1AVNs0=
github.com/hashicorp/go-plugin v1.4.1 h1:6UltRQlLN9iZO513VveELp5xyaFxVD2+1OVylE+2E+w=
github.com/hashicorp/go-plugin v1.4.1/go.mod h1:5fGEH17QVwTTcR0zV7yhDPLLmFX9YSZ38b18Udy6vYQ=
github.com/hashicorp/go-retryablehttp v0.7.0 h1:eu1EI/mbirUgP5C8hVsTNaGZreBDlYiwC1FZWkvQPQ4=
github.com/hashicorp/go-retryablehttp v0.7.0/go.mod h1:vAew36LZh98gCBJNLH42IQ1ER/9wtLZZ8meHqQvEYWY=
github.com/hashicorp/go-safetemp v1.0.0 h1:2HR189eFNrjHQyENnQMMpCiBAsRxzbTMIgBhEyExpmo=
github.com/hashicorp/go-safetemp v1.0.0/go.mod h1:oaerMy3BhqiTbVye6QuFhFtIceqFoDHxNAB65b+Rj1I=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
Expand Down Expand Up @@ -303,8 +306,8 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/russross/blackfriday v1.6.0 h1:KqfZb0pUVN2lYqZUYRddxF4OR8ZMURnJIG5Y3VRLtww=
github.com/russross/blackfriday v1.6.0/go.mod h1:ti0ldHuxg49ri4ksnFxlkCfN+hvslNlmVHqNRXXJNAY=
github.com/ryanwholey/go-pihole v0.0.2 h1:ge+62TQWMbbF9QPKiUWZCbyhPeLMOCg87H/XOosh2ng=
github.com/ryanwholey/go-pihole v0.0.2/go.mod h1:QQb3D17SHsK2dyq9sKbBCvjnDisDkahwbdIEsHmElUg=
github.com/ryanwholey/go-pihole v0.0.3 h1:Z7qHaCdmlzbrvGtnCAj/Cn3tv/sXTcVwZHay3f8e9Po=
github.com/ryanwholey/go-pihole v0.0.3/go.mod h1:phf6eVUv7QXC8OspfnIu6fGe+4jguTtPqyiMwh4AOBM=
github.com/sebdah/goldie v1.0.0/go.mod h1:jXP4hmWywNEwZzhMuv2ccnqTSFpuq8iyQhtQdkkZBH4=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
Expand Down
20 changes: 15 additions & 5 deletions internal/pihole/cname.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pihole
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/url"

Expand Down Expand Up @@ -33,7 +34,7 @@ type CNAMERecordList = pihole.CNAMERecordList
// ListCNAMERecords returns a list of the configured CNAME Pi-hole records
func (c Client) ListCNAMERecords(ctx context.Context) (CNAMERecordList, error) {
if c.tokenClient != nil {
return c.tokenClient.CNAME.List(ctx)
return c.tokenClient.LocalCNAME.List(ctx)
}

req, err := c.RequestWithSession(ctx, "POST", "/admin/scripts/pi-hole/php/customcname.php", &url.Values{
Expand Down Expand Up @@ -61,7 +62,16 @@ func (c Client) ListCNAMERecords(ctx context.Context) (CNAMERecordList, error) {
// GetCNAMERecord returns a CNAMERecord for the passed domain if found
func (c Client) GetCNAMERecord(ctx context.Context, domain string) (*CNAMERecord, error) {
if c.tokenClient != nil {
return c.tokenClient.CNAME.Read(ctx, domain)
record, err := c.tokenClient.LocalCNAME.Get(ctx, domain)
if err != nil {
if errors.Is(err, pihole.ErrorLocalCNAMENotFound) {
return nil, NewNotFoundError(fmt.Sprintf("cname with domain %q not found", domain))
}

return nil, err
}

return record, nil
}

list, err := c.ListCNAMERecords(ctx)
Expand All @@ -86,7 +96,7 @@ type CreateCNAMERecordResponse struct {
// CreateCNAMERecord handles CNAME record creation
func (c Client) CreateCNAMERecord(ctx context.Context, record *CNAMERecord) (*CNAMERecord, error) {
if c.tokenClient != nil {
return c.tokenClient.CNAME.Create(ctx, record.Domain, record.Target)
return c.tokenClient.LocalCNAME.Create(ctx, record.Domain, record.Target)
}

req, err := c.RequestWithSession(ctx, "POST", "/admin/scripts/pi-hole/php/customcname.php", &url.Values{
Expand Down Expand Up @@ -120,7 +130,7 @@ func (c Client) CreateCNAMERecord(ctx context.Context, record *CNAMERecord) (*CN
// DeleteCNAMERecord handles CNAME record deletion for the passed domain
func (c Client) DeleteCNAMERecord(ctx context.Context, domain string) error {
if c.tokenClient != nil {
return c.tokenClient.CNAME.Delete(ctx, domain)
return c.tokenClient.LocalCNAME.Delete(ctx, domain)
}

record, err := c.GetCNAMERecord(ctx, domain)
Expand Down Expand Up @@ -150,7 +160,7 @@ func (c Client) DeleteCNAMERecord(ctx context.Context, domain string) error {
// UpdateCNAMERecord handles updates for CNAME records
func (c Client) UpdateCNAMERecord(ctx context.Context, record *CNAMERecord) (*CNAMERecord, error) {
if c.tokenClient != nil {
return c.tokenClient.CNAME.Update(ctx, record.Domain, record.Target)
return c.tokenClient.LocalCNAME.Update(ctx, record.Domain, record.Target)
}

current, err := c.GetCNAMERecord(ctx, record.Domain)
Expand Down
20 changes: 15 additions & 5 deletions internal/pihole/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pihole
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/url"

Expand Down Expand Up @@ -33,7 +34,7 @@ type DNSRecord = pihole.DNSRecord
// ListDNSRecords Returns the list of custom DNS records configured in pihole
func (c Client) ListDNSRecords(ctx context.Context) (DNSRecordList, error) {
if c.tokenClient != nil {
return c.tokenClient.DNS.List(ctx)
return c.tokenClient.LocalDNS.List(ctx)
}

req, err := c.RequestWithSession(ctx, "POST", "/admin/scripts/pi-hole/php/customdns.php", &url.Values{
Expand Down Expand Up @@ -66,7 +67,7 @@ type CreateDNSRecordResponse struct {
// CreateDNSRecord creates a pihole DNS record entry
func (c Client) CreateDNSRecord(ctx context.Context, record *DNSRecord) (*DNSRecord, error) {
if c.tokenClient != nil {
return c.tokenClient.DNS.Create(ctx, record.Domain, record.IP)
return c.tokenClient.LocalDNS.Create(ctx, record.Domain, record.IP)
}

req, err := c.RequestWithSession(ctx, "POST", "/admin/scripts/pi-hole/php/customdns.php", &url.Values{
Expand Down Expand Up @@ -100,7 +101,16 @@ func (c Client) CreateDNSRecord(ctx context.Context, record *DNSRecord) (*DNSRec
// GetDNSRecord searches the pihole local DNS records for the passed domain and returns a result if found
func (c Client) GetDNSRecord(ctx context.Context, domain string) (*DNSRecord, error) {
if c.tokenClient != nil {
return c.tokenClient.DNS.Read(ctx, domain)
record, err := c.tokenClient.LocalDNS.Get(ctx, domain)
if err != nil {
if errors.Is(err, pihole.ErrorLocalDNSNotFound) {
return nil, NewNotFoundError(fmt.Sprintf("dns record with domain %q not found", domain))
}

return nil, err
}

return record, nil
}

list, err := c.ListDNSRecords(ctx)
Expand All @@ -120,7 +130,7 @@ func (c Client) GetDNSRecord(ctx context.Context, domain string) (*DNSRecord, er
// UpdateDNSRecord deletes a pihole local DNS record by domain name
func (c Client) UpdateDNSRecord(ctx context.Context, record *DNSRecord) (*DNSRecord, error) {
if c.tokenClient != nil {
return c.tokenClient.DNS.Update(ctx, record.Domain, record.IP)
return c.tokenClient.LocalDNS.Update(ctx, record.Domain, record.IP)
}

current, err := c.GetDNSRecord(ctx, record.Domain)
Expand All @@ -147,7 +157,7 @@ func (c Client) UpdateDNSRecord(ctx context.Context, record *DNSRecord) (*DNSRec
// DeleteDNSRecord deletes a pihole local DNS record by domain name
func (c Client) DeleteDNSRecord(ctx context.Context, domain string) error {
if c.tokenClient != nil {
return c.tokenClient.DNS.Delete(ctx, domain)
return c.tokenClient.LocalDNS.Delete(ctx, domain)
}

record, err := c.GetDNSRecord(ctx, domain)
Expand Down

0 comments on commit 58b0e37

Please sign in to comment.