github.com/akamai/AkamaiOPEN-edgegrid-golang/v5@v5.0.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  		// NewAuthorityResponse instantiate authority response.
    21  		NewAuthorityResponse(context.Context, string) *AuthorityResponse
    22  	}
    23  
    24  	// Contract contains contractID and a list of currently assigned Akamai authoritative nameservers
    25  	Contract struct {
    26  		ContractID  string   `json:"contractId"`
    27  		Authorities []string `json:"authorities"`
    28  	}
    29  
    30  	// AuthorityResponse contains response with a list of one or more Contracts
    31  	AuthorityResponse struct {
    32  		Contracts []Contract `json:"contracts"`
    33  	}
    34  )
    35  
    36  func (p *dns) NewAuthorityResponse(ctx context.Context, _ string) *AuthorityResponse {
    37  
    38  	logger := p.Log(ctx)
    39  	logger.Debug("NewAuthorityResponse")
    40  
    41  	authorities := &AuthorityResponse{}
    42  	return authorities
    43  }
    44  
    45  func (p *dns) GetAuthorities(ctx context.Context, contractID string) (*AuthorityResponse, error) {
    46  
    47  	logger := p.Log(ctx)
    48  	logger.Debug("GetAuthorities")
    49  
    50  	if contractID == "" {
    51  		return nil, fmt.Errorf("%w: GetAuthorities reqs valid contractId", ErrBadRequest)
    52  	}
    53  
    54  	getURL := fmt.Sprintf("/config-dns/v2/data/authorities?contractIds=%s", contractID)
    55  
    56  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, getURL, nil)
    57  	if err != nil {
    58  		return nil, fmt.Errorf("failed to create getauthorities request: %w", err)
    59  	}
    60  
    61  	var authNames AuthorityResponse
    62  	resp, err := p.Exec(req, &authNames)
    63  	if err != nil {
    64  		return nil, fmt.Errorf("getauthorities request failed: %w", err)
    65  	}
    66  
    67  	if resp.StatusCode != http.StatusOK {
    68  		return nil, p.Error(resp)
    69  	}
    70  
    71  	return &authNames, nil
    72  }
    73  
    74  func (p *dns) GetNameServerRecordList(ctx context.Context, contractID string) ([]string, error) {
    75  
    76  	logger := p.Log(ctx)
    77  	logger.Debug("GetNameServerRecordList")
    78  
    79  	if contractID == "" {
    80  		return nil, fmt.Errorf("%w: GetAuthorities reqs valid contractId", ErrBadRequest)
    81  	}
    82  
    83  	NSrecords, err := p.GetAuthorities(ctx, contractID)
    84  
    85  	if err != nil {
    86  		return nil, err
    87  	}
    88  
    89  	var arrLength int
    90  	for _, c := range NSrecords.Contracts {
    91  		arrLength = len(c.Authorities)
    92  	}
    93  
    94  	ns := make([]string, 0, arrLength)
    95  
    96  	for _, r := range NSrecords.Contracts {
    97  		for _, n := range r.Authorities {
    98  			ns = append(ns, n)
    99  		}
   100  	}
   101  	return ns, nil
   102  }