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