github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/edgeworkers/edgekv_groups.go (about) 1 package edgeworkers 2 3 import ( 4 "context" 5 "errors" 6 "fmt" 7 "net/http" 8 9 "github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/edgegriderr" 10 11 validation "github.com/go-ozzo/ozzo-validation/v4" 12 ) 13 14 type ( 15 // Groups is EdgeKV groups within a namespace API interface 16 Groups interface { 17 // ListGroupsWithinNamespace lists group identifiers created when writing items to a namespace 18 // 19 // See: https://techdocs.akamai.com/edgekv/reference/get-groups 20 ListGroupsWithinNamespace(context.Context, ListGroupsWithinNamespaceRequest) ([]string, error) 21 } 22 23 // ListGroupsWithinNamespaceRequest contains parameters used to get groups within a namespace 24 ListGroupsWithinNamespaceRequest struct { 25 Network NamespaceNetwork 26 NamespaceID string 27 } 28 ) 29 30 // Validate validates ListGroupsWithinNamespaceRequest 31 func (g ListGroupsWithinNamespaceRequest) Validate() error { 32 return edgegriderr.ParseValidationErrors(validation.Errors{ 33 "Network": validation.Validate(g.Network, validation.Required), 34 "NamespaceID": validation.Validate(g.NamespaceID, validation.Required), 35 }) 36 } 37 38 // ErrListGroupsWithinNamespace is returned in case an error occurs on ListGroupsWithinNamespace operation 39 var ErrListGroupsWithinNamespace = errors.New("list groups within namespace") 40 41 func (e *edgeworkers) ListGroupsWithinNamespace(ctx context.Context, params ListGroupsWithinNamespaceRequest) ([]string, error) { 42 logger := e.Log(ctx) 43 logger.Debug("ListGroupsWithinNamespace") 44 45 if err := params.Validate(); err != nil { 46 return nil, fmt.Errorf("%s: %w:\n%s", ErrListGroupsWithinNamespace, ErrStructValidation, err) 47 } 48 49 uri := fmt.Sprintf("/edgekv/v1/networks/%s/namespaces/%s/groups", params.Network, params.NamespaceID) 50 51 req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil) 52 if err != nil { 53 return nil, fmt.Errorf("%w: failed to create request: %s", ErrListGroupsWithinNamespace, err) 54 } 55 56 var result []string 57 resp, err := e.Exec(req, &result) 58 if err != nil { 59 return nil, fmt.Errorf("%w: request failed: %s", ErrListGroupsWithinNamespace, err) 60 } 61 62 if resp.StatusCode != http.StatusOK { 63 return nil, fmt.Errorf("%s: %w", ErrListGroupsWithinNamespace, e.Error(resp)) 64 } 65 66 return result, nil 67 }