Skip to content
Draft
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
17 changes: 10 additions & 7 deletions api/apihelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,19 @@ const (
)

type APIHelper struct {
Endpoint *APIEndpoint
Client *CFClient
Logger trace.Printer
Endpoint *APIEndpoint
Client *CFClient
Logger trace.Printer
UserAgent string
}

func NewAPIHelper(endpoint *APIEndpoint, cfclient *CFClient, traceEnabled string) *APIHelper {
func NewAPIHelper(endpoint *APIEndpoint, cfclient *CFClient, traceEnabled string, userAgent string) *APIHelper {

return &APIHelper{
Endpoint: endpoint,
Client: cfclient,
Logger: trace.NewLogger(os.Stdout, false, traceEnabled, ""),
Endpoint: endpoint,
Client: cfclient,
Logger: trace.NewLogger(os.Stdout, false, traceEnabled, ""),
UserAgent: userAgent,
}
}

Expand All @@ -71,6 +73,7 @@ func makeTransport(skipSSLValidation bool, logger trace.Printer) http.RoundTripp
}

func (helper *APIHelper) DoRequest(req *http.Request) (*http.Response, error) {
req.Header.Set("User-Agent", helper.UserAgent)

client := newHTTPClient(helper.Endpoint.SkipSSLValidation || helper.Client.IsSSLDisabled, helper.Logger)
resp, err := client.Do(req)
Expand Down
16 changes: 16 additions & 0 deletions api/apihelper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var _ = Describe("API Helper Test", func() {
const (
fakeAppId string = "fakeAppId"
fakeAccessToken string = "fakeAccessToken"
fakeUserAgent string = "app-autoscaler-cli-plugin/1.2.3 (https://github.com/test) Go/test"
)

var (
Expand Down Expand Up @@ -112,6 +113,7 @@ var _ = Describe("API Helper Test", func() {
AppName: "fakeAppName",
},
"false",
fakeUserAgent,
)

})
Expand All @@ -120,6 +122,19 @@ var _ = Describe("API Helper Test", func() {
apiServer.Close()
})

Context("User-Agent header", func() {
It("is set on requests", func() {
apiServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.RespondWith(http.StatusOK, ""),
ghttp.VerifyHeaderKV("User-Agent", fakeUserAgent),
),
)
err = apihelper.CheckHealth()
Expect(err).NotTo(HaveOccurred())
})
})

