|
| 1 | +package vultr |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "net/http" |
| 9 | + "net/netip" |
| 10 | + "net/url" |
| 11 | + |
| 12 | + "github.com/qdm12/ddns-updater/internal/provider/errors" |
| 13 | +) |
| 14 | + |
| 15 | +// https://www.vultr.com/api/#tag/dns/operation/list-dns-domain-records |
| 16 | +func (p *Provider) getRecord(ctx context.Context, client *http.Client, |
| 17 | + recordType string) (recordID string, recordIP netip.Addr, err error, |
| 18 | +) { |
| 19 | + u := url.URL{ |
| 20 | + Scheme: "https", |
| 21 | + Host: "api.vultr.com", |
| 22 | + Path: fmt.Sprintf("/v2/domains/%s/records", p.domain), |
| 23 | + } |
| 24 | + |
| 25 | + // max return of get records is 500 records |
| 26 | + values := url.Values{} |
| 27 | + values.Set("per_page", "500") |
| 28 | + u.RawQuery = values.Encode() |
| 29 | + |
| 30 | + request, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil) |
| 31 | + if err != nil { |
| 32 | + return "", netip.Addr{}, fmt.Errorf("creating http request: %w", err) |
| 33 | + } |
| 34 | + p.setHeaders(request) |
| 35 | + |
| 36 | + response, err := client.Do(request) |
| 37 | + if err != nil { |
| 38 | + return "", netip.Addr{}, err |
| 39 | + } |
| 40 | + |
| 41 | + bodyBytes, err := io.ReadAll(response.Body) |
| 42 | + if err != nil { |
| 43 | + _ = response.Body.Close() |
| 44 | + return "", netip.Addr{}, fmt.Errorf("reading response body: %w", err) |
| 45 | + } |
| 46 | + |
| 47 | + err = response.Body.Close() |
| 48 | + if err != nil { |
| 49 | + return "", netip.Addr{}, fmt.Errorf("closing response body: %w", err) |
| 50 | + } |
| 51 | + |
| 52 | + // todo: implement pagination |
| 53 | + var parsedJSON struct { |
| 54 | + Error string `json:"error"` |
| 55 | + Records []struct { |
| 56 | + ID string `json:"id"` |
| 57 | + Name string `json:"name"` |
| 58 | + Type string `json:"type"` |
| 59 | + Data string `json:"data"` |
| 60 | + } `json:"records"` |
| 61 | + Meta struct { |
| 62 | + Total uint32 `json:"total"` |
| 63 | + Links struct { |
| 64 | + Next string `json:"next"` |
| 65 | + Previous string `json:"prev"` |
| 66 | + } `json:"links"` |
| 67 | + } `json:"meta"` |
| 68 | + } |
| 69 | + err = json.Unmarshal(bodyBytes, &parsedJSON) |
| 70 | + switch { |
| 71 | + case err != nil: |
| 72 | + return "", netip.Addr{}, fmt.Errorf("json decoding response body: %w", err) |
| 73 | + case response.StatusCode == http.StatusBadRequest: |
| 74 | + return "", netip.Addr{}, fmt.Errorf("%w: %s", errors.ErrBadRequest, parsedJSON.Error) |
| 75 | + case response.StatusCode == http.StatusUnauthorized: |
| 76 | + return "", netip.Addr{}, fmt.Errorf("%w: %s", errors.ErrAuth, parsedJSON.Error) |
| 77 | + case response.StatusCode == http.StatusNotFound: |
| 78 | + return "", netip.Addr{}, fmt.Errorf("%w: %s", errors.ErrDomainNotFound, parsedJSON.Error) |
| 79 | + case response.StatusCode != http.StatusOK: |
| 80 | + return "", netip.Addr{}, fmt.Errorf("%w: %d: %s", |
| 81 | + errors.ErrHTTPStatusNotValid, response.StatusCode, parseJSONErrorOrFullBody(bodyBytes)) |
| 82 | + case parsedJSON.Error != "": |
| 83 | + return "", netip.Addr{}, fmt.Errorf("%w: %s", errors.ErrUnsuccessful, parsedJSON.Error) |
| 84 | + } |
| 85 | + |
| 86 | + // Status is OK (200) and error field is empty |
| 87 | + |
| 88 | + for _, record := range parsedJSON.Records { |
| 89 | + if record.Name != p.owner || record.Type != recordType { |
| 90 | + continue |
| 91 | + } |
| 92 | + recordIP, err = netip.ParseAddr(record.Data) |
| 93 | + if err != nil { |
| 94 | + return "", netip.Addr{}, fmt.Errorf("parsing existing IP: %w", err) |
| 95 | + } |
| 96 | + return record.ID, recordIP, nil |
| 97 | + } |
| 98 | + |
| 99 | + return "", netip.Addr{}, fmt.Errorf("%w: in %d records", errors.ErrRecordNotFound, len(parsedJSON.Records)) |
| 100 | +} |
0 commit comments