feat(iaas): set custom user-agent header for STACKIT API calls (#821)

relates to STACKITTPR-184
This commit is contained in:
Ruben Hönle 2025-05-14 16:46:16 +02:00 committed by GitHub
parent 536d824e5d
commit 53ec994a7d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
39 changed files with 575 additions and 740 deletions

View file

@ -4,6 +4,8 @@ import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
@ -167,3 +169,17 @@ func ToJSONMapPartialUpdatePayload(ctx context.Context, current, desired types.M
}
return mapPayload, nil
}
func ParseProviderData(ctx context.Context, providerData any, diags *diag.Diagnostics) (core.ProviderData, bool) {
// Prevent panic if the provider has not been configured.
if providerData == nil {
return core.ProviderData{}, false
}
stackitProviderData, ok := providerData.(core.ProviderData)
if !ok {
core.LogAndAddError(ctx, diags, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", providerData))
return core.ProviderData{}, false
}
return stackitProviderData, true
}

View file

@ -5,6 +5,9 @@ import (
"reflect"
"testing"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/types"
@ -217,3 +220,87 @@ func TestToJSONMapUpdatePayload(t *testing.T) {
})
}
}
func TestParseProviderData(t *testing.T) {
type args struct {
providerData any
}
type want struct {
ok bool
providerData core.ProviderData
}
tests := []struct {
name string
args args
want want
wantErr bool
}{
{
name: "provider has not been configured",
args: args{
providerData: nil,
},
want: want{
ok: false,
},
wantErr: false,
},
{
name: "invalid provider data",
args: args{
providerData: struct{}{},
},
want: want{
ok: false,
},
wantErr: true,
},
{
name: "valid provider data 1",
args: args{
providerData: core.ProviderData{},
},
want: want{
ok: true,
providerData: core.ProviderData{},
},
wantErr: false,
},
{
name: "valid provider data 2",
args: args{
providerData: core.ProviderData{
DefaultRegion: "eu02",
RabbitMQCustomEndpoint: "https://rabbitmq-custom-endpoint.api.stackit.cloud",
Version: "1.2.3",
},
},
want: want{
ok: true,
providerData: core.ProviderData{
DefaultRegion: "eu02",
RabbitMQCustomEndpoint: "https://rabbitmq-custom-endpoint.api.stackit.cloud",
Version: "1.2.3",
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
diags := diag.Diagnostics{}
actual, ok := ParseProviderData(ctx, tt.args.providerData, &diags)
if diags.HasError() != tt.wantErr {
t.Errorf("ConfigureClient() error = %v, want %v", diags.HasError(), tt.wantErr)
}
if ok != tt.want.ok {
t.Errorf("ParseProviderData() got = %v, want %v", ok, tt.want.ok)
}
if !reflect.DeepEqual(actual, tt.want.providerData) {
t.Errorf("ParseProviderData() got = %v, want %v", actual, tt.want)
}
})
}
}