Skip to content
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
38 changes: 2 additions & 36 deletions .github/docs/contribution-guide/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,12 @@ package client

import (
"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"
genericclient "github.com/stackitcloud/stackit-cli/internal/pkg/generic-client"
"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(p *print.Printer, cliVersion string) (*foo.APIClient, error) {
authCfgOption, err := auth.AuthenticationConfig(p, auth.AuthorizeUser)
if err != nil {
return nil, &errors.AuthError{}
}

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))
}

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{}
}

return apiClient, nil
return genericclient.ConfigureClientGeneric(p, cliVersion, viper.GetString(config.fooCustomEndpointKey), false, genericclient.CreateApiClient[*foo.APIClient](foo.NewAPIClient))
}
2 changes: 1 addition & 1 deletion internal/cmd/git/flavor/list/list.go
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
2 changes: 1 addition & 1 deletion internal/cmd/git/instance/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,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/git/instance/delete/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,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/git/instance/describe/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,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/git/instance/list/list.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
3 changes: 3 additions & 0 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
AllowedUrlDomainKey = "allowed_url_domain"

AuthorizationCustomEndpointKey = "authorization_custom_endpoint"
AlbCustomEndpoint = "alb_custom _endpoint"
DNSCustomEndpointKey = "dns_custom_endpoint"
LoadBalancerCustomEndpointKey = "load_balancer_custom_endpoint"
LogMeCustomEndpointKey = "logme_custom_endpoint"
Expand Down Expand Up @@ -107,6 +108,7 @@ var ConfigKeys = []string{
IaaSCustomEndpointKey,
TokenCustomEndpointKey,
GitCustomEndpointKey,
AlbCustomEndpoint,
}

var defaultConfigFolderPath string
Expand Down Expand Up @@ -193,6 +195,7 @@ func setConfigDefaults() {
viper.SetDefault(IaaSCustomEndpointKey, "")
viper.SetDefault(TokenCustomEndpointKey, "")
viper.SetDefault(GitCustomEndpointKey, "")
viper.SetDefault(AlbCustomEndpoint, "")
}

