8000 feat(fastly/objectstorage): adds crud operations for access keys in object storage by anthony-gomez-fastly · Pull Request #612 · fastly/go-fastly · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat(fastly/objectstorage): adds crud operations for access keys in object storage #612

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 21, 2025
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
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## Unreleased

[Full Changelog](https://github.com/fastly/go-fastly/compare/v9.13.1...)

**Enhancements:**

- feat(fastly/objectstorage): adds crud operations for access keys in object storage [#612](https://github.com/fastly/go-fastly/pull/612)

**Bug fixes:**

**Dependencies:**

## [v9.13.1](https://github.com/fastly/go-fastly/releases/tag/v9.13.1) (2025-02-14)

[Full Changelog](https://github.com/fastly/go-fastly/compare/v9.13.0...v9.13.1)
Expand Down
4 changes: 2 additions & 2 deletions fastly/computeacls/api_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type ComputeACL struct {

// ComputeACLs is the API response structure for the list compute ACLs operation.
type ComputeACLs struct {
// Data is the list of returned cumpute ACLs.
// Data is the list of returned compute ACLs.
Data []ComputeACL `json:"data"`
// Meta is the information for total compute ACLs.
Meta MetaACLs `json:"meta"`
Expand All @@ -32,7 +32,7 @@ type ComputeACLEntry struct {

// 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 is the list of returned compute ACL entries.
Entries []ComputeACLEntry
// Meta is the information for pagination.
Meta MetaEntries `json:"meta"`
Expand Down
12 changes: 12 additions & 0 deletions fastly/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@ var ErrMissingType = NewFieldError("Type")
// requires a "CustomerID" key, but one was not set.
var ErrMissingCustomerID = NewFieldError("CustomerID")

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

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

// ErrMissingDictionaryID is an error that is returned when an input struct
// requires a "DictionaryID" key, but one was not set.
var ErrMissingDictionaryID = NewFieldError("DictionaryID")
Expand Down Expand Up @@ -237,6 +245,10 @@ var ErrMissingUserID = NewFieldError("UserID")
// requires a "Permission" key, but one was not set.
var ErrMissingPermission = NewFieldError("Permission")

// ErrInvalidPermission is an error that is returned when an input struct
// has a "Permission" key, but the one provided is invalid.
var ErrInvalidPermission = NewFieldError("Permission").Message("invalid")

// ErrMissingServiceVersion is an error that is returned when an input struct
// requires a "ServiceVersion" key, but one was not set.
var ErrMissingServiceVersion = NewFieldError("ServiceVersion")
Expand Down
59 changes: 59 additions & 0 deletions fastly/objectstorage/accesskeys/api_create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package accesskeys

import (
"encoding/json"
"fmt"
"slices"

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

// Permissions can be one of these values only
const (
READ_WRITE_ADMIN = "read-write-admin"
READ_ONLY_ADMIN = "read-only-admin"
READ_WRITE_OBJECTS = "read-write-objects"
READ_ONLY_OBJECTS = "read-only-objects"
)

var PERMISSONS = []string{READ_WRITE_ADMIN, READ_ONLY_ADMIN, READ_WRITE_OBJECTS, READ_ONLY_OBJECTS}

// CreateInput specifies the information needed for the Create() function to
// perform the operation.
type CreateInput struct {
// Description is a description of the access key (required).
Description *string `json:"description"`
// Permission is the perm DDA9 issions the access key will have (required).
Permission *string `json:"permission"`
// Buckets are the buckets the access key will have (optional).
Buckets *[]string `json:"buckets"`
}

// Create creates a new Object Storage Access Key.
func Create(c *fastly.Client, i *CreateInput) (*AccessKey, error) {
if i.Description == nil {
return nil, fastly.ErrMissingDescription
}

if i.Permission == nil {
return nil, fastly.ErrMissingPermission
}

// Check if the provided permission is in the set of valid permissions
if !slices.Contains(PERMISSONS, *i.Permission) {
return nil, fastly.ErrInvalidPermission
}

resp, err := c.PostJSON("/resources/object-storage/access-keys", i, nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()

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

return accessKey, nil
}
34 changes: 34 additions & 0 deletions fastly/objectstorage/accesskeys/api_delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package accesskeys

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 {
// AccessKeyID is an AccessKey Identifier (required).
AccessKeyID *string
}

// DeleteAccessKey deletes an access key.
func Delete(c *fastly.Client, i *DeleteInput) error {
if i.AccessKeyID == nil {
return fastly.ErrMissingAccessKeyID
}

path := fastly.ToSafeURL("resources", "object-storage", "access-keys", *i.AccessKeyID)

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

if resp.StatusCode != http.StatusNoContent {
return fastly.NewHTTPError(resp)
}
return nil
}
37 changes: 37 additions & 0 deletions fastly/objectstorage/accesskeys/api_get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package accesskeys

import (
"encoding/json"
"fmt"

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

// GetInput specifies the information needed for the Get() function to perform
// the operation.
type GetInput struct {
// AccessKeyID is an AccessKey Identifier (required).
AccessKeyID *string
}

// GetAccessKey finds an access key with the given ID if the user has the correct permisssions.
func Get(c *fastly.Client, i *GetInput) (*AccessKey, error) {
if i.AccessKeyID == nil {
return nil, fastly.ErrMissingAccessKeyID
}

path := fastly.ToSafeURL("resources", "object-storage", "access-keys", *i.AccessKeyID)

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

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

return accessKey, nil
}
25 changes: 25 additions & 0 deletions fastly/objectstorage/accesskeys/api_list_access_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package accesskeys

import (
"encoding/json"
"fmt"

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

// ListAccessKeys retrieves all access keys within object storage.
func ListAccessKeys(c *fastly.Client) (*AccessKeys, error) {
resp, err := c.Get("/resources/object-storage/access-keys", nil)
if err != nil {
return nil, err
}
defer resp.Body.Close()

var accessKeys *AccessKeys

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

return accessKeys, nil
}
29 changes: 29 additions & 0 deletions fastly/objectstorage/accesskeys/api_response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package accesskeys

import "time"

// AccessKey is the API response structure for the create and describe operations.
type AccessKey struct {
// AccessKey is an AccessKey identifier.
AccessKeyID string `json:"access_key"`
// SecretKey is the secret for the access key
SecretKey string `json:"secret_key"`
// Description is human readable description for the access key.
Description string `json:"description"`
// Permission is the permissions the key has.
Permission string `json:"permission"`
// Buckets is the list of buckets associated with the access key.
Buckets []string `json:"buckets"`
// CreatedAt is the timestamp associated with the creation of the access key.
CreatedAt time.Time `json:"created_at"`
}
74A2

// AccessKeys is the API response structure for the list access keys operation.
type AccessKeys struct {
// Data is the list of returned AccessKeys.
Data []AccessKey `json:"data"`
// Meta is additional information about the request
Meta MetaAccessKeys `json:"meta"`
}

type MetaAccessKeys struct{}
Loading
Loading
0