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