Skip to content

Commit 34586f7

Browse files
authored
[CDTOOL-1218] Add Support for NGWAF Redaction Commands (#1581)
### Change summary This PR adds support for the NGWAF `redaction` commands. All Submissions: * [x] Have you followed the guidelines in our Contributing document? * [x] Have you checked to ensure there aren't other open [Pull Requests](https://github.com/fastly/cli/pulls) for the same update/change? <!-- You can erase any parts of this template not applicable to your Pull Request. --> ### New Feature Submissions: * [x] Does your submission pass tests? ``` make test TEST_ARGS="-run TestRedactionCreate ./pkg/commands/ngwaf/redaction" ok github.com/fastly/cli/pkg/commands/ngwaf/redaction (cached) make test TEST_ARGS="-run TestRedactionDelete ./pkg/commands/ngwaf/redaction" ok github.com/fastly/cli/pkg/commands/ngwaf/redaction (cached) make test TEST_ARGS="-run TestRedactionRetrieve ./pkg/commands/ngwaf/redaction" ok github.com/fastly/cli/pkg/commands/ngwaf/redaction (cached) make test TEST_ARGS="-run TestRedactionList ./pkg/commands/ngwaf/redaction" ok github.com/fastly/cli/pkg/commands/ngwaf/redaction (cached) make test TEST_ARGS="-run TestRedactionUpdate ./pkg/commands/ngwaf/redaction" ok github.com/fastly/cli/pkg/commands/ngwaf/redaction 1.049s ``` ### Changes to Core Features: * [x] Have you written new tests for your core changes, as applicable? * [x] Have you successfully run tests with your changes locally?
1 parent 1ba4fee commit 34586f7

10 files changed

Lines changed: 897 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
- feat(compute/build): Allow usage of Rust 1.91.1 and later patch releases ([#1576](https://github.com/fastly/cli/pull/1576))
2020
- feat(commands/ngwaf/workspaces): add support for CRUD operations for NGWAF workspaces ([#1570](https://github.com/fastly/cli/pull/1570))
2121
- feat(commands/ngwaf/virtualpatch): add support for CRUD operations for NGWAF virtual patches ([#1579](https://github.com/fastly/cli/pull/1579))
22+
- feat(commands/ngwaf/redaction): add support for CRUD operations for NGWAF redactions ([#1581](https://github.com/fastly/cli/pull/1581))
2223

2324
### Dependencies:
2425
- build(deps): `golangci/golangci-lint-action` from 8 to 9 ([#1575](https://github.com/fastly/cli/pull/1575))

pkg/commands/commands.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import (
5656
"github.com/fastly/cli/pkg/commands/logging/syslog"
5757
"github.com/fastly/cli/pkg/commands/logtail"
5858
"github.com/fastly/cli/pkg/commands/ngwaf"
59+
"github.com/fastly/cli/pkg/commands/ngwaf/redaction"
5960
"github.com/fastly/cli/pkg/commands/ngwaf/virtualpatch"
6061
"github.com/fastly/cli/pkg/commands/ngwaf/workspace"
6162
"github.com/fastly/cli/pkg/commands/objectstorage"
@@ -397,6 +398,12 @@ func Define( // nolint:revive // function-length
397398
loggingSyslogList := syslog.NewListCommand(loggingSyslogCmdRoot.CmdClause, data)
398399
loggingSyslogUpdate := syslog.NewUpdateCommand(loggingSyslogCmdRoot.CmdClause, data)
399400
ngwafRoot := ngwaf.NewRootCommand(app, data)
401+
ngwafRedactionRoot := redaction.NewRootCommand(ngwafRoot.CmdClause, data)
402+
ngwafRedactionCreate := redaction.NewCreateCommand(ngwafRedactionRoot.CmdClause, data)
403+
ngwafRedactionDelete := redaction.NewDeleteCommand(ngwafRedactionRoot.CmdClause, data)
404+
ngwafRedactionList := redaction.NewListCommand(ngwafRedactionRoot.CmdClause, data)
405+
ngwafRedactionRetrieve := redaction.NewRetrieveCommand(ngwafRedactionRoot.CmdClause, data)
406+
ngwafRedactionUpdate := redaction.NewUpdateCommand(ngwafRedactionRoot.CmdClause, data)
400407
ngwafVirtualpatchRoot := virtualpatch.NewRootCommand(ngwafRoot.CmdClause, data)
401408
ngwafVirtualpatchList := virtualpatch.NewListCommand(ngwafVirtualpatchRoot.CmdClause, data)
402409
ngwafVirtualpatchUpdate := virtualpatch.NewUpdateCommand(ngwafVirtualpatchRoot.CmdClause, data)
@@ -827,6 +834,12 @@ func Define( // nolint:revive // function-length
827834
loggingSyslogList,
828835
loggingSyslogUpdate,
829836
ngwafRoot,
837+
ngwafRedactionCreate,
838+
ngwafRedactionDelete,
839+
ngwafRedactionList,
840+
ngwafRedactionRetrieve,
841+
ngwafRedactionUpdate,
842+
ngwafRedactionRoot,
830843
ngwafVirtualpatchList,
831844
ngwafVirtualpatchRetrieve,
832845
ngwafVirtualpatchRoot,
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package redaction
2+
3+
import (
4+
"context"
5+
"errors"
6+
"io"
7+
8+
"github.com/fastly/go-fastly/v12/fastly"
9+
"github.com/fastly/go-fastly/v12/fastly/ngwaf/v1/workspaces/redactions"
10+
11+
"github.com/fastly/cli/pkg/argparser"
12+
"github.com/fastly/cli/pkg/global"
13+
"github.com/fastly/cli/pkg/text"
14+
)
15+
16+
// CreateCommand calls the Fastly API to create a redaction.
17+
type CreateCommand struct {
18+
argparser.Base
19+
argparser.JSONOutput
20+
21+
// Required.
22+
field string
23+
redactionType string
24+
workspaceID argparser.OptionalWorkspaceID
25+
}
26+
27+
// NewUpdateCommand returns a usable command registered under the parent.
28+
func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateCommand {
29+
c := CreateCommand{
30+
Base: argparser.Base{
31+
Globals: g,
32+
},
33+
}
34+
c.CmdClause = parent.Command("create", "Create a redaction")
35+
36+
// Required.
37+
c.CmdClause.Flag("field", "The name of the field that should be redacted.").Required().StringVar(&c.field)
38+
c.CmdClause.Flag("type", "The type of field that is being redacted.").Required().StringVar(&c.redactionType)
39+
c.RegisterFlag(argparser.StringFlagOpts{
40+
Name: argparser.FlagNGWAFWorkspaceID,
41+
Description: argparser.FlagNGWAFWorkspaceIDDesc,
42+
Dst: &c.workspaceID.Value,
43+
Action: c.workspaceID.Set,
44+
})
45+
46+
// Optional.
47+
c.RegisterFlagBool(c.JSONFlag())
48+
49+
return &c
50+
}
51+
52+
// Exec invokes the application logic for the command.
53+
func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
54+
// Call Parse() to ensure that we check if workspaceID
55+
// is set or to throw the appropriate error.
56+
if err := c.workspaceID.Parse(); err != nil {
57+
return err
58+
}
59+
60+
var err error
61+
input := &redactions.CreateInput{
62+
Field: &c.field,
63+
Type: &c.redactionType,
64+
WorkspaceID: &c.workspaceID.Value,
65+
}
66+
67+
fc, ok := c.Globals.APIClient.(*fastly.Client)
68+
if !ok {
69+
return errors.New("failed to convert interface to a fastly client")
70+
}
71+
72+
data, err := redactions.Create(context.TODO(), fc, input)
73+
if err != nil {
74+
return err
75+
}
76+
77+
if ok, err := c.WriteJSON(out, data); ok {
78+
return err
79+
}
80+
81+
text.Success(out, "Created redaction '%s' (field: %s, type: %s)", data.RedactionID, data.Field, data.Type)
82+
return nil
83+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package redaction
2+
3+
import (
4+
"context"
5+
"errors"
6+
"io"
7+
8+
"github.com/fastly/go-fastly/v12/fastly"
9+
10+
"github.com/fastly/go-fastly/v12/fastly/ngwaf/v1/workspaces/redactions"
11+
12+
"github.com/fastly/cli/pkg/argparser"
13+
fsterr "github.com/fastly/cli/pkg/errors"
14+
"github.com/fastly/cli/pkg/global"
15+
"github.com/fastly/cli/pkg/text"
16+
)
17+
18+
// DeleteCommand calls the Fastly API to delete a workspace.
19+
type DeleteCommand struct {
20+
argparser.Base
21+
argparser.JSONOutput
22+
23+
// Required.
24+
workspaceID argparser.OptionalWorkspaceID
25+
redactionID string
26+
}
27+
28+
// NewDeleteCommand returns a usable command registered under the parent.
29+
func NewDeleteCommand(parent argparser.Registerer, g *global.Data) *DeleteCommand {
30+
c := DeleteCommand{
31+
Base: argparser.Base{
32+
Globals: g,
33+
},
34+
}
35+
36+
c.CmdClause = parent.Command("delete", "Delete a redaction")
37+
38+
// Required.
39+
c.CmdClause.Flag("redaction-id", "Workspace ID").Required().StringVar(&c.redactionID)
40+
c.RegisterFlag(argparser.StringFlagOpts{
41+
Name: argparser.FlagNGWAFWorkspaceID,
42+
Description: argparser.FlagNGWAFWorkspaceIDDesc,
43+
Dst: &c.workspaceID.Value,
44+
Action: c.workspaceID.Set,
45+
})
46+
47+
// Optional.
48+
c.RegisterFlagBool(c.JSONFlag())
49+
50+
return &c
51+
}
52+
53+
// Exec invokes the application logic for the command.
54+
func (c *DeleteCommand) Exec(_ io.Reader, out io.Writer) error {
55+
// Call Parse() to ensure that we check if workspaceID
56+
// is set or to throw the appropriate error.
57+
if err := c.workspaceID.Parse(); err != nil {
58+
return err
59+
}
60+
61+
if c.Globals.Verbose() && c.JSONOutput.Enabled {
62+
return fsterr.ErrInvalidVerboseJSONCombo
63+
}
64+
65+
fc, ok := c.Globals.APIClient.(*fastly.Client)
66+
if !ok {
67+
return errors.New("failed to convert interface to a fastly client")
68+
}
69+
70+
err := redactions.Delete(context.TODO(), fc, &redactions.DeleteInput{
71+
RedactionID: &c.redactionID,
72+
WorkspaceID: &c.workspaceID.Value,
73+
})
74+
if err != nil {
75+
c.Globals.ErrLog.Add(err)
76+
return err
77+
}
78+
79+
if c.JSONOutput.Enabled {
80+
o := struct {
81+
ID string `json:"id"`
82+
Deleted bool `json:"deleted"`
83+
}{
84+
c.redactionID,
85+
true,
86+
}
87+
_, err := c.WriteJSON(out, o)
88+
return err
89+
}
90+
91+
text.Success(out, "Deleted redaction (id: %s)", c.redactionID)
92+
return nil
93+
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package redaction
2+
3+
import (
4+
"context"
5+
"errors"
6+
"io"
7+
8+
"github.com/fastly/go-fastly/v12/fastly"
9+
"github.com/fastly/go-fastly/v12/fastly/ngwaf/v1/workspaces/redactions"
10+
11+
"github.com/fastly/cli/pkg/argparser"
12+
fsterr "github.com/fastly/cli/pkg/errors"
13+
"github.com/fastly/cli/pkg/global"
14+
"github.com/fastly/cli/pkg/text"
15+
)
16+
17+
// ListCommand calls the Fastly API to list redactions in a workspace.
18+
type ListCommand struct {
19+
argparser.Base
20+
argparser.JSONOutput
21+
22+
// Required.
23+
workspaceID argparser.OptionalWorkspaceID
24+
25+
// Optional.
26+
limit argparser.OptionalInt
27+
}
28+
29+
// NewListCommand returns a usable command registered under the parent.
30+
func NewListCommand(parent argparser.Registerer, g *global.Data) *ListCommand {
31+
c := ListCommand{
32+
Base: argparser.Base{
33+
Globals: g,
34+
},
35+
}
36+
37+
c.CmdClause = parent.Command("list", "List redactions in a workspace")
38+
39+
// Required.
40+
c.RegisterFlag(argparser.StringFlagOpts{
41+
Name: argparser.FlagNGWAFWorkspaceID,
42+
Description: argparser.FlagNGWAFWorkspaceIDDesc,
43+
Dst: &c.workspaceID.Value,
44+
Action: c.workspaceID.Set,
45+
})
46+
47+
// Optional.
48+
c.CmdClause.Flag("limit", "Limit how many results are returned").Action(c.limit.Set).IntVar(&c.limit.Value)
49+
c.RegisterFlagBool(c.JSONFlag())
50+
51+
return &c
52+
}
53+
54+
// Exec invokes the application logic for the command.
55+
func (c *ListCommand) Exec(_ io.Reader, out io.Writer) error {
56+
// Call Parse() to ensure that we check if workspaceID
57+
// is set or to throw the appropriate error.
58+
if err := c.workspaceID.Parse(); err != nil {
59+
return err
60+
}
61+
62+
if c.Globals.Verbose() && c.JSONOutput.Enabled {
63+
return fsterr.ErrInvalidVerboseJSONCombo
64+
}
65+
66+
fc, ok := c.Globals.APIClient.(*fastly.Client)
67+
if !ok {
68+
return errors.New("failed to convert interface to a fastly client")
69+
}
70+
71+
input := &redactions.ListInput{
72+
WorkspaceID: &c.workspaceID.Value,
73+
}
74+
75+
if c.limit.WasSet {
76+
input.Limit = &c.limit.Value
77+
}
78+
79+
data, err := redactions.List(context.TODO(), fc, input)
80+
if err != nil {
81+
c.Globals.ErrLog.Add(err)
82+
return err
83+
}
84+
85+
if ok, err := c.WriteJSON(out, data); ok {
86+
return err
87+
}
88+
89+
text.PrintRedactionTbl(out, data.Data)
90+
return nil
91+
}

0 commit comments

Comments
 (0)