diff --git a/.github/actions/build/action.yaml b/.github/actions/build/action.yaml index 1fa83ee1..cf19610a 100644 --- a/.github/actions/build/action.yaml +++ b/.github/actions/build/action.yaml @@ -22,6 +22,8 @@ runs: apt-get -y -qq update apt-get -y -qq install jq python3 python3-pip python-is-python3 s3cmd git make wget + - name: Checkout + uses: actions/checkout@v6 - name: Install Go ${{ inputs.go-version }} uses: actions/setup-go@v6 @@ -35,10 +37,9 @@ runs: run: | set -e go install golang.org/x/tools/cmd/goimports@latest - go install github.com/hashicorp/terraform-plugin-codegen-framework/cmd/tfplugingen-framework@latest - go install github.com/hashicorp/terraform-plugin-codegen-openapi/cmd/tfplugingen-openapi@latest - go install github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs@v0.24.0 - +# go install github.com/hashicorp/terraform-plugin-codegen-framework/cmd/tfplugingen-framework@latest +# go install github.com/hashicorp/terraform-plugin-codegen-openapi/cmd/tfplugingen-openapi@latest +# go install github.com/hashicorp/terraform-plugin-docs/cmd/tfplugindocs@v0.24.0 - name: Setup JAVA ${{ inputs.java-distribution }} ${{ inputs.go-version }} uses: actions/setup-java@v5 @@ -46,16 +47,12 @@ runs: distribution: ${{ inputs.java-distribution }} # See 'Supported distributions' for available options java-version: ${{ inputs.java-version }} - - name: Checkout - uses: actions/checkout@v6 - - name: Run build pkg directory shell: bash run: | set -e go run cmd/main.go build - - name: Run make to build app shell: bash run: | diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index f229dcb4..8897094b 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -83,7 +83,7 @@ jobs: env: GITHUB_TOKEN: ${{ env.FORGEJO_TOKEN }} GPG_FINGERPRINT: ${{ secrets.GPG_FINGERPRINT }} - uses: goreleaser/goreleaser-action@v6 + uses: goreleaser/goreleaser-action@v7 with: args: release --skip publish --clean --snapshot @@ -171,6 +171,16 @@ jobs: - name: Checkout uses: actions/checkout@v6 + - uses: actions/cache@v5 + id: cache + with: + path: path/to/dependencies + key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }} + + - name: Install Dependencies + if: steps.cache.outputs.cache-hit != 'true' + run: /install.sh + - name: Build uses: ./.github/actions/build with: @@ -193,37 +203,13 @@ jobs: - name: golangci-lint uses: golangci/golangci-lint-action@v9 with: - version: v2.9 + version: v2.10 args: --config=golang-ci.yaml --allow-parallel-runners --timeout=5m continue-on-error: true - - name: Linting - run: make lint + - name: Linting terraform files + run: make lint-tf continue-on-error: true - -# - name: Testing -# run: make test -# -# - name: Acceptance Testing -# if: ${{ github.event_name == 'pull_request' }} -# run: make test-acceptance-tf -# -# - name: Check coverage threshold -# shell: bash -# run: | -# make coverage -# COVERAGE=$(go tool cover -func=coverage.out | grep total | awk '{print $3}' | sed 's/%//') -# echo "Coverage: $COVERAGE%" -# if (( $(echo "$COVERAGE < 80" | bc -l) )); then -# echo "Coverage is below 80%" -# # exit 1 -# fi - -# - name: Archive code coverage results -# uses: actions/upload-artifact@v4 -# with: -# name: ${{ env.CODE_COVERAGE_ARTIFACT_NAME }} -# path: "stackit/${{ env.CODE_COVERAGE_FILE_NAME }}" config: if: ${{ github.event_name != 'schedule' }} @@ -234,7 +220,7 @@ jobs: uses: actions/checkout@v6 - name: Check GoReleaser - uses: goreleaser/goreleaser-action@v6 + uses: goreleaser/goreleaser-action@v7 with: args: check diff --git a/.github/workflows/publish.yaml b/.github/workflows/publish.yaml index b86dba35..6d7e89b3 100644 --- a/.github/workflows/publish.yaml +++ b/.github/workflows/publish.yaml @@ -23,7 +23,7 @@ jobs: uses: actions/checkout@v6 - name: Check GoReleaser - uses: goreleaser/goreleaser-action@v6 + uses: goreleaser/goreleaser-action@v7 with: args: check @@ -93,7 +93,7 @@ jobs: env: GITHUB_TOKEN: ${{ env.FORGEJO_TOKEN }} GPG_FINGERPRINT: ${{ secrets.GPG_FINGERPRINT }} - uses: goreleaser/goreleaser-action@v6 + uses: goreleaser/goreleaser-action@v7 with: args: release --skip publish --clean --snapshot @@ -103,7 +103,7 @@ jobs: env: GITHUB_TOKEN: ${{ env.FORGEJO_TOKEN }} GPG_FINGERPRINT: ${{ secrets.GPG_FINGERPRINT }} - uses: goreleaser/goreleaser-action@v6 + uses: goreleaser/goreleaser-action@v7 with: args: release --skip publish --clean diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 7d7106ed..ac77cdaa 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -32,7 +32,7 @@ jobs: with: gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }} - name: Run GoReleaser - uses: goreleaser/goreleaser-action@v6 + uses: goreleaser/goreleaser-action@v7 with: args: release --clean env: diff --git a/Makefile b/Makefile index 680cf020..0c902fea 100644 --- a/Makefile +++ b/Makefile @@ -17,7 +17,8 @@ lint-golangci-lint: lint-tf: @echo "Linting terraform files" - @terraform fmt -check -diff -recursive + @terraform fmt -check -diff -recursive examples/ + @terraform fmt -check -diff -recursive stackit/ lint: lint-golangci-lint lint-tf diff --git a/stackit/internal/services/postgresflexalpha/instance/functions.go b/stackit/internal/services/postgresflexalpha/instance/functions.go index e9c9c9c2..28a567d2 100644 --- a/stackit/internal/services/postgresflexalpha/instance/functions.go +++ b/stackit/internal/services/postgresflexalpha/instance/functions.go @@ -169,7 +169,7 @@ func handleConnectionInfo(ctx context.Context, m *dataSourceModel, resp *postgre m.ConnectionInfo = postgresflexalphadatasource.NewConnectionInfoValueMust( postgresflexalphadatasource.ConnectionInfoValue{}.AttributeTypes(ctx), map[string]attr.Value{ - "write": postgresflexalphadatasource.NewWriteValueMust( + "write": types.ObjectValueMust( postgresflexalphadatasource.WriteValue{}.AttributeTypes(ctx), map[string]attr.Value{ "host": types.StringPointerValue(resp.ConnectionInfo.Write.Host), diff --git a/stackit/internal/services/postgresflexalpha/instance/functions_test.go b/stackit/internal/services/postgresflexalpha/instance/functions_test.go index ae2e55e3..2d22006f 100644 --- a/stackit/internal/services/postgresflexalpha/instance/functions_test.go +++ b/stackit/internal/services/postgresflexalpha/instance/functions_test.go @@ -1,746 +1,186 @@ package postgresflexalpha import ( + "context" + "testing" + + "github.com/hashicorp/terraform-plugin-framework/types" "github.com/stackitcloud/stackit-sdk-go/core/utils" postgresflex "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha" + postgresflexalpharesource "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/instance/resources_gen" + utils2 "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/utils" ) -//nolint:unused // TODO: remove when used -type testFlavor struct { - Cpu int64 - Description string - Id string - MaxGB int64 - Memory int64 - MinGB int64 - NodeType string - StorageClasses []testFlavorStorageClass -} - -//nolint:unused // TODO: remove when used -type testFlavorStorageClass struct { - Class string - MaxIoPerSec int64 - MaxThroughInMb int64 -} - -//nolint:unused // TODO: remove when used -var responseList = []testFlavor{ - { - Cpu: 1, - Description: "flavor 1.1", - Id: "flv1.1", - MaxGB: 500, - Memory: 1, - MinGB: 5, - NodeType: "single", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - { - Cpu: 1, - Description: "flavor 1.2", - Id: "flv1.2", - MaxGB: 500, - Memory: 2, - MinGB: 5, - NodeType: "single", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - { - Cpu: 1, - Description: "flavor 1.3", - Id: "flv1.3", - MaxGB: 500, - Memory: 3, - MinGB: 5, - NodeType: "single", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - { - Cpu: 1, - Description: "flavor 1.4", - Id: "flv1.4", - MaxGB: 500, - Memory: 4, - MinGB: 5, - NodeType: "single", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - { - Cpu: 1, - Description: "flavor 1.5", - Id: "flv1.5", - MaxGB: 500, - Memory: 5, - MinGB: 5, - NodeType: "single", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - { - Cpu: 1, - Description: "flavor 1.6", - Id: "flv1.6", - MaxGB: 500, - Memory: 6, - MinGB: 5, - NodeType: "single", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - { - Cpu: 1, - Description: "flavor 1.7", - Id: "flv1.7", - MaxGB: 500, - Memory: 7, - MinGB: 5, - NodeType: "single", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - { - Cpu: 1, - Description: "flavor 1.8", - Id: "flv1.8", - MaxGB: 500, - Memory: 8, - MinGB: 5, - NodeType: "single", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - { - Cpu: 1, - Description: "flavor 1.9", - Id: "flv1.9", - MaxGB: 500, - Memory: 9, - MinGB: 5, - NodeType: "single", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - /* ......................................................... */ - { - Cpu: 2, - Description: "flavor 2.1", - Id: "flv2.1", - MaxGB: 500, - Memory: 1, - MinGB: 5, - NodeType: "single", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - { - Cpu: 2, - Description: "flavor 2.2", - Id: "flv2.2", - MaxGB: 500, - Memory: 2, - MinGB: 5, - NodeType: "single", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - { - Cpu: 2, - Description: "flavor 2.3", - Id: "flv2.3", - MaxGB: 500, - Memory: 3, - MinGB: 5, - NodeType: "single", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - { - Cpu: 2, - Description: "flavor 2.4", - Id: "flv2.4", - MaxGB: 500, - Memory: 4, - MinGB: 5, - NodeType: "single", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - { - Cpu: 2, - Description: "flavor 2.5", - Id: "flv2.5", - MaxGB: 500, - Memory: 5, - MinGB: 5, - NodeType: "single", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - { - Cpu: 2, - Description: "flavor 2.6", - Id: "flv2.6", - MaxGB: 500, - Memory: 6, - MinGB: 5, - NodeType: "single", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - /* ......................................................... */ - { - Cpu: 1, - Description: "flavor 1.1 replica", - Id: "flv1.1r", - MaxGB: 500, - Memory: 1, - MinGB: 5, - NodeType: "Replica", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - { - Cpu: 1, - Description: "flavor 1.2 replica", - Id: "flv1.2r", - MaxGB: 500, - Memory: 2, - MinGB: 5, - NodeType: "Replica", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - { - Cpu: 1, - Description: "flavor 1.3 replica", - Id: "flv1.3r", - MaxGB: 500, - Memory: 3, - MinGB: 5, - NodeType: "Replica", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - { - Cpu: 1, - Description: "flavor 1.4 replica", - Id: "flv1.4r", - MaxGB: 500, - Memory: 4, - MinGB: 5, - NodeType: "Replica", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - { - Cpu: 1, - Description: "flavor 1.5 replica", - Id: "flv1.5r", - MaxGB: 500, - Memory: 5, - MinGB: 5, - NodeType: "Replica", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - { - Cpu: 1, - Description: "flavor 1.6 replica", - Id: "flv1.6r", - MaxGB: 500, - Memory: 6, - MinGB: 5, - NodeType: "Replica", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - /* ......................................................... */ - { - Cpu: 2, - Description: "flavor 2.1 replica", - Id: "flv2.1r", - MaxGB: 500, - Memory: 1, - MinGB: 5, - NodeType: "Replica", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - { - Cpu: 2, - Description: "flavor 2.2 replica", - Id: "flv2.2r", - MaxGB: 500, - Memory: 2, - MinGB: 5, - NodeType: "Replica", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - { - Cpu: 2, - Description: "flavor 2.3 replica", - Id: "flv2.3r", - MaxGB: 500, - Memory: 3, - MinGB: 5, - NodeType: "Replica", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - { - Cpu: 2, - Description: "flavor 2.4 replica", - Id: "flv2.4r", - MaxGB: 500, - Memory: 4, - MinGB: 5, - NodeType: "Replica", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - { - Cpu: 2, - Description: "flavor 2.5 replica", - Id: "flv2.5r", - MaxGB: 500, - Memory: 5, - MinGB: 5, - NodeType: "Replica", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - { - Cpu: 2, - Description: "flavor 2.6 replica", - Id: "flv2.6r", - MaxGB: 500, - Memory: 6, - MinGB: 5, - NodeType: "Replica", - StorageClasses: []testFlavorStorageClass{ - {Class: "sc1", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc2", MaxIoPerSec: 0, MaxThroughInMb: 0}, - {Class: "sc3", MaxIoPerSec: 0, MaxThroughInMb: 0}, - }, - }, - /* ......................................................... */ -} - -//nolint:unused // TODO: remove when used -func testFlavorListToResponseFlavorList(f []testFlavor) []postgresflex.ListFlavors { - result := make([]postgresflex.ListFlavors, len(f)) - for i, flavor := range f { - result[i] = testFlavorToResponseFlavor(flavor) +func Test_handleConnectionInfo(t *testing.T) { + type args struct { + ctx context.Context + m *dataSourceModel + hostName string + port int64 } - return result -} - -//nolint:unused // TODO: remove when used -func testFlavorToResponseFlavor(f testFlavor) postgresflex.ListFlavors { - var scList []postgresflex.FlavorStorageClassesStorageClass - for _, fl := range f.StorageClasses { - scList = append( - scList, postgresflex.FlavorStorageClassesStorageClass{ - Class: utils.Ptr(fl.Class), - MaxIoPerSec: utils.Ptr(fl.MaxIoPerSec), - MaxThroughInMb: utils.Ptr(fl.MaxThroughInMb), + tests := []struct { + name string + args args + }{ + { + name: "empty connection info", + args: args{ + ctx: context.TODO(), + m: &dataSourceModel{}, + hostName: "", + port: 0, }, - ) + }, + { + name: "empty connection info host", + args: args{ + ctx: context.TODO(), + m: &dataSourceModel{}, + hostName: "", + port: 1234, + }, + }, + { + name: "empty connection info port", + args: args{ + ctx: context.TODO(), + m: &dataSourceModel{}, + hostName: "hostname", + port: 0, + }, + }, + { + name: "valid connection info", + args: args{ + ctx: context.TODO(), + m: &dataSourceModel{}, + hostName: "host", + port: 1000, + }, + }, } - return postgresflex.ListFlavors{ - Cpu: utils.Ptr(f.Cpu), - Description: utils.Ptr(f.Description), - Id: utils.Ptr(f.Id), - MaxGB: utils.Ptr(f.MaxGB), - Memory: utils.Ptr(f.Memory), - MinGB: utils.Ptr(f.MinGB), - NodeType: utils.Ptr(f.NodeType), - StorageClasses: &scList, + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + resp := &postgresflex.GetInstanceResponse{ + ConnectionInfo: &postgresflex.InstanceConnectionInfo{ + Write: &postgresflex.InstanceConnectionInfoWrite{ + Host: utils.Ptr(tt.args.hostName), + Port: utils.Ptr(tt.args.port), + }, + }, + } + + handleConnectionInfo(tt.args.ctx, tt.args.m, resp) + + if tt.args.hostName == "" || tt.args.port == 0 { + if !tt.args.m.ConnectionInfo.IsNull() { + t.Errorf("expected connection info to be null") + } + } + + if tt.args.hostName != "" && tt.args.port != 0 { + res := tt.args.m.ConnectionInfo.Write.Attributes() + gotHost := "" + if r, ok := res["host"]; ok { + gotHost = utils2.RemoveQuotes(r.String()) + } + if gotHost != tt.args.hostName { + t.Errorf("host value incorrect: want: %s - got: %s", tt.args.hostName, gotHost) + } + + gotPort, ok := res["port"] + if !ok { + t.Errorf("could not find a value for port in connection_info.write") + } + if !gotPort.Equal(types.Int64Value(tt.args.port)) { + t.Errorf("port value incorrect: want: %d - got: %s", tt.args.port, gotPort.String()) + } + } + }) } } -// func Test_getAllFlavors(t *testing.T) { -// type args struct { -// projectId string -// region string -// } -// tests := []struct { -// name string -// args args -// firstItem int -// lastItem int -// want []postgresflex.ListFlavors -// wantErr bool -// }{ -// { -// name: "find exactly one flavor", -// args: args{ -// projectId: "project", -// region: "region", -// }, -// firstItem: 0, -// lastItem: 0, -// want: []postgresflex.ListFlavors{ -// testFlavorToResponseFlavor(responseList[0]), -// }, -// wantErr: false, -// }, -// { -// name: "get exactly 1 page flavors", -// args: args{ -// projectId: "project", -// region: "region", -// }, -// firstItem: 0, -// lastItem: 9, -// want: testFlavorListToResponseFlavorList(responseList[0:10]), -// wantErr: false, -// }, -// { -// name: "get exactly 20 flavors", -// args: args{ -// projectId: "project", -// region: "region", -// }, -// firstItem: 0, -// lastItem: 20, -// // 0 indexed therefore we want :21 -// want: testFlavorListToResponseFlavorList(responseList[0:21]), -// wantErr: false, -// }, -// { -// name: "get all flavors", -// args: args{ -// projectId: "project", -// region: "region", -// }, -// firstItem: 0, -// lastItem: len(responseList), -// want: testFlavorListToResponseFlavorList(responseList), -// wantErr: false, -// }, -// } -// for _, tt := range tests { -// t.Run(tt.name, func(t *testing.T) { -// first := tt.firstItem -// if first > len(responseList)-1 { -// first = len(responseList) - 1 -// } -// last := tt.lastItem -// if last > len(responseList)-1 { -// last = len(responseList) - 1 -// } -// mockClient := postgresFlexClientMocked{ -// returnError: tt.wantErr, -// firstItem: first, -// lastItem: last, -// } -// got, err := getAllFlavors(context.TODO(), mockClient, tt.args.projectId, tt.args.region) -// if (err != nil) != tt.wantErr { -// t.Errorf("getAllFlavors() error = %v, wantErr %v", err, tt.wantErr) -// return -// } -// -// if diff := cmp.Diff(tt.want, got); diff != "" { -// t.Errorf("mismatch (-want +got):\n%s", diff) -// } -// -// if !reflect.DeepEqual(got, tt.want) { -// t.Errorf("getAllFlavors() got = %v, want %v", got, tt.want) -// } -// }) -// } -//} +func Test_handleEncryption(t *testing.T) { + type args struct { + m *dataSourceModel + resp *postgresflex.GetInstanceResponse + } + tests := []struct { + name string + args args + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + handleEncryption(tt.args.m, tt.args.resp) + }) + } +} -// func Test_loadFlavorId(t *testing.T) { -// type args struct { -// ctx context.Context -// model *Model -// storage *storageModel -// } -// tests := []struct { -// name string -// args args -// firstItem int -// lastItem int -// want []postgresflex.ListFlavors -// wantErr bool -// }{ -// { -// name: "find a single flavor", -// args: args{ -// ctx: context.Background(), -// model: &Model{ -// ProjectId: basetypes.NewStringValue("project"), -// Region: basetypes.NewStringValue("region"), -// }, -// storage: &storageModel{ -// Class: basetypes.NewStringValue("sc1"), -// Size: basetypes.NewInt64Value(100), -// }, -// }, -// firstItem: 0, -// lastItem: 3, -// want: []postgresflex.ListFlavors{ -// testFlavorToResponseFlavor(responseList[0]), -// }, -// wantErr: false, -// }, -// { -// name: "find a single flavor by replicas option", -// args: args{ -// ctx: context.Background(), -// model: &Model{ -// ProjectId: basetypes.NewStringValue("project"), -// Region: basetypes.NewStringValue("region"), -// Replicas: basetypes.NewInt64Value(1), -// }, -// storage: &storageModel{ -// Class: basetypes.NewStringValue("sc1"), -// Size: basetypes.NewInt64Value(100), -// }, -// }, -// firstItem: 0, -// lastItem: 3, -// want: []postgresflex.ListFlavors{ -// testFlavorToResponseFlavor(responseList[0]), -// }, -// wantErr: false, -// }, -// { -// name: "fail finding find a single flavor by replicas option", -// args: args{ -// ctx: context.Background(), -// model: &Model{ -// ProjectId: basetypes.NewStringValue("project"), -// Region: basetypes.NewStringValue("region"), -// Replicas: basetypes.NewInt64Value(1), -// }, -// storage: &storageModel{ -// Class: basetypes.NewStringValue("sc1"), -// Size: basetypes.NewInt64Value(100), -// }, -// }, -// firstItem: 13, -// lastItem: 23, -// want: []postgresflex.ListFlavors{}, -// wantErr: true, -// }, -// { -// name: "find a replicas flavor lower case", -// args: args{ -// ctx: context.Background(), -// model: &Model{ -// ProjectId: basetypes.NewStringValue("project"), -// Region: basetypes.NewStringValue("region"), -// }, -// storage: &storageModel{ -// Class: basetypes.NewStringValue("sc1"), -// Size: basetypes.NewInt64Value(100), -// }, -// }, -// firstItem: 0, -// lastItem: len(responseList) - 1, -// want: []postgresflex.ListFlavors{ -// testFlavorToResponseFlavor(responseList[16]), -// }, -// wantErr: false, -// }, -// { -// name: "find a replicas flavor CamelCase", -// args: args{ -// ctx: context.Background(), -// model: &Model{ -// ProjectId: basetypes.NewStringValue("project"), -// Region: basetypes.NewStringValue("region"), -// }, -// storage: &storageModel{ -// Class: basetypes.NewStringValue("sc1"), -// Size: basetypes.NewInt64Value(100), -// }, -// }, -// firstItem: 0, -// lastItem: len(responseList) - 1, -// want: []postgresflex.ListFlavors{ -// testFlavorToResponseFlavor(responseList[16]), -// }, -// wantErr: false, -// }, -// { -// name: "find a replicas flavor by replicas option", -// args: args{ -// ctx: context.Background(), -// model: &Model{ -// ProjectId: basetypes.NewStringValue("project"), -// Region: basetypes.NewStringValue("region"), -// Replicas: basetypes.NewInt64Value(3), -// }, -// flavor: &flavorModel{ -// CPU: basetypes.NewInt64Value(1), -// RAM: basetypes.NewInt64Value(1), -// }, -// storage: &storageModel{ -// Class: basetypes.NewStringValue("sc1"), -// Size: basetypes.NewInt64Value(100), -// }, -// }, -// firstItem: 0, -// lastItem: len(responseList) - 1, -// want: []postgresflex.ListFlavors{ -// testFlavorToResponseFlavor(responseList[16]), -// }, -// wantErr: false, -// }, -// { -// name: "fail finding a replica flavor", -// args: args{ -// ctx: context.Background(), -// model: &Model{ -// ProjectId: basetypes.NewStringValue("project"), -// Region: basetypes.NewStringValue("region"), -// Replicas: basetypes.NewInt64Value(3), -// }, -// flavor: &flavorModel{ -// CPU: basetypes.NewInt64Value(1), -// RAM: basetypes.NewInt64Value(1), -// }, -// storage: &storageModel{ -// Class: basetypes.NewStringValue("sc1"), -// Size: basetypes.NewInt64Value(100), -// }, -// }, -// firstItem: 0, -// lastItem: 10, -// want: []postgresflex.ListFlavors{}, -// wantErr: true, -// }, -// { -// name: "no flavor found error", -// args: args{ -// ctx: context.Background(), -// model: &Model{ -// ProjectId: basetypes.NewStringValue("project"), -// Region: basetypes.NewStringValue("region"), -// }, -// flavor: &flavorModel{ -// CPU: basetypes.NewInt64Value(10), -// RAM: basetypes.NewInt64Value(1000), -// NodeType: basetypes.NewStringValue("Single"), -// }, -// storage: &storageModel{ -// Class: basetypes.NewStringValue("sc1"), -// Size: basetypes.NewInt64Value(100), -// }, -// }, -// firstItem: 0, -// lastItem: 3, -// want: []postgresflex.ListFlavors{}, -// wantErr: true, -// }, -// } -// for _, tt := range tests { -// t.Run(tt.name, func(t *testing.T) { -// first := tt.firstItem -// if first > len(responseList)-1 { -// first = len(responseList) - 1 -// } -// last := tt.lastItem -// if last > len(responseList)-1 { -// last = len(responseList) - 1 -// } -// mockClient := postgresFlexClientMocked{ -// returnError: tt.wantErr, -// firstItem: first, -// lastItem: last, -// } -// if err := loadFlavorId(tt.args.ctx, mockClient, tt.args.model, tt.args.flavor, tt.args.storage); (err != nil) != tt.wantErr { -// t.Errorf("loadFlavorId() error = %v, wantErr %v", err, tt.wantErr) -// } -// }) -// } -//} +func Test_handleNetwork(t *testing.T) { + type args struct { + ctx context.Context + m *dataSourceModel + resp *postgresflex.GetInstanceResponse + } + tests := []struct { + name string + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := handleNetwork(tt.args.ctx, tt.args.m, tt.args.resp); (err != nil) != tt.wantErr { + t.Errorf("handleNetwork() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func Test_mapGetDataInstanceResponseToModel(t *testing.T) { + type args struct { + ctx context.Context + m *dataSourceModel + resp *postgresflex.GetInstanceResponse + } + tests := []struct { + name string + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := mapGetDataInstanceResponseToModel(tt.args.ctx, tt.args.m, tt.args.resp); (err != nil) != tt.wantErr { + t.Errorf("mapGetDataInstanceResponseToModel() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} + +func Test_mapGetInstanceResponseToModel(t *testing.T) { + type args struct { + ctx context.Context + m *postgresflexalpharesource.InstanceModel + resp *postgresflex.GetInstanceResponse + } + tests := []struct { + name string + args args + wantErr bool + }{ + // TODO: Add test cases. + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := mapGetInstanceResponseToModel(tt.args.ctx, tt.args.m, tt.args.resp); (err != nil) != tt.wantErr { + t.Errorf("mapGetInstanceResponseToModel() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +} diff --git a/stackit/internal/services/postgresflexalpha/postgresflex_acc_test.go b/stackit/internal/services/postgresflexalpha/postgresflex_acc_test.go index d5ffd00c..7b16f03d 100644 --- a/stackit/internal/services/postgresflexalpha/postgresflex_acc_test.go +++ b/stackit/internal/services/postgresflexalpha/postgresflex_acc_test.go @@ -14,8 +14,8 @@ import ( "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/stackitcloud/stackit-sdk-go/core/config" - postgresflexalpha2 "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha" - postgresflexalpha "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/instance" + postgresflexalphaPkgGen "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/pkg_gen/postgresflexalpha" + postgresflexalphaInstance "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/stackit/internal/services/postgresflexalpha/instance" "tf-provider.git.onstackit.cloud/stackit-dev-tools/terraform-provider-stackitprivatepreview/internal/testutils" // The fwresource import alias is so there is no collision @@ -36,7 +36,7 @@ func init() { F: func(_ string) error { // region is passed by the testing framework ctx := context.Background() apiClientConfigOptions := []config.ConfigurationOption{} - apiClient, err := postgresflexalpha2.NewAPIClient(apiClientConfigOptions...) + apiClient, err := postgresflexalphaPkgGen.NewAPIClient(apiClientConfigOptions...) if err != nil { log.Fatalln(err) } @@ -80,7 +80,7 @@ func TestInstanceResourceSchema(t *testing.T) { schemaResponse := &fwresource.SchemaResponse{} // Instantiate the resource.Resource and call its Schema method - postgresflexalpha.NewInstanceResource().Schema(ctx, schemaRequest, schemaResponse) + postgresflexalphaInstance.NewInstanceResource().Schema(ctx, schemaRequest, schemaResponse) if schemaResponse.Diagnostics.HasError() { t.Fatalf("Schema method diagnostics: %+v", schemaResponse.Diagnostics) @@ -402,19 +402,6 @@ func TestAccInstanceWithDatabases(t *testing.T) { // // Run unit tests against mock //} -// type postgresFlexClientMocked struct { -// returnError bool -// getFlavorsResp *postgresflex.GetFlavorsResponse -// } -// -// func (c *postgresFlexClientMocked) ListFlavorsExecute(_ context.Context, _, _ string) (*postgresflex.GetFlavorsResponse, error) { -// if c.returnError { -// return nil, fmt.Errorf("get flavors failed") -// } -// -// return c.getFlavorsResp, nil -// } - // func TestNewInstanceResource(t *testing.T) { // exData := resData{ // Region: "eu01", diff --git a/stackit/internal/services/postgresflexalpha/testdata/resource-no-enc.tf b/stackit/internal/services/postgresflexalpha/testdata/resource-no-enc.tf index 8e81b998..79ce16e3 100644 --- a/stackit/internal/services/postgresflexalpha/testdata/resource-no-enc.tf +++ b/stackit/internal/services/postgresflexalpha/testdata/resource-no-enc.tf @@ -12,7 +12,7 @@ resource "stackitprivatepreview_postgresflexalpha_instance" "msh-instance-only" size = 10 } network = { - acl = ["0.0.0.0/0"] + acl = ["0.0.0.0/0"] access_scope = "PUBLIC" } version = 17 diff --git a/stackit/internal/utils/strings.go b/stackit/internal/utils/strings.go new file mode 100644 index 00000000..21703484 --- /dev/null +++ b/stackit/internal/utils/strings.go @@ -0,0 +1,12 @@ +package utils + +func RemoveQuotes(src string) string { + var res string + if len(src) > 0 && src[0] == '"' { + res = src[1:] + } + if len(res) > 0 && res[len(res)-1] == '"' { + res = res[:len(res)-1] + } + return res +}