Skip to content

Commit b8bd1f8

Browse files
committed
chore(client): Refactor ConfigureClient() function to use a generic function instead
Signed-off-by: Alexander Dahmen <[email protected]>
1 parent af803a3 commit b8bd1f8

File tree

32 files changed

+110
-858
lines changed

32 files changed

+110
-858
lines changed

internal/cmd/git/flavor/list/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
4949
}
5050

5151
// Configure API client
52-
apiClient, err := client.ConfigureClient(params.Printer)
52+
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
5353
if err != nil {
5454
return err
5555
}

internal/cmd/git/instance/create/create.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
6161
}
6262

6363
// Configure API client
64-
apiClient, err := client.ConfigureClient(params.Printer)
64+
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
6565
if err != nil {
6666
return err
6767
}

internal/cmd/git/instance/delete/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
4646
}
4747

4848
// Configure API client
49-
apiClient, err := client.ConfigureClient(params.Printer)
49+
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
5050
if err != nil {
5151
return err
5252
}

internal/cmd/git/instance/describe/describe.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
4242
}
4343

4444
// Configure API client
45-
apiClient, err := client.ConfigureClient(params.Printer)
45+
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
4646
if err != nil {
4747
return err
4848
}

internal/cmd/git/instance/list/list.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
5050
}
5151

5252
// Configure API client
53-
apiClient, err := client.ConfigureClient(params.Printer)
53+
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
5454
if err != nil {
5555
return err
5656
}