Context("Common invalid access errors", func() {

Context("Server not started", func() {
Expand Down Expand Up @@ -184,6 +199,7 @@ var _ = Describe("API Helper Test", func() {
AppName: "fakeAppName",
},
"false",
fakeUserAgent,
)

})
Expand Down
3 changes: 2 additions & 1 deletion api/cf_api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ type CFAPIClient struct {
client *cf_client.Client
}

func NewCFAPIClient(ccAPIEndpoint *url.URL, authToken string, skipTLSValidation bool) (*CFAPIClient, error) {
func NewCFAPIClient(ccAPIEndpoint *url.URL, authToken string, skipTLSValidation bool, userAgent string) (*CFAPIClient, error) {
// A refresh token is not provided by the CF CLI Plugin API and is not required as
// "AccessToken() now provides a refreshed o-auth token.",
// see https://github.com/cloudfoundry/cli/blob/main/plugin/plugin_examples/CHANGELOG.md#changes-in-v614
refreshToken := ""

cfClientConfigOptions := []cf_client_config.Option{
cf_client_config.Token(authToken, refreshToken),
cf_client_config.UserAgent(userAgent),
}

if skipTLSValidation {
Expand Down
6 changes: 4 additions & 2 deletions api/cfclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type CFClient struct {
AppId string
AppName string
IsSSLDisabled bool
UserAgent string
}

type Connection interface {
Expand All @@ -27,7 +28,7 @@ type Connection interface {
IsSSLDisabled() (bool, error)
}

func NewCFClient(connection Connection) (*CFClient, error) {
func NewCFClient(connection Connection, userAgent string) (*CFClient, error) {

ccAPIEndpoint, err := connection.ApiEndpoint()
if err != nil {
Expand All @@ -46,6 +47,7 @@ func NewCFClient(connection Connection) (*CFClient, error) {
connection: connection,
CCAPIEndpoint: ccAPIEndpoint,
IsSSLDisabled: isSSLDisabled,
UserAgent: userAgent,
}

return client, nil
Expand Down Expand Up @@ -83,7 +85,7 @@ func (client *CFClient) Configure(appName string) error {
return err
}

cfAPIClient, err := NewCFAPIClient(ccAPIURL, authToken, client.IsSSLDisabled)
cfAPIClient, err := NewCFAPIClient(ccAPIURL, authToken, client.IsSSLDisabled, client.UserAgent)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func UnsetEndpoint() error {
return nil
}

func SetEndpoint(cfclient *CFClient, url string, skipSSLValidation bool) error {
func SetEndpoint(cfclient *CFClient, url string, skipSSLValidation bool, userAgent string) error {

cfDomain := getDomain(cfclient.CCAPIEndpoint)
autoscalerDomain := getDomain(url)
Expand All @@ -55,7 +55,7 @@ func SetEndpoint(cfclient *CFClient, url string, skipSSLValidation bool) error {
SkipSSLValidation: skipSSLValidation,
}

apihelper := NewAPIHelper(endpoint, cfclient, os.Getenv("CF_TRACE"))
apihelper := NewAPIHelper(endpoint, cfclient, os.Getenv("CF_TRACE"), userAgent)
err := apihelper.CheckHealth()
if err != nil {
return err
Expand All @@ -72,7 +72,7 @@ func SetEndpoint(cfclient *CFClient, url string, skipSSLValidation bool) error {
return nil
}

func GetEndpoint(cfclient *CFClient) (*APIEndpoint, error) {
func GetEndpoint(cfclient *CFClient, userAgent string) (*APIEndpoint, error) {

endpoint, err := getEndpointFromConfig()
if err != nil {
Expand All @@ -89,7 +89,7 @@ func GetEndpoint(cfclient *CFClient) (*APIEndpoint, error) {
}

if endpoint.URL == "" {
endpoint, err = getDefaultEndpoint(cfclient)
endpoint, err = getDefaultEndpoint(cfclient, userAgent)
if err != nil {
return nil, err
}
Expand All @@ -98,13 +98,13 @@ func GetEndpoint(cfclient *CFClient) (*APIEndpoint, error) {

}

func getDefaultEndpoint(cfclient *CFClient) (*APIEndpoint, error) {
func getDefaultEndpoint(cfclient *CFClient, userAgent string) (*APIEndpoint, error) {

ccAPIURL := cfclient.CCAPIEndpoint
asAPIURL := strings.Replace(ccAPIURL, "api.", "autoscaler.", 1)

//ignore all erros here if the default value won't work
SetEndpoint(cfclient, asAPIURL, cfclient.IsSSLDisabled)
SetEndpoint(cfclient, asAPIURL, cfclient.IsSSLDisabled, userAgent)
return getEndpointFromConfig()

}
Expand Down
29 changes: 15 additions & 14 deletions api/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var _ = Describe("Endpoint Helper Test", func() {

const (
fakeApiEndpoint = "autoscaler.boshlite.com"
fakeUserAgent = "test-agent"
)

var (
Expand Down Expand Up @@ -50,18 +51,18 @@ var _ = Describe("Endpoint Helper Test", func() {

cliConnection.ApiEndpointReturns(apiServer.URL(), nil)
cliConnection.IsSSLDisabledReturns(false, nil)
cfclient, err = NewCFClient(cliConnection)
cfclient, err = NewCFClient(cliConnection, fakeUserAgent)
Expect(err).NotTo(HaveOccurred())
})

Context("When endpoint is valid", func() {
BeforeEach(func() {
err = SetEndpoint(cfclient, apiServer.URL()+"/", false)
err = SetEndpoint(cfclient, apiServer.URL()+"/", false, fakeUserAgent)
Expect(err).NotTo(HaveOccurred())
})

It("Set a valid json to config file", func() {
err = SetEndpoint(cfclient, apiServer.URL(), false)
err = SetEndpoint(cfclient, apiServer.URL(), false, fakeUserAgent)
Expect(err).NotTo(HaveOccurred())

content, err = ioutil.ReadFile(configFilePath)
Expand All @@ -80,11 +81,11 @@ var _ = Describe("Endpoint Helper Test", func() {
BeforeEach(func() {
cliConnection.ApiEndpointReturns("api.bosh-lite.com", nil)
cliConnection.IsSSLDisabledReturns(false, nil)
cfclient, err = NewCFClient(cliConnection)
cfclient, err = NewCFClient(cliConnection, fakeUserAgent)
Expect(err).NotTo(HaveOccurred())
})
It("it fails", func() {
err = SetEndpoint(cfclient, apiServer.URL(), false)
err = SetEndpoint(cfclient, apiServer.URL(), false, fakeUserAgent)
Expect(err).To(HaveOccurred())
})
})
Expand All @@ -97,7 +98,7 @@ var _ = Describe("Endpoint Helper Test", func() {
})

It("it fails", func() {
err = SetEndpoint(cfclient, apiServer.URL(), false)
err = SetEndpoint(cfclient, apiServer.URL(), false, fakeUserAgent)
Expect(err).To(HaveOccurred())
})
})
Expand Down Expand Up @@ -131,7 +132,7 @@ var _ = Describe("Endpoint Helper Test", func() {

cliConnection.ApiEndpointReturns(apiServer.URL(), nil)
cliConnection.IsSSLDisabledReturns(false, nil)
cfclient, err = NewCFClient(cliConnection)
cfclient, err = NewCFClient(cliConnection, fakeUserAgent)
Expect(err).NotTo(HaveOccurred())
})

Expand All @@ -144,7 +145,7 @@ var _ = Describe("Endpoint Helper Test", func() {
})

It("Return the existing URL when it's domain still consistent with the current cf domain", func() {
endpoint, err = GetEndpoint(cfclient)
endpoint, err = GetEndpoint(cfclient, fakeUserAgent)
Expect(err).NotTo(HaveOccurred())
Expect(endpoint.URL).Should(Equal(apiServer.URL()))
})
Expand All @@ -158,7 +159,7 @@ var _ = Describe("Endpoint Helper Test", func() {
})

It("Clear staled setting and return the default autoscaler endpoint if it does work ", func() {
endpoint, err = GetEndpoint(cfclient)
endpoint, err = GetEndpoint(cfclient, fakeUserAgent)
Expect(err).NotTo(HaveOccurred())
Expect(endpoint.URL).Should(Equal(apiServer.URL()))
})
Expand All @@ -171,7 +172,7 @@ var _ = Describe("Endpoint Helper Test", func() {
})

It("Clear staled setting and set the endpoint to empty", func() {
endpoint, err = GetEndpoint(cfclient)
endpoint, err = GetEndpoint(cfclient, fakeUserAgent)
Expect(err).NotTo(HaveOccurred())
Expect(endpoint.URL).Should(Equal(""))
})
Expand All @@ -187,7 +188,7 @@ var _ = Describe("Endpoint Helper Test", func() {
})

It("Return a default URL when it is an valid autoscaler api server", func() {
endpoint, err = GetEndpoint(cfclient)
endpoint, err = GetEndpoint(cfclient, fakeUserAgent)
Expect(err).NotTo(HaveOccurred())
Expect(endpoint.URL).Should(Equal(apiServer.URL()))
})
Expand All @@ -201,7 +202,7 @@ var _ = Describe("Endpoint Helper Test", func() {
})

It("Return empty string ", func() {
endpoint, err = GetEndpoint(cfclient)
endpoint, err = GetEndpoint(cfclient, fakeUserAgent)
Expect(err).NotTo(HaveOccurred())
Expect(endpoint.URL).Should(Equal(""))
})
Expand All @@ -219,7 +220,7 @@ var _ = Describe("Endpoint Helper Test", func() {
})

It("Clear the wrong setting and return the default autoscaler endpoint if it works", func() {
endpoint, err = GetEndpoint(cfclient)
endpoint, err = GetEndpoint(cfclient, fakeUserAgent)
Expect(err).NotTo(HaveOccurred())
Expect(endpoint.URL).Should(Equal(apiServer.URL()))
})
Expand All @@ -235,7 +236,7 @@ var _ = Describe("Endpoint Helper Test", func() {
})

It("Clear the wrong setting and return the default autoscaler endpoint if it works", func() {
endpoint, err = GetEndpoint(cfclient)
endpoint, err = GetEndpoint(cfclient, fakeUserAgent)
Expect(err).NotTo(HaveOccurred())
Expect(endpoint.URL).Should(Equal(apiServer.URL()))
})
Expand Down
10 changes: 5 additions & 5 deletions commands/attach_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ type AttachPolicyPositionalArgs struct {
}

func (command AttachPolicyCommand) Execute([]string) error {
return CreatePolicy(AutoScaler.CLIConnection, command.RequiredlArgs.AppName, command.RequiredlArgs.PolicyFile)
return CreatePolicy(AutoScaler.CLIConnection, command.RequiredlArgs.AppName, command.RequiredlArgs.PolicyFile, AutoScaler.UserAgent)
}

func CreatePolicy(cliConnection api.Connection, appName string, policyFile string) error {
func CreatePolicy(cliConnection api.Connection, appName string, policyFile string, userAgent string) error {

cfclient, err := api.NewCFClient(cliConnection)
cfclient, err := api.NewCFClient(cliConnection, userAgent)
if err != nil {
return err
}
endpoint, err := api.GetEndpoint(cfclient)
endpoint, err := api.GetEndpoint(cfclient, userAgent)
if err != nil {
return err
}
Expand All @@ -43,7 +43,7 @@ func CreatePolicy(cliConnection api.Connection, appName string, policyFile strin
return err
}

apihelper := api.NewAPIHelper(endpoint, cfclient, os.Getenv("CF_TRACE"))
apihelper := api.NewAPIHelper(endpoint, cfclient, os.Getenv("CF_TRACE"), userAgent)

ui.SayMessage(ui.AttachPolicyHint, appName)
contents, err := ioutil.ReadFile(policyFile)
Expand Down
1 change: 1 addition & 0 deletions commands/autoscaler.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

type AutoScalerCmds struct {
CLIConnection api.Connection
UserAgent string

API ApiCommand `command:"autoscaling-api" description:"Set or view AutoScaler service API endpoint"`
Policy PolicyCommand `command:"autoscaling-policy" description:"Retrieve the scaling policy of an application"`
Expand Down
10 changes: 5 additions & 5 deletions commands/detach_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ type DetachPolicyPositionalArgs struct {
}

func (command DetachPolicyCommand) Execute([]string) error {
return DetachPolicy(AutoScaler.CLIConnection, command.RequiredlArgs.AppName)
return DetachPolicy(AutoScaler.CLIConnection, command.RequiredlArgs.AppName, AutoScaler.UserAgent)
}

func DetachPolicy(cliConnection api.Connection, appName string) error {
func DetachPolicy(cliConnection api.Connection, appName string, userAgent string) error {

cfclient, err := api.NewCFClient(cliConnection)
cfclient, err := api.NewCFClient(cliConnection, userAgent)
if err != nil {
return err
}

endpoint, err := api.GetEndpoint(cfclient)
endpoint, err := api.GetEndpoint(cfclient, userAgent)
if err != nil {
return err
}
Expand All @@ -40,7 +40,7 @@ func DetachPolicy(cliConnection api.Connection, appName string) error {
return err
}

apihelper := api.NewAPIHelper(endpoint, cfclient, os.Getenv("CF_TRACE"))
apihelper := api.NewAPIHelper(endpoint, cfclient, os.Getenv("CF_TRACE"), userAgent)

ui.SayMessage(ui.DetachPolicyHint, appName)
err = apihelper.DeletePolicy()
Expand Down
Loading