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

User agent #273

Merged
merged 2 commits into from
Jan 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
11 changes: 10 additions & 1 deletion pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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(),
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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" {
Expand Down
3 changes: 2 additions & 1 deletion pkg/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion pkg/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion pkg/cli/x_userdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"strings"

"github.com/scaleway/scaleway-cli/pkg/api"
"github.com/scaleway/scaleway-cli/pkg/scwversion"
)

var cmdUserdata = &Command{
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 3 additions & 2 deletions pkg/commands/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/commands/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/commands/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion pkg/scwversion/version.go
Original file line number Diff line number Diff line change
@@ -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)
}