internal/pkg/config/config.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const (
2323
AllowedUrlDomainKey = "allowed_url_domain"
2424

2525
AuthorizationCustomEndpointKey = "authorization_custom_endpoint"
26+
AlbCustomEndpoint = "alb_custom _endpoint"
2627
DNSCustomEndpointKey = "dns_custom_endpoint"
2728
LoadBalancerCustomEndpointKey = "load_balancer_custom_endpoint"
2829
LogMeCustomEndpointKey = "logme_custom_endpoint"
@@ -107,6 +108,7 @@ var ConfigKeys = []string{
107108
IaaSCustomEndpointKey,
108109
TokenCustomEndpointKey,
109110
GitCustomEndpointKey,
111+
AlbCustomEndpoint,
110112
}
111113

112114
var defaultConfigFolderPath string
@@ -193,6 +195,7 @@ func setConfigDefaults() {
193195
viper.SetDefault(IaaSCustomEndpointKey, "")
194196
viper.SetDefault(TokenCustomEndpointKey, "")
195197
viper.SetDefault(GitCustomEndpointKey, "")
198+
viper.SetDefault(AlbCustomEndpoint, "")
196199
}
197200

198201
func getConfigFilePath(configFolder string) string {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package genericclient
2+
3+
import (
4+
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
5+
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
6+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
7+
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
8+
sdkConfig "github.com/stackitcloud/stackit-sdk-go/core/config"
9+
)
10+
11+
type CreateApiClient[T any] func(opts ...sdkConfig.ConfigurationOption) (T, error)
12+
13+
// ConfigureClientGeneric contains the generic code which needs to be executed in order to configure the api client.
14+
// TODO: region parameter will be removed when every API implemented the new region
15+
func ConfigureClientGeneric[T any](p *print.Printer, cliVersion, customEndpoint, region string, createApiClient CreateApiClient[T]) (T, error) {
16+
// return value if an error happens
17+
var zero T
18+
authCfgOption, err := auth.AuthenticationConfig(p, auth.AuthorizeUser)
19+
if err != nil {
20+
p.Debug(print.ErrorLevel, "configure authentication: %v", err)
21+
return zero, &errors.AuthError{}
22+
}
23+
cfgOptions := []sdkConfig.ConfigurationOption{
24+
utils.UserAgentConfigOption(cliVersion),
25+
authCfgOption,
26+
}
27+
28+
if customEndpoint != "" {
29+
cfgOptions = append(cfgOptions, sdkConfig.WithEndpoint(customEndpoint))
30+
}
31+
32+
// TODO: this will be removed when every API implemented the new region
33+
if region != "" {
34+
cfgOptions = append(cfgOptions, authCfgOption, sdkConfig.WithRegion(region))
35+
}
36+
37+
if p.IsVerbosityDebug() {
38+
cfgOptions = append(cfgOptions,
39+
sdkConfig.WithMiddleware(print.RequestResponseCapturer(p, nil)),
40+
)
41+
}
42+
43+
apiClient, err := createApiClient(cfgOptions...)
44+
if err != nil {
45+
p.Debug(print.ErrorLevel, "create new API client: %v", err)
46+
return zero, &errors.AuthError{}
47+
}
48+
49+
return apiClient, nil
50+
}
Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,14 @@
11
package client
22

33
import (
4-
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
54
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
6-
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
5+
genericclient "github.com/stackitcloud/stackit-cli/internal/pkg/generic-client"
76
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
8-
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
97

108
"github.com/spf13/viper"
11-
sdkConfig "github.com/stackitcloud/stackit-sdk-go/core/config"
129
"github.com/stackitcloud/stackit-sdk-go/services/alb"
1310
)
1411

1512
func ConfigureClient(p *print.Printer, cliVersion string) (*alb.APIClient, error) {
16-
authCfgOption, err := auth.AuthenticationConfig(p, auth.AuthorizeUser)
17-
if err != nil {
18-
p.Debug(print.ErrorLevel, "configure authentication: %v", err)
19-
return nil, &errors.AuthError{}
20-
}
21-
cfgOptions := []sdkConfig.ConfigurationOption{
22-
utils.UserAgentConfigOption(cliVersion),
23-
authCfgOption,
24-
}
25-
26-
customEndpoint := viper.GetString(config.IaaSCustomEndpointKey)
27-
if customEndpoint != "" {
28-
cfgOptions = append(cfgOptions, sdkConfig.WithEndpoint(customEndpoint))
29-
}
30-
31-
if p.IsVerbosityDebug() {
32-
cfgOptions = append(cfgOptions,
33-
sdkConfig.WithMiddleware(print.RequestResponseCapturer(p, nil)),
34-
)
35-
}
36-
37-
apiClient, err := alb.NewAPIClient(cfgOptions...)
38-
if err != nil {
39-
p.Debug(print.ErrorLevel, "create new API client: %v", err)
40-
return nil, &errors.AuthError{}
41-
}
42-
43-
return apiClient, nil
13+
return genericclient.ConfigureClientGeneric(p, cliVersion, viper.GetString(config.AlbCustomEndpoint), viper.GetString(config.RegionKey), genericclient.CreateApiClient[*alb.APIClient](alb.NewAPIClient))
4414
}
Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,14 @@
11
package client
22

33
import (
4-
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
54
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
6-
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
5+
genericclient "github.com/stackitcloud/stackit-cli/internal/pkg/generic-client"
76
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
8-
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
97

108
"github.com/spf13/viper"
11-
sdkConfig "github.com/stackitcloud/stackit-sdk-go/core/config"
129
"github.com/stackitcloud/stackit-sdk-go/services/authorization"
1310
)
1411

1512
func ConfigureClient(p *print.Printer, cliVersion string) (*authorization.APIClient, error) {
16-
authCfgOption, err := auth.AuthenticationConfig(p, auth.AuthorizeUser)
17-
if err != nil {
18-
p.Debug(print.ErrorLevel, "configure authentication: %v", err)
19-
return nil, &errors.AuthError{}
20-
}
21-
cfgOptions := []sdkConfig.ConfigurationOption{
22-
utils.UserAgentConfigOption(cliVersion),
23-
authCfgOption,
24-
}
25-
26-
customEndpoint := viper.GetString(config.AuthorizationCustomEndpointKey)
27-
28-
if customEndpoint != "" {
29-
cfgOptions = append(cfgOptions, sdkConfig.WithEndpoint(customEndpoint))
30-
}
31-
32-
if p.IsVerbosityDebug() {
33-
cfgOptions = append(cfgOptions,
34-
sdkConfig.WithMiddleware(print.RequestResponseCapturer(p, nil)),
35-
)
36-
}
37-
38-
apiClient, err := authorization.NewAPIClient(cfgOptions...)
39-
if err != nil {
40-
p.Debug(print.ErrorLevel, "create new API client: %v", err)
41-
return nil, &errors.AuthError{}
42-
}
43-
44-
return apiClient, nil
13+
return genericclient.ConfigureClientGeneric(p, cliVersion, viper.GetString(config.AuthorizationCustomEndpointKey), "", genericclient.CreateApiClient[*authorization.APIClient](authorization.NewAPIClient))
4514
}
Lines changed: 2 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,15 @@
11
package client
22

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

6-
"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
76
"github.com/stackitcloud/stackit-cli/internal/pkg/config"
8-
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
97
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
108

119
"github.com/spf13/viper"
12-
sdkConfig "github.com/stackitcloud/stackit-sdk-go/core/config"
1310
"github.com/stackitcloud/stackit-sdk-go/services/dns"
1411
)
1512

1613
func ConfigureClient(p *print.Printer, cliVersion string) (*dns.APIClient, error) {
17-
authCfgOption, err := auth.AuthenticationConfig(p, auth.AuthorizeUser)
18-
if err != nil {
19-
p.Debug(print.ErrorLevel, "configure authentication: %v", err)
20-
return nil, &errors.AuthError{}
21-
}
22-
cfgOptions := []sdkConfig.ConfigurationOption{
23-
utils.UserAgentConfigOption(cliVersion),
24-
authCfgOption,
25-
}
26-
27-
customEndpoint := viper.GetString(config.DNSCustomEndpointKey)
28-
29-
if customEndpoint != "" {
30-
cfgOptions = append(cfgOptions, sdkConfig.WithEndpoint(customEndpoint))
31-
}
32-
33-
if p.IsVerbosityDebug() {
34-
cfgOptions = append(cfgOptions,
35-
sdkConfig.WithMiddleware(print.RequestResponseCapturer(p, nil)),
36-
)
37-
}
38-
39-
apiClient, err := dns.NewAPIClient(cfgOptions...)
40-
if err != nil {
41-
p.Debug(print.ErrorLevel, "create new API client: %v", err)
42-
return nil, &errors.AuthError{}
43-
}
44-
45-
return apiClient, nil
14+
return genericclient.ConfigureClientGeneric(p, cliVersion, viper.GetString(config.DNSCustomEndpointKey), "", genericclient.CreateApiClient[*dns.APIClient](dns.NewAPIClient))
4615
}

0 commit comments

Comments
 (0)