| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- package api
- import "context"
- // Task codes, from the AutoDNS documentation.
- const (
- // TaskZoneInquire reads one zone or lists all of them.
- TaskZoneInquire = "0205"
- // TaskZoneUpdateBulk adds and removes records in one job.
- TaskZoneUpdateBulk = "0202001"
- )
- // defaultListLimit is how many zones one list call asks for. A customer with
- // more zones than this needs paging, which Zones does transparently.
- const defaultListLimit = 500
- // ZoneSummary is a zone without its records.
- type ZoneSummary struct {
- // Name is the zone itself, for example example.com.
- Name string
- // SystemNS is the name server that serves the zone. It is mandatory on
- // update tasks, so it is carried through from the list.
- SystemNS string
- }
- // Zones lists every zone the account can see. It pages until the gateway
- // returns fewer zones than requested.
- func (c *Client) Zones(ctx context.Context) ([]ZoneSummary, error) {
- var out []ZoneSummary
- for offset := 0; ; offset += defaultListLimit {
- task := Task{
- Code: TaskZoneInquire,
- View: &View{Offset: offset, Limit: defaultListLimit, Children: 1},
- Key: []string{"name", "system_ns"},
- }
- _, result, err := c.run(ctx, task)
- if err != nil {
- return nil, err
- }
- if result.Data == nil || len(result.Data.Zone) == 0 {
- return out, nil
- }
- for _, z := range result.Data.Zone {
- out = append(out, ZoneSummary{Name: z.Name, SystemNS: z.SystemNS})
- }
- if len(result.Data.Zone) < defaultListLimit {
- return out, nil
- }
- }
- }
- // Zone returns one zone including every record in it.
- func (c *Client) Zone(ctx context.Context, name string) (*Zone, error) {
- task := Task{
- Code: TaskZoneInquire,
- Zone: &ZoneRef{Name: name},
- Key: []string{"name", "system_ns", "soa"},
- }
- _, result, err := c.run(ctx, task)
- if err != nil {
- return nil, err
- }
- if result.Data == nil || len(result.Data.Zone) == 0 {
- return nil, &APIError{Code: "EF02020", Text: "no such zone exists", Object: "zone " + name}
- }
- zone := result.Data.Zone[0]
- return &zone, nil
- }
- // Records returns the records of a zone, optionally filtered by type. An empty
- // type returns every record.
- func (c *Client) Records(ctx context.Context, zone, recordType string) ([]Record, error) {
- z, err := c.Zone(ctx, zone)
- if err != nil {
- return nil, err
- }
- if recordType == "" {
- return z.RRs, nil
- }
- out := make([]Record, 0, len(z.RRs))
- for _, rr := range z.RRs {
- if equalFold(rr.Type, recordType) {
- out = append(out, rr)
- }
- }
- return out, nil
- }
|