feat(ske): set custom user-agent header for STACKIT API calls (#829)
relates to STACKITTPR-184
This commit is contained in:
parent
b5c4927cad
commit
6ded78f250
5 changed files with 145 additions and 92 deletions
|
|
@ -5,12 +5,14 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
|
||||||
|
skeUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/ske/utils"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||||
"github.com/stackitcloud/stackit-sdk-go/core/config"
|
|
||||||
"github.com/stackitcloud/stackit-sdk-go/services/ske"
|
"github.com/stackitcloud/stackit-sdk-go/services/ske"
|
||||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
|
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
|
||||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
|
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
|
||||||
|
|
@ -40,37 +42,16 @@ func (r *clusterDataSource) Metadata(_ context.Context, req datasource.MetadataR
|
||||||
|
|
||||||
// Configure adds the provider configured client to the data source.
|
// Configure adds the provider configured client to the data source.
|
||||||
func (r *clusterDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
|
func (r *clusterDataSource) 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
|
var ok bool
|
||||||
r.providerData, ok = req.ProviderData.(core.ProviderData)
|
r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
|
||||||
if !ok {
|
if !ok {
|
||||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var apiClient *ske.APIClient
|
apiClient := skeUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
|
||||||
var err error
|
if resp.Diagnostics.HasError() {
|
||||||
if r.providerData.SKECustomEndpoint != "" {
|
|
||||||
apiClient, err = ske.NewAPIClient(
|
|
||||||
config.WithCustomAuth(r.providerData.RoundTripper),
|
|
||||||
config.WithEndpoint(r.providerData.SKECustomEndpoint),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
apiClient, err = ske.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))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
r.client = apiClient
|
r.client = apiClient
|
||||||
tflog.Info(ctx, "SKE client configured")
|
tflog.Info(ctx, "SKE client configured")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,9 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
serviceenablementUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/serviceenablement/utils"
|
||||||
|
skeUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/ske/utils"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform-plugin-framework-validators/resourcevalidator"
|
"github.com/hashicorp/terraform-plugin-framework-validators/resourcevalidator"
|
||||||
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
|
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/attr"
|
"github.com/hashicorp/terraform-plugin-framework/attr"
|
||||||
|
|
@ -29,7 +32,6 @@ import (
|
||||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
|
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
|
||||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
"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/core/oapierror"
|
||||||
sdkUtils "github.com/stackitcloud/stackit-sdk-go/core/utils"
|
sdkUtils "github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||||
"github.com/stackitcloud/stackit-sdk-go/services/serviceenablement"
|
"github.com/stackitcloud/stackit-sdk-go/services/serviceenablement"
|
||||||
|
|
@ -279,57 +281,22 @@ func (r *clusterResource) Metadata(_ context.Context, req resource.MetadataReque
|
||||||
|
|
||||||
// Configure adds the provider configured client to the resource.
|
// Configure adds the provider configured client to the resource.
|
||||||
func (r *clusterResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
|
func (r *clusterResource) 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
|
var ok bool
|
||||||
r.providerData, ok = req.ProviderData.(core.ProviderData)
|
r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
|
||||||
if !ok {
|
if !ok {
|
||||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var skeClient *ske.APIClient
|
skeClient := skeUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
|
||||||
var enablementClient *serviceenablement.APIClient
|
if resp.Diagnostics.HasError() {
|
||||||
var err error
|
|
||||||
if r.providerData.SKECustomEndpoint != "" {
|
|
||||||
skeClient, err = ske.NewAPIClient(
|
|
||||||
config.WithCustomAuth(r.providerData.RoundTripper),
|
|
||||||
config.WithEndpoint(r.providerData.SKECustomEndpoint),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
skeClient, err = ske.NewAPIClient(
|
|
||||||
config.WithCustomAuth(r.providerData.RoundTripper),
|
|
||||||
config.WithRegion(r.providerData.GetRegion()),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring SKE API client", fmt.Sprintf("Configuring client: %v. This is an error related to the provider configuration, not to the resource configuration", err))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
serviceEnablementClient := serviceenablementUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
|
||||||
if r.providerData.ServiceEnablementCustomEndpoint != "" {
|
if resp.Diagnostics.HasError() {
|
||||||
enablementClient, err = serviceenablement.NewAPIClient(
|
|
||||||
config.WithCustomAuth(r.providerData.RoundTripper),
|
|
||||||
config.WithEndpoint(r.providerData.ServiceEnablementCustomEndpoint),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
enablementClient, err = serviceenablement.NewAPIClient(
|
|
||||||
config.WithCustomAuth(r.providerData.RoundTripper),
|
|
||||||
config.WithRegion(r.providerData.GetRegion()),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring Service Enablement API client", fmt.Sprintf("Configuring client: %v. This is an error related to the provider configuration, not to the resource configuration", err))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
r.skeClient = skeClient
|
r.skeClient = skeClient
|
||||||
r.enablementClient = enablementClient
|
r.enablementClient = serviceEnablementClient
|
||||||
tflog.Info(ctx, "SKE cluster clients configured")
|
tflog.Info(ctx, "SKE cluster clients configured")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,8 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
skeUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/ske/utils"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
||||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||||
|
|
@ -24,7 +26,6 @@ import (
|
||||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
|
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
|
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||||
"github.com/stackitcloud/stackit-sdk-go/core/config"
|
|
||||||
sdkUtils "github.com/stackitcloud/stackit-sdk-go/core/utils"
|
sdkUtils "github.com/stackitcloud/stackit-sdk-go/core/utils"
|
||||||
"github.com/stackitcloud/stackit-sdk-go/services/ske"
|
"github.com/stackitcloud/stackit-sdk-go/services/ske"
|
||||||
)
|
)
|
||||||
|
|
@ -65,36 +66,15 @@ func (r *kubeconfigResource) Metadata(_ context.Context, req resource.MetadataRe
|
||||||
|
|
||||||
// Configure adds the provider configured client to the resource.
|
// Configure adds the provider configured client to the resource.
|
||||||
func (r *kubeconfigResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
|
func (r *kubeconfigResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
|
||||||
// Prevent panic if the provider has not been configured.
|
providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
|
||||||
if req.ProviderData == nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
providerData, ok := req.ProviderData.(core.ProviderData)
|
|
||||||
if !ok {
|
if !ok {
|
||||||
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var apiClient *ske.APIClient
|
apiClient := skeUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics)
|
||||||
var err error
|
if resp.Diagnostics.HasError() {
|
||||||
if providerData.SKECustomEndpoint != "" {
|
|
||||||
apiClient, err = ske.NewAPIClient(
|
|
||||||
config.WithCustomAuth(providerData.RoundTripper),
|
|
||||||
config.WithEndpoint(providerData.SKECustomEndpoint),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
apiClient, err = ske.NewAPIClient(
|
|
||||||
config.WithCustomAuth(providerData.RoundTripper),
|
|
||||||
config.WithRegion(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))
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
r.client = apiClient
|
r.client = apiClient
|
||||||
tflog.Info(ctx, "SKE kubeconfig client configured")
|
tflog.Info(ctx, "SKE kubeconfig client configured")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
31
stackit/internal/services/ske/utils/util.go
Normal file
31
stackit/internal/services/ske/utils/util.go
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
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/ske"
|
||||||
|
"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) *ske.APIClient {
|
||||||
|
apiClientConfigOptions := []config.ConfigurationOption{
|
||||||
|
config.WithCustomAuth(providerData.RoundTripper),
|
||||||
|
utils.UserAgentConfigOption(providerData.Version),
|
||||||
|
}
|
||||||
|
if providerData.SKECustomEndpoint != "" {
|
||||||
|
apiClientConfigOptions = append(apiClientConfigOptions, config.WithEndpoint(providerData.SKECustomEndpoint))
|
||||||
|
} else {
|
||||||
|
apiClientConfigOptions = append(apiClientConfigOptions, config.WithRegion(providerData.GetRegion()))
|
||||||
|
}
|
||||||
|
apiClient, err := ske.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
|
||||||
|
}
|
||||||
94
stackit/internal/services/ske/utils/util_test.go
Normal file
94
stackit/internal/services/ske/utils/util_test.go
Normal file
|
|
@ -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/ske"
|
||||||
|
"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://ske-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 *ske.APIClient
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "default endpoint",
|
||||||
|
args: args{
|
||||||
|
providerData: &core.ProviderData{
|
||||||
|
Version: testVersion,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: func() *ske.APIClient {
|
||||||
|
apiClient, err := ske.NewAPIClient(
|
||||||
|
utils.UserAgentConfigOption(testVersion),
|
||||||
|
config.WithRegion("eu01"),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("error configuring client: %v", err)
|
||||||
|
}
|
||||||
|
return apiClient
|
||||||
|
}(),
|
||||||
|
wantErr: false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "custom endpoint",
|
||||||
|
args: args{
|
||||||
|
providerData: &core.ProviderData{
|
||||||
|
Version: testVersion,
|
||||||
|
SKECustomEndpoint: testCustomEndpoint,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: func() *ske.APIClient {
|
||||||
|
apiClient, err := ske.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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue