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 @@ -9,6 +9,7 @@
### Enhancements:

- feat(ngwaf): add support for rules ([#691](https://github.com/fastly/go-fastly/pull/691))
- feat(ngwaf): add support for lists ([#700](https://github.com/fastly/go-fastly/pull/700))

### Bug fixes:

Expand Down
8 changes: 8 additions & 0 deletions fastly/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ var ErrMissingComputeACLIP = NewFieldError("ComputeACLIP")
// requires a "WorkspaceID" key, but one was not set.
var ErrMissingWorkspaceID = NewFieldError("WorkspaceID")

// ErrMissingListID is an error that is returned when an input struct
// requires a "ListID" key, but one was not set.
var ErrMissingListID = NewFieldError("ListID")

// ErrMissingEntries is an error that is returned when an input struct
// requires a "Entries" key, but one was not set.
var ErrMissingEntries = NewFieldError("Entries")

// ErrMissingIsExpired is an error that is returned when an input struct
// requires an "IsExpired" key, but one was not set.
var ErrMissingIsExpired = NewFieldError("IsExpired")
Expand Down
57 changes: 57 additions & 0 deletions fastly/ngwaf/v1/lists/api_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package lists

import (
"context"
"encoding/json"
"fmt"

"github.com/fastly/go-fastly/v10/fastly"
)

// CreateInput specifies the information needed for the Create() function to
// perform the operation.
type CreateInput struct {
// Context, if supplied, will be used as the Request's context.
Context *context.Context
// Description is the description of the list.
Description *string `json:"description"`
// Entries are the entries of the list (required).
Entries *[]string `json:"entries"`
// Name is the name of the list (required).
Name *string `json:"name"`
// Type is the type of the list. Must be one of string | wildcard | ip | country | signal (required).
Type *string `json:"type"`
// WorkspaceID is the workspace identifier (required).
WorkspaceID *string
}

// Create creates a new workspace.
func Create(c *fastly.Client, i *CreateInput) (*List, error) {
if i.WorkspaceID == nil {
return nil, fastly.ErrMissingWorkspaceID
}
if i.Entries == nil {
return nil, fastly.ErrMissingEntries
}
if i.Name == nil {
return nil, fastly.ErrMissingName
}
if i.Type == nil {
return nil, fastly.ErrMissingType
}

path := fastly.ToSafeURL("ngwaf", "v1", "workspaces", *i.WorkspaceID, "lists")

resp, err := c.PostJSON(path, i, fastly.CreateRequestOptions(i.Context))
if err != nil {
return nil, err
}
defer resp.Body.Close()

var list *List
if err := json.NewDecoder(resp.Body).Decode(&list); err != nil {
return nil, fmt.Errorf("failed to decode json response: %w", err)
}

return list, nil
}
43 changes: 43 additions & 0 deletions fastly/ngwaf/v1/lists/api_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package lists

import (
"context"
"net/http"

"github.com/fastly/go-fastly/v10/fastly"
)

// DeleteInput specifies the information needed for the Delete() function to
// perform the operation.
type DeleteInput struct {
// Context, if supplied, will be used as the Request's context.
Context *context.Context
// ListID is the id of the list to be deleted (required).
ListID *string
// WorkspaceID is the workspace identifier (required).
WorkspaceID *string
}

// Delete deletes the specified workspace.
func Delete(c *fastly.Client, i *DeleteInput) error {
if i.WorkspaceID == nil {
return fastly.ErrMissingWorkspaceID
}
if i.ListID == nil {
return fastly.ErrMissingListID
}

path := fastly.ToSafeURL("ngwaf", "v1", "workspaces", *i.WorkspaceID, "lists", *i.ListID)

resp, err := c.Delete(path, fastly.CreateRequestOptions(i.Context))
if err != nil {
return err
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusNoContent {
return fastly.NewHTTPError(resp)
}

return nil
}
45 changes: 45 additions & 0 deletions fastly/ngwaf/v1/lists/api_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package lists

import (
"context"
"encoding/json"
"fmt"

"github.com/fastly/go-fastly/v10/fastly"
)

// GetInput specifies the information needed for the Get() function to perform
// the operation.
type GetInput struct {
// Context, if supplied, will be used as the Request's context.
Context *context.Context
// ListID is the workspace identifier (required).
ListID *string
// WorkspaceID is the workspace identifier (required).
WorkspaceID *string
}

// Get retrieves the specified workspace.
func Get(c *fastly.Client, i *GetInput) (*List, error) {
if i.WorkspaceID == nil {
return nil, fastly.ErrMissingWorkspaceID
}
if i.ListID == nil {
return nil, fastly.ErrMissingListID
}

path := fastly.ToSafeURL("ngwaf", "v1", "workspaces", *i.WorkspaceID, "lists", *i.ListID)

resp, err := c.Get(path, fastly.CreateRequestOptions(i.Context))
if err != nil {
return nil, err
}
defer resp.Body.Close()

var list *List
if err := json.NewDecoder(resp.Body).Decode(&list); err != nil {
return nil, fmt.Errorf("failed to decode json response: %w", err)
}

return list, nil
}
40 changes: 40 additions & 0 deletions fastly/ngwaf/v1/lists/api_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package lists

import (
"context"
"encoding/json"
"fmt"

"github.com/fastly/go-fastly/v10/fastly"
)

// ListInput specifies the information needed for the List() function to perform
// the operation.
type ListInput struct {
// Context, if supplied, will be used as the Request's context.
Context *context.Context
// WorkspaceID is the workspace identifier (required).
WorkspaceID *string
}

// ListLists retrieves a list of lists for the given workspace.
func ListLists(c *fastly.Client, i *ListInput) (*Lists, error) {
if i.WorkspaceID == nil {
return nil, fastly.ErrMissingWorkspaceID
}

path := fastly.ToSafeURL("ngwaf", "v1", "workspaces", *i.WorkspaceID, "lists")

resp, err := c.Get(path, fastly.CreateRequestOptions(i.Context))
if err != nil {
return nil, err
}
defer resp.Body.Close()

var lists *Lists
if err := json.NewDecoder(resp.Body).Decode(&lists); err != nil {
return nil, fmt.Errorf("failed to decode json response: %w", err)
}

return lists, nil
}
45 changes: 45 additions & 0 deletions fastly/ngwaf/v1/lists/api_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package lists

import "time"

// List is the API response structure for the create, update, and get operations.
type List struct {
// CreatedAt is the date and time in ISO 8601 format.
CreatedAt time.Time `json:"created_at"`
// Description is the description of the list.
Description string `json:"description"`
// Entries are the entries of the list.
Entries []string `json:"entries"`
// ListID is the list identifier (UUID).
ListID string `json:"id"`
// Name is the name of the list.
Name string `json:"name"`
// ReferenceID is the reference ID of the list.
ReferenceID string `json:"reference_id"`
// Scope is the scope of the list.
Scope Scope `json:"scope"`
// Type is the type of the list.
Type string `json:"type"`
// UpdatedAt is the date and time in ISO 8601 format.
UpdatedAt time.Time `json:"updated_at"`
}

// Scope is the API response structure for the scope of the list.
type Scope struct {
// Type is the type of the scope.
Type string `json:"type"`
}

// Lists is the API response structure for the list lists operation.
type Lists struct {
// Data is the list of returned lists.
Data []List `json:"data"`
// Meta is the information for total lists.
Meta MetaLists `json:"meta"`
}

// MetaLists is a subset of the Lists response structure.
type MetaLists struct {
// Total is the sum of lists.
Total int `json:"total"`
}
Loading