Skip to content
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

Rework objectstore subcommand #792

Merged
merged 16 commits into from
Feb 6, 2023
2 changes: 2 additions & 0 deletions pkg/api/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,10 @@ type Interface interface {
CreateObjectStore(i *fastly.CreateObjectStoreInput) (*fastly.ObjectStore, error)
ListObjectStores(i *fastly.ListObjectStoresInput) (*fastly.ListObjectStoresResponse, error)
DeleteObjectStore(i *fastly.DeleteObjectStoreInput) error
GetObjectStore(i *fastly.GetObjectStoreInput) (*fastly.ObjectStore, error)
ListObjectStoreKeys(i *fastly.ListObjectStoreKeysInput) (*fastly.ListObjectStoreKeysResponse, error)
GetObjectStoreKey(i *fastly.GetObjectStoreKeyInput) (string, error)
DeleteObjectStoreKey(i *fastly.DeleteObjectStoreKeyInput) error
InsertObjectStoreKey(i *fastly.InsertObjectStoreKeyInput) error

CreateSecretStore(i *fastly.CreateSecretStoreInput) (*fastly.SecretStore, error)
Expand Down
18 changes: 12 additions & 6 deletions pkg/app/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"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/objectstorekeys"
"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 @@ -299,11 +300,14 @@ func defineCommands(
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)
objectstoreDescribe := objectstore.NewDescribeCommand(objectstoreCmdRoot.CmdClause, globals, data)
objectstoreList := objectstore.NewListCommand(objectstoreCmdRoot.CmdClause, globals, data)
objectstoreKeysCmdRoot := objectstorekeys.NewRootCommand(app, globals)
objectstoreCreateKey := objectstorekeys.NewCreateCommand(objectstoreKeysCmdRoot.CmdClause, globals, data)
objectstoreDeleteKey := objectstorekeys.NewDeleteCommand(objectstoreKeysCmdRoot.CmdClause, globals, data)
objectstoreGetKey := objectstorekeys.NewGetCommand(objectstoreKeysCmdRoot.CmdClause, globals, data)
objectstoreListKeys := objectstorekeys.NewListCommand(objectstoreKeysCmdRoot.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 @@ -629,9 +633,11 @@ func defineCommands(
objectstoreCreate,
objectstoreList,
objectstoreDelete,
objectstoreKeys,
objectstoreInsertKey,
objectstoreDescribe,
objectstoreListKeys,
objectstoreCreateKey,
objectstoreGetKey,
objectstoreDeleteKey,
popCmdRoot,
profileCmdRoot,
profileCreate,
Expand Down
3 changes: 2 additions & 1 deletion pkg/app/run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ healthcheck
ip-list
log-tail
logging
objectstore
object-store
object-store-keys
pops
profile
purge
Expand Down
33 changes: 30 additions & 3 deletions pkg/commands/objectstore/create.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package objectstore

import (
"encoding/json"
"fmt"
"io"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/config"
fsterr "github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/text"
"github.com/fastly/go-fastly/v7/fastly"
Expand All @@ -13,6 +16,7 @@ import (
// CreateCommand calls the Fastly API to create an object store.
type CreateCommand struct {
cmd.Base
json bool
manifest manifest.Data
Input fastly.CreateObjectStoreInput
}
Expand All @@ -25,19 +29,42 @@ func NewCreateCommand(parent cmd.Registerer, globals *config.Data, data manifest
},
manifest: data,
}
c.CmdClause = parent.Command("create", "Create a Fastly object store")
c.CmdClause = parent.Command("create", "Create an object store")
c.CmdClause.Flag("name", "Name of Object Store").Short('n').Required().StringVar(&c.Input.Name)
c.RegisterFlagBool(cmd.BoolFlagOpts{
Name: cmd.FlagJSONName,
Description: cmd.FlagJSONDesc,
Dst: &c.json,
Short: 'j',
})
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 c.Globals.Verbose() && c.json {
return fsterr.ErrInvalidVerboseJSONCombo
}

o, 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)
if c.json {
data, err := json.Marshal(o)
if err != nil {
return err
}
_, err = out.Write(data)
if err != nil {
c.Globals.ErrLog.Add(err)
return fmt.Errorf("error: unable to write data to stdout: %w", err)
}
return nil
}

text.Success(out, "Created object store %s (name %s)", o.ID, o.Name)
return nil
}
6 changes: 3 additions & 3 deletions pkg/commands/objectstore/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func NewDeleteCommand(parent cmd.Registerer, globals *config.Data, data manifest
},
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)
c.CmdClause = parent.Command("delete", "Delete an object store")
c.CmdClause.Flag("store-id", "Store ID").Short('s').Required().StringVar(&c.Input.ID)
return &c
}

Expand All @@ -38,6 +38,6 @@ func (c *DeleteCommand) Exec(_ io.Reader, out io.Writer) error {
return err
}

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

