Skip to content

Commit 588f96b

Browse files
[CDTOOL-1055] Add support for ngwaf workspaces
1 parent 965baa2 commit 588f96b

17 files changed

+743
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
### Breaking:
88

99
### Enhancements:
10+
- feat(ngwaf): add support for workspaces ([#679](https://github.com/fastly/go-fastly/pull/679))
1011

1112
### Bug fixes:
1213

fastly/errors.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ var ErrMissingComputeACLID = NewFieldError("ComputeACLID")
9797
// requires a "ComputeACLIP" key, but one was not set.
9898
var ErrMissingComputeACLIP = NewFieldError("ComputeACLIP")
9999

100+
// ErrMissingWorkspaceID is an error that is returned when an input struct
101+
// requires a "WorkspaceID" key, but one was not set.
102+
var ErrMissingWorkspaceID = NewFieldError("WorkspaceID")
103+
100104
// ErrMissingContent is an error that is returned when an input struct
101105
// requires a "Content" key, but one was not set.
102106
var ErrMissingContent = NewFieldError("Content")

fastly/ngwaf/doc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// Package ngwaf contains subpackages which offer various operations to
2-
// configure ngwaf.
2+
// configure the Fastly Next-Gen WAF.
33
package ngwaf

fastly/ngwaf/v1/doc.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Package v1 contains functionality which allows you to manage
2+
// Fastly Next-Gen WAF workspaces, requests, events, redactions,
3+
// tags, and rules using its v1 implementation.
4+
package v1
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package workspaces
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
8+
"github.com/fastly/go-fastly/v10/fastly"
9+
)
10+
11+
// AttackSignalThresholdsCreateInput are the parameters for system site alerts.
12+
type AttackSignalThresholdsCreateInput struct {
13+
OneMinute *int `json:"one_minute,omitempty"`
14+
TenMinutes *int `json:"ten_minutes,omitempty"`
15+
OneHour *int `json:"one_hour,omitempty"`
16+
Immediate *bool `json:"immediate,omitempty"`
17+
}
18+
19+
// CreateInput specifies the information needed for the Create() function to
20+
// perform the operation.
21+
type CreateInput struct {
22+
// Context, if supplied, will be used as the Request's context.
23+
Context *context.Context `json:"-"`
24+
// Name is the name of a workspace to create (required).
25+
Name *string `json:"name"`
26+
// Description is the description of a workspace.
27+
Description *string `json:"description"`
28+
// Mode is the mode of a workspace.
29+
Mode *string `json:"mode"`
30+
// AttackSignalThresholds are the parameters for system site alerts.
31+
AttackSignalThresholds *AttackSignalThresholdsCreateInput `json:"attack_signal_thresholds,omitempty"`
32+
// IPAnonymization is the selected option to anonymize IP addresses.
33+
IPAnonymization *string `json:"ip_anonymization"`
34+
}
35+
36+
// Create creates a new workspace.
37+
func Create(c *fastly.Client, i *CreateInput) (*Workspace, error) {
38+
if i.Name == nil {
39+
return nil, fastly.ErrMissingName
40+
}
41+
42+
resp, err := c.PostJSON("/ngwaf/v1/workspaces", i, fastly.CreateRequestOptions(i.Context))
43+
if err != nil {
44+
return nil, err
45+
}
46+
defer resp.Body.Close()
47+
48+
var ws *Workspace
49+
if err := json.NewDecoder(resp.Body).Decode(&ws); err != nil {
50+
return nil, fmt.Errorf("failed to decode json response: %w", err)
51+
}
52+
53+
return ws, nil
54+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package workspaces
2+
3+
import (
4+
"context"
5+
"net/http"
6+
7+
"github.com/fastly/go-fastly/v10/fastly"
8+
)
9+
10+
// DeleteInput specifies the information needed for the Delete() function to
11+
// perform the operation.
12+
type DeleteInput struct {
13+
// Context, if supplied, will be used as the Request's context.
14+
Context *context.Context
15+
// WorkspaceID is the workspace identifier (required).
16+
WorkspaceID *string
17+
}
18+
19+
// Delete deletes the specified workspace.
20+
func Delete(c *fastly.Client, i *DeleteInput) error {
21+
if i.WorkspaceID == nil {
22+
return fastly.ErrMissingWorkspaceID
23+
}
24+
25+
path := fastly.ToSafeURL("ngwaf", "v1", "workspaces", *i.WorkspaceID)
26+
27+
resp, err := c.Delete(path, fastly.CreateRequestOptions(i.Context))
28+
if err != nil {
29+
return err
30+
}
31+
defer resp.Body.Close()
32+
33+
if resp.StatusCode != http.StatusNoContent {
34+
return fastly.NewHTTPError(resp)
35+
}
36+
37+
return nil
38+
}

fastly/ngwaf/v1/workspaces/api_get.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package workspaces
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
8+
"github.com/fastly/go-fastly/v10/fastly"
9+
)
10+
11+
// GetInput specifies the information needed for the Get() function to perform
12+
// the operation.
13+
type GetInput struct {
14+
// Context, if supplied, will be used as the Request's context.
15+
Context *context.Context
16+
// WorkspaceID is the workspace identifier (required).
17+
WorkspaceID *string
18+
}
19+
20+
// Get retrieves the specified workspace.
21+
func Get(c *fastly.Client, i *GetInput) (*Workspace, error) {
22+
if i.WorkspaceID == nil {
23+
return nil, fastly.ErrMissingWorkspaceID
24+
}
25+
26+
path := fastly.ToSafeURL("ngwaf", "v1", "workspaces", *i.WorkspaceID)
27+
28+
resp, err := c.Get(path, fastly.CreateRequestOptions(i.Context))
29+
if err != nil {
30+
return nil, err
31+
}
32+
defer resp.Body.Close()
33+
34+
var ws *Workspace
35+
if err := json.NewDecoder(resp.Body).Decode(&ws); err != nil {
36+
return nil, fmt.Errorf("failed to decode json response: %w", err)
37+
}
38+
39+
return ws, nil
40+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package workspaces
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
"strconv"
8+
9+
"github.com/fastly/go-fastly/v10/fastly"
10+
)
11+
12+
// ListInput specifies the information needed for the List() function to perform
13+
// the operation.
14+
type ListInput struct {
15+
// Context, if supplied, will be used as the Request's context.
16+
Context *context.Context
17+
// Limit how many results are returned.
18+
Limit *int
19+
// Mode filter results based on mode.
20+
Mode *string
21+
// Page number of the collection to request.
22+
Page *int
23+
}
24+
25+
// List retrieves a list of workspaces, with optional filtering and pagination.
26+
func List(c *fastly.Client, i *ListInput) (*Workspaces, error) {
27+
requestOptions := fastly.CreateRequestOptions(i.Context)
28+
if i.Limit != nil {
29+
requestOptions.Params["limit"] = strconv.Itoa(*i.Limit)
30+
}
31+
if i.Mode != nil {
32+
requestOptions.Params["mode"] = *i.Mode
33+
}
34+
if i.Page != nil {
35+
requestOptions.Params["page"] = strconv.Itoa(*i.Page)
36+
}
37+
38+
resp, err := c.Get("/ngwaf/v1/workspaces", requestOptions)
39+
if err != nil {
40+
return nil, err
41+
}
42+
defer resp.Body.Close()
43+
44+
var ws *Workspaces
45+
if err := json.NewDecoder(resp.Body).Decode(&ws); err != nil {
46+
return nil, fmt.Errorf("failed to decode json response: %w", err)
47+
}
48+
49+
return ws, nil
50+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package workspaces
2+
3+
import "time"
4+
5+
// Workspace is the API response structure for the create, update, and get operations.
6+
type Workspace struct {
7+
// WorkspaceID is the workspace identifier (UUID).
8+
WorkspaceID string `json:"id"`
9+
// Name is the name of the workspace.
10+
Name string `json:"name"`
11+
// Description is the description of the workspace.
12+
Description string `json:"description"`
13+
// Mode is the mode of the workspace.
14+
Mode string `json:"mode"`
15+
// AttackSignalThresholds are the parameters for system site alerts.
16+
AttackSignalThresholds AttackSignalThresholds `json:"attack_signal_thresholds"`
17+
// IPAnonymization is the selected option to anonymize IP addresses.
18+
IPAnonymization string `json:"ip_anonymization"`
19+
// CreatedAt is the date and time in ISO 8601 format.
20+
CreatedAt time.Time `json:"created_at"`
21+
// UpdatedAt is the date and time in ISO 8601 format.
22+
UpdatedAt time.Time `json:"updated_at"`
23+
}
24+
25+
// AttackSignalThresholds are the parameters for system site alerts.
26+
type AttackSignalThresholds struct {
27+
OneMinute int `json:"one_minute"`
28+
TenMinutes int `json:"ten_minutes"`
29+
OneHour int `json:"one_hour"`
30+
Immediate bool `json:"immediate"`
31+
}
32+
33+
// Workspaces is the API response structure for the list workspaces operation.
34+
type Workspaces struct {
35+
// Data is the list of returned workspaces.
36+
Data []Workspace `json:"data"`
37+
// Meta is the information for total workspaces.
38+
Meta MetaWorkspaces `json:"meta"`
39+
}
40+
41+
// MetaWorkspaces is a subset of the Workspaces response structure.
42+
type MetaWorkspaces struct {
43+
// Limit is the limit of workspaces.
44+
Limit int `json:"limit"`
45+
// Total is the sum of workspaces.
46+
Total int `json:"total"`
47+
}

0 commit comments

Comments
 (0)