Skip to content

Commit

Permalink
all: add object store API calls
Browse files Browse the repository at this point in the history
  • Loading branch information
dgryski committed Oct 14, 2022
1 parent 0f5dc34 commit c2ec805
Show file tree
Hide file tree
Showing 12 changed files with 378 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,13 @@ type Interface interface {
CreateServiceAuthorization(i *fastly.CreateServiceAuthorizationInput) (*fastly.ServiceAuthorization, error)
UpdateServiceAuthorization(i *fastly.UpdateServiceAuthorizationInput) (*fastly.ServiceAuthorization, error)
DeleteServiceAuthorization(i *fastly.DeleteServiceAuthorizationInput) error

CreateObjectStore(i *fastly.CreateObjectStoreInput) (*fastly.ObjectStore, error)
ListObjectStores(i *fastly.ListObjectStoresInput) (*fastly.ListObjectStoresResponse, error)
DeleteObjectStore(i *fastly.DeleteObjectStoreInput) error
ListObjectStoreKeys(i *fastly.ListObjectStoreKeysInput) (*fastly.ListObjectStoreKeysResponse, error)
GetObjectStoreKey(i *fastly.GetObjectStoreKeyInput) (string, error)
InsertObjectStoreKey(i *fastly.InsertObjectStoreKeyInput) error
}

