github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/api_endpoints.go (about) 1 package appsec 2 3 import ( 4 "context" 5 "fmt" 6 "net/http" 7 8 validation "github.com/go-ozzo/ozzo-validation/v4" 9 ) 10 11 type ( 12 // The ApiEndpoints interface supports retrieving the API endpoints associated with a security policy. 13 ApiEndpoints interface { 14 // GetApiEndpoints lists the API endpoints associated with a security policy. 15 // 16 // See: https://techdocs.akamai.com/application-security/reference/get-api-endpoints 17 GetApiEndpoints(ctx context.Context, params GetApiEndpointsRequest) (*GetApiEndpointsResponse, error) 18 } 19 20 // GetApiEndpointsRequest is used to retrieve the endpoints associated with a security policy. 21 GetApiEndpointsRequest struct { 22 ConfigID int `json:"-"` 23 Version int `json:"-"` 24 Name string `json:"-"` 25 PolicyID string `json:"-"` 26 ID int `json:"-"` 27 } 28 29 // GetApiEndpointsResponse is returned from a call to GetApiEndpoints. 30 GetApiEndpointsResponse struct { 31 APIEndpoints []struct { 32 ID int `json:"id"` 33 Name string `json:"name"` 34 BasePath string `json:"basePath"` 35 APIEndPointHosts []string `json:"apiEndPointHosts"` 36 StagingVersion struct { 37 Status string `json:"status"` 38 VersionNumber int `json:"versionNumber"` 39 } `json:"stagingVersion"` 40 ProductionVersion struct { 41 Status string `json:"status"` 42 VersionNumber int `json:"versionNumber"` 43 } `json:"productionVersion"` 44 RequestConstraintsEnabled bool `json:"requestConstraintsEnabled"` 45 } `json:"apiEndpoints"` 46 } 47 ) 48 49 // Validate validates a GetApiEndpointsRequest. 50 func (v GetApiEndpointsRequest) Validate() error { 51 return validation.Errors{ 52 "ConfigID": validation.Validate(v.ConfigID, validation.Required), 53 "Version": validation.Validate(v.Version, validation.Required), 54 }.Filter() 55 } 56 57 func (p *appsec) GetApiEndpoints(ctx context.Context, params GetApiEndpointsRequest) (*GetApiEndpointsResponse, error) { 58 logger := p.Log(ctx) 59 logger.Debug("GetApiEndpoints") 60 61 if err := params.Validate(); err != nil { 62 return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error()) 63 } 64 65 var uri string 66 if params.PolicyID != "" { 67 uri = fmt.Sprintf( 68 "/appsec/v1/configs/%d/versions/%d/security-policies/%s/api-endpoints", 69 params.ConfigID, 70 params.Version, 71 params.PolicyID) 72 } else { 73 uri = fmt.Sprintf( 74 "/appsec/v1/configs/%d/versions/%d/api-endpoints", 75 params.ConfigID, 76 params.Version, 77 ) 78 } 79 80 req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil) 81 if err != nil { 82 return nil, fmt.Errorf("failed to create GetApiEndpoints request: %w", err) 83 } 84 85 var result GetApiEndpointsResponse 86 resp, err := p.Exec(req, &result) 87 if err != nil { 88 return nil, fmt.Errorf("get API endpoints request failed: %w", err) 89 } 90 if resp.StatusCode != http.StatusOK { 91 return nil, p.Error(resp) 92 } 93 94 if params.Name != "" { 95 var filteredResult GetApiEndpointsResponse 96 for _, val := range result.APIEndpoints { 97 if val.Name == params.Name { 98 filteredResult.APIEndpoints = append(filteredResult.APIEndpoints, val) 99 } 100 } 101 return &filteredResult, nil 102 } 103 104 return &result, nil 105 }