From 11bdcf221684137e6a027c267d5fd01beeb4b6f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ruben=20H=C3=B6nle?= Date: Fri, 16 May 2025 08:23:55 +0200 Subject: [PATCH] feat(git): set custom user-agent header for STACKIT API calls (#835) relates to STACKITTPR-184 --- .../services/git/instance/datasource.go | 34 ++----- .../services/git/instance/resource.go | 34 ++----- stackit/internal/services/git/utils/util.go | 29 ++++++ .../internal/services/git/utils/util_test.go | 93 +++++++++++++++++++ 4 files changed, 134 insertions(+), 56 deletions(-) create mode 100644 stackit/internal/services/git/utils/util.go create mode 100644 stackit/internal/services/git/utils/util_test.go diff --git a/stackit/internal/services/git/instance/datasource.go b/stackit/internal/services/git/instance/datasource.go index b9fb5aed..48931d77 100644 --- a/stackit/internal/services/git/instance/datasource.go +++ b/stackit/internal/services/git/instance/datasource.go @@ -6,11 +6,13 @@ import ( "fmt" "net/http" + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" + gitUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/git/utils" + "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-log/tflog" - "github.com/stackitcloud/stackit-sdk-go/core/config" "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/services/git" "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" @@ -35,15 +37,8 @@ type gitDataSource struct { // Configure sets up the API client for the git instance resource. func (g *gitDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { - // Prevent potential panics if the provider is not properly configured. - if req.ProviderData == nil { - return - } - - // Validate provider data type before proceeding. - providerData, ok := req.ProviderData.(core.ProviderData) + providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) if !ok { - core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData)) return } @@ -52,27 +47,10 @@ func (g *gitDataSource) Configure(ctx context.Context, req datasource.ConfigureR return } - // Initialize the API client with the appropriate authentication and endpoint settings. - var apiClient *git.APIClient - var err error - if providerData.GitCustomEndpoint != "" { - apiClient, err = git.NewAPIClient( - config.WithCustomAuth(providerData.RoundTripper), - config.WithEndpoint(providerData.GitCustomEndpoint), - ) - } else { - apiClient, err = git.NewAPIClient( - config.WithCustomAuth(providerData.RoundTripper), - ) - } - - // Handle API client initialization errors. - if err != nil { - core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Configuring client: %v. This is an error related to the provider configuration, not to the resource configuration", err)) + apiClient := gitUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics) + if resp.Diagnostics.HasError() { return } - - // Store the initialized client. g.client = apiClient tflog.Info(ctx, "git client configured") } diff --git a/stackit/internal/services/git/instance/resource.go b/stackit/internal/services/git/instance/resource.go index 4a48936e..b1e9d32d 100644 --- a/stackit/internal/services/git/instance/resource.go +++ b/stackit/internal/services/git/instance/resource.go @@ -7,6 +7,9 @@ import ( "net/http" "strings" + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" + gitUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/git/utils" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/path" "github.com/hashicorp/terraform-plugin-framework/resource" @@ -16,7 +19,6 @@ import ( "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-log/tflog" - "github.com/stackitcloud/stackit-sdk-go/core/config" "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/services/git" "github.com/stackitcloud/stackit-sdk-go/services/git/wait" @@ -64,15 +66,8 @@ var descriptions = map[string]string{ // Configure sets up the API client for the git instance resource. func (g *gitResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { - // Prevent potential panics if the provider is not properly configured. - if req.ProviderData == nil { - return - } - - // Validate provider data type before proceeding. - providerData, ok := req.ProviderData.(core.ProviderData) + providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) if !ok { - core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData)) return } @@ -81,27 +76,10 @@ func (g *gitResource) Configure(ctx context.Context, req resource.ConfigureReque return } - // Initialize the API client with the appropriate authentication and endpoint settings. - var apiClient *git.APIClient - var err error - if providerData.GitCustomEndpoint != "" { - apiClient, err = git.NewAPIClient( - config.WithCustomAuth(providerData.RoundTripper), - config.WithEndpoint(providerData.GitCustomEndpoint), - ) - } else { - apiClient, err = git.NewAPIClient( - config.WithCustomAuth(providerData.RoundTripper), - ) - } - - // Handle API client initialization errors. - if err != nil { - core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Configuring client: %v. This is an error related to the provider configuration, not to the resource configuration", err)) + apiClient := gitUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics) + if resp.Diagnostics.HasError() { return } - - // Store the initialized client. g.client = apiClient tflog.Info(ctx, "git client configured") } diff --git a/stackit/internal/services/git/utils/util.go b/stackit/internal/services/git/utils/util.go new file mode 100644 index 00000000..e55f2351 --- /dev/null +++ b/stackit/internal/services/git/utils/util.go @@ -0,0 +1,29 @@ +package utils + +import ( + "context" + "fmt" + + "github.com/hashicorp/terraform-plugin-framework/diag" + "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/services/git" + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils" +) + +func ConfigureClient(ctx context.Context, providerData *core.ProviderData, diags *diag.Diagnostics) *git.APIClient { + apiClientConfigOptions := []config.ConfigurationOption{ + config.WithCustomAuth(providerData.RoundTripper), + utils.UserAgentConfigOption(providerData.Version), + } + if providerData.GitCustomEndpoint != "" { + apiClientConfigOptions = append(apiClientConfigOptions, config.WithEndpoint(providerData.GitCustomEndpoint)) + } + apiClient, err := git.NewAPIClient(apiClientConfigOptions...) + if err != nil { + core.LogAndAddError(ctx, diags, "Error configuring API client", fmt.Sprintf("Configuring client: %v. This is an error related to the provider configuration, not to the resource configuration", err)) + return nil + } + + return apiClient +} diff --git a/stackit/internal/services/git/utils/util_test.go b/stackit/internal/services/git/utils/util_test.go new file mode 100644 index 00000000..92c81202 --- /dev/null +++ b/stackit/internal/services/git/utils/util_test.go @@ -0,0 +1,93 @@ +package utils + +import ( + "context" + "os" + "reflect" + "testing" + + "github.com/hashicorp/terraform-plugin-framework/diag" + sdkClients "github.com/stackitcloud/stackit-sdk-go/core/clients" + "github.com/stackitcloud/stackit-sdk-go/core/config" + "github.com/stackitcloud/stackit-sdk-go/services/git" + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils" +) + +const ( + testVersion = "1.2.3" + testCustomEndpoint = "https://git-custom-endpoint.api.stackit.cloud" +) + +func TestConfigureClient(t *testing.T) { + /* mock authentication by setting service account token env variable */ + os.Clearenv() + err := os.Setenv(sdkClients.ServiceAccountToken, "mock-val") + if err != nil { + t.Errorf("error setting env variable: %v", err) + } + + type args struct { + providerData *core.ProviderData + } + tests := []struct { + name string + args args + wantErr bool + expected *git.APIClient + }{ + { + name: "default endpoint", + args: args{ + providerData: &core.ProviderData{ + Version: testVersion, + }, + }, + expected: func() *git.APIClient { + apiClient, err := git.NewAPIClient( + utils.UserAgentConfigOption(testVersion), + ) + if err != nil { + t.Errorf("error configuring client: %v", err) + } + return apiClient + }(), + wantErr: false, + }, + { + name: "custom endpoint", + args: args{ + providerData: &core.ProviderData{ + Version: testVersion, + GitCustomEndpoint: testCustomEndpoint, + }, + }, + expected: func() *git.APIClient { + apiClient, err := git.NewAPIClient( + utils.UserAgentConfigOption(testVersion), + config.WithEndpoint(testCustomEndpoint), + ) + if err != nil { + t.Errorf("error configuring client: %v", err) + } + return apiClient + }(), + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctx := context.Background() + diags := diag.Diagnostics{} + + actual := ConfigureClient(ctx, tt.args.providerData, &diags) + if diags.HasError() != tt.wantErr { + t.Errorf("ConfigureClient() error = %v, want %v", diags.HasError(), tt.wantErr) + } + + if !reflect.DeepEqual(actual, tt.expected) { + t.Errorf("ConfigureClient() = %v, want %v", actual, tt.expected) + } + }) + } +}