// RealtimeStatsInterface is the subset of go-fastly's realtime stats API used here.
Expand Down
14 changes: 14 additions & 0 deletions pkg/app/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import (
"github.com/fastly/cli/pkg/commands/logging/sumologic"
"github.com/fastly/cli/pkg/commands/logging/syslog"
"github.com/fastly/cli/pkg/commands/logtail"
"github.com/fastly/cli/pkg/commands/objectstore"
"github.com/fastly/cli/pkg/commands/pop"
"github.com/fastly/cli/pkg/commands/profile"
"github.com/fastly/cli/pkg/commands/purge"
Expand Down Expand Up @@ -295,6 +296,13 @@ func defineCommands(
loggingSyslogDescribe := syslog.NewDescribeCommand(loggingSyslogCmdRoot.CmdClause, globals, data)
loggingSyslogList := syslog.NewListCommand(loggingSyslogCmdRoot.CmdClause, globals, data)
loggingSyslogUpdate := syslog.NewUpdateCommand(loggingSyslogCmdRoot.CmdClause, globals, data)
objectstoreCmdRoot := objectstore.NewRootCommand(app, globals)
objectstoreCreate := objectstore.NewCreateCommand(objectstoreCmdRoot.CmdClause, globals, data)
objectstoreList := objectstore.NewListCommand(objectstoreCmdRoot.CmdClause, globals, data)
objectstoreDelete := objectstore.NewDeleteCommand(objectstoreCmdRoot.CmdClause, globals, data)
objectstoreKeys := objectstore.NewKeysCommand(objectstoreCmdRoot.CmdClause, globals, data)
objectstoreInsertKey := objectstore.NewInsertKeyCommand(objectstoreCmdRoot.CmdClause, globals, data)
objectstoreGetKey := objectstore.NewGetKeyCommand(objectstoreCmdRoot.CmdClause, globals, data)
popCmdRoot := pop.NewRootCommand(app, globals)
profileCmdRoot := profile.NewRootCommand(app, globals)
profileCreate := profile.NewCreateCommand(profileCmdRoot.CmdClause, profile.APIClientFactory(opts.APIClient), globals)
Expand Down Expand Up @@ -607,6 +615,12 @@ func defineCommands(
loggingSyslogDescribe,
loggingSyslogList,
loggingSyslogUpdate,
objectstoreCreate,
objectstoreList,
objectstoreDelete,
objectstoreKeys,
objectstoreInsertKey,
objectstoreGetKey,
popCmdRoot,
profileCmdRoot,
profileCreate,
Expand Down
1 change: 1 addition & 0 deletions pkg/app/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ healthcheck
ip-list
log-tail
logging
objectstore
pops
profile
purge
Expand Down
40 changes: 40 additions & 0 deletions pkg/commands/objectstore/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package objectstore

import (
"io"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/config"
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/text"
"github.com/fastly/go-fastly/v6/fastly"
)

// CreateCommand calls the Fastly API to create an object store
type CreateCommand struct {
cmd.Base
manifest manifest.Data
Input fastly.CreateObjectStoreInput
}

// NewCreateCommand returns a usable command registered under the parent.
func NewCreateCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *CreateCommand {
var c CreateCommand
c.Globals = globals
c.manifest = data
c.CmdClause = parent.Command("create", "Create a Fastly edge config store")
c.CmdClause.Flag("name", "Name of Object Store").Short('n').Required().StringVar(&c.Input.Name)
return &c
}

// Exec invokes the application logic for the command.
func (c *CreateCommand) Exec(_ io.Reader, out io.Writer) error {
d, err := c.Globals.APIClient.CreateObjectStore(&c.Input)
if err != nil {
c.Globals.ErrLog.Add(err)
return err
}

text.Success(out, "Created object store %s", d.Name)
return nil
}
40 changes: 40 additions & 0 deletions pkg/commands/objectstore/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package objectstore

import (
"io"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/config"
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/text"
"github.com/fastly/go-fastly/v6/fastly"
)

// DeleteCommand calls the Fastly API to delete a service.
type DeleteCommand struct {
cmd.Base
manifest manifest.Data
Input fastly.DeleteObjectStoreInput
}

// NewDeleteCommand returns a usable command registered under the parent.
func NewDeleteCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *DeleteCommand {
var c DeleteCommand
c.Globals = globals
c.manifest = data
c.CmdClause = parent.Command("delete", "Delete a Fastly object store")
c.CmdClause.Flag("id", "ID of object store").Required().StringVar(&c.Input.ID)
return &c
}

// Exec invokes the application logic for the command.
func (c *DeleteCommand) Exec(_ io.Reader, out io.Writer) error {
err := c.Globals.APIClient.DeleteObjectStore(&c.Input)
if err != nil {
c.Globals.ErrLog.Add(err)
return err
}

text.Success(out, "Deleted object store ID:%s", c.Input.ID)
return nil
}
42 changes: 42 additions & 0 deletions pkg/commands/objectstore/getkey.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package objectstore

import (
"io"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/config"
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/text"
"github.com/fastly/go-fastly/v6/fastly"
)

// GetKeyCommand calls the Fastly API to list the available object stores
type GetKeyCommand struct {
cmd.Base
manifest manifest.Data
Input fastly.GetObjectStoreKeyInput
}

// NewGetKeyCommand returns a usable command registered under the parent.
func NewGetKeyCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *GetKeyCommand {
var c GetKeyCommand
c.Globals = globals
c.manifest = data
c.CmdClause = parent.Command("get", "Get Fastly edge config store key")
c.CmdClause.Flag("id", "ID of object store").Required().StringVar(&c.Input.ID)
c.CmdClause.Flag("k", "Key to fetch").Required().StringVar(&c.Input.Key)
return &c
}

// Exec invokes the application logic for the command.
func (c *GetKeyCommand) Exec(_ io.Reader, out io.Writer) error {
value, err := c.Globals.APIClient.GetObjectStoreKey(&c.Input)
if err != nil {
c.Globals.ErrLog.Add(err)
return err
}

text.PrintObjectStoreKeyValue(out, "", c.Input.Key, value)

return nil
}
43 changes: 43 additions & 0 deletions pkg/commands/objectstore/insert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package objectstore

import (
"io"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/config"
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/text"
"github.com/fastly/go-fastly/v6/fastly"
)

// InsertKeyCommand calls the Fastly API to create an object store
type InsertKeyCommand struct {
cmd.Base
manifest manifest.Data
Input fastly.InsertObjectStoreKeyInput
}

// NewInsertKeyCommand returns a usable command registered under the parent.
func NewInsertKeyCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *InsertKeyCommand {
var c InsertKeyCommand
c.Globals = globals
c.manifest = data
c.CmdClause = parent.Command("insert", "Insert key/value pair into a Fastly edge config store")
c.CmdClause.Flag("id", "Name of Object Store").Short('n').Required().StringVar(&c.Input.ID)
c.CmdClause.Flag("k", "Key to insert").Required().StringVar(&c.Input.Key)
c.CmdClause.Flag("v", "Value to insert").Required().StringVar(&c.Input.Value)

return &c
}

// Exec invokes the application logic for the command.
func (c *InsertKeyCommand) Exec(_ io.Reader, out io.Writer) error {
err := c.Globals.APIClient.InsertObjectStoreKey(&c.Input)
if err != nil {
c.Globals.ErrLog.Add(err)
return err
}

text.Success(out, "Inserted key %s into object store %s", c.Input.Key, c.Input.ID)
return nil
}
41 changes: 41 additions & 0 deletions pkg/commands/objectstore/keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package objectstore

import (
"io"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/config"
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/text"
"github.com/fastly/go-fastly/v6/fastly"
)

// KeysCommand calls the Fastly API to list the available object stores
type KeysCommand struct {
cmd.Base
manifest manifest.Data
Input fastly.ListObjectStoreKeysInput
}

// NewKeysCommand returns a usable command registered under the parent.
func NewKeysCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *KeysCommand {
var c KeysCommand
c.Globals = globals
c.manifest = data
c.CmdClause = parent.Command("keys", "List Fastly edge config store keys")
c.CmdClause.Flag("id", "ID of object store").Required().StringVar(&c.Input.ID)
return &c
}

// Exec invokes the application logic for the command.
func (c *KeysCommand) Exec(_ io.Reader, out io.Writer) error {
o, err := c.Globals.APIClient.ListObjectStoreKeys(&c.Input)
if err != nil {
c.Globals.ErrLog.Add(err)
return err
}

text.PrintObjectStoreKeys(out, "", o.Data)

return nil
}
44 changes: 44 additions & 0 deletions pkg/commands/objectstore/list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package objectstore

import (
"io"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/config"
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/text"
"github.com/fastly/go-fastly/v6/fastly"
)

// ListCommand calls the Fastly API to list the available object stores
type ListCommand struct {
cmd.Base
manifest manifest.Data
Input fastly.ListObjectStoresInput
}

// NewListCommand returns a usable command registered under the parent.
func NewListCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *ListCommand {
var c ListCommand
c.Globals = globals
c.manifest = data
c.CmdClause = parent.Command("list", "List Fastly edge config stores")
return &c
}

// Exec invokes the application logic for the command.
func (c *ListCommand) Exec(_ io.Reader, out io.Writer) error {
o, err := c.Globals.APIClient.ListObjectStores(&c.Input)
if err != nil {
c.Globals.ErrLog.Add(err)
return err
}

for _, o := range o.Data {
// avoid gosec loop aliasing check :/
o := o
text.PrintObjectStore(out, "", &o)
}

return nil
}
28 changes: 28 additions & 0 deletions pkg/commands/objectstore/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package objectstore

import (
"io"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/config"
)

// RootCommand is the parent command for all subcommands in this package.
// It should be installed under the primary root command.
type RootCommand struct {
cmd.Base
// no flags
}

// NewRootCommand returns a new command registered in the parent.
func NewRootCommand(parent cmd.Registerer, globals *config.Data) *RootCommand {
var c RootCommand
c.Globals = globals
c.CmdClause = parent.Command("objectstore", "Manipulate Fastly object stores")
return &c
}

// Exec implements the command interface.
func (c *RootCommand) Exec(_ io.Reader, _ io.Writer) error {
panic("unreachable")
}
35 changes: 35 additions & 0 deletions pkg/mock/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,13 @@ type API struct {
CreateServiceAuthorizationFn func(i *fastly.CreateServiceAuthorizationInput) (*fastly.ServiceAuthorization, error)
UpdateServiceAuthorizationFn func(i *fastly.UpdateServiceAuthorizationInput) (*fastly.ServiceAuthorization, error)
DeleteServiceAuthorizationFn func(i *fastly.DeleteServiceAuthorizationInput) error

CreateObjectStoreFn func(i *fastly.CreateObjectStoreInput) (*fastly.ObjectStore, error)
ListObjectStoresFn func(i *fastly.ListObjectStoresInput) (*fastly.ListObjectStoresResponse, error)
DeleteObjectStoreFn func(i *fastly.DeleteObjectStoreInput) error
ListObjectStoreKeysFn func(i *fastly.ListObjectStoreKeysInput) (*fastly.ListObjectStoreKeysResponse, error)
GetObjectStoreKeyFn func(i *fastly.GetObjectStoreKeyInput) (string, error)
InsertObjectStoreKeyFn func(i *fastly.InsertObjectStoreKeyInput) error
}

// AllDatacenters implements Interface.
Expand Down Expand Up @@ -1609,3 +1616,31 @@ func (m API) UpdateServiceAuthorization(i *fastly.UpdateServiceAuthorizationInpu
func (m API) DeleteServiceAuthorization(i *fastly.DeleteServiceAuthorizationInput) error {
return m.DeleteServiceAuthorizationFn(i)
}

// CreateObjectStore implements Interface.
func (m API) CreateObjectStore(i *fastly.CreateObjectStoreInput) (*fastly.ObjectStore, error) {
return m.CreateObjectStoreFn(i)
}

// ListObjectStores implements Interface.
func (m API) ListObjectStores(i *fastly.ListObjectStoresInput) (*fastly.ListObjectStoresResponse, error) {
return m.ListObjectStoresFn(i)
}

// DeleteObjectStore implements Interface.
func (m API) DeleteObjectStore(i *fastly.DeleteObjectStoreInput) error {
return m.DeleteObjectStoreFn(i)
}

// ListObjectStoreKeys implements Interface
func (m API) ListObjectStoreKeys(i *fastly.ListObjectStoreKeysInput) (*fastly.ListObjectStoreKeysResponse, error) {
return m.ListObjectStoreKeysFn(i)
}

func (m API) GetObjectStoreKey(i *fastly.GetObjectStoreKeyInput) (string, error) {
return m.GetObjectStoreKeyFn(i)
}

func (m API) InsertObjectStoreKey(i *fastly.InsertObjectStoreKeyInput) error {
return m.InsertObjectStoreKeyFn(i)
}
Loading

0 comments on commit c2ec805

Please sign in to comment.