diff --git a/go.mod b/go.mod index 1b8a2b9..5c505d3 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 47c2190..0c0532b 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/model_test.go b/model_test.go index 57d702c..1b5c960 100644 --- a/model_test.go +++ b/model_test.go @@ -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: "admin@example.com", + TTL: 60000, + Nameserver: "ns1.example.com", + DNSSEC: false, + DNSSECEmail: "dnssec@example.com", + }, + }, + } + + 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 { diff --git a/models.go b/models.go index eb810e4..b61a352 100644 --- a/models.go +++ b/models.go @@ -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 { @@ -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), diff --git a/provider.go b/provider.go index 5f3748a..eebcbfa 100644 --- a/provider.go +++ b/provider.go @@ -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) @@ -188,4 +212,5 @@ var ( _ libdns.RecordAppender = (*Provider)(nil) _ libdns.RecordSetter = (*Provider)(nil) _ libdns.RecordDeleter = (*Provider)(nil) + _ libdns.ZoneLister = (*Provider)(nil) ) diff --git a/provider_example.go b/provider_example.go index 94c2e48..c57e8e6 100644 --- a/provider_example.go +++ b/provider_example.go @@ -9,7 +9,7 @@ import ( func main() { provider := Provider{ - APIToken: "Your API Token", + APIToken: "Your API token", } //Set your zone with the domain @@ -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) + } } diff --git a/wrapper.go b/wrapper.go index ff38728..35a0b39 100644 --- a/wrapper.go +++ b/wrapper.go @@ -6,6 +6,10 @@ import ( "github.com/libdns/libdns" ) +type HosttechZoneListResponseWrapper struct { + Data []HosttechZone `json:"data"` +} + type HosttechListResponseWrapper struct { Data []HosttechRecordWrapper `json:"data"` }