Skip to content

Commit

Permalink
Merge pull request #10 from libdns/feature/upgrade-to-libdns-0.2.2
Browse files Browse the repository at this point in the history
Feature/upgrade to libdns 0.2.2
  • Loading branch information
MasterEvarior authored Apr 19, 2024
2 parents da232fd + 0a1c8ae commit d0316c1
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 3 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/libdns/hosttech
go 1.18

require (
github.com/libdns/libdns v0.2.1
github.com/libdns/libdns v0.2.2
github.com/stretchr/testify v1.9.0
)

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/libdns/libdns v0.2.1 h1:Wu59T7wSHRgtA0cfxC+n1c/e+O3upJGWytknkmFEDis=
github.com/libdns/libdns v0.2.1/go.mod h1:yQCXzk1lEZmmCPa857bnk4TsOiqYasqpyOEeSObbb40=
github.com/libdns/libdns v0.2.2 h1:O6ws7bAfRPaBsgAYt8MDe2HcNBGC29hkZ9MX2eUSX3s=
github.com/libdns/libdns v0.2.2/go.mod h1:4Bj9+5CQiNMVGf87wjX4CY3HQJypUHRuLvlsfsZqLWQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
Expand Down
28 changes: 28 additions & 0 deletions model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,34 @@ import (
"time"
)

func TestHosttechZoneToLibdnsZone(t *testing.T) {
input := map[string]struct {
expectedResult libdns.Zone
data HosttechZone
}{
"Example": {
expectedResult: libdns.Zone{Name: "example.com"},
data: HosttechZone{
Id: 1,
Name: "example.com",
Email: "[email protected]",
TTL: 60000,
Nameserver: "ns1.example.com",
DNSSEC: false,
DNSSECEmail: "[email protected]",
},
},
}

for name, testStruct := range input {
t.Run(name, func(t *testing.T) {
output := testStruct.data.toLibdnsZone()

assert.Equal(t, testStruct.expectedResult, output)
})
}
}

func TestHosttechRecordToLibdnsRecord(t *testing.T) {
zone := "example.com"
input := map[string]struct {
Expand Down
19 changes: 18 additions & 1 deletion models.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ type MXRecord struct {
Base
Name string `json:"name,omitempty"`
OwnerName string `json:"ownername,omitempty"`
Pref int `json:"pref,omitempty"`
Pref uint `json:"pref,omitempty"`
}

func (m MXRecord) toLibdnsRecord(zone string) libdns.Record {
Expand Down Expand Up @@ -193,6 +193,23 @@ type TLSARecord struct {
Text string `json:"text,omitempty"`
}

// HosttechZone is an implementation of the zone without records
type HosttechZone struct {
Id uint `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Email string `json:"email,omitempty"`
TTL uint32 `json:"ttl,omitempty"`
Nameserver string `json:"nameserver,omitempty"`
DNSSEC bool `json:"dnssec,omitempty"`
DNSSECEmail string `json:"dnssec_email,omitempty"`
}

func (z HosttechZone) toLibdnsZone() libdns.Zone {
return libdns.Zone{
Name: z.Name,
}
}

func (t TLSARecord) toLibdnsRecord(zone string) libdns.Record {
return libdns.Record{
ID: strconv.Itoa(t.Id),
Expand Down
25 changes: 25 additions & 0 deletions provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,30 @@ func (p *Provider) DeleteRecords(ctx context.Context, zone string, records []lib
return successfullyDeletedRecords, nil
}

// List all available zones
func (p *Provider) ListZones(ctx context.Context) ([]libdns.Zone, error) {
reqUrl := fmt.Sprintf("%s/zones", apiHost)
responseBody, err := p.makeApiCall(ctx, http.MethodGet, reqUrl, nil)

if err != nil {
return nil, err
}

var parsedResponse = HosttechZoneListResponseWrapper{}
err = json.Unmarshal(responseBody, &parsedResponse)

if err != nil {
return nil, err
}

var libdnsZones []libdns.Zone
for _, zone := range parsedResponse.Data {
libdnsZones = append(libdnsZones, zone.toLibdnsZone())
}

return libdnsZones, nil
}

func (p *Provider) makeApiCall(ctx context.Context, httpMethod string, reqUrl string, body io.Reader) (response []byte, err error) {
req, err := http.NewRequestWithContext(ctx, httpMethod, reqUrl, body)
req.Header.Set("Authorization", "Bearer "+p.APIToken)
Expand Down Expand Up @@ -188,4 +212,5 @@ var (
_ libdns.RecordAppender = (*Provider)(nil)
_ libdns.RecordSetter = (*Provider)(nil)
_ libdns.RecordDeleter = (*Provider)(nil)
_ libdns.ZoneLister = (*Provider)(nil)
)
12 changes: 11 additions & 1 deletion provider_example.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func main() {
provider := Provider{
APIToken: "Your API Token",
APIToken: "Your API token",
}

//Set your zone with the domain
Expand Down Expand Up @@ -53,4 +53,14 @@ func main() {
fmt.Println("Deleted record: ", deletedRecord)
}
}

//List all available zones
allZones, err := provider.ListZones(context.Background())
if err != nil {
fmt.Print(err.Error())
}

for _, zone := range allZones {
fmt.Println("Zone: ", zone.Name)
}
}
4 changes: 4 additions & 0 deletions wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
"github.com/libdns/libdns"
)

type HosttechZoneListResponseWrapper struct {
Data []HosttechZone `json:"data"`
}

type HosttechListResponseWrapper struct {
Data []HosttechRecordWrapper `json:"data"`
}
Expand Down

0 comments on commit d0316c1

Please sign in to comment.