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