File: authorities.go

package info (click to toggle)
golang-github-akamai-akamaiopen-edgegrid-golang 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,408 kB
  • sloc: sh: 532; makefile: 3
file content (77 lines) | stat: -rw-r--r-- 1,573 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package dnsv2

import (
	"github.com/akamai/AkamaiOPEN-edgegrid-golang/client-v1"
	edge "github.com/akamai/AkamaiOPEN-edgegrid-golang/edgegrid"
)

type AuthorityResponse struct {
	Contracts []struct {
		ContractID  string   `json:"contractId"`
		Authorities []string `json:"authorities"`
	} `json:"contracts"`
}

func NewAuthorityResponse(contract string) *AuthorityResponse {
	authorities := &AuthorityResponse{}
	return authorities
}

func GetAuthorities(contractId string) (*AuthorityResponse, error) {
	authorities := NewAuthorityResponse(contractId)

	req, err := client.NewRequest(
		Config,
		"GET",
		"/config-dns/v2/data/authorities?contractIds="+contractId,
		nil,
	)
	if err != nil {
		return nil, err
	}

	edge.PrintHttpRequest(req, true)

	res, err := client.Do(Config, req)
	if err != nil {
		return nil, err
	}

	edge.PrintHttpResponse(res, true)

	if client.IsError(res) && res.StatusCode != 404 {
		return nil, client.NewAPIError(res)
	} else if res.StatusCode == 404 {
		return nil, &ZoneError{zoneName: contractId}
	} else {

		err = client.BodyJSON(res, authorities)
		if err != nil {
			return nil, err
		}
		return authorities, nil
	}
}

func GetNameServerRecordList(contractId string) ([]string, error) {

	NSrecords, err := GetAuthorities(contractId)

	if err != nil {
		return nil, err
	}

	var arrLength int
	for _, c := range NSrecords.Contracts {
		arrLength = len(c.Authorities)
	}

	ns := make([]string, 0, arrLength)

	for _, r := range NSrecords.Contracts {
		for _, n := range r.Authorities {
			ns = append(ns, n)
		}
	}
	return ns, nil
}