diff --git a/stackit/internal/services/sqlserverflex/instance/datasource.go b/stackit/internal/services/sqlserverflex/instance/datasource.go index ea1570ac..f1af60fc 100644 --- a/stackit/internal/services/sqlserverflex/instance/datasource.go +++ b/stackit/internal/services/sqlserverflex/instance/datasource.go @@ -5,6 +5,9 @@ import ( "fmt" "net/http" + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" + sqlserverflexUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/sqlserverflex/utils" + "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types/basetypes" @@ -15,7 +18,6 @@ import ( "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" - "github.com/stackitcloud/stackit-sdk-go/core/config" "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex" ) @@ -42,37 +44,16 @@ func (r *instanceDataSource) Metadata(_ context.Context, req datasource.Metadata // Configure adds the provider configured client to the data source. func (r *instanceDataSource) 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 } - var apiClient *sqlserverflex.APIClient - var err error - if r.providerData.SQLServerFlexCustomEndpoint != "" { - apiClient, err = sqlserverflex.NewAPIClient( - config.WithCustomAuth(r.providerData.RoundTripper), - config.WithEndpoint(r.providerData.SQLServerFlexCustomEndpoint), - ) - } else { - apiClient, err = sqlserverflex.NewAPIClient( - config.WithCustomAuth(r.providerData.RoundTripper), - config.WithRegion(r.providerData.GetRegion()), - ) - } - - 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 := sqlserverflexUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics) + if resp.Diagnostics.HasError() { return } - r.client = apiClient tflog.Info(ctx, "SQLServer Flex instance client configured") } diff --git a/stackit/internal/services/sqlserverflex/instance/resource.go b/stackit/internal/services/sqlserverflex/instance/resource.go index 0c5bcd6c..08413861 100644 --- a/stackit/internal/services/sqlserverflex/instance/resource.go +++ b/stackit/internal/services/sqlserverflex/instance/resource.go @@ -9,6 +9,8 @@ import ( "strings" "time" + sqlserverflexUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/sqlserverflex/utils" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/diag" @@ -29,7 +31,6 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" - "github.com/stackitcloud/stackit-sdk-go/core/config" "github.com/stackitcloud/stackit-sdk-go/core/oapierror" coreUtils "github.com/stackitcloud/stackit-sdk-go/core/utils" "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex" @@ -117,37 +118,16 @@ func (r *instanceResource) Metadata(_ context.Context, req resource.MetadataRequ // Configure adds the provider configured client to the resource. func (r *instanceResource) 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 } - var apiClient *sqlserverflex.APIClient - var err error - if r.providerData.SQLServerFlexCustomEndpoint != "" { - apiClient, err = sqlserverflex.NewAPIClient( - config.WithCustomAuth(r.providerData.RoundTripper), - config.WithEndpoint(r.providerData.SQLServerFlexCustomEndpoint), - ) - } else { - apiClient, err = sqlserverflex.NewAPIClient( - config.WithCustomAuth(r.providerData.RoundTripper), - config.WithRegion(r.providerData.GetRegion()), - ) - } - - 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 := sqlserverflexUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics) + if resp.Diagnostics.HasError() { return } - r.client = apiClient tflog.Info(ctx, "SQLServer Flex instance client configured") } diff --git a/stackit/internal/services/sqlserverflex/user/datasource.go b/stackit/internal/services/sqlserverflex/user/datasource.go index 8cd066c1..4a9429d3 100644 --- a/stackit/internal/services/sqlserverflex/user/datasource.go +++ b/stackit/internal/services/sqlserverflex/user/datasource.go @@ -6,6 +6,9 @@ import ( "net/http" "strings" + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" + sqlserverflexUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/sqlserverflex/utils" + "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/datasource" "github.com/hashicorp/terraform-plugin-framework/schema/validator" @@ -16,7 +19,6 @@ import ( "github.com/hashicorp/terraform-plugin-framework/datasource/schema" "github.com/hashicorp/terraform-plugin-framework/types" - "github.com/stackitcloud/stackit-sdk-go/core/config" "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex" ) @@ -55,37 +57,16 @@ func (r *userDataSource) Metadata(_ context.Context, req datasource.MetadataRequ // Configure adds the provider configured client to the data source. func (r *userDataSource) 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 } - var apiClient *sqlserverflex.APIClient - var err error - if r.providerData.SQLServerFlexCustomEndpoint != "" { - apiClient, err = sqlserverflex.NewAPIClient( - config.WithCustomAuth(r.providerData.RoundTripper), - config.WithEndpoint(r.providerData.SQLServerFlexCustomEndpoint), - ) - } else { - apiClient, err = sqlserverflex.NewAPIClient( - config.WithCustomAuth(r.providerData.RoundTripper), - config.WithRegion(r.providerData.GetRegion()), - ) - } - - 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 := sqlserverflexUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics) + if resp.Diagnostics.HasError() { return } - r.client = apiClient tflog.Info(ctx, "SQLServer Flex user client configured") } diff --git a/stackit/internal/services/sqlserverflex/user/resource.go b/stackit/internal/services/sqlserverflex/user/resource.go index 1e60ac5e..3dc7c89b 100644 --- a/stackit/internal/services/sqlserverflex/user/resource.go +++ b/stackit/internal/services/sqlserverflex/user/resource.go @@ -6,6 +6,8 @@ import ( "net/http" "strings" + sqlserverflexUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/sqlserverflex/utils" + "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" @@ -21,7 +23,6 @@ import ( "github.com/hashicorp/terraform-plugin-framework/resource/schema/setplanmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" "github.com/hashicorp/terraform-plugin-framework/types" - "github.com/stackitcloud/stackit-sdk-go/core/config" "github.com/stackitcloud/stackit-sdk-go/core/oapierror" "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex" ) @@ -65,37 +66,16 @@ func (r *userResource) Metadata(_ context.Context, req resource.MetadataRequest, // Configure adds the provider configured client to the resource. func (r *userResource) 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 } - var apiClient *sqlserverflex.APIClient - var err error - if r.providerData.SQLServerFlexCustomEndpoint != "" { - apiClient, err = sqlserverflex.NewAPIClient( - config.WithCustomAuth(r.providerData.RoundTripper), - config.WithEndpoint(r.providerData.SQLServerFlexCustomEndpoint), - ) - } else { - apiClient, err = sqlserverflex.NewAPIClient( - config.WithCustomAuth(r.providerData.RoundTripper), - config.WithRegion(r.providerData.GetRegion()), - ) - } - - 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 := sqlserverflexUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics) + if resp.Diagnostics.HasError() { return } - r.client = apiClient tflog.Info(ctx, "SQLServer Flex user client configured") } diff --git a/stackit/internal/services/sqlserverflex/utils/util.go b/stackit/internal/services/sqlserverflex/utils/util.go new file mode 100644 index 00000000..5c14c085 --- /dev/null +++ b/stackit/internal/services/sqlserverflex/utils/util.go @@ -0,0 +1,32 @@ +package utils + +import ( + "context" + "fmt" + + "github.com/stackitcloud/stackit-sdk-go/services/sqlserverflex" + + "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) *sqlserverflex.APIClient { + apiClientConfigOptions := []config.ConfigurationOption{ + config.WithCustomAuth(providerData.RoundTripper), + utils.UserAgentConfigOption(providerData.Version), + } + if providerData.SQLServerFlexCustomEndpoint != "" { + apiClientConfigOptions = append(apiClientConfigOptions, config.WithEndpoint(providerData.SQLServerFlexCustomEndpoint)) + } else { + apiClientConfigOptions = append(apiClientConfigOptions, config.WithRegion(providerData.GetRegion())) + } + apiClient, err := sqlserverflex.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/sqlserverflex/utils/util_test.go b/stackit/internal/services/sqlserverflex/utils/util_test.go new file mode 100644 index 00000000..5ee93949 --- /dev/null +++ b/stackit/internal/services/sqlserverflex/utils/util_test.go @@ -0,0 +1,94 @@ +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/sqlserverflex" + "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://sqlserverflex-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 *sqlserverflex.APIClient + }{ + { + name: "default endpoint", + args: args{ + providerData: &core.ProviderData{ + Version: testVersion, + }, + }, + expected: func() *sqlserverflex.APIClient { + apiClient, err := sqlserverflex.NewAPIClient( + config.WithRegion("eu01"), + 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, + SQLServerFlexCustomEndpoint: testCustomEndpoint, + }, + }, + expected: func() *sqlserverflex.APIClient { + apiClient, err := sqlserverflex.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) + } + }) + } +}