github.com/akamai/AkamaiOPEN-edgegrid-golang/v8@v8.1.0/pkg/appsec/match_target_sequence.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 MatchTargetSequence interface supports querying and modifying the order of match targets.
    13  	MatchTargetSequence interface {
    14  		// GetMatchTargetSequence returns match targets defined in the specified security configuration version.
    15  		//
    16  		// See: https://techdocs.akamai.com/application-security/reference/get-match-targets
    17  		GetMatchTargetSequence(ctx context.Context, params GetMatchTargetSequenceRequest) (*GetMatchTargetSequenceResponse, error)
    18  
    19  		// UpdateMatchTargetSequence updates the sequence of Match Targets in a configuration version.
    20  		//
    21  		// See: https://techdocs.akamai.com/application-security/reference/put-match-targets-sequence
    22  		UpdateMatchTargetSequence(ctx context.Context, params UpdateMatchTargetSequenceRequest) (*UpdateMatchTargetSequenceResponse, error)
    23  	}
    24  
    25  	// GetMatchTargetSequenceRequest is used to retrieve the sequence of match targets for a configuration.
    26  	GetMatchTargetSequenceRequest struct {
    27  		ConfigID      int    `json:"configId"`
    28  		ConfigVersion int    `json:"configVersion"`
    29  		Type          string `json:"type"`
    30  	}
    31  
    32  	// GetMatchTargetSequenceResponse is returned from a call to GetMatchTargetSequence.
    33  	GetMatchTargetSequenceResponse struct {
    34  		TargetSequence []MatchTargetItem `json:"targetSequence"`
    35  		Type           string            `json:"type"`
    36  	}
    37  
    38  	// UpdateMatchTargetSequenceRequest UpdateMatchTargetSequenceRequest is used to modify an existing match target sequence.
    39  	UpdateMatchTargetSequenceRequest struct {
    40  		ConfigID       int               `json:"-"`
    41  		ConfigVersion  int               `json:"-"`
    42  		TargetSequence []MatchTargetItem `json:"targetSequence"`
    43  		Type           string            `json:"type"`
    44  	}
    45  
    46  	// UpdateMatchTargetSequenceResponse is returned from a call to UpdateMatchTargetSequence.
    47  	UpdateMatchTargetSequenceResponse struct {
    48  		TargetSequence []MatchTargetItem `json:"targetSequence"`
    49  		Type           string            `json:"type"`
    50  	}
    51  
    52  	// MatchTargetItem describes a match target and its sequence number.
    53  	MatchTargetItem struct {
    54  		Sequence int `json:"sequence"`
    55  		TargetID int `json:"targetId"`
    56  	}
    57  )
    58  
    59  // Validate validates a GetMatchTargetSequenceRequest.
    60  func (v GetMatchTargetSequenceRequest) Validate() error {
    61  	return validation.Errors{
    62  		"ConfigID":      validation.Validate(v.ConfigID, validation.Required),
    63  		"ConfigVersion": validation.Validate(v.ConfigVersion, validation.Required),
    64  		"Type":          validation.Validate(v.Type, validation.Required),
    65  	}.Filter()
    66  }
    67  
    68  // Validate validates an UpdateMatchTargetSequenceRequest.
    69  func (v UpdateMatchTargetSequenceRequest) Validate() error {
    70  	return validation.Errors{
    71  		"ConfigID":      validation.Validate(v.ConfigID, validation.Required),
    72  		"ConfigVersion": validation.Validate(v.ConfigVersion, validation.Required),
    73  		"Type":          validation.Validate(v.ConfigVersion, validation.Required),
    74  	}.Filter()
    75  }
    76  
    77  func (p *appsec) GetMatchTargetSequence(ctx context.Context, params GetMatchTargetSequenceRequest) (*GetMatchTargetSequenceResponse, error) {
    78  	logger := p.Log(ctx)
    79  	logger.Debug("GetMatchTargetSequence")
    80  
    81  	if err := params.Validate(); err != nil {
    82  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
    83  	}
    84  
    85  	uri := fmt.Sprintf(
    86  		"/appsec/v1/configs/%d/versions/%d/match-targets/sequence?type=%s",
    87  		params.ConfigID,
    88  		params.ConfigVersion,
    89  		params.Type,
    90  	)
    91  
    92  	req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil)
    93  	if err != nil {
    94  		return nil, fmt.Errorf("failed to create GetMatchTargetSequence request: %w", err)
    95  	}
    96  
    97  	var result GetMatchTargetSequenceResponse
    98  	resp, err := p.Exec(req, &result)
    99  	if err != nil {
   100  		return nil, fmt.Errorf("get match target sequence request failed: %w", err)
   101  	}
   102  	if resp.StatusCode != http.StatusOK {
   103  		return nil, p.Error(resp)
   104  	}
   105  
   106  	return &result, nil
   107  }
   108  
   109  func (p *appsec) UpdateMatchTargetSequence(ctx context.Context, params UpdateMatchTargetSequenceRequest) (*UpdateMatchTargetSequenceResponse, error) {
   110  	logger := p.Log(ctx)
   111  	logger.Debug("UpdateMatchTargetSequence")
   112  
   113  	if err := params.Validate(); err != nil {
   114  		return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error())
   115  	}
   116  
   117  	uri := fmt.Sprintf(
   118  		"/appsec/v1/configs/%d/versions/%d/match-targets/sequence",
   119  		params.ConfigID,
   120  		params.ConfigVersion,
   121  	)
   122  
   123  	req, err := http.NewRequestWithContext(ctx, http.MethodPut, uri, nil)
   124  	if err != nil {
   125  		return nil, fmt.Errorf("failed to create UpdateMatchTargetSequence request: %w", err)
   126  	}
   127  
   128  	var result UpdateMatchTargetSequenceResponse
   129  	resp, err := p.Exec(req, &result, params)
   130  	if err != nil {
   131  		return nil, fmt.Errorf("update match target sequence request failed: %w", err)
   132  	}
   133  	if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
   134  		return nil, p.Error(resp)
   135  	}
   136  
   137  	return &result, nil
   138  }