diff --git a/README.md b/README.md index 548010a149..d692571888 100644 --- a/README.md +++ b/README.md @@ -1154,7 +1154,7 @@ $ scw inspect myserver | jq '.[0].public_ip.address' ### master (unreleased) -* No entry +* Add User-Agent with scw version ([#269](https://github.com/scaleway/scaleway-cli/issues/269)) View full [commits list](https://github.com/scaleway/scaleway-cli/compare/v1.7.0...master) diff --git a/pkg/api/api.go b/pkg/api/api.go index 6666289e95..291d806f77 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -58,6 +58,8 @@ type ScalewayAPI struct { // Password is the authentication password password string + userAgent string + // Cache is used to quickly resolve identifiers from names Cache *ScalewayCache @@ -784,7 +786,7 @@ var FuncMap = template.FuncMap{ } // NewScalewayAPI creates a ready-to-use ScalewayAPI client -func NewScalewayAPI(apiEndPoint, accountEndPoint, organization, token string) (*ScalewayAPI, error) { +func NewScalewayAPI(apiEndPoint, accountEndPoint, organization, token, userAgent string) (*ScalewayAPI, error) { cache, err := NewScalewayCache() if err != nil { return nil, err @@ -799,6 +801,7 @@ func NewScalewayAPI(apiEndPoint, accountEndPoint, organization, token string) (* Cache: cache, verbose: os.Getenv("SCW_VERBOSE_API") != "", password: "", + userAgent: userAgent, // internal anonuuid: *anonuuid.New(), @@ -829,6 +832,7 @@ func (s *ScalewayAPI) GetResponse(resource string) (*http.Response, error) { } req.Header.Set("X-Auth-Token", s.Token) req.Header.Set("Content-Type", "application/json") + req.Header.Set("User-Agent", s.userAgent) curl, err := http2curl.GetCurlCommand(req) if err != nil { @@ -857,6 +861,7 @@ func (s *ScalewayAPI) PostResponse(resource string, data interface{}) (*http.Res } req.Header.Set("X-Auth-Token", s.Token) req.Header.Set("Content-Type", "application/json") + req.Header.Set("User-Agent", s.userAgent) curl, err := http2curl.GetCurlCommand(req) if err != nil { @@ -886,6 +891,7 @@ func (s *ScalewayAPI) PatchResponse(resource string, data interface{}) (*http.Re } req.Header.Set("X-Auth-Token", s.Token) req.Header.Set("Content-Type", "application/json") + req.Header.Set("User-Agent", s.userAgent) curl, err := http2curl.GetCurlCommand(req) if err != nil { @@ -915,6 +921,7 @@ func (s *ScalewayAPI) PutResponse(resource string, data interface{}) (*http.Resp } req.Header.Set("X-Auth-Token", s.Token) req.Header.Set("Content-Type", "application/json") + req.Header.Set("User-Agent", s.userAgent) curl, err := http2curl.GetCurlCommand(req) if err != nil { @@ -939,6 +946,7 @@ func (s *ScalewayAPI) DeleteResponse(resource string) (*http.Response, error) { } req.Header.Set("X-Auth-Token", s.Token) req.Header.Set("Content-Type", "application/json") + req.Header.Set("User-Agent", s.userAgent) curl, err := http2curl.GetCurlCommand(req) if err != nil { @@ -1657,6 +1665,7 @@ func (s *ScalewayAPI) PatchUserdata(serverID string, key string, value []byte) e req.Header.Set("X-Auth-Token", s.Token) req.Header.Set("Content-Type", "text/plain") + req.Header.Set("User-Agent", s.userAgent) curl, err := http2curl.GetCurlCommand(req) if os.Getenv("SCW_SENSITIVE") != "1" { diff --git a/pkg/api/api_test.go b/pkg/api/api_test.go index 26bac3fc56..ad4d80c3e9 100644 --- a/pkg/api/api_test.go +++ b/pkg/api/api_test.go @@ -3,12 +3,13 @@ package api import ( "testing" + "github.com/scaleway/scaleway-cli/pkg/scwversion" . "github.com/smartystreets/goconvey/convey" ) func TestNewScalewayAPI(t *testing.T) { Convey("Testing NewScalewayAPI()", t, func() { - api, err := NewScalewayAPI("http://api-endpoint.com", "http://account-endpoint.com", "my-organization", "my-token") + api, err := NewScalewayAPI("http://api-endpoint.com", "http://account-endpoint.com", "my-organization", "my-token", scwversion.UserAgent()) So(err, ShouldBeNil) So(api, ShouldNotBeNil) So(api.ComputeAPI, ShouldEqual, "http://api-endpoint.com") diff --git a/pkg/cli/main.go b/pkg/cli/main.go index f7b98704d3..c6228174cf 100644 --- a/pkg/cli/main.go +++ b/pkg/cli/main.go @@ -146,7 +146,7 @@ func getScalewayAPI() (*api.ScalewayAPI, error) { if err != nil { return nil, err } - return api.NewScalewayAPI(os.Getenv("scaleway_api_endpoint"), config.AccountAPI, config.Organization, config.Token) + return api.NewScalewayAPI(os.Getenv("scaleway_api_endpoint"), config.AccountAPI, config.Organization, config.Token, scwversion.UserAgent()) } func initLogging(debug bool, verbose bool, streams *commands.Streams) { diff --git a/pkg/cli/x_userdata.go b/pkg/cli/x_userdata.go index bdf1594640..35092b2fea 100644 --- a/pkg/cli/x_userdata.go +++ b/pkg/cli/x_userdata.go @@ -10,6 +10,7 @@ import ( "strings" "github.com/scaleway/scaleway-cli/pkg/api" + "github.com/scaleway/scaleway-cli/pkg/scwversion" ) var cmdUserdata = &Command{ @@ -46,7 +47,7 @@ func runUserdata(cmd *Command, args []string) error { var err error var serverID string if args[0] == "local" { - API, err = api.NewScalewayAPI("", "", "", "") + API, err = api.NewScalewayAPI("", "", "", "", scwversion.UserAgent()) if err != nil { return err } diff --git a/pkg/commands/command_test.go b/pkg/commands/command_test.go index 6252a44bc1..0fec34d8c1 100644 --- a/pkg/commands/command_test.go +++ b/pkg/commands/command_test.go @@ -11,11 +11,12 @@ import ( "testing" "github.com/scaleway/scaleway-cli/pkg/api" + "github.com/scaleway/scaleway-cli/pkg/scwversion" "github.com/stretchr/testify/assert" ) func testCommandContext() CommandContext { - apiClient, err := api.NewScalewayAPI("https://example.org/", "https://example.org/", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") + apiClient, err := api.NewScalewayAPI("https://example.org/", "https://example.org/", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", scwversion.UserAgent()) if err != nil { panic(err) } @@ -36,7 +37,7 @@ func testCommandContext() CommandContext { } func ExampleCommandContext() { - apiClient, err := api.NewScalewayAPI("https://example.org/", "https://example.org/", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") + apiClient, err := api.NewScalewayAPI("https://example.org/", "https://example.org/", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",scwversion.UserAgent()) if err != nil { panic(err) } diff --git a/pkg/commands/login.go b/pkg/commands/login.go index 80fe4cefae..c86d63db7f 100644 --- a/pkg/commands/login.go +++ b/pkg/commands/login.go @@ -19,6 +19,7 @@ import ( "github.com/scaleway/scaleway-cli/pkg/api" "github.com/scaleway/scaleway-cli/pkg/config" + "github.com/scaleway/scaleway-cli/pkg/scwversion" ) // LoginArgs are arguments passed to `RunLogin` @@ -83,7 +84,7 @@ func selectKey(args *LoginArgs) error { } func getToken(connect api.ScalewayConnect) (string, error) { - FakeConnection, err := api.NewScalewayAPI(api.ComputeAPI, api.AccountAPI, "", "") + FakeConnection, err := api.NewScalewayAPI(api.ComputeAPI, api.AccountAPI, "", "", scwversion.UserAgent()) if err != nil { return "", fmt.Errorf("Unable to create a fake ScalewayAPI: %s", err) } @@ -113,7 +114,7 @@ func getToken(connect api.ScalewayConnect) (string, error) { } func getOrganization(token string, email string) (string, error) { - FakeConnection, err := api.NewScalewayAPI(api.ComputeAPI, api.AccountAPI, "", token) + FakeConnection, err := api.NewScalewayAPI(api.ComputeAPI, api.AccountAPI, "", token, scwversion.UserAgent()) if err != nil { return "", fmt.Errorf("Unable to create a fake ScalewayAPI: %s", err) } @@ -217,7 +218,7 @@ func RunLogin(ctx CommandContext, args LoginArgs) error { Token: strings.Trim(args.Token, "\n"), } - apiConnection, err := api.NewScalewayAPI(cfg.ComputeAPI, cfg.AccountAPI, cfg.Organization, cfg.Token) + apiConnection, err := api.NewScalewayAPI(cfg.ComputeAPI, cfg.AccountAPI, cfg.Organization, cfg.Token, scwversion.UserAgent()) if err != nil { return fmt.Errorf("Unable to create ScalewayAPI: %s", err) } diff --git a/pkg/commands/test.go b/pkg/commands/test.go index c206688f54..706034df51 100644 --- a/pkg/commands/test.go +++ b/pkg/commands/test.go @@ -6,10 +6,11 @@ import ( "os" "strings" - "github.com/scaleway/scaleway-cli/pkg/api" - "github.com/scaleway/scaleway-cli/pkg/config" "github.com/Sirupsen/logrus" "github.com/moul/anonuuid" + "github.com/scaleway/scaleway-cli/pkg/api" + "github.com/scaleway/scaleway-cli/pkg/config" + "github.com/scaleway/scaleway-cli/pkg/scwversion" ) func shouldBeAnUUID(actual interface{}, expected ...interface{}) string { @@ -41,7 +42,7 @@ func RealAPIContext() *CommandContext { return nil } - apiClient, err := api.NewScalewayAPI(config.ComputeAPI, config.AccountAPI, config.Organization, config.Token) + apiClient, err := api.NewScalewayAPI(config.ComputeAPI, config.AccountAPI, config.Organization, config.Token, scwversion.UserAgent()) if err != nil { logrus.Warnf("RealAPIContext: failed to call api.NewScalewayAPI(): %v", err) return nil diff --git a/pkg/scwversion/version.go b/pkg/scwversion/version.go index e0f1cd8ba3..a19cc5aa22 100644 --- a/pkg/scwversion/version.go +++ b/pkg/scwversion/version.go @@ -1,9 +1,16 @@ package scwversion +import "fmt" + var ( // VERSION represents the semver version of the package, it is configured at build time - VERSION string + VERSION = "v1.7.0" // GITCOMMIT represents the git commit hash of the package, it is configured at build time GITCOMMIT string ) + +// UserAgent returns a string to be used by API +func UserAgent() string { + return fmt.Sprintf("scw/%v", VERSION) +}