From b94bc7b4cb7eec8633c8cf95b7e067132550882f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ruben=20H=C3=B6nle?= Date: Fri, 16 May 2025 15:33:57 +0200 Subject: [PATCH] feat(serverupdate): set custom user-agent header for STACKIT API calls (#823) relates to STACKITTPR-184 --- .../serverupdate/schedule/resource.go | 30 +----- .../schedule/schedule_datasource.go | 31 ++----- .../schedule/schedules_datasource.go | 31 ++----- .../services/serverupdate/utils/util.go | 30 ++++++ .../services/serverupdate/utils/util_test.go | 93 +++++++++++++++++++ 5 files changed, 140 insertions(+), 75 deletions(-) create mode 100644 stackit/internal/services/serverupdate/utils/util.go create mode 100644 stackit/internal/services/serverupdate/utils/util_test.go diff --git a/stackit/internal/services/serverupdate/schedule/resource.go b/stackit/internal/services/serverupdate/schedule/resource.go index 22ce7284..9e5f6a3a 100644 --- a/stackit/internal/services/serverupdate/schedule/resource.go +++ b/stackit/internal/services/serverupdate/schedule/resource.go @@ -7,6 +7,8 @@ import ( "strconv" "strings" + serverupdateUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/serverupdate/utils" + "github.com/hashicorp/terraform-plugin-framework-validators/int64validator" "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/path" @@ -25,7 +27,6 @@ import ( "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/features" "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" - "github.com/stackitcloud/stackit-sdk-go/core/config" "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/services/serverupdate" ) @@ -98,15 +99,9 @@ func (r *scheduleResource) Metadata(_ context.Context, req resource.MetadataRequ // Configure adds the provider configured client to the resource. func (r *scheduleResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { - // Prevent panic if the provider has not been configured. - if req.ProviderData == nil { - return - } - var ok bool - r.providerData, ok = req.ProviderData.(core.ProviderData) + r.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 } @@ -115,25 +110,10 @@ func (r *scheduleResource) Configure(ctx context.Context, req resource.Configure return } - var apiClient *serverupdate.APIClient - var err error - if r.providerData.ServerUpdateCustomEndpoint != "" { - ctx = tflog.SetField(ctx, "server_update_custom_endpoint", r.providerData.ServerUpdateCustomEndpoint) - apiClient, err = serverupdate.NewAPIClient( - config.WithCustomAuth(r.providerData.RoundTripper), - config.WithEndpoint(r.providerData.ServerUpdateCustomEndpoint), - ) - } else { - apiClient, err = serverupdate.NewAPIClient( - config.WithCustomAuth(r.providerData.RoundTripper), - ) - } - - 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 := serverupdateUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics) + if resp.Diagnostics.HasError() { return } - r.client = apiClient tflog.Info(ctx, "Server update client configured.") } diff --git a/stackit/internal/services/serverupdate/schedule/schedule_datasource.go b/stackit/internal/services/serverupdate/schedule/schedule_datasource.go index 18d54b90..561bf914 100644 --- a/stackit/internal/services/serverupdate/schedule/schedule_datasource.go +++ b/stackit/internal/services/serverupdate/schedule/schedule_datasource.go @@ -6,6 +6,9 @@ import ( "net/http" "strconv" + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" + serverupdateUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/serverupdate/utils" + "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/schema/validator" @@ -16,7 +19,6 @@ import ( "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/features" "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" - "github.com/stackitcloud/stackit-sdk-go/core/config" "github.com/stackitcloud/stackit-sdk-go/services/serverupdate" ) @@ -48,15 +50,9 @@ func (r *scheduleDataSource) Metadata(_ context.Context, req datasource.Metadata // Configure adds the provider configured client to the data source. func (r *scheduleDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { - // Prevent panic if the provider has not been configured. - if req.ProviderData == nil { - return - } - var ok bool - r.providerData, ok = req.ProviderData.(core.ProviderData) + r.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 } @@ -68,25 +64,10 @@ func (r *scheduleDataSource) Configure(ctx context.Context, req datasource.Confi scheduleDataSourceBetaCheckDone = true } - var apiClient *serverupdate.APIClient - var err error - if r.providerData.ServerUpdateCustomEndpoint != "" { - ctx = tflog.SetField(ctx, "server_update_custom_endpoint", r.providerData.ServerUpdateCustomEndpoint) - apiClient, err = serverupdate.NewAPIClient( - config.WithCustomAuth(r.providerData.RoundTripper), - config.WithEndpoint(r.providerData.ServerUpdateCustomEndpoint), - ) - } else { - apiClient, err = serverupdate.NewAPIClient( - config.WithCustomAuth(r.providerData.RoundTripper), - ) - } - - 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 data source configuration", err)) + apiClient := serverupdateUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics) + if resp.Diagnostics.HasError() { return } - r.client = apiClient tflog.Info(ctx, "Server update client configured") } diff --git a/stackit/internal/services/serverupdate/schedule/schedules_datasource.go b/stackit/internal/services/serverupdate/schedule/schedules_datasource.go index e9d6b474..1b4cf068 100644 --- a/stackit/internal/services/serverupdate/schedule/schedules_datasource.go +++ b/stackit/internal/services/serverupdate/schedule/schedules_datasource.go @@ -6,6 +6,9 @@ import ( "net/http" "strings" + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" + serverupdateUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/serverupdate/utils" + "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types" @@ -16,7 +19,6 @@ import ( "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" "github.com/hashicorp/terraform-plugin-framework/datasource/schema" - "github.com/stackitcloud/stackit-sdk-go/core/config" "github.com/stackitcloud/stackit-sdk-go/services/serverupdate" ) @@ -48,15 +50,9 @@ func (r *schedulesDataSource) Metadata(_ context.Context, req datasource.Metadat // Configure adds the provider configured client to the data source. func (r *schedulesDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { - // Prevent panic if the provider has not been configured. - if req.ProviderData == nil { - return - } - var ok bool - r.providerData, ok = req.ProviderData.(core.ProviderData) + r.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 } @@ -68,25 +64,10 @@ func (r *schedulesDataSource) Configure(ctx context.Context, req datasource.Conf schedulesDataSourceBetaCheckDone = true } - var apiClient *serverupdate.APIClient - var err error - if r.providerData.ServerUpdateCustomEndpoint != "" { - ctx = tflog.SetField(ctx, "server_update_custom_endpoint", r.providerData.ServerUpdateCustomEndpoint) - apiClient, err = serverupdate.NewAPIClient( - config.WithCustomAuth(r.providerData.RoundTripper), - config.WithEndpoint(r.providerData.ServerUpdateCustomEndpoint), - ) - } else { - apiClient, err = serverupdate.NewAPIClient( - config.WithCustomAuth(r.providerData.RoundTripper), - ) - } - - 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 data source configuration", err)) + apiClient := serverupdateUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics) + if resp.Diagnostics.HasError() { return } - r.client = apiClient tflog.Info(ctx, "Server update client configured") } diff --git a/stackit/internal/services/serverupdate/utils/util.go b/stackit/internal/services/serverupdate/utils/util.go new file mode 100644 index 00000000..cfd39299 --- /dev/null +++ b/stackit/internal/services/serverupdate/utils/util.go @@ -0,0 +1,30 @@ +package utils + +import ( + "context" + "fmt" + + "github.com/stackitcloud/stackit-sdk-go/services/serverupdate" + + "github.com/hashicorp/terraform-plugin-framework/diag" + "github.com/stackitcloud/stackit-sdk-go/core/config" + "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) *serverupdate.APIClient { + apiClientConfigOptions := []config.ConfigurationOption{ + config.WithCustomAuth(providerData.RoundTripper), + utils.UserAgentConfigOption(providerData.Version), + } + if providerData.ServerUpdateCustomEndpoint != "" { + apiClientConfigOptions = append(apiClientConfigOptions, config.WithEndpoint(providerData.ServerUpdateCustomEndpoint)) + } + apiClient, err := serverupdate.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/serverupdate/utils/util_test.go b/stackit/internal/services/serverupdate/utils/util_test.go new file mode 100644 index 00000000..1b687588 --- /dev/null +++ b/stackit/internal/services/serverupdate/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/serverupdate" + "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://serverupdate-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 *serverupdate.APIClient + }{ + { + name: "default endpoint", + args: args{ + providerData: &core.ProviderData{ + Version: testVersion, + }, + }, + expected: func() *serverupdate.APIClient { + apiClient, err := serverupdate.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, + ServerUpdateCustomEndpoint: testCustomEndpoint, + }, + }, + expected: func() *serverupdate.APIClient { + apiClient, err := serverupdate.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) + } + }) + } +}