data.go raw
1 package dns
2
3 import (
4 "context"
5 "errors"
6 "fmt"
7 "net/http"
8 "net/url"
9
10 "github.com/akamai/AkamaiOPEN-edgegrid-golang/v11/pkg/session"
11 )
12
13 type (
14 // ListGroupResponse lists the groups accessible to the current user
15 ListGroupResponse struct {
16 Groups []Group `json:"groups"`
17 }
18
19 // ListGroupRequest is a request struct
20 ListGroupRequest struct {
21 GroupID string
22 }
23
24 // Group contain the information of the particular group
25 Group struct {
26 GroupID int `json:"groupId"`
27 GroupName string `json:"groupName"`
28 ContractIDs []string `json:"contractIds"`
29 Permissions []string `json:"permissions"`
30 }
31 )
32
33 var (
34 // ErrListGroups is returned in case an error occurs on ListGroups operation
35 ErrListGroups = errors.New("list groups")
36 )
37
38 func (d *dns) ListGroups(ctx context.Context, params ListGroupRequest) (*ListGroupResponse, error) {
39 logger := d.Log(ctx)
40 logger.Debug("ListGroups")
41
42 uri, err := url.Parse("/config-dns/v2/data/groups")
43 if err != nil {
44 return nil, fmt.Errorf("%w: failed to parse url: %s", ErrListGroups, err)
45 }
46
47 q := uri.Query()
48 if params.GroupID != "" {
49 q.Add("gid", params.GroupID)
50 }
51 uri.RawQuery = q.Encode()
52
53 req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri.String(), nil)
54 if err != nil {
55 return nil, fmt.Errorf("failed to list listZoneGroups request: %w", err)
56 }
57
58 var result ListGroupResponse
59 resp, err := d.Exec(req, &result)
60 if err != nil {
61 return nil, fmt.Errorf("ListZoneGroups request failed: %w", err)
62 }
63 defer session.CloseResponseBody(resp)
64
65 if resp.StatusCode != http.StatusOK {
66 return nil, d.Error(resp)
67 }
68
69 return &result, nil
70 }
71