From 3880654ca6d26351e4b305a1561d3e147f819f2d Mon Sep 17 00:00:00 2001 From: Ahmed Abdalla Date: Thu, 15 Aug 2024 09:26:29 +0200 Subject: [PATCH] OCM-10455 | feat: Add a user agent config This is needed by arbitrary services which use the client package to communicate with ocm / rosa. Signed-off-by: Ahmed Abdalla --- pkg/config/config.go | 1 + pkg/config/config_test.go | 1 + pkg/info/info.go | 2 +- pkg/ocm/client.go | 7 ++++++- 4 files changed, 9 insertions(+), 2 deletions(-) diff --git a/pkg/config/config.go b/pkg/config/config.go index 5033587e4e..fc847ecacd 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -54,6 +54,7 @@ type Config struct { Scopes []string `json:"scopes,omitempty" doc:"OpenID scope."` TokenURL string `json:"token_url,omitempty" doc:"OpenID token URL."` URL string `json:"url,omitempty" doc:"URL of the API gateway."` + UserAgent string `json:"user_agent,omitempty" doc:"OCM clients UserAgent. Default value is used if not set."` FedRAMP bool `json:"fedramp,omitempty" doc:"Indicates FedRAMP."` } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 7b04eb16b4..21602db3a7 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -53,6 +53,7 @@ var _ = Describe("Config", Ordered, func() { "scopes": "OpenID scope.", "token_url": "OpenID token URL.", "url": "URL of the API gateway.", + "user_agent": "OCM clients UserAgent. Default value is used if not set.", "fedramp": "Indicates FedRAMP.", } diff --git a/pkg/info/info.go b/pkg/info/info.go index db389e98eb..5e3cfc3a74 100644 --- a/pkg/info/info.go +++ b/pkg/info/info.go @@ -23,4 +23,4 @@ const Version = "1.2.45" // Build contains the short Git SHA of the CLI at the point it was build. Set via `-ldflags` at build time var Build = "local" -const UserAgent = "ROSACLI" +const DefaultUserAgent = "ROSACLI" diff --git a/pkg/ocm/client.go b/pkg/ocm/client.go index 33534c1256..3d90da450c 100644 --- a/pkg/ocm/client.go +++ b/pkg/ocm/client.go @@ -118,7 +118,12 @@ func (b *ClientBuilder) Build() (result *Client, err error) { // values in the configuration, so that default values won't be overridden: builder := sdk.NewConnectionBuilder() builder.Logger(logger) - builder.Agent(info.UserAgent + "/" + info.Version + " " + sdk.DefaultAgent) + + userAgent := info.DefaultUserAgent + if b.cfg.UserAgent != "" { + userAgent = b.cfg.UserAgent + } + builder.Agent(userAgent + "/" + info.Version + " " + sdk.DefaultAgent) if b.cfg.TokenURL != "" { builder.TokenURL(b.cfg.TokenURL) }