github.com/akamai/AkamaiOPEN-edgegrid-golang/v5@v5.0.0/pkg/appsec/rate_protection.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 RateProtection interface supports retrieving and updating rate protection for a configuration and policy. 13 RateProtection interface { 14 // GetRateProtections retrieves the current rate protection setting for a configuration and policy. 15 // 16 // See: https://techdocs.akamai.com/application-security/reference/get-policy-protections 17 // Deprecated: this method will be removed in a future release. Use GetRateProtection instead. 18 GetRateProtections(ctx context.Context, params GetRateProtectionsRequest) (*GetRateProtectionsResponse, error) 19 20 // GetRateProtection retrieves the current rate protection setting for a configuration and policy. 21 // 22 // See: https://techdocs.akamai.com/application-security/reference/get-policy-protections 23 GetRateProtection(ctx context.Context, params GetRateProtectionRequest) (*GetRateProtectionResponse, error) 24 25 // UpdateRateProtection updates the rate protection setting for a configuration and policy. 26 // 27 // See: https://techdocs.akamai.com/application-security/reference/put-policy-protections 28 UpdateRateProtection(ctx context.Context, params UpdateRateProtectionRequest) (*UpdateRateProtectionResponse, error) 29 } 30 31 // GetRateProtectionRequest is used to retrieve the rate protection setting. 32 GetRateProtectionRequest struct { 33 ConfigID int `json:"-"` 34 Version int `json:"-"` 35 PolicyID string `json:"-"` 36 ApplyRateControls bool `json:"applyRateControls"` 37 } 38 39 // GetRateProtectionResponse is returned from a call to GetRateProtection. 40 GetRateProtectionResponse ProtectionsResponse 41 42 // GetRateProtectionsRequest is used to retrieve the rate protection setting. 43 // Deprecated: this struct will be removed in a future release. 44 GetRateProtectionsRequest struct { 45 ConfigID int `json:"-"` 46 Version int `json:"-"` 47 PolicyID string `json:"-"` 48 ApplyRateControls bool `json:"applyRateControls"` 49 } 50 51 // GetRateProtectionsResponse is returned from a call to GetRateProtection. 52 // Deprecated: this struct will be removed in a future release. 53 GetRateProtectionsResponse ProtectionsResponse 54 55 // UpdateRateProtectionRequest is used to modify the rate protection setting. 56 UpdateRateProtectionRequest struct { 57 ConfigID int `json:"-"` 58 Version int `json:"-"` 59 PolicyID string `json:"-"` 60 ApplyRateControls bool `json:"applyRateControls"` 61 } 62 63 // UpdateRateProtectionResponse is returned from a call to UpdateRateProtection. 64 UpdateRateProtectionResponse ProtectionsResponse 65 ) 66 67 // Validate validates a GetRateProtectionRequest. 68 func (v GetRateProtectionRequest) Validate() error { 69 return validation.Errors{ 70 "ConfigID": validation.Validate(v.ConfigID, validation.Required), 71 "Version": validation.Validate(v.Version, validation.Required), 72 "PolicyID": validation.Validate(v.PolicyID, validation.Required), 73 }.Filter() 74 } 75 76 // Validate validates a GetRateProtectionsRequest. 77 func (v GetRateProtectionsRequest) Validate() error { 78 return validation.Errors{ 79 "ConfigID": validation.Validate(v.ConfigID, validation.Required), 80 "Version": validation.Validate(v.Version, validation.Required), 81 "PolicyID": validation.Validate(v.PolicyID, validation.Required), 82 }.Filter() 83 } 84 85 // Validate validates an UpdateRateProtectionRequest. 86 func (v UpdateRateProtectionRequest) Validate() error { 87 return validation.Errors{ 88 "ConfigID": validation.Validate(v.ConfigID, validation.Required), 89 "Version": validation.Validate(v.Version, validation.Required), 90 "PolicyID": validation.Validate(v.PolicyID, validation.Required), 91 }.Filter() 92 } 93 94 func (p *appsec) GetRateProtection(ctx context.Context, params GetRateProtectionRequest) (*GetRateProtectionResponse, error) { 95 logger := p.Log(ctx) 96 logger.Debug("GetRateProtection") 97 98 if err := params.Validate(); err != nil { 99 return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error()) 100 } 101 102 uri := fmt.Sprintf( 103 "/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections", 104 params.ConfigID, 105 params.Version, 106 params.PolicyID) 107 108 req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil) 109 if err != nil { 110 return nil, fmt.Errorf("failed to create GetRateProtection request: %w", err) 111 } 112 113 var result GetRateProtectionResponse 114 resp, err := p.Exec(req, &result) 115 if err != nil { 116 return nil, fmt.Errorf("get rate protection request failed: %w", err) 117 } 118 if resp.StatusCode != http.StatusOK { 119 return nil, p.Error(resp) 120 } 121 122 return &result, nil 123 } 124 125 func (p *appsec) GetRateProtections(ctx context.Context, params GetRateProtectionsRequest) (*GetRateProtectionsResponse, error) { 126 logger := p.Log(ctx) 127 logger.Debug("GetRateProtections") 128 129 if err := params.Validate(); err != nil { 130 return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error()) 131 } 132 133 uri := fmt.Sprintf( 134 "/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections", 135 params.ConfigID, 136 params.Version, 137 params.PolicyID) 138 139 req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil) 140 if err != nil { 141 return nil, fmt.Errorf("failed to create GetRateProtections request: %w", err) 142 } 143 144 var result GetRateProtectionsResponse 145 resp, err := p.Exec(req, &result) 146 if err != nil { 147 return nil, fmt.Errorf("get rate protections request failed: %w", err) 148 } 149 if resp.StatusCode != http.StatusOK { 150 return nil, p.Error(resp) 151 } 152 153 return &result, nil 154 } 155 156 func (p *appsec) UpdateRateProtection(ctx context.Context, params UpdateRateProtectionRequest) (*UpdateRateProtectionResponse, error) { 157 logger := p.Log(ctx) 158 logger.Debug("UpdateRateProtection") 159 160 if err := params.Validate(); err != nil { 161 return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error()) 162 } 163 164 uri := fmt.Sprintf( 165 "/appsec/v1/configs/%d/versions/%d/security-policies/%s/protections", 166 params.ConfigID, 167 params.Version, 168 params.PolicyID, 169 ) 170 171 req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil) 172 if err != nil { 173 return nil, fmt.Errorf("failed to create UpdateRateProtection request: %w", err) 174 } 175 176 var result UpdateRateProtectionResponse 177 resp, err := p.Exec(req, &result, params) 178 if err != nil { 179 return nil, fmt.Errorf("update rate protection request failed: %w", err) 180 } 181 if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { 182 return nil, p.Error(resp) 183 } 184 185 return &result, nil 186 }