func getConfigFilePath(configFolder string) string {
Expand Down
50 changes: 50 additions & 0 deletions internal/pkg/generic-client/generic_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package genericclient

import (
"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"
)

type CreateApiClient[T any] func(opts ...sdkConfig.ConfigurationOption) (T, error)

// ConfigureClientGeneric contains the generic code which needs to be executed in order to configure the api client.
func ConfigureClientGeneric[T any](p *print.Printer, cliVersion, customEndpoint string, useRegion bool, createApiClient CreateApiClient[T]) (T, error) {
// return value if an error happens
var zero T
authCfgOption, err := auth.AuthenticationConfig(p, auth.AuthorizeUser)
if err != nil {
p.Debug(print.ErrorLevel, "configure authentication: %v", err)
return zero, &errors.AuthError{}
}
cfgOptions := []sdkConfig.ConfigurationOption{
utils.UserAgentConfigOption(cliVersion),
authCfgOption,
}

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

if useRegion {
cfgOptions = append(cfgOptions, authCfgOption, sdkConfig.WithRegion(viper.GetString(config.RegionKey)))
}

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

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

return apiClient, nil
}
34 changes: 2 additions & 32 deletions internal/pkg/services/alb/client/client.go
Original file line number Diff line number Diff line change
@@ -1,44 +1,14 @@
package client

import (
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
genericclient "github.com/stackitcloud/stackit-cli/internal/pkg/generic-client"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/spf13/viper"
sdkConfig "github.com/stackitcloud/stackit-sdk-go/core/config"
"github.com/stackitcloud/stackit-sdk-go/services/alb"
)

func ConfigureClient(p *print.Printer, cliVersion string) (*alb.APIClient, error) {
authCfgOption, err := auth.AuthenticationConfig(p, auth.AuthorizeUser)
if err != nil {
p.Debug(print.ErrorLevel, "configure authentication: %v", err)
return nil, &errors.AuthError{}
}
cfgOptions := []sdkConfig.ConfigurationOption{
utils.UserAgentConfigOption(cliVersion),
authCfgOption,
}

customEndpoint := viper.GetString(config.IaaSCustomEndpointKey)
if customEndpoint != "" {
cfgOptions = append(cfgOptions, sdkConfig.WithEndpoint(customEndpoint))
}

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

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

return apiClient, nil
return genericclient.ConfigureClientGeneric(p, cliVersion, viper.GetString(config.AlbCustomEndpoint), true, genericclient.CreateApiClient[*alb.APIClient](alb.NewAPIClient))
}
35 changes: 2 additions & 33 deletions internal/pkg/services/authorization/client/client.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,14 @@
package client

import (
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
genericclient "github.com/stackitcloud/stackit-cli/internal/pkg/generic-client"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"

"github.com/spf13/viper"
sdkConfig "github.com/stackitcloud/stackit-sdk-go/core/config"
"github.com/stackitcloud/stackit-sdk-go/services/authorization"
)

func ConfigureClient(p *print.Printer, cliVersion string) (*authorization.APIClient, error) {
authCfgOption, err := auth.AuthenticationConfig(p, auth.AuthorizeUser)
if err != nil {
p.Debug(print.ErrorLevel, "configure authentication: %v", err)
return nil, &errors.AuthError{}
}
cfgOptions := []sdkConfig.ConfigurationOption{
utils.UserAgentConfigOption(cliVersion),
authCfgOption,
}

customEndpoint := viper.GetString(config.AuthorizationCustomEndpointKey)

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

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

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

return apiClient, nil
return genericclient.ConfigureClientGeneric(p, cliVersion, viper.GetString(config.AuthorizationCustomEndpointKey), false, genericclient.CreateApiClient[*authorization.APIClient](authorization.NewAPIClient))
}
35 changes: 2 additions & 33 deletions internal/pkg/services/dns/client/client.go
Original file line number Diff line number Diff line change
@@ -1,46 +1,15 @@
package client

import (
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
genericclient "github.com/stackitcloud/stackit-cli/internal/pkg/generic-client"

"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/spf13/viper"
sdkConfig "github.com/stackitcloud/stackit-sdk-go/core/config"
"github.com/stackitcloud/stackit-sdk-go/services/dns"
)

func ConfigureClient(p *print.Printer, cliVersion string) (*dns.APIClient, error) {
authCfgOption, err := auth.AuthenticationConfig(p, auth.AuthorizeUser)
if err != nil {
p.Debug(print.ErrorLevel, "configure authentication: %v", err)
return nil, &errors.AuthError{}
}
cfgOptions := []sdkConfig.ConfigurationOption{
utils.UserAgentConfigOption(cliVersion),
authCfgOption,
}

customEndpoint := viper.GetString(config.DNSCustomEndpointKey)

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

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

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

return apiClient, nil
return genericclient.ConfigureClientGeneric(p, cliVersion, viper.GetString(config.DNSCustomEndpointKey), false, genericclient.CreateApiClient[*dns.APIClient](dns.NewAPIClient))
}
37 changes: 3 additions & 34 deletions internal/pkg/services/git/client/client.go
Original file line number Diff line number Diff line change
@@ -1,45 +1,14 @@
package client

import (
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
genericclient "github.com/stackitcloud/stackit-cli/internal/pkg/generic-client"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"

"github.com/spf13/viper"
sdkConfig "github.com/stackitcloud/stackit-sdk-go/core/config"
"github.com/stackitcloud/stackit-sdk-go/services/git"
)

func ConfigureClient(p *print.Printer) (*git.APIClient, error) {
var err error
var apiClient *git.APIClient
var cfgOptions []sdkConfig.ConfigurationOption

authCfgOption, err := auth.AuthenticationConfig(p, auth.AuthorizeUser)
if err != nil {
p.Debug(print.ErrorLevel, "configure authentication: %v", err)
return nil, &errors.AuthError{}
}
cfgOptions = append(cfgOptions, authCfgOption)

customEndpoint := viper.GetString(config.GitCustomEndpointKey)

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

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

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

return apiClient, nil
func ConfigureClient(p *print.Printer, cliVersion string) (*git.APIClient, error) {
return genericclient.ConfigureClientGeneric(p, cliVersion, viper.GetString(config.GitCustomEndpointKey), false, genericclient.CreateApiClient[*git.APIClient](git.NewAPIClient))
}
Loading
Loading