Skip to content

feat(cli): set custom user-agent header for STACKIT API calls #741

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

Merged
merged 3 commits into from
May 12, 2025
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
30 changes: 21 additions & 9 deletions .github/docs/contribution-guide/client.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,45 @@
package client

import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
sdkConfig "github.com/stackitcloud/stackit-sdk-go/core/config"
"github.com/stackitcloud/stackit-sdk-go/services/foo"
// (...)
)

func ConfigureClient(cmd *cobra.Command) (*foo.APIClient, error) {
var err error
var apiClient foo.APIClient
var cfgOptions []sdkConfig.ConfigurationOption

authCfgOption, err := auth.AuthenticationConfig(cmd, auth.AuthorizeUser)
func ConfigureClient(p *print.Printer, cliVersion string) (*foo.APIClient, error) {
authCfgOption, err := auth.AuthenticationConfig(p, auth.AuthorizeUser)
if err != nil {
return nil, &errors.AuthError{}
}
cfgOptions = append(cfgOptions, authCfgOption, sdkConfig.WithRegion("eu01")) // Configuring region is needed if "foo" is a regional API

region := viper.GetString(config.RegionKey)
cfgOptions := []sdkConfig.ConfigurationOption{
utils.UserAgentConfigOption(cliVersion),
sdkConfig.WithRegion(region), // Configuring region is needed if "foo" is a regional API
authCfgOption,
}

customEndpoint := viper.GetString(config.fooCustomEndpointKey)

if customEndpoint != "" {
cfgOptions = append(cfgOptions, sdkConfig.WithEndpoint(customEndpoint))
}

apiClient, err = foo.NewAPIClient(cfgOptions...)
if p.IsVerbosityDebug() {
cfgOptions = append(cfgOptions,
sdkConfig.WithMiddleware(print.RequestResponseCapturer(p, nil)),
)
}

apiClient, err := foo.NewAPIClient(cfgOptions...)
if err != nil {
p.Debug(print.ErrorLevel, "create new API client: %v", err)
return nil, &errors.AuthError{}
}

Expand Down
15 changes: 8 additions & 7 deletions .github/docs/contribution-guide/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/alb/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
Expand Down Expand Up @@ -54,7 +55,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
}

// Configure API client
apiClient, err := client.ConfigureClient(params.Printer, cmd)
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}
Expand All @@ -66,7 +67,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
return fmt.Errorf("(...): %w", err)
}

projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd)
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
if err != nil {
projectLabel = model.ProjectId
}
Expand All @@ -86,22 +87,22 @@ func NewCmd(params *params.CmdParams) *cobra.Command {

// Configure command flags (type, default value, and description)
func configureFlags(cmd *cobra.Command) {
cmd.Flags().StringP(myFlag, "defaultValue", "My flag description")
cmd.Flags().StringP(someFlag, "shorthand", "defaultValue", "My flag description")
}

// Parse user input (arguments and/or flags)
func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
myArg := inputArgs[0]

globalFlags := globalflags.Parse(cmd)
globalFlags := globalflags.Parse(p, cmd)
if globalFlags.ProjectId == "" {
return nil, &errors.ProjectIdError{}
}

model := inputModel{
GlobalFlagModel: globalFlags,
MyArg: myArg,
MyFlag: flags.FlagToStringPointer(cmd, myFlag),
MyFlag: flags.FlagToStringPointer(p, cmd, someFlag),
}

// Write the input model to the debug logs
Expand All @@ -119,7 +120,7 @@ func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inpu

// Build request to the API
func buildRequest(ctx context.Context, model *inputModel, apiClient *foo.APIClient) foo.ApiListInstancesRequest {
req := apiClient.GetBar(ctx, model.ProjectId, model.MyArg, someParam)
req := apiClient.GetBar(ctx, model.ProjectId, model.MyArg, someArg)
return req
}

Expand Down Expand Up @@ -147,7 +148,7 @@ func outputResult(p *print.Printer, cmd *cobra.Command, outputFormat string, res
resource := resources[i]
table.AddRow(*resource.ResourceId, *resource.Name, *resource.State)
}
err := table.Display(cmd)
err := table.Display(p)
if err != nil {
return fmt.Errorf("render table: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions CONTRIBUTION.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Please remember to run `make generate-docs` after your changes to keep the comma

Below is a typical structure of a CLI command:

https://github.com/stackitcloud/stackit-cli/blob/6f762bd56407ed232080efabc4d2bf87f260b71d/.github/docs/contribution-guide/cmd.go#L23-L156
https://github.com/stackitcloud/stackit-cli/blob/85ce44cd18d11169f2548d8657031b5fc6f94740/.github/docs/contribution-guide/cmd.go#L23-L156

Please remember to always add unit tests for `parseInput`, `buildRequest` (in `bar_test.go`), and any other util functions used.

Expand Down Expand Up @@ -83,7 +83,7 @@ If you want to add a command that uses a STACKIT service `foo` that was not yet
1. This is done in `internal/pkg/services/foo/client/client.go`
2. Below is an example of a typical `client.go` file structure:

https://github.com/stackitcloud/stackit-cli/blob/6f762bd56407ed232080efabc4d2bf87f260b71d/.github/docs/contribution-guide/client.go#L12-L35
https://github.com/stackitcloud/stackit-cli/blob/85ce44cd18d11169f2548d8657031b5fc6f94740/.github/docs/contribution-guide/client.go#L12-L35

### Local development

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/affinity-groups/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
}

// Configure API client
apiClient, err := client.ConfigureClient(params.Printer)
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/affinity-groups/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
}

// Configure API client
apiClient, err := client.ConfigureClient(params.Printer)
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}

projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd)
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
projectLabel = model.ProjectId
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/affinity-groups/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
}

// Configure API client
apiClient, err := client.ConfigureClient(params.Printer)
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/affinity-groups/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
}

// Configure API client
apiClient, err := client.ConfigureClient(params.Printer)
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/beta/alb/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
}

// Configure API client
apiClient, err := client.ConfigureClient(params.Printer)
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}

projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd)
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
projectLabel = model.ProjectId
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/beta/alb/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
}

// Configure API client
apiClient, err := client.ConfigureClient(params.Printer)
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}

projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd)
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
projectLabel = model.ProjectId
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/beta/alb/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
}

// Configure API client
apiClient, err := client.ConfigureClient(params.Printer)
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/beta/alb/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
}

// Configure API client
apiClient, err := client.ConfigureClient(params.Printer)
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}

projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd)
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
projectLabel = model.ProjectId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
}

// Configure client
apiClient, err := client.ConfigureClient(params.Printer)
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
}

// Configure API client
apiClient, err := client.ConfigureClient(params.Printer)
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
}

// Configure API client
apiClient, err := client.ConfigureClient(params.Printer)
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
}

// Configure API client
apiClient, err := client.ConfigureClient(params.Printer)
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
model := parseInput(params.Printer, cmd, args)

// Configure API client
apiClient, err := client.ConfigureClient(params.Printer)
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}
Expand All @@ -60,7 +60,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
if err != nil {
return err
}
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd)
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
projectLabel = model.ProjectId
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/beta/alb/plans/plans.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
}

// Configure API client
apiClient, err := client.ConfigureClient(params.Printer)
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}

projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd)
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
projectLabel = model.ProjectId
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/beta/alb/pool/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
}

// Configure API client
apiClient, err := client.ConfigureClient(params.Printer)
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}

projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd)
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
projectLabel = model.ProjectId
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/beta/alb/quotas/quotas.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
}

// Configure API client
apiClient, err := client.ConfigureClient(params.Printer)
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/beta/alb/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
}

// Configure API client
apiClient, err := client.ConfigureClient(params.Printer)
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}

projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd)
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
projectLabel = model.ProjectId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
}

// Configure API client
apiClient, err := client.ConfigureClient(params.Printer)
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
}

// Configure API client
apiClient, err := client.ConfigureClient(params.Printer)
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
return err
}
// Configure API client
apiClient, err := client.ConfigureClient(params.Printer)
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/beta/sqlserverflex/database/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
}

// Configure API client
apiClient, err := client.ConfigureClient(params.Printer)
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}
Expand All @@ -69,7 +69,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
return fmt.Errorf("get SQLServer Flex databases: %w", err)
}
if resp.Databases == nil || len(*resp.Databases) == 0 {
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, cmd)
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
projectLabel = model.ProjectId
Expand Down
Loading