import (
"encoding/json"
"fmt"
"io"

"github.com/fastly/cli/pkg/cmd"
"github.com/fastly/cli/pkg/config"
fsterr "github.com/fastly/cli/pkg/errors"
"github.com/fastly/cli/pkg/manifest"
"github.com/fastly/cli/pkg/text"
"github.com/fastly/go-fastly/v7/fastly"
)

// DescribeCommand calls the Fastly API to fetch the value of a key from an object store.
type DescribeCommand struct {
cmd.Base
json bool
manifest manifest.Data
Input fastly.GetObjectStoreInput
}

// NewDescribeCommand returns a usable command registered under the parent.
func NewDescribeCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *DescribeCommand {
c := DescribeCommand{
Base: cmd.Base{
Globals: globals,
},
manifest: data,
}
c.CmdClause = parent.Command("describe", "Describe an object store").Alias("get")
c.CmdClause.Flag("store-id", "Store ID").Short('s').Required().StringVar(&c.Input.ID)

// optional
c.RegisterFlagBool(cmd.BoolFlagOpts{
Name: cmd.FlagJSONName,
Description: cmd.FlagJSONDesc,
Dst: &c.json,
Short: 'j',
})

return &c
}

// Exec invokes the application logic for the command.
func (c *DescribeCommand) Exec(_ io.Reader, out io.Writer) error {
if c.Globals.Verbose() && c.json {
return fsterr.ErrInvalidVerboseJSONCombo
}

o, err := c.Globals.APIClient.GetObjectStore(&c.Input)
if err != nil {
c.Globals.ErrLog.Add(err)
return err
}

if c.json {
data, err := json.Marshal(o)
if err != nil {
return err
}
_, err = out.Write(data)
if err != nil {
c.Globals.ErrLog.Add(err)
return fmt.Errorf("error: unable to write data to stdout: %w", err)
}
return nil
}

text.PrintObjectStore(out, "", o)
return nil
}
43 changes: 0 additions & 43 deletions pkg/commands/objectstore/insert.go

This file was deleted.

2 changes: 1 addition & 1 deletion pkg/commands/objectstore/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewListCommand(parent cmd.Registerer, globals *config.Data, data manifest.D
},
manifest: data,
}
c.CmdClause = parent.Command("list", "List Fastly object stores")
c.CmdClause = parent.Command("list", "List object stores")

// optional
c.RegisterFlagBool(cmd.BoolFlagOpts{
Expand Down
2 changes: 1 addition & 1 deletion pkg/commands/objectstore/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type RootCommand struct {
func NewRootCommand(parent cmd.Registerer, globals *config.Data) *RootCommand {
var c RootCommand
c.Globals = globals
c.CmdClause = parent.Command("objectstore", "Manipulate Fastly object stores")
c.CmdClause = parent.Command("object-store", "Manipulate Fastly Object Stores")
return &c
}

Expand Down
45 changes: 45 additions & 0 deletions pkg/commands/objectstorekeys/create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package objectstorekeys

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/v7/fastly"
)

// CreateCommand calls the Fastly API to insert a key into an object store.
type CreateCommand struct {
cmd.Base
manifest manifest.Data
Input fastly.InsertObjectStoreKeyInput
}

// NewCreateCommand returns a usable command registered under the parent.
func NewCreateCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *CreateCommand {
c := CreateCommand{
Base: cmd.Base{
Globals: globals,
},
manifest: data,
}
c.CmdClause = parent.Command("create", "Insert a key-value pair").Alias("insert")
c.CmdClause.Flag("store-id", "Store ID").Short('s').Required().StringVar(&c.Input.ID)
c.CmdClause.Flag("key-name", "Key name").Short('k').Required().StringVar(&c.Input.Key)
c.CmdClause.Flag("value", "Value").Required().StringVar(&c.Input.Value)
return &c
}

// Exec invokes the application logic for the command.
func (c *CreateCommand) 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
}
44 changes: 44 additions & 0 deletions pkg/commands/objectstorekeys/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package objectstorekeys

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/v7/fastly"
)

// DeleteCommand calls the Fastly API to delete an object store.
type DeleteCommand struct {
cmd.Base
manifest manifest.Data
Input fastly.DeleteObjectStoreKeyInput
}

// NewDeleteCommand returns a usable command registered under the parent.
func NewDeleteCommand(parent cmd.Registerer, globals *config.Data, data manifest.Data) *DeleteCommand {
c := DeleteCommand{
Base: cmd.Base{
Globals: globals,
},
manifest: data,
}
c.CmdClause = parent.Command("delete", "Delete a key")
c.CmdClause.Flag("store-id", "Store ID").Short('s').Required().StringVar(&c.Input.ID)
c.CmdClause.Flag("key-name", "Key name").Short('k').Required().StringVar(&c.Input.Key)
return &c
}

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

text.Success(out, "Deleted key %s from store ID %s", c.Input.Key, c.Input.ID)
return nil
}
3 changes: 3 additions & 0 deletions pkg/commands/objectstorekeys/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Package objectstorekeys contains commands to inspect and manipulate Fastly edge
// object stores keys.
package objectstorekeys
Loading