authorities.go raw

   1  package dns
   2  
   3  import (
   4  	"context"
   5  	"errors"
   6  	"fmt"
   7  	"net/http"
   8  
   9  	"github.com/akamai/AkamaiOPEN-edgegrid-golang/v11/pkg/edgegriderr"
  10  	"github.com/akamai/AkamaiOPEN-edgegrid-golang/v11/pkg/session"
  11  	validation "github.com/go-ozzo/ozzo-validation/v4"
  12  )
  13  
  14  type (
  15  	// Contract contains contractID and a list of currently assigned Akamai authoritative nameservers
  16  	Contract struct {
  17  		ContractID  string   `json:"contractId"`
  18  		Authorities []string `json:"authorities"`
  19  	}
  20  	// AuthorityResponse contains response with a list of one or more Contracts
  21  	AuthorityResponse struct {
  22  		Contracts []Contract `json:"contracts"`
  23  	}
  24  	// GetAuthoritiesRequest contains request parameters for GetAuthorities
  25  	GetAuthoritiesRequest struct {
  26  		ContractIDs string
  27  	}
  28  
  29  	// GetAuthoritiesResponse contains the response data from GetAuthorities operation
  30  	GetAuthoritiesResponse struct {
  31  		Contracts []Contract `json:"contracts"`
  32  	}
  33  
  34  	// GetNameServerRecordListRequest contains request parameters for GetNameServerRecordList
  35  	GetNameServerRecordListRequest struct {
  36  		ContractIDs string
  37  	}
  38  )
  39  
  40  var (
  41  	// ErrGetAuthorities is returned when GetAuthorities fails
  42  	ErrGetAuthorities = errors.New("get authorities")
  43  	// ErrGetNameServerRecordList is returned when GetNameServerRecordList fails
  44  	ErrGetNameServerRecordList = errors.New("get name server record list")
  45  )
  46  
  47  // Validate validates GetAuthoritiesRequest
  48  func (r GetAuthoritiesRequest) Validate() error {
  49  	return edgegriderr.ParseValidationErrors(validation.Errors{
  50  		"ContractIDs": validation.Validate(r.ContractIDs, validation.Required),
  51  	})
  52  }
  53  
  54  // Validate validates GetNameServerRecordListRequest
  55  func (r GetNameServerRecordListRequest) Validate() error {
  56  	return edgegriderr.ParseValidationErrors(validation.Errors{
  57  		"ContractIDs": validation.Validate(r.ContractIDs, validation.Required),
  58  	})
  59  }
  60  
  61  func (d *dns) GetAuthorities(ctx context.Context, params GetAuthoritiesRequest) (*GetAuthoritiesResponse, error) {
  62  	logger := d.Log(ctx)
  63  	logger.Debug("GetAuthorities")
  64  
  65  	if err := params.Validate(); err != nil {
  66  		return nil, fmt.Errorf("%s: %w: %s", ErrGetAuthorities, ErrStructValidation, err)
  67  	}
  68  
  69  	getURL := fmt.Sprintf("/config-dns/v2/data/authorities?contractIds=%s", params.ContractIDs)
  70  
  71  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
  72  	if err != nil {
  73  		return nil, fmt.Errorf("failed to create getauthorities request: %w", err)
  74  	}
  75  
  76  	var result GetAuthoritiesResponse
  77  	resp, err := d.Exec(req, &result)
  78  	if err != nil {
  79  		return nil, fmt.Errorf("GetAuthorities request failed: %w", err)
  80  	}
  81  	defer session.CloseResponseBody(resp)
  82  
  83  	if resp.StatusCode != http.StatusOK {
  84  		return nil, d.Error(resp)
  85  	}
  86  
  87  	return &result, nil
  88  }
  89  
  90  func (d *dns) GetNameServerRecordList(ctx context.Context, params GetNameServerRecordListRequest) ([]string, error) {
  91  	logger := d.Log(ctx)
  92  	logger.Debug("GetNameServerRecordList")
  93  
  94  	if err := params.Validate(); err != nil {
  95  		return nil, fmt.Errorf("%s: %w: %s", ErrGetNameServerRecordList, ErrStructValidation, err)
  96  	}
  97  
  98  	NSrecords, err := d.GetAuthorities(ctx, GetAuthoritiesRequest(params))
  99  	if err != nil {
 100  		return nil, err
 101  	}
 102  
 103  	var result []string
 104  	for _, r := range NSrecords.Contracts {
 105  		result = append(result, r.Authorities...)
 106  	}
 107  
 108  	return result, nil
 109  }
 110