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