-
Notifications
You must be signed in to change notification settings - Fork 73
[CDTOOL-1221] Support for NGWAF Threshold Commands #1595
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a7f5d0d
crud operations
rcaril 0e9e79a
Bool Fixes & Test
rcaril 97afa3f
Merge branch 'main' into rcaril/CDTOOL-1221-ngwaf-thresholds-crud-sup…
rcaril 5d84d19
changelog and lint fixes
rcaril 29f6091
Merge branch 'main' into rcaril/CDTOOL-1221-ngwaf-thresholds-crud-sup…
rcaril File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,130 @@ | ||
| package threshold | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "io" | ||
|
|
||
| "github.com/fastly/go-fastly/v12/fastly" | ||
| "github.com/fastly/go-fastly/v12/fastly/ngwaf/v1/workspaces/thresholds" | ||
|
|
||
| "github.com/fastly/cli/pkg/argparser" | ||
| fsterr "github.com/fastly/cli/pkg/errors" | ||
| "github.com/fastly/cli/pkg/global" | ||
| "github.com/fastly/cli/pkg/text" | ||
| ) | ||
|
|
||
| // CreateCommand calls the Fastly API to create a workspace threshold. | ||
| type CreateCommand struct { | ||
| argparser.Base | ||
| argparser.JSONOutput | ||
|
|
||
| // Required. | ||
| action string | ||
| dontNotify argparser.OptionalString | ||
| duration int | ||
| enabled argparser.OptionalString | ||
| interval int | ||
| limit int | ||
| name string | ||
| signal string | ||
| workspaceID argparser.OptionalWorkspaceID | ||
|
|
||
| // Optional. | ||
| } | ||
|
|
||
| // NewCreateCommand returns a usable command registered under the parent. | ||
| func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateCommand { | ||
| c := CreateCommand{ | ||
| Base: argparser.Base{ | ||
| Globals: g, | ||
| }, | ||
| } | ||
| c.CmdClause = parent.Command("create", "Create a workspace threshold").Alias("add") | ||
|
|
||
| // Required. | ||
| c.CmdClause.Flag("action", "The action to take when the threshold is exceeded. [block, log]").Required().StringVar(&c.action) | ||
| c.CmdClause.Flag("do-not-notify", "Whether to silence notifications when action is taken. [true, false]").Required().Action(c.dontNotify.Set).StringVar(&c.dontNotify.Value) | ||
| c.CmdClause.Flag("duration", "The duration the action is in place in seconds. Default duration is 86,400 seconds (1 day).").Required().IntVar(&c.duration) | ||
| c.CmdClause.Flag("enabled", "Whether the threshold is active. [true, false]").Required().Action(c.enabled.Set).StringVar(&c.enabled.Value) | ||
| c.CmdClause.Flag("interval", "The threshold interval in seconds. The default interval is 3600 seconds (1 hour).").Required().IntVar(&c.interval) | ||
| c.CmdClause.Flag("limit", "The threshold limit. Input must be between 1 and 10000. Default limit is 10.").Required().IntVar(&c.limit) | ||
| c.CmdClause.Flag("name", "User submitted display name of a signal threshold. Input must be between 3 and 50 characters").Required().StringVar(&c.name) | ||
| c.CmdClause.Flag("signal", "The name of the signal this threshold is acting on").Required().StringVar(&c.signal) | ||
| c.RegisterFlag(argparser.StringFlagOpts{ | ||
| Name: argparser.FlagNGWAFWorkspaceID, | ||
| Description: argparser.FlagNGWAFWorkspaceIDDesc, | ||
| Dst: &c.workspaceID.Value, | ||
| Action: c.workspaceID.Set, | ||
| }) | ||
|
|
||
| // Optional. | ||
| c.RegisterFlagBool(c.JSONFlag()) | ||
|
|
||
| return &c | ||
| } | ||
|
|
||
| // Exec invokes the application logic for the command. | ||
| func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error { | ||
| if c.Globals.Verbose() && c.JSONOutput.Enabled { | ||
| return fsterr.ErrInvalidVerboseJSONCombo | ||
| } | ||
| // Call Parse() to ensure that we check if workspaceID | ||
| // is set or to throw the appropriate error. | ||
| if err := c.workspaceID.Parse(); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| var enabled bool | ||
| switch c.enabled.Value { | ||
| case "true": | ||
| enabled = true | ||
| case "false": | ||
| enabled = false | ||
| default: | ||
| err := errors.New("'enabled' flag must be one of the following [true, false]") | ||
| c.Globals.ErrLog.Add(err) | ||
| return err | ||
| } | ||
|
|
||
| var dontNotify bool | ||
| switch c.dontNotify.Value { | ||
| case "true": | ||
| dontNotify = true | ||
| case "false": | ||
| dontNotify = false | ||
| default: | ||
| err := errors.New("'do-not-notify' flag must be one of the following [true, false]") | ||
| c.Globals.ErrLog.Add(err) | ||
| return err | ||
| } | ||
|
|
||
| input := &thresholds.CreateInput{ | ||
| Action: &c.action, | ||
| Duration: &c.duration, | ||
| Enabled: &enabled, | ||
| Interval: &c.interval, | ||
| Limit: &c.limit, | ||
| Name: &c.name, | ||
| DontNotify: &dontNotify, | ||
| Signal: &c.signal, | ||
| WorkspaceID: &c.workspaceID.Value, | ||
| } | ||
|
|
||
| fc, ok := c.Globals.APIClient.(*fastly.Client) | ||
| if !ok { | ||
| return errors.New("failed to convert interface to a fastly client") | ||
| } | ||
|
|
||
| data, err := thresholds.Create(context.TODO(), fc, input) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if ok, err := c.WriteJSON(out, data); ok { | ||
| return err | ||
| } | ||
|
|
||
| text.Success(out, "Created threshold '%s' for workspace '%s'", data.ThresholdID, c.workspaceID.Value) | ||
| return nil | ||
| } | ||
This file contains hidden or 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,92 @@ | ||
| package threshold | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "io" | ||
|
|
||
| "github.com/fastly/go-fastly/v12/fastly" | ||
| "github.com/fastly/go-fastly/v12/fastly/ngwaf/v1/workspaces/thresholds" | ||
|
|
||
| "github.com/fastly/cli/pkg/argparser" | ||
| fsterr "github.com/fastly/cli/pkg/errors" | ||
| "github.com/fastly/cli/pkg/global" | ||
| "github.com/fastly/cli/pkg/text" | ||
| ) | ||
|
|
||
| // DeleteCommand calls the Fastly API to delete a workspace threshold. | ||
| type DeleteCommand struct { | ||
| argparser.Base | ||
| argparser.JSONOutput | ||
|
|
||
| // Required. | ||
| thresholdID string | ||
| workspaceID argparser.OptionalWorkspaceID | ||
|
|
||
| // Optional. | ||
| } | ||
|
|
||
| // NewDeleteCommand returns a usable command registered under the parent. | ||
| func NewDeleteCommand(parent argparser.Registerer, g *global.Data) *DeleteCommand { | ||
| c := DeleteCommand{ | ||
| Base: argparser.Base{ | ||
| Globals: g, | ||
| }, | ||
| } | ||
| c.CmdClause = parent.Command("delete", "Deletes a workspace threshold") | ||
|
|
||
| // Required. | ||
| c.RegisterFlag(argparser.StringFlagOpts{ | ||
| Name: argparser.FlagNGWAFWorkspaceID, | ||
| Description: argparser.FlagNGWAFWorkspaceIDDesc, | ||
| Dst: &c.workspaceID.Value, | ||
| Action: c.workspaceID.Set, | ||
| }) | ||
| c.CmdClause.Flag("threshold-id", "Threshold ID").Required().StringVar(&c.thresholdID) | ||
|
|
||
| // Optional. | ||
| c.RegisterFlagBool(c.JSONFlag()) | ||
|
|
||
| return &c | ||
| } | ||
|
|
||
| // Exec invokes the application logic for the command. | ||
| func (c *DeleteCommand) Exec(_ io.Reader, out io.Writer) error { | ||
| if c.Globals.Verbose() && c.JSONOutput.Enabled { | ||
| return fsterr.ErrInvalidVerboseJSONCombo | ||
| } | ||
| // Call Parse() to ensure that we check if workspaceID | ||
| // is set or to throw the appropriate error. | ||
| if err := c.workspaceID.Parse(); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| fc, ok := c.Globals.APIClient.(*fastly.Client) | ||
| if !ok { | ||
| return errors.New("failed to convert interface to a fastly client") | ||
| } | ||
|
|
||
| err := thresholds.Delete(context.TODO(), fc, &thresholds.DeleteInput{ | ||
| ThresholdID: &c.thresholdID, | ||
| WorkspaceID: &c.workspaceID.Value, | ||
| }) | ||
| if err != nil { | ||
| c.Globals.ErrLog.Add(err) | ||
| return err | ||
| } | ||
|
|
||
| if c.JSONOutput.Enabled { | ||
| o := struct { | ||
| ID string `json:"id"` | ||
| Deleted bool `json:"deleted"` | ||
| }{ | ||
| c.thresholdID, | ||
| true, | ||
| } | ||
| _, err := c.WriteJSON(out, o) | ||
| return err | ||
| } | ||
|
|
||
| text.Success(out, "Deleted threshold (id: %s)", c.thresholdID) | ||
| return nil | ||
| } |
This file contains hidden or 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,2 @@ | ||
| // Package threshold contains commands to inspect and manipulate NGWAF workspace thresholds. | ||
| package threshold |
This file contains hidden or 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,84 @@ | ||
| package threshold | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "io" | ||
|
|
||
| "github.com/fastly/go-fastly/v12/fastly" | ||
| "github.com/fastly/go-fastly/v12/fastly/ngwaf/v1/workspaces/thresholds" | ||
|
|
||
| "github.com/fastly/cli/pkg/argparser" | ||
| fsterr "github.com/fastly/cli/pkg/errors" | ||
| "github.com/fastly/cli/pkg/global" | ||
| "github.com/fastly/cli/pkg/text" | ||
| ) | ||
|
|
||
| // GetCommand calls the Fastly API to get a workspace threshold. | ||
| type GetCommand struct { | ||
| argparser.Base | ||
| argparser.JSONOutput | ||
|
|
||
| // Required. | ||
| thresholdID string | ||
| workspaceID argparser.OptionalWorkspaceID | ||
|
|
||
| // Optional. | ||
| } | ||
|
|
||
| // NewGetCommand returns a usable command registered under the parent. | ||
| func NewGetCommand(parent argparser.Registerer, g *global.Data) *GetCommand { | ||
| c := GetCommand{ | ||
| Base: argparser.Base{ | ||
| Globals: g, | ||
| }, | ||
| } | ||
| c.CmdClause = parent.Command("get", "Retrieves a workspace threshold") | ||
|
|
||
| // Required. | ||
| c.RegisterFlag(argparser.StringFlagOpts{ | ||
| Name: argparser.FlagNGWAFWorkspaceID, | ||
| Description: argparser.FlagNGWAFWorkspaceIDDesc, | ||
| Dst: &c.workspaceID.Value, | ||
| Action: c.workspaceID.Set, | ||
| }) | ||
| c.CmdClause.Flag("threshold-id", "Threshold ID").Required().StringVar(&c.thresholdID) | ||
|
|
||
| // Optional. | ||
| c.RegisterFlagBool(c.JSONFlag()) | ||
|
|
||
| return &c | ||
| } | ||
|
|
||
| // Exec invokes the application logic for the command. | ||
| func (c *GetCommand) Exec(_ io.Reader, out io.Writer) error { | ||
| if c.Globals.Verbose() && c.JSONOutput.Enabled { | ||
| return fsterr.ErrInvalidVerboseJSONCombo | ||
| } | ||
| // Call Parse() to ensure that we check if workspaceID | ||
| // is set or to throw the appropriate error. | ||
| if err := c.workspaceID.Parse(); err != nil { | ||
| return err | ||
| } | ||
| input := &thresholds.GetInput{ | ||
| ThresholdID: &c.thresholdID, | ||
| WorkspaceID: &c.workspaceID.Value, | ||
| } | ||
|
|
||
| fc, ok := c.Globals.APIClient.(*fastly.Client) | ||
| if !ok { | ||
| return errors.New("failed to convert interface to a fastly client") | ||
| } | ||
|
|
||
| data, err := thresholds.Get(context.TODO(), fc, input) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if ok, err := c.WriteJSON(out, data); ok { | ||
| return err | ||
| } | ||
|
|
||
| text.PrintThreshold(out, data) | ||
| return nil | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.