Skip to content

Commit

Permalink
feat(computeacls): Add support for Compute platform ACLs. (#574)
Browse files Browse the repository at this point in the history
* feat(compute_acl): add support for compute ACLs

* test(compute_acl_test.go): fix array indexing

* refactor(compute_acl): use struct as input to update an acl

* refactor(compute_acls): use new project layout

* chore(api_delete.go): update struct field description

Co-authored-by: Mark McDonnell <[email protected]>

* chore(api_describe.go): update struct field description

Co-authored-by: Mark McDonnell <[email protected]>

* chore(api_list_entries.go): update struct field description

Co-authored-by: Mark McDonnell <[email protected]>

* chore(api_lookup.go): update struct field description

Co-authored-by: Mark McDonnell <[email protected]>

* chore(api_lookup.go): update struct field description

Co-authored-by: Mark McDonnell <[email protected]>

* chore(api_response.go): add struct field description

Co-authored-by: Mark McDonnell <[email protected]>

* chore(api_response.go): add struct field description

Co-authored-by: Mark McDonnell <[email protected]>

* chore(api_update.go): update struct field description

Co-authored-by: Mark McDonnell <[email protected]>

* chore(api_update.go): add struct field description

Co-authored-by: Mark McDonnell <[email protected]>

* chore(api_update.go): add struct field description

Co-authored-by: Mark McDonnell <[email protected]>

* chore(api_update.go): add struct field description

Co-authored-by: Mark McDonnell <[email protected]>

* chore(api_response.go): add struct field description

Co-authored-by: Mark McDonnell <[email protected]>

* chore(api_response.go): add struct field description

Co-authored-by: Mark McDonnell <[email protected]>

* refactor(computeacls): remove reference to API version

---------

Co-authored-by: Mark McDonnell <[email protected]>
Co-authored-by: Kevin P. Fleming <[email protected]>
  • Loading branch information
3 people authored Jan 25, 2025
1 parent e9d4a62 commit 5632665
Show file tree
Hide file tree
Showing 18 changed files with 1,177 additions and 0 deletions.
35 changes: 35 additions & 0 deletions fastly/computeacls/api_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package computeacls

import (
"encoding/json"
"fmt"

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

// CreateInput specifies the information needed for the Create() function to
// perform the operation.
type CreateInput struct {
// Name is the name of the compute ACL to create (required).
Name *string `json:"name"`
}

// Create creates a new compute ACL.
func Create(c *fastly.Client, i *CreateInput) (*ComputeACL, error) {
if i.Name == nil {
return nil, fastly.ErrMissingName
}

resp, err := c.PostJSON("/resources/acls", i, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()

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

return acl, nil
}
35 changes: 35 additions & 0 deletions fastly/computeacls/api_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package computeacls

import (
"net/http"

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

// DeleteInput specifies the information needed for the Delete() function to
// perform the operation.
type DeleteInput struct {
// ComputeACLID is an ACL Identifier (required).
ComputeACLID *string
}

// DeleteComputeACL deletes the specified compute ACL.
func Delete(c *fastly.Client, i *DeleteInput) error {
if i.ComputeACLID == nil {
return fastly.ErrMissingComputeACLID
}

path := fastly.ToSafeURL("resources", "acls", *i.ComputeACLID)

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

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

return nil
}
37 changes: 37 additions & 0 deletions fastly/computeacls/api_describe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package computeacls

import (
"encoding/json"
"fmt"

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

// DescribeInput specifies the information needed for the Describe() function to perform
// the operation.
type DescribeInput struct {
// ComputeACLID is an ACL Identifier (required).
ComputeACLID *string
}

// Describe describes a specified compute ACL.
func Describe(c *fastly.Client, i *DescribeInput) (*ComputeACL, error) {
if i.ComputeACLID == nil {
return nil, fastly.ErrMissingComputeACLID
}

path := fastly.ToSafeURL("resources", "acls", *i.ComputeACLID)

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

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

return acl, nil
}
25 changes: 25 additions & 0 deletions fastly/computeacls/api_list_acls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package computeacls

import (
"encoding/json"
"fmt"

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

// ListACLs retrieves all compute ACLs.
func ListACLs(c *fastly.Client) (*ComputeACLs, error) {
resp, err := c.Get("/resources/acls", nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()

var acls *ComputeACLs

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

return acls, nil
}
52 changes: 52 additions & 0 deletions fastly/computeacls/api_list_entries.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package computeacls

import (
"encoding/json"
"fmt"
"strconv"

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

// ListEntriesInput specifies the information needed for the ListEntries() function to perform
// the operation.
type ListEntriesInput struct {
// ComputeACLID is an ACL Identifier (required).
ComputeACLID *string
// Cursor is used for paginating through results.
Cursor *string
// Limit is the maximum number of entries included the response.
Limit *int
}

// ListEntries
func ListEntries(c *fastly.Client, i *ListEntriesInput) (*ComputeACLEntries, error) {
if i.ComputeACLID == nil {
return nil, fastly.ErrMissingComputeACLID
}

ro := &fastly.RequestOptions{
Params: map[string]string{},
}
if i.Cursor != nil {
ro.Params["cursor"] = *i.Cursor
}
if i.Limit != nil {
ro.Params["limit"] = strconv.Itoa(*i.Limit)
}

path := fastly.ToSafeURL("resources", "acls", *i.ComputeACLID, "entries")

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

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

return entries, nil
}
42 changes: 42 additions & 0 deletions fastly/computeacls/api_lookup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package computeacls

import (
"encoding/json"
"fmt"

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

// LookupInput specifies the information needed for the Lookup() function to perform
// the operation.
type LookupInput struct {
// ComputeACLID is an ACL Identifier (required).
ComputeACLID *string
// ComputeACLIP is a valid IPv4 or IPv6 address (required).
ComputeACLIP *string
}

// Lookup finds a matching ACL entry for an IP address.
func Lookup(c *fastly.Client, i *LookupInput) (*ComputeACLEntry, error) {
if i.ComputeACLID == nil {
return nil, fastly.ErrMissingComputeACLID
}
if i.ComputeACLIP == nil {
return nil, fastly.ErrMissingComputeACLIP
}

path := fastly.ToSafeURL("resources", "acls", *i.ComputeACLID, "entry", *i.ComputeACLIP)

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

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

return entry, nil
}
47 changes: 47 additions & 0 deletions fastly/computeacls/api_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package computeacls

// ComputeACL is the API response structure for the create and describe operations.
type ComputeACL struct {
// Name is an ACL name.
Name string `json:"name"`
// ComputeACLID is an ACL Identifier.
ComputeACLID string `json:"id"`
}

// ComputeACLs is the API response structure for the list compute ACLs operation.
type ComputeACLs struct {
// Data is the list of returned cumpute ACLs.
Data []ComputeACL `json:"data"`
// Meta is the information for total compute ACLs.
Meta MetaACLs `json:"meta"`
}

// MetaACLs is a subset of the ComputeACLs response structure.
type MetaACLs struct {
// Total is the sum of compute ACLs.
Total int `json:"total"`
}

// ComputeACLEntry is the API response structure for the lookup operation.
type ComputeACLEntry struct {
// Prefix is an IP prefix defined in Classless Inter-Domain Routing (CIDR) format.
Prefix string `json:"prefix"`
// Action is one of "ALLOW" or "BLOCK".
Action string `json:"action"`
}

// ComputeACLEntries is the API response structure for the list compute ACL entries operation.
type ComputeACLEntries struct {
// Entries is the list of returned cumpute ACL entries.
Entries []ComputeACLEntry
// Meta is the information for pagination.
Meta MetaEntries `json:"meta"`
}

// MetaEntries is a subset of the ComputeACLs response structure.
type MetaEntries struct {
// Limit is how many results are included in this response.
Limit int `json:"limit"`
// NextCursor is the cursor value used to retrieve the next page.
NextCursor string `json:"next_cursor"`
}
Loading

0 comments on commit 5632665

Please sign in to comment.