github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/papi/group.go (about)

     1  package papi
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"fmt"
     7  	"net/http"
     8  )
     9  
    10  type (
    11  	// Groups contains operations available on Group resource
    12  	Groups interface {
    13  		// GetGroups provides a read-only list of groups, which may contain properties.
    14  		//
    15  		// See: https://techdocs.akamai.com/property-mgr/reference/get-groups
    16  		GetGroups(context.Context) (*GetGroupsResponse, error)
    17  	}
    18  
    19  	// Group represents a property group resource
    20  	Group struct {
    21  		GroupID       string   `json:"groupId"`
    22  		GroupName     string   `json:"groupName"`
    23  		ParentGroupID string   `json:"parentGroupId,omitempty"`
    24  		ContractIDs   []string `json:"contractIds"`
    25  	}
    26  
    27  	// GroupItems represents sub-component of the group response
    28  	GroupItems struct {
    29  		Items []*Group `json:"items"`
    30  	}
    31  
    32  	// GetGroupsResponse represents a collection of groups
    33  	// This is the response to the /papi/v1/groups request
    34  	GetGroupsResponse struct {
    35  		AccountID   string     `json:"accountId"`
    36  		AccountName string     `json:"accountName"`
    37  		Groups      GroupItems `json:"groups"`
    38  	}
    39  )
    40  
    41  var (
    42  	// ErrGetGroups represents error when fetching groups fails
    43  	ErrGetGroups = errors.New("fetching groups")
    44  )
    45  
    46  func (p *papi) GetGroups(ctx context.Context) (*GetGroupsResponse, error) {
    47  	var groups GetGroupsResponse
    48  
    49  	logger := p.Log(ctx)
    50  	logger.Debug("GetGroups")
    51  
    52  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, "/papi/v1/groups", nil)
    53  	if err != nil {
    54  		return nil, fmt.Errorf("%w: failed to create request: %s", ErrGetGroups, err)
    55  	}
    56  
    57  	resp, err := p.Exec(req, &groups)
    58  	if err != nil {
    59  		return nil, fmt.Errorf("%w: request failed: %s", ErrGetGroups, err)
    60  	}
    61  
    62  	if resp.StatusCode != http.StatusOK {
    63  		return nil, fmt.Errorf("%s: %w", ErrGetGroups, p.Error(resp))
    64  	}
    65  
    66  	return &groups, nil
    67  }