Skip to content

Commit 5d844a8

Browse files
authored
feat(cdn): set custom user-agent header for STACKIT API calls (#830)
relates to STACKITTPR-184
1 parent 1e87271 commit 5d844a8

6 files changed

Lines changed: 158 additions & 89 deletions

File tree

stackit/internal/services/cdn/customdomain/datasource.go

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ import (
66
"fmt"
77
"net/http"
88

9+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
10+
cdnUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/cdn/utils"
11+
912
"github.com/hashicorp/terraform-plugin-framework/datasource"
1013
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
1114
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1215
"github.com/hashicorp/terraform-plugin-framework/types"
1316
"github.com/hashicorp/terraform-plugin-log/tflog"
14-
"github.com/stackitcloud/stackit-sdk-go/core/config"
1517
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
1618
"github.com/stackitcloud/stackit-sdk-go/services/cdn"
1719
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
@@ -34,34 +36,18 @@ func NewCustomDomainDataSource() datasource.DataSource {
3436
}
3537

3638
func (d *customDomainDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
37-
if req.ProviderData == nil {
38-
return
39-
}
40-
providerData, ok := req.ProviderData.(core.ProviderData)
39+
providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
4140
if !ok {
42-
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData))
4341
return
4442
}
43+
4544
features.CheckBetaResourcesEnabled(ctx, &providerData, &resp.Diagnostics, "stackit_cdn_custom_domain", "datasource")
4645
if resp.Diagnostics.HasError() {
4746
return
4847
}
4948

50-
var apiClient *cdn.APIClient
51-
var err error
52-
if providerData.CdnCustomEndpoint != "" {
53-
ctx = tflog.SetField(ctx, "cdn_custom_endpoint", providerData.CdnCustomEndpoint)
54-
apiClient, err = cdn.NewAPIClient(
55-
config.WithCustomAuth(providerData.RoundTripper),
56-
config.WithEndpoint(providerData.CdnCustomEndpoint),
57-
)
58-
} else {
59-
apiClient, err = cdn.NewAPIClient(
60-
config.WithCustomAuth(providerData.RoundTripper),
61-
)
62-
}
63-
if err != nil {
64-
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))
49+
apiClient := cdnUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics)
50+
if resp.Diagnostics.HasError() {
6551
return
6652
}
6753
d.client = apiClient

stackit/internal/services/cdn/customdomain/resource.go

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
"strings"
99
"time"
1010

11+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
12+
cdnUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/cdn/utils"
13+
1114
"github.com/google/uuid"
1215
"github.com/hashicorp/terraform-plugin-framework/attr"
1316
"github.com/hashicorp/terraform-plugin-framework/path"
@@ -18,7 +21,6 @@ import (
1821
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1922
"github.com/hashicorp/terraform-plugin-framework/types"
2023
"github.com/hashicorp/terraform-plugin-log/tflog"
21-
"github.com/stackitcloud/stackit-sdk-go/core/config"
2224
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
2325
"github.com/stackitcloud/stackit-sdk-go/services/cdn"
2426
"github.com/stackitcloud/stackit-sdk-go/services/cdn/wait"
@@ -52,40 +54,26 @@ type CustomDomainModel struct {
5254
}
5355

5456
type customDomainResource struct {
55-
client *cdn.APIClient
56-
providerData core.ProviderData
57+
client *cdn.APIClient
5758
}
5859

5960
func NewCustomDomainResource() resource.Resource {
6061
return &customDomainResource{}
6162
}
6263

6364
func (r *customDomainResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
64-
if req.ProviderData == nil {
65+
providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
66+
if !ok {
6567
return
6668
}
67-
var ok bool
68-
if r.providerData, ok = req.ProviderData.(core.ProviderData); !ok {
69-
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData))
69+
70+
features.CheckBetaResourcesEnabled(ctx, &providerData, &resp.Diagnostics, "stackit_cdn_custom_domain", "resource")
71+
if resp.Diagnostics.HasError() {
7072
return
7173
}
72-
features.CheckBetaResourcesEnabled(ctx, &r.providerData, &resp.Diagnostics, "stackit_cdn_custom_domain", "resource")
73-
74-
var apiClient *cdn.APIClient
75-
var err error
76-
if r.providerData.CdnCustomEndpoint != "" {
77-
ctx = tflog.SetField(ctx, "cdn_custom_endpoint", r.providerData.CdnCustomEndpoint)
78-
apiClient, err = cdn.NewAPIClient(
79-
config.WithCustomAuth(r.providerData.RoundTripper),
80-
config.WithEndpoint(r.providerData.CdnCustomEndpoint),
81-
)
82-
} else {
83-
apiClient, err = cdn.NewAPIClient(
84-
config.WithCustomAuth(r.providerData.RoundTripper),
85-
)
86-
}
87-
if err != nil {
88-
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))
74+
75+
apiClient := cdnUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics)
76+
if resp.Diagnostics.HasError() {
8977
return
9078
}
9179
r.client = apiClient

