Skip to content

Commit b8a9aac

Browse files
feat(ngwaf): add CRUD operations for ngwaf custom signals at the account and workspace levels
1 parent 07ef70b commit b8a9aac

19 files changed

Lines changed: 1736 additions & 1 deletion

File tree

pkg/commands/commands.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ import (
5757
"github.com/fastly/cli/pkg/commands/logtail"
5858
"github.com/fastly/cli/pkg/commands/ngwaf"
5959
"github.com/fastly/cli/pkg/commands/ngwaf/countrylist"
60+
"github.com/fastly/cli/pkg/commands/ngwaf/customsignal"
6061
"github.com/fastly/cli/pkg/commands/ngwaf/iplist"
6162
"github.com/fastly/cli/pkg/commands/ngwaf/signallist"
6263
"github.com/fastly/cli/pkg/commands/ngwaf/stringlist"
6364
"github.com/fastly/cli/pkg/commands/ngwaf/wildcardlist"
6465
"github.com/fastly/cli/pkg/commands/ngwaf/workspace"
6566
wscountrylist "github.com/fastly/cli/pkg/commands/ngwaf/workspace/countrylist"
67+
wscustomsignal "github.com/fastly/cli/pkg/commands/ngwaf/workspace/customsignal"
6668
wsiplist "github.com/fastly/cli/pkg/commands/ngwaf/workspace/iplist"
6769
"github.com/fastly/cli/pkg/commands/ngwaf/workspace/redaction"
6870
wssignallistlist "github.com/fastly/cli/pkg/commands/ngwaf/workspace/signallist"
@@ -426,6 +428,12 @@ func Define( // nolint:revive // function-length
426428
ngwafCountryListGet := countrylist.NewGetCommand(ngwafCountryListRoot.CmdClause, data)
427429
ngwafCountryListList := countrylist.NewListCommand(ngwafCountryListRoot.CmdClause, data)
428430
ngwafCountryListUpdate := countrylist.NewUpdateCommand(ngwafCountryListRoot.CmdClause, data)
431+
ngwafCustomSignalRoot := customsignal.NewRootCommand(ngwafRoot.CmdClause, data)
432+
ngwafCustomSignalCreate := customsignal.NewCreateCommand(ngwafCustomSignalRoot.CmdClause, data)
433+
ngwafCustomSignalDelete := customsignal.NewDeleteCommand(ngwafCustomSignalRoot.CmdClause, data)
434+
ngwafCustomSignalGet := customsignal.NewGetCommand(ngwafCustomSignalRoot.CmdClause, data)
435+
ngwafCustomSignalList := customsignal.NewListCommand(ngwafCustomSignalRoot.CmdClause, data)
436+
ngwafCustomSignalUpdate := customsignal.NewUpdateCommand(ngwafCustomSignalRoot.CmdClause, data)
429437
ngwafIPListRoot := iplist.NewRootCommand(ngwafRoot.CmdClause, data)
430438
ngwafIPListCreate := iplist.NewCreateCommand(ngwafIPListRoot.CmdClause, data)
431439
ngwafIPListDelete := iplist.NewDeleteCommand(ngwafIPListRoot.CmdClause, data)
@@ -456,6 +464,12 @@ func Define( // nolint:revive // function-length
456464
ngwafWorkspaceCountryListGet := wscountrylist.NewGetCommand(ngwafWorkspaceCountryListRoot.CmdClause, data)
457465
ngwafWorkspaceCountryListList := wscountrylist.NewListCommand(ngwafWorkspaceCountryListRoot.CmdClause, data)
458466
ngwafWorkspaceCountryListUpdate := wscountrylist.NewUpdateCommand(ngwafWorkspaceCountryListRoot.CmdClause, data)
467+
ngwafWorkspaceCustomSignalRoot := wscustomsignal.NewRootCommand(ngwafWorkspaceRoot.CmdClause, data)
468+
ngwafWorkspaceCustomSignalCreate := wscustomsignal.NewCreateCommand(ngwafWorkspaceCustomSignalRoot.CmdClause, data)
469+
ngwafWorkspaceCustomSignalDelete := wscustomsignal.NewDeleteCommand(ngwafWorkspaceCustomSignalRoot.CmdClause, data)
470+
ngwafWorkspaceCustomSignalGet := wscustomsignal.NewGetCommand(ngwafWorkspaceCustomSignalRoot.CmdClause, data)
471+
ngwafWorkspaceCustomSignalList := wscustomsignal.NewListCommand(ngwafWorkspaceCustomSignalRoot.CmdClause, data)
472+
ngwafWorkspaceCustomSignalUpdate := wscustomsignal.NewUpdateCommand(ngwafWorkspaceCustomSignalRoot.CmdClause, data)
459473
ngwafWorkspaceIPListRoot := wsiplist.NewRootCommand(ngwafWorkspaceRoot.CmdClause, data)
460474
ngwafWorkspaceIPListCreate := wsiplist.NewCreateCommand(ngwafWorkspaceIPListRoot.CmdClause, data)
461475
ngwafWorkspaceIPListDelete := wsiplist.NewDeleteCommand(ngwafWorkspaceIPListRoot.CmdClause, data)
@@ -916,6 +930,12 @@ func Define( // nolint:revive // function-length
916930
ngwafCountryListGet,
917931
ngwafCountryListList,
918932
ngwafCountryListUpdate,
933+
ngwafCustomSignalRoot,
934+
ngwafCustomSignalCreate,
935+
ngwafCustomSignalDelete,
936+
ngwafCustomSignalGet,
937+
ngwafCustomSignalList,
938+
ngwafCustomSignalUpdate,
919939
ngwafIPListRoot,
920940
ngwafIPListCreate,
921941
ngwafIPListDelete,
@@ -945,6 +965,12 @@ func Define( // nolint:revive // function-length
945965
ngwafWorkspaceCountryListGet,
946966
ngwafWorkspaceCountryListList,
947967
ngwafWorkspaceCountryListUpdate,
968+
ngwafWorkspaceCustomSignalRoot,
969+
ngwafWorkspaceCustomSignalCreate,
970+
ngwafWorkspaceCustomSignalDelete,
971+
ngwafWorkspaceCustomSignalGet,
972+
ngwafWorkspaceCustomSignalList,
973+
ngwafWorkspaceCustomSignalUpdate,
948974
ngwafWorkspaceIPListRoot,
949975
ngwafWorkspaceIPListCreate,
950976
ngwafWorkspaceIPListDelete,
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package customsignal
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/scope"
10+
"github.com/fastly/go-fastly/v12/fastly/ngwaf/v1/signals"
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+
// CreateCommand calls the Fastly API to create account-level custom signals.
19+
type CreateCommand struct {
20+
argparser.Base
21+
argparser.JSONOutput
22+
23+
// Required.
24+
name string
25+
26+
// Optional.
27+
description argparser.OptionalString
28+
}
29+
30+
// NewCreateCommand returns a usable command registered under the parent.
31+
func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateCommand {
32+
c := CreateCommand{
33+
Base: argparser.Base{
34+
Globals: g,
35+
},
36+
}
37+
c.CmdClause = parent.Command("create", "Create an account-level custom signal").Alias("add")
38+
39+
// Required.
40+
c.CmdClause.Flag("name", "User submitted display name of a custom signal. Is immutable and must be between 3 and 25 characters").Required().StringVar(&c.name)
41+
42+
// Optional.
43+
c.CmdClause.Flag("description", "User submitted description of a custom signal.").Action(c.description.Set).StringVar(&c.description.Value)
44+
c.RegisterFlagBool(c.JSONFlag())
45+
46+
return &c
47+
}
48+
49+
// Exec invokes the application logic for the command.
50+
func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
51+
if c.Globals.Verbose() && c.JSONOutput.Enabled {
52+
return fsterr.ErrInvalidVerboseJSONCombo
53+
}
54+
var err error
55+
input := &signals.CreateInput{
56+
Name: &c.name,
57+
Scope: &scope.Scope{
58+
Type: scope.ScopeTypeAccount,
59+
AppliesTo: []string{"*"},
60+
},
61+
}
62+
if c.description.WasSet {
63+
input.Description = &c.description.Value
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+
data, err := signals.Create(context.TODO(), fc, input)
72+
if err != nil {
73+
return err
74+
}
75+
76+
if ok, err := c.WriteJSON(out, data); ok {
77+
return err
78+
}
79+
80+
text.Success(out, "Created account-level custom signal '%s' (signal-id: %s)", data.Name, data.SignalID)
81+
return nil
82+
}

0 commit comments

Comments
 (0)