Skip to content

Commit 0e669d1

Browse files
authored
[CDTOOL- 1216] Support for NGWAF Alerts (#1589)
### Change summary <!-- Briefly describe the changes introduced in this pull request. Include context or reasoning behind the changes, even if they seem minor. If relevant, link to any related discussions (e.g. Slack threads, tickets, documents). --> 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? ### 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 71b90c7 commit 0e669d1

73 files changed

Lines changed: 8066 additions & 1 deletion

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
### Enhancements:
88
- feat(commands/ngwaf/workspaces): add support for update operation for NGWAF workspaces ([#1578](https://github.com/fastly/cli/pull/1578))
9-
- feat(commands/ngwaf/lists): add support for CRUD operations for NGWAF Lists ([#1582](https://github.com/fastly/cli/pull/1582))
9+
- feat(commands/ngwaf/lists): add support for CRUD operations for NGWAF Lists at account and workspace levels ([#1582](https://github.com/fastly/cli/pull/1582))
10+
- feat(commands/ngwaf/workspaces/alerts): add support for operations for NGWAF alerts ([#1589](https://github.com/fastly/cli/pull/1589))
1011
- feat(commands/ngwaf/customsignals): add support for CRUD operations for NGWAF Custom Signals ([#1592](https://github.com/fastly/cli/pull/1592))
1112

1213
### Bug fixes:

pkg/argparser/common.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ var (
99
FlagJSONName = "json"
1010
// FlagJSONDesc is the flag description.
1111
FlagJSONDesc = "Render output as JSON"
12+
// FlagNGWAFAlertID is the alert ID.
13+
FlagNGWAFAlertID = "alert-id"
14+
// FlagNGWAFAlertIDDesc is the alert ID flag description.
15+
FlagNGWAFAlertIDDesc = "Alphanumeric string identifying the alert"
1216
// FlagNGWAFWorkspaceID is the workspace ID.
1317
FlagNGWAFWorkspaceID = "workspace-id"
1418
// FlagNGWAFWorkspaceIDDesc is the workspace ID flag description.

pkg/commands/commands.go

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,15 @@ import (
6363
"github.com/fastly/cli/pkg/commands/ngwaf/stringlist"
6464
"github.com/fastly/cli/pkg/commands/ngwaf/wildcardlist"
6565
"github.com/fastly/cli/pkg/commands/ngwaf/workspace"
66+
"github.com/fastly/cli/pkg/commands/ngwaf/workspace/alert"
67+
workspaceAlertDatadog "github.com/fastly/cli/pkg/commands/ngwaf/workspace/alert/datadog"
68+
workspaceAlertJira "github.com/fastly/cli/pkg/commands/ngwaf/workspace/alert/jira"
69+
workspaceAlertMailinglist "github.com/fastly/cli/pkg/commands/ngwaf/workspace/alert/mailinglist"
70+
workspaceAlertMicrosoftteams "github.com/fastly/cli/pkg/commands/ngwaf/workspace/alert/microsoftteams"
71+
workspaceAlertOpsgenie "github.com/fastly/cli/pkg/commands/ngwaf/workspace/alert/opsgenie"
72+
workspaceAlertPagerduty "github.com/fastly/cli/pkg/commands/ngwaf/workspace/alert/pagerduty"
73+
workspaceAlertSlack "github.com/fastly/cli/pkg/commands/ngwaf/workspace/alert/slack"
74+
workspaceAlertWebhook "github.com/fastly/cli/pkg/commands/ngwaf/workspace/alert/webhook"
6675
wscountrylist "github.com/fastly/cli/pkg/commands/ngwaf/workspace/countrylist"
6776
wscustomsignal "github.com/fastly/cli/pkg/commands/ngwaf/workspace/customsignal"
6877
wsiplist "github.com/fastly/cli/pkg/commands/ngwaf/workspace/iplist"
@@ -498,6 +507,57 @@ func Define( // nolint:revive // function-length
498507
ngwafVirtualpatchList := virtualpatch.NewListCommand(ngwafVirtualpatchRoot.CmdClause, data)
499508
ngwafVirtualpatchUpdate := virtualpatch.NewUpdateCommand(ngwafVirtualpatchRoot.CmdClause, data)
500509
ngwafVirtualpatchRetrieve := virtualpatch.NewRetrieveCommand(ngwafVirtualpatchRoot.CmdClause, data)
510+
ngwafWorkspaceAlertRoot := alert.NewRootCommand(ngwafWorkspaceRoot.CmdClause, data)
511+
ngwafWorkspaceAlertDatadogRoot := workspaceAlertDatadog.NewRootCommand(ngwafWorkspaceAlertRoot.CmdClause, data)
512+
ngwafWorkspaceAlertDatadogCreate := workspaceAlertDatadog.NewCreateCommand(ngwafWorkspaceAlertDatadogRoot.CmdClause, data)
513+
ngwafWorkspaceAlertDatadogDelete := workspaceAlertDatadog.NewDeleteCommand(ngwafWorkspaceAlertDatadogRoot.CmdClause, data)
514+
ngwafWorkspaceAlertDatadogGet := workspaceAlertDatadog.NewGetCommand(ngwafWorkspaceAlertDatadogRoot.CmdClause, data)
515+
ngwafWorkspaceAlertDatadogList := workspaceAlertDatadog.NewListCommand(ngwafWorkspaceAlertDatadogRoot.CmdClause, data)
516+
ngwafWorkspaceAlertDatadogUpdate := workspaceAlertDatadog.NewUpdateCommand(ngwafWorkspaceAlertDatadogRoot.CmdClause, data)
517+
ngwafWorkspaceAlertJiraRoot := workspaceAlertJira.NewRootCommand(ngwafWorkspaceAlertRoot.CmdClause, data)
518+
ngwafWorkspaceAlertJiraCreate := workspaceAlertJira.NewCreateCommand(ngwafWorkspaceAlertJiraRoot.CmdClause, data)
519+
ngwafWorkspaceAlertJiraDelete := workspaceAlertJira.NewDeleteCommand(ngwafWorkspaceAlertJiraRoot.CmdClause, data)
520+
ngwafWorkspaceAlertJiraGet := workspaceAlertJira.NewGetCommand(ngwafWorkspaceAlertJiraRoot.CmdClause, data)
521+
ngwafWorkspaceAlertJiraList := workspaceAlertJira.NewListCommand(ngwafWorkspaceAlertJiraRoot.CmdClause, data)
522+
ngwafWorkspaceAlertJiraUpdate := workspaceAlertJira.NewUpdateCommand(ngwafWorkspaceAlertJiraRoot.CmdClause, data)
523+
ngwafWorkspaceAlertMailinglistRoot := workspaceAlertMailinglist.NewRootCommand(ngwafWorkspaceAlertRoot.CmdClause, data)
524+
ngwafWorkspaceAlertMailinglistCreate := workspaceAlertMailinglist.NewCreateCommand(ngwafWorkspaceAlertMailinglistRoot.CmdClause, data)
525+
ngwafWorkspaceAlertMailinglistDelete := workspaceAlertMailinglist.NewDeleteCommand(ngwafWorkspaceAlertMailinglistRoot.CmdClause, data)
526+
ngwafWorkspaceAlertMailinglistGet := workspaceAlertMailinglist.NewGetCommand(ngwafWorkspaceAlertMailinglistRoot.CmdClause, data)
527+
ngwafWorkspaceAlertMailinglistList := workspaceAlertMailinglist.NewListCommand(ngwafWorkspaceAlertMailinglistRoot.CmdClause, data)
528+
ngwafWorkspaceAlertMailinglistUpdate := workspaceAlertMailinglist.NewUpdateCommand(ngwafWorkspaceAlertMailinglistRoot.CmdClause, data)
529+
ngwafWorkspaceAlertMicrosoftteamsRoot := workspaceAlertMicrosoftteams.NewRootCommand(ngwafWorkspaceAlertRoot.CmdClause, data)
530+
ngwafWorkspaceAlertMicrosoftteamsCreate := workspaceAlertMicrosoftteams.NewCreateCommand(ngwafWorkspaceAlertMicrosoftteamsRoot.CmdClause, data)
531+
ngwafWorkspaceAlertMicrosoftteamsDelete := workspaceAlertMicrosoftteams.NewDeleteCommand(ngwafWorkspaceAlertMicrosoftteamsRoot.CmdClause, data)
532+
ngwafWorkspaceAlertMicrosoftteamsGet := workspaceAlertMicrosoftteams.NewGetCommand(ngwafWorkspaceAlertMicrosoftteamsRoot.CmdClause, data)
533+
ngwafWorkspaceAlertMicrosoftteamsList := workspaceAlertMicrosoftteams.NewListCommand(ngwafWorkspaceAlertMicrosoftteamsRoot.CmdClause, data)
534+
ngwafWorkspaceAlertMicrosoftteamsUpdate := workspaceAlertMicrosoftteams.NewUpdateCommand(ngwafWorkspaceAlertMicrosoftteamsRoot.CmdClause, data)
535+
ngwafWorkspaceAlertOpsgenieRoot := workspaceAlertOpsgenie.NewRootCommand(ngwafWorkspaceAlertRoot.CmdClause, data)
536+
ngwafWorkspaceAlertOpsgenieCreate := workspaceAlertOpsgenie.NewCreateCommand(ngwafWorkspaceAlertOpsgenieRoot.CmdClause, data)
537+
ngwafWorkspaceAlertOpsgenieDelete := workspaceAlertOpsgenie.NewDeleteCommand(ngwafWorkspaceAlertOpsgenieRoot.CmdClause, data)
538+
ngwafWorkspaceAlertOpsgenieGet := workspaceAlertOpsgenie.NewGetCommand(ngwafWorkspaceAlertOpsgenieRoot.CmdClause, data)
539+
ngwafWorkspaceAlertOpsgenieList := workspaceAlertOpsgenie.NewListCommand(ngwafWorkspaceAlertOpsgenieRoot.CmdClause, data)
540+
ngwafWorkspaceAlertOpsgenieUpdate := workspaceAlertOpsgenie.NewUpdateCommand(ngwafWorkspaceAlertOpsgenieRoot.CmdClause, data)
541+
ngwafWorkspaceAlertPagerdutyRoot := workspaceAlertPagerduty.NewRootCommand(ngwafWorkspaceAlertRoot.CmdClause, data)
542+
ngwafWorkspaceAlertPagerdutyCreate := workspaceAlertPagerduty.NewCreateCommand(ngwafWorkspaceAlertPagerdutyRoot.CmdClause, data)
543+
ngwafWorkspaceAlertPagerdutyDelete := workspaceAlertPagerduty.NewDeleteCommand(ngwafWorkspaceAlertPagerdutyRoot.CmdClause, data)
544+
ngwafWorkspaceAlertPagerdutyGet := workspaceAlertPagerduty.NewGetCommand(ngwafWorkspaceAlertPagerdutyRoot.CmdClause, data)
545+
ngwafWorkspaceAlertPagerdutyList := workspaceAlertPagerduty.NewListCommand(ngwafWorkspaceAlertPagerdutyRoot.CmdClause, data)
546+
ngwafWorkspaceAlertPagerdutyUpdate := workspaceAlertPagerduty.NewUpdateCommand(ngwafWorkspaceAlertPagerdutyRoot.CmdClause, data)
547+
ngwafWorkspaceAlertSlackRoot := workspaceAlertSlack.NewRootCommand(ngwafWorkspaceAlertRoot.CmdClause, data)
548+
ngwafWorkspaceAlertSlackCreate := workspaceAlertSlack.NewCreateCommand(ngwafWorkspaceAlertSlackRoot.CmdClause, data)
549+
ngwafWorkspaceAlertSlackDelete := workspaceAlertSlack.NewDeleteCommand(ngwafWorkspaceAlertSlackRoot.CmdClause, data)
550+
ngwafWorkspaceAlertSlackGet := workspaceAlertSlack.NewGetCommand(ngwafWorkspaceAlertSlackRoot.CmdClause, data)
551+
ngwafWorkspaceAlertSlackList := workspaceAlertSlack.NewListCommand(ngwafWorkspaceAlertSlackRoot.CmdClause, data)
552+
ngwafWorkspaceAlertSlackUpdate := workspaceAlertSlack.NewUpdateCommand(ngwafWorkspaceAlertSlackRoot.CmdClause, data)
553+
ngwafWorkspaceAlertWebhookRoot := workspaceAlertWebhook.NewRootCommand(ngwafWorkspaceAlertRoot.CmdClause, data)
554+
ngwafWorkspaceAlertWebhookCreate := workspaceAlertWebhook.NewCreateCommand(ngwafWorkspaceAlertWebhookRoot.CmdClause, data)
555+
ngwafWorkspaceAlertWebhookDelete := workspaceAlertWebhook.NewDeleteCommand(ngwafWorkspaceAlertWebhookRoot.CmdClause, data)
556+
ngwafWorkspaceAlertWebhookGet := workspaceAlertWebhook.NewGetCommand(ngwafWorkspaceAlertWebhookRoot.CmdClause, data)
557+
ngwafWorkspaceAlertWebhookGetSigningKey := workspaceAlertWebhook.NewGetSigningKeyCommand(ngwafWorkspaceAlertWebhookRoot.CmdClause, data)
558+
ngwafWorkspaceAlertWebhookList := workspaceAlertWebhook.NewListCommand(ngwafWorkspaceAlertWebhookRoot.CmdClause, data)
559+
ngwafWorkspaceAlertWebhookRotateSigningKey := workspaceAlertWebhook.NewRotateSigningKeyCommand(ngwafWorkspaceAlertWebhookRoot.CmdClause, data)
560+
ngwafWorkspaceAlertWebhookUpdate := workspaceAlertWebhook.NewUpdateCommand(ngwafWorkspaceAlertWebhookRoot.CmdClause, data)
501561
objectStorageRoot := objectstorage.NewRootCommand(app, data)
502562
objectStorageAccesskeysRoot := accesskeys.NewRootCommand(objectStorageRoot.CmdClause, data)
503563
objectStorageAccesskeysCreate := accesskeys.NewCreateCommand(objectStorageAccesskeysRoot.CmdClause, data)
@@ -998,6 +1058,57 @@ func Define( // nolint:revive // function-length
9981058
ngwafVirtualpatchRetrieve,
9991059
ngwafVirtualpatchRoot,
10001060
ngwafVirtualpatchUpdate,
1061+
ngwafWorkspaceAlertRoot,
1062+
ngwafWorkspaceAlertDatadogRoot,
1063+
ngwafWorkspaceAlertDatadogCreate,
1064+
ngwafWorkspaceAlertDatadogDelete,
1065+
ngwafWorkspaceAlertDatadogGet,
1066+
ngwafWorkspaceAlertDatadogList,
1067+
ngwafWorkspaceAlertDatadogUpdate,
1068+
ngwafWorkspaceAlertJiraRoot,
1069+
ngwafWorkspaceAlertJiraCreate,
1070+
ngwafWorkspaceAlertJiraDelete,
1071+
ngwafWorkspaceAlertJiraGet,
1072+
ngwafWorkspaceAlertJiraList,
1073+
ngwafWorkspaceAlertJiraUpdate,
1074+
ngwafWorkspaceAlertMailinglistRoot,
1075+
ngwafWorkspaceAlertMailinglistCreate,
1076+
ngwafWorkspaceAlertMailinglistDelete,
1077+
ngwafWorkspaceAlertMailinglistGet,
1078+
ngwafWorkspaceAlertMailinglistList,
1079+
ngwafWorkspaceAlertMailinglistUpdate,
1080+
ngwafWorkspaceAlertMicrosoftteamsRoot,
1081+
ngwafWorkspaceAlertMicrosoftteamsCreate,
1082+
ngwafWorkspaceAlertMicrosoftteamsDelete,
1083+
ngwafWorkspaceAlertMicrosoftteamsGet,
1084+
ngwafWorkspaceAlertMicrosoftteamsList,
1085+
ngwafWorkspaceAlertMicrosoftteamsUpdate,
1086+
ngwafWorkspaceAlertOpsgenieRoot,
1087+
ngwafWorkspaceAlertOpsgenieCreate,
1088+
ngwafWorkspaceAlertOpsgenieDelete,
1089+
ngwafWorkspaceAlertOpsgenieGet,
1090+
ngwafWorkspaceAlertOpsgenieList,
1091+
ngwafWorkspaceAlertOpsgenieUpdate,
1092+
ngwafWorkspaceAlertPagerdutyRoot,
1093+
ngwafWorkspaceAlertPagerdutyCreate,
1094+
ngwafWorkspaceAlertPagerdutyDelete,
1095+
ngwafWorkspaceAlertPagerdutyGet,
1096+
ngwafWorkspaceAlertPagerdutyList,
1097+
ngwafWorkspaceAlertPagerdutyUpdate,
1098+
ngwafWorkspaceAlertSlackRoot,
1099+
ngwafWorkspaceAlertSlackCreate,
1100+
ngwafWorkspaceAlertSlackDelete,
1101+
ngwafWorkspaceAlertSlackGet,
1102+
ngwafWorkspaceAlertSlackList,
1103+
ngwafWorkspaceAlertSlackUpdate,
1104+
ngwafWorkspaceAlertWebhookRoot,
1105+
ngwafWorkspaceAlertWebhookCreate,
1106+
ngwafWorkspaceAlertWebhookDelete,
1107+
ngwafWorkspaceAlertWebhookGet,
1108+
ngwafWorkspaceAlertWebhookGetSigningKey,
1109+
ngwafWorkspaceAlertWebhookList,
1110+
ngwafWorkspaceAlertWebhookRotateSigningKey,
1111+
ngwafWorkspaceAlertWebhookUpdate,
10011112
ngwafWorkspaceRoot,
10021113
ngwafWorkspaceCreate,
10031114
ngwafWorkspaceDelete,
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package datadog
2+
3+
import (
4+
"context"
5+
"errors"
6+
"io"
7+
8+
"github.com/fastly/cli/pkg/argparser"
9+
"github.com/fastly/cli/pkg/commands/ngwaf/workspace/alert"
10+
11+
fsterr "github.com/fastly/cli/pkg/errors"
12+
"github.com/fastly/cli/pkg/global"
13+
"github.com/fastly/cli/pkg/text"
14+
"github.com/fastly/go-fastly/v12/fastly"
15+
"github.com/fastly/go-fastly/v12/fastly/ngwaf/v1/workspaces/alerts/datadog"
16+
)
17+
18+
// CreateCommand calls the Fastly API to create Datadog alerts.
19+
type CreateCommand struct {
20+
argparser.Base
21+
argparser.JSONOutput
22+
23+
// Required.
24+
WorkspaceID argparser.OptionalWorkspaceID
25+
Key string
26+
Site string
27+
28+
// Optional.
29+
Description argparser.OptionalString
30+
}
31+
32+
// NewCreateCommand returns a usable command registered under the parent.
33+
func NewCreateCommand(parent argparser.Registerer, g *global.Data) *CreateCommand {
34+
c := CreateCommand{
35+
Base: argparser.Base{
36+
Globals: g,
37+
},
38+
}
39+
c.CmdClause = parent.Command("create", "Create a Datadog alert").Alias("add")
40+
41+
// Required.
42+
c.RegisterFlag(argparser.StringFlagOpts{
43+
Name: argparser.FlagNGWAFWorkspaceID,
44+
Description: argparser.FlagNGWAFWorkspaceIDDesc,
45+
Dst: &c.WorkspaceID.Value,
46+
Action: c.WorkspaceID.Set,
47+
})
48+
c.CmdClause.Flag("key", "Datadog integration key.").Required().StringVar(&c.Key)
49+
c.CmdClause.Flag("site", "Datadog site.").Required().StringVar(&c.Site)
50+
51+
// Optional.
52+
c.CmdClause.Flag("description", "An optional description for the alert.").Action(c.Description.Set).StringVar(&c.Description.Value)
53+
c.RegisterFlagBool(c.JSONFlag())
54+
55+
return &c
56+
}
57+
58+
// Exec invokes the application logic for the command.
59+
func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
60+
// Call Parse() to ensure that we check if workspaceID
61+
// is set or to throw the appropriate error.
62+
if err := c.WorkspaceID.Parse(); err != nil {
63+
return err
64+
}
65+
if c.Globals.Verbose() && c.JSONOutput.Enabled {
66+
return fsterr.ErrInvalidVerboseJSONCombo
67+
}
68+
69+
input := &datadog.CreateInput{
70+
WorkspaceID: &c.WorkspaceID.Value,
71+
Config: &datadog.CreateConfig{
72+
Key: &c.Key,
73+
Site: &c.Site,
74+
},
75+
// Set 'Events' to the only possible value, 'flag'
76+
Events: alert.GetDefaultEvents(),
77+
}
78+
if c.Description.WasSet {
79+
input.Description = &c.Description.Value
80+
}
81+
82+
fc, ok := c.Globals.APIClient.(*fastly.Client)
83+
if !ok {
84+
return errors.New("failed to convert interface to a fastly client")
85+
}
86+
87+
data, err := datadog.Create(context.TODO(), fc, input)
88+
if err != nil {
89+
return err
90+
}
91+
92+
if ok, err := c.WriteJSON(out, data); ok {
93+
return err
94+
}
95+
96+
text.Success(out, "Created a '%s' alert '%s' (workspace-id: %s)", data.Type, data.ID, c.WorkspaceID.Value)
97+
return nil
98+
}

0 commit comments

Comments
 (0)