github.com/akamai/AkamaiOPEN-edgegrid-golang/v2@v2.17.0/pkg/appsec/rate_policy_action.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 RatePolicyAction interface supports retrieving and modifying the action associated with
    13  	// a specified rate policy, or with all rate policies in a security policy.
    14  	//
    15  	// https://developer.akamai.com/api/cloud_security/application_security/v1.html#ratepolicyaction
    16  	RatePolicyAction interface {
    17  		// https://developer.akamai.com/api/cloud_security/application_security/v1.html#getratepolicyactions
    18  		GetRatePolicyActions(ctx context.Context, params GetRatePolicyActionsRequest) (*GetRatePolicyActionsResponse, error)
    19  
    20  		// https://developer.akamai.com/api/cloud_security/application_security/v1.html#getratepolicyactions
    21  		// Deprecated: this method will be removed in a future release. Use GetRatePolicyActions instead.
    22  		GetRatePolicyAction(ctx context.Context, params GetRatePolicyActionRequest) (*GetRatePolicyActionResponse, error)
    23  
    24  		// https://developer.akamai.com/api/cloud_security/application_security/v1.html#putactionsperratepolicy
    25  		UpdateRatePolicyAction(ctx context.Context, params UpdateRatePolicyActionRequest) (*UpdateRatePolicyActionResponse, error)
    26  	}
    27  
    28  	// GetRatePolicyActionsRequest is used to retrieve a configuration's rate policies and their associated actions.
    29  	GetRatePolicyActionsRequest struct {
    30  		ConfigID     int    `json:"configId"`
    31  		Version      int    `json:"version"`
    32  		PolicyID     string `json:"policyId"`
    33  		RatePolicyID int    `json:"id"`
    34  		Ipv4Action   string `json:"ipv4Action"`
    35  		Ipv6Action   string `json:"ipv6Action"`
    36  	}
    37  
    38  	// GetRatePolicyActionsResponse is returned from a call to GetRatePolicyActions.
    39  	GetRatePolicyActionsResponse struct {
    40  		RatePolicyActions []struct {
    41  			ID         int    `json:"id"`
    42  			Ipv4Action string `json:"ipv4Action,omitempty"`
    43  			Ipv6Action string `json:"ipv6Action,omitempty"`
    44  		} `json:"ratePolicyActions,omitempty"`
    45  	}
    46  
    47  	// GetRatePolicyActionRequest is used to retrieve a configuration's rate policies and their associated actions.
    48  	// Deprecated: this struct will be removed in a future release.
    49  	GetRatePolicyActionRequest struct {
    50  		ConfigID   int    `json:"configId"`
    51  		Version    int    `json:"version"`
    52  		PolicyID   string `json:"policyId"`
    53  		ID         int    `json:"id"`
    54  		Ipv4Action string `json:"ipv4Action"`
    55  		Ipv6Action string `json:"ipv6Action"`
    56  	}
    57  
    58  	// GetRatePolicyActionResponse is returned from a call to GetRatePolicyAction.
    59  	// Deprecated: this struct will be removed in a future release.
    60  	GetRatePolicyActionResponse struct {
    61  		RatePolicyActions []struct {
    62  			ID         int    `json:"id"`
    63  			Ipv4Action string `json:"ipv4Action,omitempty"`
    64  			Ipv6Action string `json:"ipv6Action,omitempty"`
    65  		} `json:"ratePolicyActions"`
    66  	}
    67  
    68  	// UpdateRatePolicyActionRequest is used to update the actions for a rate policy.
    69  	UpdateRatePolicyActionRequest struct {
    70  		ConfigID     int    `json:"-"`
    71  		Version      int    `json:"-"`
    72  		PolicyID     string `json:"-"`
    73  		RatePolicyID int    `json:"-"`
    74  		Ipv4Action   string `json:"ipv4Action"`
    75  		Ipv6Action   string `json:"ipv6Action"`
    76  	}
    77  
    78  	// UpdateRatePolicyActionResponse is returned from a call to UpdateRatePolicy.
    79  	UpdateRatePolicyActionResponse struct {
    80  		ID         int    `json:"id"`
    81  		Ipv4Action string `json:"ipv4Action"`
    82  		Ipv6Action string `json:"ipv6Action"`
    83  	}
    84  
    85  	// RatePolicyActionPost is used to describe actions that may be taken as part of a rate policy.
    86  	RatePolicyActionPost struct {
    87  		ID         int    `json:"id"`
    88  		Ipv4Action string `json:"ipv4Action"`
    89  		Ipv6Action string `json:"ipv6Action"`
    90  	}
    91  )
    92  
    93  // Validate validates a GetRatePolicyActionRequest.
    94  // Deprecated: this method will be removed in a future release.
    95  func (v GetRatePolicyActionRequest) Validate() error {
    96  	return validation.Errors{
    97  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
    98  		"Version":  validation.Validate(v.Version, validation.Required),
    99  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
   100  	}.Filter()
   101  }
   102  
   103  // Validate validates a GetRatePolicyActionsRequest.
   104  func (v GetRatePolicyActionsRequest) Validate() error {
   105  	return validation.Errors{
   106  		"ConfigID": validation.Validate(v.ConfigID, validation.Required),
   107  		"Version":  validation.Validate(v.Version, validation.Required),
   108  		"PolicyID": validation.Validate(v.PolicyID, validation.Required),
   109  	}.Filter()
   110  }
   111  
   112  // Validate validates an UpdateRatePolicyActionRequest.
   113  func (v UpdateRatePolicyActionRequest) Validate() error {
   114  	return validation.Errors{
   115  		"ConfigID":     validation.Validate(v.ConfigID, validation.Required),
   116  		"Version":      validation.Validate(v.Version, validation.Required),
   117  		"PolicyID":     validation.Validate(v.PolicyID, validation.Required),
   118  		"RatePolicyID": validation.Validate(v.RatePolicyID, validation.Required),
   119  	}.Filter()
   120  }
   121  
   122  // Deprecated: this method will be removed in a future release.
   123  func (p *appsec) GetRatePolicyAction(ctx context.Context, params GetRatePolicyActionRequest) (*GetRatePolicyActionResponse, error) {
   124  	logger := p.Log(ctx)
   125  	logger.Debug("GetRatePolicyAction")
   126  
   127  	if err := params.Validate(); err != nil {
   128  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   129  	}
   130  
   131  	uri := fmt.Sprintf(
   132  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/rate-policies",
   133  		params.ConfigID,
   134  		params.Version,
   135  		params.PolicyID)
   136  
   137  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   138  	if err != nil {
   139  		return nil, fmt.Errorf("failed to create GetRatePolicyAction request: %w", err)
   140  	}
   141  
   142  	var result GetRatePolicyActionResponse
   143  	resp, err := p.Exec(req, &result)
   144  	if err != nil {
   145  		return nil, fmt.Errorf("get rate policy action request failed: %w", err)
   146  	}
   147  	if resp.StatusCode != http.StatusOK {
   148  		return nil, p.Error(resp)
   149  	}
   150  
   151  	return &result, nil
   152  }
   153  
   154  func (p *appsec) GetRatePolicyActions(ctx context.Context, params GetRatePolicyActionsRequest) (*GetRatePolicyActionsResponse, error) {
   155  	logger := p.Log(ctx)
   156  	logger.Debug("GetRatePolicyActions")
   157  
   158  	if err := params.Validate(); err != nil {
   159  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   160  	}
   161  
   162  	uri := fmt.Sprintf(
   163  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/rate-policies",
   164  		params.ConfigID,
   165  		params.Version,
   166  		params.PolicyID)
   167  
   168  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
   169  	if err != nil {
   170  		return nil, fmt.Errorf("failed to create GetRatePolicyActions request: %w", err)
   171  	}
   172  
   173  	var result GetRatePolicyActionsResponse
   174  	resp, err := p.Exec(req, &result)
   175  	if err != nil {
   176  		return nil, fmt.Errorf("get rate policy actions request failed: %w", err)
   177  	}
   178  	if resp.StatusCode != http.StatusOK {
   179  		return nil, p.Error(resp)
   180  	}
   181  
   182  	if params.RatePolicyID != 0 {
   183  		var filteredResult GetRatePolicyActionsResponse
   184  		for _, val := range result.RatePolicyActions {
   185  			if val.ID == params.RatePolicyID {
   186  				filteredResult.RatePolicyActions = append(filteredResult.RatePolicyActions, val)
   187  			}
   188  		}
   189  		return &filteredResult, nil
   190  	}
   191  
   192  	return &result, nil
   193  }
   194  
   195  func (p *appsec) UpdateRatePolicyAction(ctx context.Context, params UpdateRatePolicyActionRequest) (*UpdateRatePolicyActionResponse, error) {
   196  	logger := p.Log(ctx)
   197  	logger.Debug("UpdateRatePolicyAction")
   198  
   199  	if err := params.Validate(); err != nil {
   200  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   201  	}
   202  
   203  	uri := fmt.Sprintf(
   204  		"/appsec/v1/configs/%d/versions/%d/security-policies/%s/rate-policies/%d",
   205  		params.ConfigID,
   206  		params.Version,
   207  		params.PolicyID,
   208  		params.RatePolicyID,
   209  	)
   210  
   211  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   212  	if err != nil {
   213  		return nil, fmt.Errorf("failed to create UpdateRatePolicyAction request: %w", err)
   214  	}
   215  
   216  	var result UpdateRatePolicyActionResponse
   217  	resp, err := p.Exec(req, &result, params)
   218  	if err != nil {
   219  		return nil, fmt.Errorf("update rate policy action request failed: %w", err)
   220  	}
   221  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   222  		return nil, p.Error(resp)
   223  	}
   224  
   225  	return &result, nil
   226  }