github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/dns/authorities.go (about)

     1  package dns
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  )
     8  
     9  type (
    10  	// Authorities contains operations available on Authorities data sources.
    11  	Authorities interface {
    12  		// GetAuthorities provides a list of structured read-only list of name servers.
    13  		//
    14  		// See: https://techdocs.akamai.com/edge-dns/reference/get-data-authorities
    15  		GetAuthorities(context.Context, string) (*AuthorityResponse, error)
    16  		// GetNameServerRecordList provides a list of name server records.
    17  		//
    18  		// See: https://techdocs.akamai.com/edge-dns/reference/get-data-authorities
    19  		GetNameServerRecordList(context.Context, string) ([]string, error)
    20  	}
    21  
    22  	// Contract contains contractID and a list of currently assigned Akamai authoritative nameservers
    23  	Contract struct {
    24  		ContractID  string   `json:"contractId"`
    25  		Authorities []string `json:"authorities"`
    26  	}
    27  
    28  	// AuthorityResponse contains response with a list of one or more Contracts
    29  	AuthorityResponse struct {
    30  		Contracts []Contract `json:"contracts"`
    31  	}
    32  )
    33  
    34  func (d *dns) GetAuthorities(ctx context.Context, contractID string) (*AuthorityResponse, error) {
    35  	logger := d.Log(ctx)
    36  	logger.Debug("GetAuthorities")
    37  
    38  	if contractID == "" {
    39  		return nil, fmt.Errorf("%w: GetAuthorities reqs valid contractId", ErrBadRequest)
    40  	}
    41  
    42  	getURL := fmt.Sprintf("/config-dns/v2/data/authorities?contractIds=%s", contractID)
    43  
    44  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
    45  	if err != nil {
    46  		return nil, fmt.Errorf("failed to create getauthorities request: %w", err)
    47  	}
    48  
    49  	var result AuthorityResponse
    50  	resp, err := d.Exec(req, &result)
    51  	if err != nil {
    52  		return nil, fmt.Errorf("GetAuthorities request failed: %w", err)
    53  	}
    54  
    55  	if resp.StatusCode != http.StatusOK {
    56  		return nil, d.Error(resp)
    57  	}
    58  
    59  	return &result, nil
    60  }
    61  
    62  func (d *dns) GetNameServerRecordList(ctx context.Context, contractID string) ([]string, error) {
    63  	logger := d.Log(ctx)
    64  	logger.Debug("GetNameServerRecordList")
    65  
    66  	if contractID == "" {
    67  		return nil, fmt.Errorf("%w: GetAuthorities requires valid contractId", ErrBadRequest)
    68  	}
    69  
    70  	NSrecords, err := d.GetAuthorities(ctx, contractID)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  
    75  	var result []string
    76  	for _, r := range NSrecords.Contracts {
    77  		for _, n := range r.Authorities {
    78  			result = append(result, n)
    79  		}
    80  	}
    81  
    82  	return result, nil
    83  }