-
Notifications
You must be signed in to change notification settings - Fork 131
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
anthony-gomez-fastly
merged 4 commits into
main
from
CDTOOL-1006-add-object-storage-functions
Feb 21, 2025
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
207f799
feat(fastly/objectstorage/accesskeys): adds crud operations for acces…
anthony-gomez-fastly 5ef6997
address PR comments
anthony-gomez-fastly 12c8358
add checking for permissions
anthony-gomez-fastly d050170
change type for time
anthony-gomez-fastly File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
anthony-gomez-fastly marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 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{} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.