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