Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
- feat(compute/build): Allow usage of Rust 1.91.1 and later patch releases ([#1576](https://github.com/fastly/cli/pull/1576))
- feat(commands/ngwaf/workspaces): add support for CRUD operations for NGWAF workspaces ([#1570](https://github.com/fastly/cli/pull/1570))
- feat(commands/ngwaf/virtualpatch): add support for CRUD operations for NGWAF virtual patches ([#1579](https://github.com/fastly/cli/pull/1579))
- feat(commands/ngwaf/redaction): add support for CRUD operations for NGWAF redactions ([#1581](https://github.com/fastly/cli/pull/1581))

### Dependencies:
- build(deps): `golangci/golangci-lint-action` from 8 to 9 ([#1575](https://github.com/fastly/cli/pull/1575))
Expand Down
13 changes: 13 additions & 0 deletions pkg/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import (
"github.com/fastly/cli/pkg/commands/logging/syslog"
"github.com/fastly/cli/pkg/commands/logtail"
"github.com/fastly/cli/pkg/commands/ngwaf"
"github.com/fastly/cli/pkg/commands/ngwaf/redaction"
"github.com/fastly/cli/pkg/commands/ngwaf/virtualpatch"
"github.com/fastly/cli/pkg/commands/ngwaf/workspace"
"github.com/fastly/cli/pkg/commands/objectstorage"
Expand Down Expand Up @@ -397,6 +398,12 @@ func Define( // nolint:revive // function-length
loggingSyslogList := syslog.NewListCommand(loggingSyslogCmdRoot.CmdClause, data)
loggingSyslogUpdate := syslog.NewUpdateCommand(loggingSyslogCmdRoot.CmdClause, data)
ngwafRoot := ngwaf.NewRootCommand(app, data)
ngwafRedactionRoot := redaction.NewRootCommand(ngwafRoot.CmdClause, data)
ngwafRedactionCreate := redaction.NewCreateCommand(ngwafRedactionRoot.CmdClause, data)
ngwafRedactionDelete := redaction.NewDeleteCommand(ngwafRedactionRoot.CmdClause, data)
ngwafRedactionList := redaction.NewListCommand(ngwafRedactionRoot.CmdClause, data)
ngwafRedactionRetrieve := redaction.NewRetrieveCommand(ngwafRedactionRoot.CmdClause, data)
ngwafRedactionUpdate := redaction.NewUpdateCommand(ngwafRedactionRoot.CmdClause, data)
ngwafVirtualpatchRoot := virtualpatch.NewRootCommand(ngwafRoot.CmdClause, data)
ngwafVirtualpatchList := virtualpatch.NewListCommand(ngwafVirtualpatchRoot.CmdClause, data)
ngwafVirtualpatchUpdate := virtualpatch.NewUpdateCommand(ngwafVirtualpatchRoot.CmdClause, data)
Expand Down Expand Up @@ -827,6 +834,12 @@ func Define( // nolint:revive // function-length
loggingSyslogList,
loggingSyslogUpdate,
ngwafRoot,
ngwafRedactionCreate,
ngwafRedactionDelete,
ngwafRedactionList,
ngwafRedactionRetrieve,
ngwafRedactionUpdate,
ngwafRedactionRoot,
ngwafVirtualpatchList,
ngwafVirtualpatchRetrieve,
ngwafVirtualpatchRoot,
Expand Down
83 changes: 83 additions & 0 deletions pkg/commands/ngwaf/redaction/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package redaction

import (
"context"
"errors"
"io"

"github.com/fastly/go-fastly/v12/fastly"
"github.com/fastly/go-fastly/v12/fastly/ngwaf/v1/workspaces/redactions"

"github.com/fastly/cli/pkg/argparser"
"github.com/fastly/cli/pkg/global"
"github.com/fastly/cli/pkg/text"
)

// CreateCommand calls the Fastly API to create a redaction.
type CreateCommand struct {
argparser.Base
argparser.JSONOutput

// Required.
field string
redactionType string
workspaceID argparser.OptionalWorkspaceID
}

// NewUpdateCommand 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 redaction")

// Required.
c.CmdClause.Flag("field", "The name of the field that should be redacted.").Required().StringVar(&c.field)
c.CmdClause.Flag("type", "The type of field that is being redacted.").Required().StringVar(&c.redactionType)
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 {
// 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 err error
input := &redactions.CreateInput{
Field: &c.field,
Type: &c.redactionType,
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 := redactions.Create(context.TODO(), fc, input)
if err != nil {
return err
}

if ok, err := c.WriteJSON(out, data); ok {
return err
}

text.Success(out, "Created redaction '%s' (field: %s, type: %s)", data.RedactionID, data.Field, data.Type)
return nil
}
93 changes: 93 additions & 0 deletions pkg/commands/ngwaf/redaction/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package redaction

import (
"context"
"errors"
"io"

"github.com/fastly/go-fastly/v12/fastly"

"github.com/fastly/go-fastly/v12/fastly/ngwaf/v1/workspaces/redactions"

"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.
type DeleteCommand struct {
argparser.Base
argparser.JSONOutput

// Required.
workspaceID argparser.OptionalWorkspaceID
redactionID string
}

// 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", "Delete a redaction")

// Required.
c.CmdClause.Flag("redaction-id", "Workspace ID").Required().StringVar(&c.redactionID)
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 *DeleteCommand) Exec(_ io.Reader, out io.Writer) error {
// 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
}

if c.Globals.Verbose() && c.JSONOutput.Enabled {
return fsterr.ErrInvalidVerboseJSONCombo
}

fc, ok := c.Globals.APIClient.(*fastly.Client)
if !ok {
return errors.New("failed to convert interface to a fastly client")
}

err := redactions.Delete(context.TODO(), fc, &redactions.DeleteInput{
RedactionID: &c.redactionID,
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.redactionID,
true,
}
_, err := c.WriteJSON(out, o)
return err
}

text.Success(out, "Deleted redaction (id: %s)", c.redactionID)
return nil
}
91 changes: 91 additions & 0 deletions pkg/commands/ngwaf/redaction/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package redaction

import (
"context"
"errors"
"io"

"github.com/fastly/go-fastly/v12/fastly"
"github.com/fastly/go-fastly/v12/fastly/ngwaf/v1/workspaces/redactions"

"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"
)

// ListCommand calls the Fastly API to list redactions in a workspace.
type ListCommand struct {
argparser.Base
argparser.JSONOutput

// Required.
workspaceID argparser.OptionalWorkspaceID

// Optional.
limit argparser.OptionalInt
}

// NewListCommand returns a usable command registered under the parent.
func NewListCommand(parent argparser.Registerer, g *global.Data) *ListCommand {
c := ListCommand{
Base: argparser.Base{
Globals: g,
},
}

c.CmdClause = parent.Command("list", "List redactions in a workspace")

// Required.
c.RegisterFlag(argparser.StringFlagOpts{
Name: argparser.FlagNGWAFWorkspaceID,
Description: argparser.FlagNGWAFWorkspaceIDDesc,
Dst: &c.workspaceID.Value,
Action: c.workspaceID.Set,
})

// Optional.
c.CmdClause.Flag("limit", "Limit how many results are returned").Action(c.limit.Set).IntVar(&c.limit.Value)
c.RegisterFlagBool(c.JSONFlag())

return &c
}

// Exec invokes the application logic for the command.
func (c *ListCommand) Exec(_ io.Reader, out io.Writer) error {
// 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
}

if c.Globals.Verbose() && c.JSONOutput.Enabled {
return fsterr.ErrInvalidVerboseJSONCombo
}

fc, ok := c.Globals.APIClient.(*fastly.Client)
if !ok {
return errors.New("failed to convert interface to a fastly client")
}

input := &redactions.ListInput{
WorkspaceID: &c.workspaceID.Value,
}

if c.limit.WasSet {
input.Limit = &c.limit.Value
}

data, err := redactions.List(context.TODO(), fc, input)
if err != nil {
c.Globals.ErrLog.Add(err)
return err
}

if ok, err := c.WriteJSON(out, data); ok {
return err
}

text.PrintRedactionTbl(out, data.Data)
return nil
}
Loading