-
Notifications
You must be signed in to change notification settings - Fork 69
feat(product): Add 'product' commands for each Fastly product. #1362
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
Draft
kpfleming
wants to merge
9
commits into
fastly:main
Choose a base branch
from
kpfleming:cdtool-967
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
900190c
feat(product): Add 'product' commands for each Fastly product.
kpfleming b641e84
Fix shell-completion test.
kpfleming 6f9b7c0
WIP
kpfleming bef33e0
More WIP
kpfleming 0476c47
MOAR GENERICS
kpfleming a564056
Fixes and Makefile
kpfleming a48f954
Remaining enableable products except NGWAF.
kpfleming 08979fe
Review feedback... removing unnecessary source files.
kpfleming 8cf280d
More WIP
kpfleming 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package productcore | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/fastly/cli/pkg/api" | ||
"github.com/fastly/cli/pkg/argparser" | ||
"github.com/fastly/cli/pkg/global" | ||
"github.com/fastly/cli/pkg/manifest" | ||
"github.com/fastly/go-fastly/v9/fastly/products" | ||
) | ||
|
||
// Base is a base type for all product commands. | ||
type Base struct { | ||
argparser.Base | ||
argparser.JSONOutput | ||
Manifest manifest.Data | ||
|
||
ServiceName argparser.OptionalServiceNameID | ||
} | ||
|
||
// Init prepares the structure for use by the CLI core. | ||
func (cmd *Base) Init(parent argparser.Registerer, g *global.Data) { | ||
cmd.Globals = g | ||
|
||
// Optional flags. | ||
cmd.RegisterFlag(argparser.StringFlagOpts{ | ||
Name: argparser.FlagServiceIDName, | ||
Description: argparser.FlagServiceIDDesc, | ||
Dst: &g.Manifest.Flag.ServiceID, | ||
Short: 's', | ||
}) | ||
cmd.RegisterFlag(argparser.StringFlagOpts{ | ||
Action: cmd.ServiceName.Set, | ||
Name: argparser.FlagServiceName, | ||
Description: argparser.FlagServiceNameDesc, | ||
Dst: &cmd.ServiceName.Value, | ||
}) | ||
cmd.RegisterFlagBool(cmd.JSONFlag()) // --json | ||
} | ||
|
||
// EnablementStatus is a structure used to generate output from | ||
// the enablement-related commands | ||
type EnablementStatus[_ products.ProductOutput] struct { | ||
ProductName string `json:"-"` | ||
ProductID string `json:"product_id"` | ||
ServiceID string `json:"service_id"` | ||
Enabled bool `json:"enabled"` | ||
} | ||
|
||
type StatusManager[O products.ProductOutput] interface { | ||
SetEnabled(bool) | ||
GetEnabled() string | ||
GetProductName() string | ||
SetProductID(string) | ||
SetServiceID(string) | ||
TransformOutput(O) | ||
GetTextResult() string | ||
} | ||
|
||
func (s *EnablementStatus[_]) SetEnabled(e bool) { | ||
s.Enabled = e | ||
} | ||
|
||
func (s *EnablementStatus[_]) GetEnabled() string { | ||
if s.Enabled { | ||
return "enabled" | ||
} | ||
return "disabled" | ||
} | ||
|
||
func (s *EnablementStatus[_]) GetProductName() string { | ||
return s.ProductName | ||
} | ||
|
||
func (s *EnablementStatus[_]) SetProductID(id string) { | ||
s.ProductID = id | ||
} | ||
|
||
func (s *EnablementStatus[_]) SetServiceID(id string) { | ||
s.ServiceID = id | ||
} | ||
|
||
func (s *EnablementStatus[O]) TransformOutput(o O) { | ||
s.ProductID = o.ProductID() | ||
s.ServiceID = o.ServiceID() | ||
} | ||
|
||
func (s *EnablementStatus[O]) GetTextResult() string { | ||
return fmt.Sprintf("%s is %s on service %s", s.ProductName, s.GetEnabled(), s.ServiceID) | ||
} | ||
|
||
// EnablementHookFuncs is a structure of dependency-injection points | ||
// used by unit tests to provide mock behaviors | ||
type EnablementHookFuncs[O products.ProductOutput] struct { | ||
DisableFunc func(api.Interface, string) error | ||
EnableFunc func(api.Interface, string) (O, error) | ||
GetFunc func(api.Interface, string) (O, error) | ||
} | ||
|
||
// ConfigurationHookFuncs is a structure of dependency-injection | ||
// points used by unit tests to provide mock behaviors | ||
type ConfigurationHookFuncs[O, I any] struct { | ||
GetConfigurationFunc func(api.Interface, string) (O, error) | ||
UpdateConfigurationFunc func(api.Interface, string, I) (O, error) | ||
} |
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,69 @@ | ||
package productcore | ||
|
||
import ( | ||
"io" | ||
|
||
fsterr "github.com/fastly/cli/pkg/errors" | ||
|
||
"github.com/fastly/cli/pkg/argparser" | ||
"github.com/fastly/cli/pkg/global" | ||
"github.com/fastly/cli/pkg/text" | ||
"github.com/fastly/go-fastly/v9/fastly/products" | ||
) | ||
|
||
// Disable is a base type for all 'disable' commands. | ||
type Disable[O products.ProductOutput, _ StatusManager[O]] struct { | ||
Base | ||
ProductID string | ||
// hooks is a pointer to an EnablementHookFuncs structure so | ||
// that tests can modify the contents of the structure after | ||
// this structure has been initialized | ||
hooks *EnablementHookFuncs[O] | ||
} | ||
|
||
// Init prepares the structure for use by the CLI core. | ||
func (cmd *Disable[O, _]) Init(parent argparser.Registerer, g *global.Data, productID, productName string, hooks *EnablementHookFuncs[O]) { | ||
cmd.CmdClause = parent.Command("disable", "Disable the "+productName+" product") | ||
cmd.hooks = hooks | ||
|
||
cmd.Base.Init(parent, g) | ||
cmd.ProductID = productID | ||
} | ||
|
||
// Exec executes the disablement operation. | ||
func (cmd *Disable[O, S]) Exec(out io.Writer, status S) error { | ||
if cmd.Globals.Verbose() && cmd.JSONOutput.Enabled { | ||
return fsterr.ErrInvalidVerboseJSONCombo | ||
} | ||
|
||
serviceID, source, flag, err := argparser.ServiceID(cmd.ServiceName, *cmd.Globals.Manifest, cmd.Globals.APIClient, cmd.Globals.ErrLog) | ||
if err != nil { | ||
cmd.Globals.ErrLog.Add(err) | ||
return err | ||
} | ||
|
||
if cmd.Globals.Verbose() { | ||
argparser.DisplayServiceID(serviceID, flag, source, out) | ||
} | ||
|
||
err = cmd.hooks.DisableFunc(cmd.Globals.APIClient, serviceID) | ||
if err != nil { | ||
cmd.Globals.ErrLog.Add(err) | ||
return err | ||
} | ||
|
||
status.SetEnabled(false) | ||
// The API does not return details of the service and product | ||
// which were disabled, so they have to be inserted into | ||
// 'status' directly | ||
status.SetProductID(cmd.ProductID) | ||
status.SetServiceID(serviceID) | ||
|
||
if ok, err := cmd.WriteJSON(out, status); ok { | ||
return err | ||
} | ||
|
||
text.Success(out, status.GetTextResult()) | ||
|
||
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,4 @@ | ||
// Package productcore contains a group of generic structures and | ||
// functions which are used to implement common behaviors in product | ||
// enablement/configuration commands | ||
package productcore |
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,62 @@ | ||
package productcore | ||
|
||
import ( | ||
"io" | ||
|
||
"github.com/fastly/cli/pkg/argparser" | ||
fsterr "github.com/fastly/cli/pkg/errors" | ||
"github.com/fastly/cli/pkg/global" | ||
"github.com/fastly/cli/pkg/text" | ||
"github.com/fastly/go-fastly/v9/fastly/products" | ||
) | ||
|
||
// Enable is a base type for all 'enable' commands. | ||
type Enable[O products.ProductOutput, _ StatusManager[O]] struct { | ||
Base | ||
// hooks is a pointer to an EnablementHookFuncs structure so | ||
// that tests can modify the contents of the structure after | ||
// this structure has been initialized | ||
hooks *EnablementHookFuncs[O] | ||
} | ||
|
||
// Init prepares the structure for use by the CLI core. | ||
func (cmd *Enable[O, _]) Init(parent argparser.Registerer, g *global.Data, productName string, hooks *EnablementHookFuncs[O]) { | ||
cmd.CmdClause = parent.Command("enable", "Enable the "+productName+" product") | ||
cmd.hooks = hooks | ||
|
||
cmd.Base.Init(parent, g) | ||
} | ||
|
||
// Exec executes the enablement operation. | ||
func (cmd *Enable[O, S]) Exec(out io.Writer, status S) error { | ||
if cmd.Globals.Verbose() && cmd.JSONOutput.Enabled { | ||
return fsterr.ErrInvalidVerboseJSONCombo | ||
} | ||
|
||
serviceID, source, flag, err := argparser.ServiceID(cmd.ServiceName, *cmd.Globals.Manifest, cmd.Globals.APIClient, cmd.Globals.ErrLog) | ||
if err != nil { | ||
cmd.Globals.ErrLog.Add(err) | ||
return err | ||
} | ||
|
||
if cmd.Globals.Verbose() { | ||
argparser.DisplayServiceID(serviceID, flag, source, out) | ||
} | ||
|
||
o, err := cmd.hooks.EnableFunc(cmd.Globals.APIClient, serviceID) | ||
if err != nil { | ||
cmd.Globals.ErrLog.Add(err) | ||
return err | ||
} | ||
|
||
status.SetEnabled(true) | ||
status.TransformOutput(o) | ||
|
||
if ok, err := cmd.WriteJSON(out, status); ok { | ||
return err | ||
} | ||
|
||
text.Success(out, status.GetTextResult()) | ||
|
||
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,71 @@ | ||
package productcore | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
|
||
"github.com/fastly/cli/pkg/argparser" | ||
fsterr "github.com/fastly/cli/pkg/errors" | ||
"github.com/fastly/cli/pkg/global" | ||
"github.com/fastly/cli/pkg/text" | ||
"github.com/fastly/go-fastly/v9/fastly" | ||
"github.com/fastly/go-fastly/v9/fastly/products" | ||
) | ||
|
||
// Status is a base type for all 'status' commands. | ||
type Status[O products.ProductOutput, _ StatusManager[O]] struct { | ||
Base | ||
// hooks is a pointer to an EnablementHookFuncs structure so | ||
// that tests can modify the contents of the structure after | ||
// this structure has been initialized | ||
hooks *EnablementHookFuncs[O] | ||
} | ||
|
||
// Init prepares the structure for use by the CLI core. | ||
func (cmd *Status[O, _]) Init(parent argparser.Registerer, g *global.Data, productName string, hooks *EnablementHookFuncs[O]) { | ||
cmd.CmdClause = parent.Command("status", "Get the enablement status of the "+productName+" product") | ||
cmd.hooks = hooks | ||
|
||
cmd.Base.Init(parent, g) | ||
} | ||
|
||
// Exec executes the status operation. | ||
func (cmd *Status[O, S]) Exec(out io.Writer, status S) error { | ||
if cmd.Globals.Verbose() && cmd.JSONOutput.Enabled { | ||
return fsterr.ErrInvalidVerboseJSONCombo | ||
} | ||
|
||
serviceID, source, flag, err := argparser.ServiceID(cmd.ServiceName, *cmd.Globals.Manifest, cmd.Globals.APIClient, cmd.Globals.ErrLog) | ||
if err != nil { | ||
cmd.Globals.ErrLog.Add(err) | ||
return err | ||
} | ||
|
||
if cmd.Globals.Verbose() { | ||
argparser.DisplayServiceID(serviceID, flag, source, out) | ||
} | ||
|
||
o, err := cmd.hooks.GetFunc(cmd.Globals.APIClient, serviceID) | ||
if err != nil { | ||
var herr *fastly.HTTPError | ||
|
||
// The API returns a 'Bad Request' error when the | ||
// product has not been enabled on the service; any | ||
// other error should be reported | ||
if !errors.As(err, &herr) || !herr.IsBadRequest() { | ||
cmd.Globals.ErrLog.Add(err) | ||
return err | ||
} | ||
} else { | ||
status.SetEnabled(true) | ||
status.TransformOutput(o) | ||
} | ||
|
||
if ok, err := cmd.WriteJSON(out, status); ok { | ||
return err | ||
} | ||
|
||
text.Info(out, status.GetTextResult()) | ||
|
||
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,4 @@ | ||
// Package productcore_test contains a group of generic structures and | ||
// functions which are used to implement common behaviors in product | ||
// enablement/configuration tests | ||
package productcore_test |
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.