-
Notifications
You must be signed in to change notification settings - Fork 70
/
api_endpoints.go
108 lines (94 loc) · 3.14 KB
/
api_endpoints.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package appsec
import (
"context"
"fmt"
"net/http"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v9/pkg/session"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
type (
// The ApiEndpoints interface supports retrieving the API endpoints associated with a security policy.
ApiEndpoints interface {
// GetApiEndpoints lists the API endpoints associated with a security policy.
//
// See: https://techdocs.akamai.com/application-security/reference/get-api-endpoints
GetApiEndpoints(ctx context.Context, params GetApiEndpointsRequest) (*GetApiEndpointsResponse, error)
}
// GetApiEndpointsRequest is used to retrieve the endpoints associated with a security policy.
GetApiEndpointsRequest struct {
ConfigID int `json:"-"`
Version int `json:"-"`
Name string `json:"-"`
PolicyID string `json:"-"`
ID int `json:"-"`
}
// GetApiEndpointsResponse is returned from a call to GetApiEndpoints.
GetApiEndpointsResponse struct {
APIEndpoints []struct {
ID int `json:"id"`
Name string `json:"name"`
BasePath string `json:"basePath"`
APIEndPointHosts []string `json:"apiEndPointHosts"`
StagingVersion struct {
Status string `json:"status"`
VersionNumber int `json:"versionNumber"`
} `json:"stagingVersion"`
ProductionVersion struct {
Status string `json:"status"`
VersionNumber int `json:"versionNumber"`
} `json:"productionVersion"`
RequestConstraintsEnabled bool `json:"requestConstraintsEnabled"`
} `json:"apiEndpoints"`
}
)
// Validate validates a GetApiEndpointsRequest.
func (v GetApiEndpointsRequest) Validate() error {
return validation.Errors{
"ConfigID": validation.Validate(v.ConfigID, validation.Required),
"Version": validation.Validate(v.Version, validation.Required),
}.Filter()
}
func (p *appsec) GetApiEndpoints(ctx context.Context, params GetApiEndpointsRequest) (*GetApiEndpointsResponse, error) {
logger := p.Log(ctx)
logger.Debug("GetApiEndpoints")
if err := params.Validate(); err != nil {
return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
}
var uri string
if params.PolicyID != "" {
uri = fmt.Sprintf(
"/appsec/v1/configs/%d/versions/%d/security-policies/%s/api-endpoints",
params.ConfigID,
params.Version,
params.PolicyID)
} else {
uri = fmt.Sprintf(
"/appsec/v1/configs/%d/versions/%d/api-endpoints",
params.ConfigID,
params.Version,
)
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
if err != nil {
return nil, fmt.Errorf("failed to create GetApiEndpoints request: %w", err)
}
var result GetApiEndpointsResponse
resp, err := p.Exec(req, &result)
if err != nil {
return nil, fmt.Errorf("get API endpoints request failed: %w", err)
}
defer session.CloseResponseBody(resp)
if resp.StatusCode != http.StatusOK {
return nil, p.Error(resp)
}
if params.Name != "" {
var filteredResult GetApiEndpointsResponse
for _, val := range result.APIEndpoints {
if val.Name == params.Name {
filteredResult.APIEndpoints = append(filteredResult.APIEndpoints, val)
}
}
return &filteredResult, nil
}
return &result, nil
}