feat(modelserving): set custom user-agent header for STACKIT API calls (#834)
relates to STACKITTPR-184
This commit is contained in:
parent
06af447d61
commit
0ba6e06260
3 changed files with 131 additions and 59 deletions
|
|
@ -9,6 +9,9 @@ import (
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
modelservingUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/modelserving/utils"
|
||||||
|
serviceenablementUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/serviceenablement/utils"
|
||||||
|
|
||||||
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
|
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
|
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
|
||||||
|
|
@ -18,7 +21,6 @@ import (
|
||||||
"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/core/oapierror"
|
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
|
||||||
"github.com/stackitcloud/stackit-sdk-go/services/modelserving"
|
"github.com/stackitcloud/stackit-sdk-go/services/modelserving"
|
||||||
"github.com/stackitcloud/stackit-sdk-go/services/modelserving/wait"
|
"github.com/stackitcloud/stackit-sdk-go/services/modelserving/wait"
|
||||||
|
|
@ -81,73 +83,21 @@ func (r *tokenResource) Metadata(_ context.Context, req resource.MetadataRequest
|
||||||
|
|
||||||
// Configure adds the provider configured client to the resource.
|
// Configure adds the provider configured client to the resource.
|
||||||
func (r *tokenResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
|
func (r *tokenResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
|
||||||
// Prevent panic if the provider has not been configured.
|
var ok bool
|
||||||
if req.ProviderData == nil {
|
r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
|
||||||
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 *modelserving.APIClient
|
apiClient := modelservingUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
|
||||||
var err error
|
if resp.Diagnostics.HasError() {
|
||||||
if providerData.ModelServingCustomEndpoint != "" {
|
|
||||||
ctx = tflog.SetField(
|
|
||||||
ctx,
|
|
||||||
"modelserving_custom_endpoint",
|
|
||||||
providerData.ModelServingCustomEndpoint,
|
|
||||||
)
|
|
||||||
apiClient, err = modelserving.NewAPIClient(
|
|
||||||
config.WithCustomAuth(providerData.RoundTripper),
|
|
||||||
config.WithEndpoint(providerData.ModelServingCustomEndpoint),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
apiClient, err = modelserving.NewAPIClient(
|
|
||||||
config.WithCustomAuth(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,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
serviceEnablementClient := serviceenablementUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
|
||||||
var serviceEnablementClient *serviceenablement.APIClient
|
if resp.Diagnostics.HasError() {
|
||||||
if providerData.ServiceEnablementCustomEndpoint != "" {
|
|
||||||
serviceEnablementClient, err = serviceenablement.NewAPIClient(
|
|
||||||
config.WithCustomAuth(providerData.RoundTripper),
|
|
||||||
config.WithEndpoint(providerData.ServiceEnablementCustomEndpoint),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
serviceEnablementClient, err = serviceenablement.NewAPIClient(
|
|
||||||
config.WithCustomAuth(providerData.RoundTripper),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
core.LogAndAddError(
|
|
||||||
ctx,
|
|
||||||
&resp.Diagnostics,
|
|
||||||
"Error configuring service enablement 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
|
||||||
r.providerData = providerData
|
|
||||||
r.serviceEnablementClient = serviceEnablementClient
|
r.serviceEnablementClient = serviceEnablementClient
|
||||||
tflog.Info(ctx, "Model-Serving auth token client configured")
|
tflog.Info(ctx, "Model-Serving auth token client configured")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
29
stackit/internal/services/modelserving/utils/util.go
Normal file
29
stackit/internal/services/modelserving/utils/util.go
Normal file
|
|
@ -0,0 +1,29 @@
|
||||||
|
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/modelserving"
|
||||||
|
"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) *modelserving.APIClient {
|
||||||
|
apiClientConfigOptions := []config.ConfigurationOption{
|
||||||
|
config.WithCustomAuth(providerData.RoundTripper),
|
||||||
|
utils.UserAgentConfigOption(providerData.Version),
|
||||||
|
}
|
||||||
|
if providerData.ModelServingCustomEndpoint != "" {
|
||||||
|
apiClientConfigOptions = append(apiClientConfigOptions, config.WithEndpoint(providerData.ModelServingCustomEndpoint))
|
||||||
|
}
|
||||||
|
apiClient, err := modelserving.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
|
||||||
|
}
|
||||||
93
stackit/internal/services/modelserving/utils/util_test.go
Normal file
93
stackit/internal/services/modelserving/utils/util_test.go
Normal file
|
|
@ -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/modelserving"
|
||||||
|
"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://modelserving-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 *modelserving.APIClient
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "default endpoint",
|
||||||
|
args: args{
|
||||||
|
providerData: &core.ProviderData{
|
||||||
|
Version: testVersion,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: func() *modelserving.APIClient {
|
||||||
|
apiClient, err := modelserving.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,
|
||||||
|
ModelServingCustomEndpoint: testCustomEndpoint,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: func() *modelserving.APIClient {
|
||||||
|
apiClient, err := modelserving.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