-
Notifications
You must be signed in to change notification settings - Fork 61
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
Implement Compute@Edge Free Trial Activation #531
Changes from 1 commit
e92c862
098fd8d
88aeb91
729f503
31bd6ca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package undocumented | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a new package that provides an abstraction for making API calls for undocumented endpoints. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For whatever reason I'm not overly keen on the name I tried using |
||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"strings" | ||
"time" | ||
|
||
"github.com/fastly/cli/pkg/api" | ||
fsterr "github.com/fastly/cli/pkg/errors" | ||
"github.com/fastly/cli/pkg/useragent" | ||
) | ||
|
||
// EdgeComputeTrial is the API endpoint for activating a compute trial. | ||
const EdgeComputeTrial = "/customer/%s/edge-compute-trial" | ||
|
||
// RequestTimeout is the timeout for the API network request. | ||
const RequestTimeout = 5 * time.Second | ||
|
||
// Get calls the given API endpoint and returns its response data. | ||
func Get(host, path, token string, c api.HTTPClient) (data []byte, statusCode int, err error) { | ||
host = strings.TrimSuffix(host, "/") | ||
endpoint := fmt.Sprintf("%s%s", host, path) | ||
|
||
req, err := http.NewRequest(http.MethodPost, endpoint, nil) | ||
if err != nil { | ||
return data, statusCode, err | ||
} | ||
|
||
req.Header.Set("Fastly-Key", token) | ||
req.Header.Set("User-Agent", useragent.Name) | ||
|
||
res, err := c.Do(req) | ||
if err != nil { | ||
if urlErr, ok := err.(*url.Error); ok && urlErr.Timeout() { | ||
return data, statusCode, fsterr.RemediationError{ | ||
Inner: err, | ||
Remediation: fsterr.NetworkRemediation, | ||
} | ||
} | ||
return data, statusCode, err | ||
} | ||
defer res.Body.Close() | ||
|
||
if res.StatusCode != http.StatusOK { | ||
return data, res.StatusCode, fmt.Errorf("error from API: '%s'", res.Status) | ||
} | ||
|
||
data, err = io.ReadAll(res.Body) | ||
if err != nil { | ||
return data, res.StatusCode, err | ||
} | ||
|
||
return data, res.StatusCode, nil | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -89,13 +89,13 @@ func defineCommands( | |
backendList := backend.NewListCommand(backendCmdRoot.CmdClause, globals, data) | ||
backendUpdate := backend.NewUpdateCommand(backendCmdRoot.CmdClause, globals, data) | ||
computeCmdRoot := compute.NewRootCommand(app, globals) | ||
computeBuild := compute.NewBuildCommand(computeCmdRoot.CmdClause, opts.HTTPClient, globals, data) | ||
computeDeploy := compute.NewDeployCommand(computeCmdRoot.CmdClause, opts.HTTPClient, globals, data) | ||
computeInit := compute.NewInitCommand(computeCmdRoot.CmdClause, opts.HTTPClient, globals, data) | ||
computeBuild := compute.NewBuildCommand(computeCmdRoot.CmdClause, globals, data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A bunch of commands were explicitly passing in a HTTP client and then not using it. I additionally (see below in |
||
computeDeploy := compute.NewDeployCommand(computeCmdRoot.CmdClause, globals, data) | ||
computeInit := compute.NewInitCommand(computeCmdRoot.CmdClause, globals, data) | ||
computePack := compute.NewPackCommand(computeCmdRoot.CmdClause, globals, data) | ||
computePublish := compute.NewPublishCommand(computeCmdRoot.CmdClause, globals, computeBuild, computeDeploy, data) | ||
computeServe := compute.NewServeCommand(computeCmdRoot.CmdClause, globals, computeBuild, opts.Versioners.Viceroy, data) | ||
computeUpdate := compute.NewUpdateCommand(computeCmdRoot.CmdClause, opts.HTTPClient, globals, data) | ||
computeUpdate := compute.NewUpdateCommand(computeCmdRoot.CmdClause, globals, data) | ||
computeValidate := compute.NewValidateCommand(computeCmdRoot.CmdClause, globals) | ||
configureCmdRoot := configure.NewRootCommand(app, opts.ConfigPath, configure.APIClientFactory(opts.APIClient), globals) | ||
dictionaryCmdRoot := dictionary.NewRootCommand(app, globals) | ||
|
@@ -302,7 +302,7 @@ func defineCommands( | |
statsHistorical := stats.NewHistoricalCommand(statsCmdRoot.CmdClause, globals, data) | ||
statsRealtime := stats.NewRealtimeCommand(statsCmdRoot.CmdClause, globals, data) | ||
statsRegions := stats.NewRegionsCommand(statsCmdRoot.CmdClause, globals) | ||
updateRoot := update.NewRootCommand(app, opts.ConfigPath, opts.Versioners.CLI, opts.HTTPClient, globals) | ||
updateRoot := update.NewRootCommand(app, opts.ConfigPath, opts.Versioners.CLI, globals) | ||
userCmdRoot := user.NewRootCommand(app, globals) | ||
userCreate := user.NewCreateCommand(userCmdRoot.CmdClause, globals, data) | ||
userDelete := user.NewDeleteCommand(userCmdRoot.CmdClause, globals, data) | ||
|
@@ -323,7 +323,7 @@ func defineCommands( | |
vclSnippetList := snippet.NewListCommand(vclSnippetCmdRoot.CmdClause, globals, data) | ||
vclSnippetUpdate := snippet.NewUpdateCommand(vclSnippetCmdRoot.CmdClause, globals, data) | ||
versionCmdRoot := version.NewRootCommand(app, opts.Versioners.Viceroy) | ||
whoamiCmdRoot := whoami.NewRootCommand(app, opts.HTTPClient, globals) | ||
whoamiCmdRoot := whoami.NewRootCommand(app, globals) | ||
|
||
return []cmd.Command{ | ||
shellcompleteCmdRoot, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -54,11 +54,12 @@ func Run(opts RunOpts) error { | |
// The globals will hold generally-applicable configuration parameters | ||
// from a variety of sources, and is provided to each concrete command. | ||
globals := config.Data{ | ||
File: opts.ConfigFile, | ||
Path: opts.ConfigPath, | ||
Env: opts.Env, | ||
Output: opts.Stdout, | ||
ErrLog: opts.ErrLog, | ||
File: opts.ConfigFile, | ||
Path: opts.ConfigPath, | ||
Env: opts.Env, | ||
Output: opts.Stdout, | ||
ErrLog: opts.ErrLog, | ||
HTTPClient: opts.HTTPClient, | ||
} | ||
|
||
// Set up the main application root, including global flags, and then each | ||
|
@@ -148,7 +149,7 @@ func Run(opts RunOpts) error { | |
} | ||
} | ||
|
||
globals.Client, err = opts.APIClient(token, endpoint) | ||
globals.APIClient, err = opts.APIClient(token, endpoint) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The use of |
||
if err != nil { | ||
globals.ErrLog.Add(err) | ||
return fmt.Errorf("error constructing Fastly API client: %w", err) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You shouldn't use the golang default http client as it doesn't provide preconfigured timeouts.
For some reason I had previously used a strange pattern of using it with a context object for handling timeouts. The implementation worked but it's a non-standard pattern to use so I've cleaned this up.