feat(serverupdate): set custom user-agent header for STACKIT API calls (#823)

relates to STACKITTPR-184
This commit is contained in:
Ruben Hönle 2025-05-16 15:33:57 +02:00 committed by GitHub
parent cc1f81b39a
commit b94bc7b4cb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 140 additions and 75 deletions

View file

@ -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.")
}

View file

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

View file

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