stackit/internal/services/cdn/distribution/datasource.go

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
8+
cdnUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/cdn/utils"
9+
710
"github.com/hashicorp/terraform-plugin-framework/datasource"
811
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
912
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1013
"github.com/hashicorp/terraform-plugin-framework/types"
1114
"github.com/hashicorp/terraform-plugin-log/tflog"
12-
"github.com/stackitcloud/stackit-sdk-go/core/config"
1315
"github.com/stackitcloud/stackit-sdk-go/services/cdn"
1416
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
1517
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/features"
@@ -31,13 +33,8 @@ func NewDistributionDataSource() datasource.DataSource {
3133
}
3234

3335
func (d *distributionDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
34-
if req.ProviderData == nil {
35-
return
36-
}
37-
38-
providerData, ok := req.ProviderData.(core.ProviderData)
36+
providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
3937
if !ok {
40-
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData))
4138
return
4239
}
4340

@@ -46,24 +43,10 @@ func (d *distributionDataSource) Configure(ctx context.Context, req datasource.C
4643
return
4744
}
4845

49-
var apiClient *cdn.APIClient
50-
var err error
51-
if providerData.CdnCustomEndpoint != "" {
52-
apiClient, err = cdn.NewAPIClient(
53-
config.WithCustomAuth(providerData.RoundTripper),
54-
config.WithEndpoint(providerData.CdnCustomEndpoint),
55-
)
56-
} else {
57-
apiClient, err = cdn.NewAPIClient(
58-
config.WithCustomAuth(providerData.RoundTripper),
59-
)
60-
}
61-
62-
if err != nil {
63-
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))
46+
apiClient := cdnUtils.ConfigureClient(ctx, &providerData, &resp.Diagnostics)
47+
if resp.Diagnostics.HasError() {
6448
return
6549
}
66-
6750
d.client = apiClient
6851
tflog.Info(ctx, "Service Account client configured")
6952
}

stackit/internal/services/cdn/distribution/resource.go

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
"strings"
99
"time"
1010

