Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions .custom-gcl.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@ plugins:
- module: 'github.com/stackitcloud/terraform-provider-stackit/tools'
import: 'github.com/stackitcloud/terraform-provider-stackit/tools/linters/tfmodifyplan'
path: ./tools
- module: 'github.com/stackitcloud/terraform-provider-stackit/tools'
import: 'github.com/stackitcloud/terraform-provider-stackit/tools/linters/tfclientcollection'
path: ./tools
# WARNING: when working with custom linting rules make sure to clear your cache when linting:
# golangci-lint cache clean && make lint
2 changes: 1 addition & 1 deletion .github/docs/contribution-guide/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (r *barResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanReq

// Configure adds the provider configured client to the resource.
func (r *barResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
providerData, ok := conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
providerData, clients, ok := core.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics)
if !ok {
return
}
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/google/go-cmp v0.7.0
github.com/google/uuid v1.6.0
github.com/gorilla/mux v1.8.1
github.com/hashicorp/go-retryablehttp v0.7.8
github.com/hashicorp/terraform-plugin-framework v1.19.0
github.com/hashicorp/terraform-plugin-framework-timeouts v0.7.0
github.com/hashicorp/terraform-plugin-framework-validators v0.19.0
Expand Down Expand Up @@ -53,10 +54,10 @@ require (
github.com/teambition/rrule-go v1.8.2
go.uber.org/mock v0.6.0
golang.org/x/mod v0.39.0
golang.org/x/time v0.16.0
)

require (
github.com/hashicorp/go-retryablehttp v0.7.8 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/stretchr/testify v1.11.1 // indirect
)
Expand Down Expand Up @@ -102,7 +103,7 @@ require (
github.com/zclconf/go-cty v1.18.1 // indirect
golang.org/x/crypto v0.54.0 // indirect
golang.org/x/net v0.57.0 // indirect
golang.org/x/sync v0.22.0 // indirect
golang.org/x/sync v0.22.0
golang.org/x/sys v0.47.0 // indirect
golang.org/x/text v0.40.0 // indirect
golang.org/x/tools v0.48.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@ golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.40.0 h1:Ub2Z6/xjgF1WrYQz2nuITOEegKFtiIy+rieRJ5lHZKs=
golang.org/x/text v0.40.0/go.mod h1:hpnzDAfGV753zIKo+wk3u1bVKCGPbrnF7+7LBF/UHVY=
golang.org/x/time v0.16.0 h1:vMb6ptszcQMkcwiRTAuNNU50gom6++Q/6gY2hDM6VDE=
golang.org/x/time v0.16.0/go.mod h1:rVKOqvZeKvrDKTQiAHJ7wmwP0RzleSphoEA9RcdLA0s=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
Expand Down
14 changes: 14 additions & 0 deletions golang-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ linters:
- tflogresponse # custom local linter
- tfwriteid # custom local linter
- tfmodifyplan # custom local linter
- tfclientcollection # custom local linter
- bodyclose
- depguard
- errorlint
Expand Down Expand Up @@ -46,6 +47,9 @@ linters:
tfmodifyplan:
type: module
description: "A custom local linter"
tfclientcollection:
type: module
description: "A custom local linter"
depguard:
rules:
main:
Expand Down Expand Up @@ -112,6 +116,16 @@ linters:
- third_party$
- builtin$
- examples$
rules:
- path: stackit/internal/core/
linters:
- tfclientcollection
- path: stackit/internal/services/iam/rolebindings/v1/generic/
linters:
- tfclientcollection
- path: stackit/internal/services/iam/rolebindings/v1/services/
linters:
- tfclientcollection
formatters:
settings:
goimports:
Expand Down
28 changes: 0 additions & 28 deletions stackit/internal/conversion/conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,34 +251,6 @@ 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 core.ProviderData, got %T", providerData))
return core.ProviderData{}, false
}
return stackitProviderData, true
}

func ParseEphemeralProviderData(ctx context.Context, providerData any, diags *diag.Diagnostics) (core.EphemeralProviderData, bool) {
// Prevent panic if the provider has not been configured.
if providerData == nil {
return core.EphemeralProviderData{}, false
}

stackitProviderData, ok := providerData.(core.EphemeralProviderData)
if !ok {
core.LogAndAddError(ctx, diags, "Error configuring API client", "Expected configure type core.EphemeralProviderData")
return core.EphemeralProviderData{}, false
}
return stackitProviderData, true
}

// StringListToSet safely converts a Go slice of strings into a Terraform framework types.Set.
//
// By accepting a pointer to diag.Diagnostics, it enables clean, inline assignments within
Expand Down
175 changes: 0 additions & 175 deletions stackit/internal/conversion/conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@ package conversion

import (
"context"
"crypto/tls"
"net/http"
"reflect"
"testing"

"github.com/hashicorp/terraform-plugin-framework/diag"
opensearch "github.com/stackitcloud/stackit-sdk-go/services/opensearch/v1api"

"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"
Expand Down Expand Up @@ -225,177 +221,6 @@ 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)
}
})
}
}

func TestParseEphemeralProviderData(t *testing.T) {
var randomRoundTripper http.RoundTripper = &http.Transport{
TLSClientConfig: &tls.Config{MinVersion: tls.VersionTLS13},
}
type args struct {
providerData any
}
type want struct {
ok bool
providerData core.EphemeralProviderData
}
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.EphemeralProviderData{},
},
want: want{
ok: true,
providerData: core.EphemeralProviderData{},
},
wantErr: false,
},
{
name: "valid provider data 2",
args: args{
providerData: core.EphemeralProviderData{
ProviderData: core.ProviderData{
RoundTripper: randomRoundTripper,
},
},
},
want: want{
ok: true,
providerData: core.EphemeralProviderData{
ProviderData: core.ProviderData{
RoundTripper: randomRoundTripper,
},
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ctx := context.Background()
diags := diag.Diagnostics{}

actual, ok := ParseEphemeralProviderData(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)
}
})
}
}

func TestStringSetToSlice(t *testing.T) {
t.Parallel()
tests := []struct {
Expand Down
Loading