-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DXE-3169 Merge pull request #197 from akamai/release/v7.5.0
DXE-3169 Release/v7.5.0
- Loading branch information
Showing
8 changed files
with
469 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package botman | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
validation "github.com/go-ozzo/ozzo-validation/v4" | ||
) | ||
|
||
type ( | ||
// The CustomCode interface supports retrieving and updating custom code | ||
CustomCode interface { | ||
GetCustomCode(ctx context.Context, params GetCustomCodeRequest) (map[string]interface{}, error) | ||
|
||
UpdateCustomCode(ctx context.Context, params UpdateCustomCodeRequest) (map[string]interface{}, error) | ||
} | ||
|
||
// GetCustomCodeRequest is used to retrieve custom code | ||
GetCustomCodeRequest struct { | ||
ConfigID int64 | ||
Version int64 | ||
} | ||
|
||
// UpdateCustomCodeRequest is used to modify custom code | ||
UpdateCustomCodeRequest struct { | ||
ConfigID int64 | ||
Version int64 | ||
JsonPayload json.RawMessage | ||
} | ||
) | ||
|
||
// Validate validates a GetCustomCodeRequest. | ||
func (v GetCustomCodeRequest) Validate() error { | ||
return validation.Errors{ | ||
"ConfigID": validation.Validate(v.ConfigID, validation.Required), | ||
"Version": validation.Validate(v.Version, validation.Required), | ||
}.Filter() | ||
} | ||
|
||
// Validate validates an UpdateCustomCodeRequest. | ||
func (v UpdateCustomCodeRequest) Validate() error { | ||
return validation.Errors{ | ||
"ConfigID": validation.Validate(v.ConfigID, validation.Required), | ||
"Version": validation.Validate(v.Version, validation.Required), | ||
"JsonPayload": validation.Validate(v.JsonPayload, validation.Required), | ||
}.Filter() | ||
} | ||
|
||
func (b *botman) GetCustomCode(ctx context.Context, params GetCustomCodeRequest) (map[string]interface{}, error) { | ||
logger := b.Log(ctx) | ||
logger.Debug("GetCustomCode") | ||
|
||
if err := params.Validate(); err != nil { | ||
return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error()) | ||
} | ||
|
||
uri := fmt.Sprintf( | ||
"/appsec/v1/configs/%d/versions/%d/advanced-settings/transactional-endpoint-protection/custom-code", | ||
params.ConfigID, | ||
params.Version) | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, uri, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create GetCustomCode request: %w", err) | ||
} | ||
|
||
var result map[string]interface{} | ||
resp, err := b.Exec(req, &result) | ||
if err != nil { | ||
return nil, fmt.Errorf("GetCustomCode request failed: %w", err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, b.Error(resp) | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
func (b *botman) UpdateCustomCode(ctx context.Context, params UpdateCustomCodeRequest) (map[string]interface{}, error) { | ||
logger := b.Log(ctx) | ||
logger.Debug("UpdateCustomCode") | ||
|
||
if err := params.Validate(); err != nil { | ||
return nil, fmt.Errorf("%w: %s", ErrStructValidation, err.Error()) | ||
} | ||
|
||
putURL := fmt.Sprintf( | ||
"/appsec/v1/configs/%d/versions/%d/advanced-settings/transactional-endpoint-protection/custom-code", | ||
params.ConfigID, | ||
params.Version, | ||
) | ||
|
||
req, err := http.NewRequestWithContext(ctx, http.MethodPut, putURL, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create UpdateCustomCode request: %w", err) | ||
} | ||
|
||
var result map[string]interface{} | ||
resp, err := b.Exec(req, &result, params.JsonPayload) | ||
if err != nil { | ||
return nil, fmt.Errorf("UpdateCustomCode request failed: %w", err) | ||
} | ||
|
||
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusNoContent { | ||
return nil, b.Error(resp) | ||
} | ||
|
||
return result, nil | ||
} |
Oops, something went wrong.