11+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion"
12+
cdnUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/cdn/utils"
13+
1114
"github.com/google/uuid"
1215
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
1316
"github.com/hashicorp/terraform-plugin-framework/attr"
@@ -20,7 +23,6 @@ import (
2023
"github.com/hashicorp/terraform-plugin-framework/types"
2124
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
2225
"github.com/hashicorp/terraform-plugin-log/tflog"
23-
"github.com/stackitcloud/stackit-sdk-go/core/config"
2426
"github.com/stackitcloud/stackit-sdk-go/core/oapierror"
2527
"github.com/stackitcloud/stackit-sdk-go/services/cdn"
2628
"github.com/stackitcloud/stackit-sdk-go/services/cdn/wait"
@@ -109,31 +111,19 @@ func NewDistributionResource() resource.Resource {
109111
}
110112

111113
func (r *distributionResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
112-
if req.ProviderData == nil {
113-
return
114-
}
115114
var ok bool
116-
if r.providerData, ok = req.ProviderData.(core.ProviderData); !ok {
117-
core.LogAndAddError(ctx, &resp.Diagnostics, "Error configuring API client", fmt.Sprintf("Expected configure type stackit.ProviderData, got %T", req.ProviderData))
115+
r.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
116+
if !ok {
118117
return
119118
}
120-
features.CheckBetaResourcesEnabled(ctx, &r.providerData, &resp.Diagnostics, "stackit_cdn_distribution", "resource")
121119

122-
var apiClient *cdn.APIClient
123-
var err error
124-
if r.providerData.CdnCustomEndpoint != "" {
125-
ctx = tflog.SetField(ctx, "cdn_custom_endpoint", r.providerData.CdnCustomEndpoint)
126-
apiClient, err = cdn.NewAPIClient(
127-
config.WithCustomAuth(r.providerData.RoundTripper),
128-
config.WithEndpoint(r.providerData.CdnCustomEndpoint),
129-
)
130-
} else {
131-
apiClient, err = cdn.NewAPIClient(
132-
config.WithCustomAuth(r.providerData.RoundTripper),
133-
)
120+
features.CheckBetaResourcesEnabled(ctx, &r.providerData, &resp.Diagnostics, "stackit_cdn_distribution", "resource")
121+
if resp.Diagnostics.HasError() {
122+
return
134123
}
135-
if err != nil {
136-
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))
124+
125+
apiClient := cdnUtils.ConfigureClient(ctx, &r.providerData, &resp.Diagnostics)
126+
if resp.Diagnostics.HasError() {
137127
return
138128
}
139129
r.client = apiClient
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package utils
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-framework/diag"
8+
"github.com/stackitcloud/stackit-sdk-go/core/config"
9+
"github.com/stackitcloud/stackit-sdk-go/services/cdn"
10+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
11+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
12+
)
13+
14+
func ConfigureClient(ctx context.Context, providerData *core.ProviderData, diags *diag.Diagnostics) *cdn.APIClient {
15+
apiClientConfigOptions := []config.ConfigurationOption{
16+
config.WithCustomAuth(providerData.RoundTripper),
17+
utils.UserAgentConfigOption(providerData.Version),
18+
}
19+
if providerData.CdnCustomEndpoint != "" {
20+
apiClientConfigOptions = append(apiClientConfigOptions, config.WithEndpoint(providerData.CdnCustomEndpoint))
21+
}
22+
apiClient, err := cdn.NewAPIClient(apiClientConfigOptions...)
23+
if err != nil {
24+
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))
25+
return nil
26+
}
27+
28+
return apiClient
29+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package utils
2+
3+
import (
4+
"context"
5+
"os"
6+
"reflect"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-framework/diag"
10+
sdkClients "github.com/stackitcloud/stackit-sdk-go/core/clients"
11+
"github.com/stackitcloud/stackit-sdk-go/core/config"
12+
"github.com/stackitcloud/stackit-sdk-go/services/cdn"
13+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
14+
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils"
15+
)
16+
17+
const (
18+
testVersion = "1.2.3"
19+
testCustomEndpoint = "https://cdn-custom-endpoint.api.stackit.cloud"
20+
)
21+
22+
func TestConfigureClient(t *testing.T) {
23+
/* mock authentication by setting service account token env variable */
24+
os.Clearenv()
25+
err := os.Setenv(sdkClients.ServiceAccountToken, "mock-val")
26+
if err != nil {
27+
t.Errorf("error setting env variable: %v", err)
28+
}
29+
30+
type args struct {
31+
providerData *core.ProviderData
32+
}
33+
tests := []struct {
34+
name string
35+
args args
36+
wantErr bool
37+
expected *cdn.APIClient
38+
}{
39+
{
40+
name: "default endpoint",
41+
args: args{
42+
providerData: &core.ProviderData{
43+
Version: testVersion,
44+
},
45+
},
46+
expected: func() *cdn.APIClient {
47+
apiClient, err := cdn.NewAPIClient(
48+
utils.UserAgentConfigOption(testVersion),
49+
)
50+
if err != nil {
51+
t.Errorf("error configuring client: %v", err)
52+
}
53+
return apiClient
54+
}(),
55+
wantErr: false,
56+
},
57+
{
58+
name: "custom endpoint",
59+
args: args{
60+
providerData: &core.ProviderData{
61+
Version: testVersion,
62+
CdnCustomEndpoint: testCustomEndpoint,
63+
},
64+
},
65+
expected: func() *cdn.APIClient {
66+
apiClient, err := cdn.NewAPIClient(
67+
utils.UserAgentConfigOption(testVersion),
68+
config.WithEndpoint(testCustomEndpoint),
69+
)
70+
if err != nil {
71+
t.Errorf("error configuring client: %v", err)
72+
}
73+
return apiClient
74+
}(),
75+
wantErr: false,
76+
},
77+
}
78+
for _, tt := range tests {
79+
t.Run(tt.name, func(t *testing.T) {
80+
ctx := context.Background()
81+
diags := diag.Diagnostics{}
82+
83+
actual := ConfigureClient(ctx, tt.args.providerData, &diags)
84+
if diags.HasError() != tt.wantErr {
85+
t.Errorf("ConfigureClient() error = %v, want %v", diags.HasError(), tt.wantErr)
86+
}
87+
88+
if !reflect.DeepEqual(actual, tt.expected) {
89+
t.Errorf("ConfigureClient() = %v, want %v", actual, tt.expected)
90+
}
91+
})
92+
}
93+
}

0 commit comments

Comments
 (0)