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

     1  package appsec
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/http"
     7  )
     8  
     9  type (
    10  	// The ContractsGroups interface supports listing the contracts and groups for the current
    11  	// account. Each object contains the contract, groups associated with the contract, and whether
    12  	// Kona Site Defender or Web Application Protector is the product for that contract.
    13  	ContractsGroups interface {
    14  		// GetContractsGroups lists the contracts and groups for your account.
    15  		//
    16  		// See: https://techdocs.akamai.com/application-security/reference/get-contracts-groups
    17  		GetContractsGroups(ctx context.Context, params GetContractsGroupsRequest) (*GetContractsGroupsResponse, error)
    18  	}
    19  
    20  	// GetContractsGroupsRequest is used to retrieve the list of contracts and groups for your account.
    21  	GetContractsGroupsRequest struct {
    22  		ConfigID   int    `json:"-"`
    23  		Version    int    `json:"-"`
    24  		PolicyID   string `json:"-"`
    25  		ContractID string `json:"-"`
    26  		GroupID    int    `json:"-"`
    27  	}
    28  
    29  	// GetContractsGroupsResponse is returned from a call to GetContractsGroups.
    30  	GetContractsGroupsResponse struct {
    31  		ContractGroups []struct {
    32  			ContractID  string `json:"contractId"`
    33  			DisplayName string `json:"displayName"`
    34  			GroupID     int    `json:"groupId"`
    35  		} `json:"contract_groups"`
    36  	}
    37  )
    38  
    39  func (p *appsec) GetContractsGroups(ctx context.Context, params GetContractsGroupsRequest) (*GetContractsGroupsResponse, error) {
    40  	logger := p.Log(ctx)
    41  	logger.Debug("GetContractsGroups")
    42  
    43  	uri :=
    44  		"/appsec/v1/contracts-groups"
    45  
    46  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
    47  	if err != nil {
    48  		return nil, fmt.Errorf("failed to create GetContractsGroups request: %w", err)
    49  	}
    50  
    51  	var result GetContractsGroupsResponse
    52  	resp, err := p.Exec(req, &result)
    53  	if err != nil {
    54  		return nil, fmt.Errorf("get contracts groups request failed: %w", err)
    55  	}
    56  	if resp.StatusCode != http.StatusOK {
    57  		return nil, p.Error(resp)
    58  	}
    59  
    60  	if params.GroupID != 0 {
    61  		var filteredResult GetContractsGroupsResponse
    62  		for _, val := range result.ContractGroups {
    63  			if val.ContractID == params.ContractID && val.GroupID == params.GroupID {
    64  				filteredResult.ContractGroups = append(filteredResult.ContractGroups, val)
    65  			}
    66  		}
    67  		return &filteredResult, nil
    68  	}
    69  
    70  	return &result, nil
    71  }