From 605b1df18b20b072c78e6c8ff9e9d0a2e6c90757 Mon Sep 17 00:00:00 2001 From: Volodymyr Manilo Date: Sat, 23 Dec 2023 06:34:59 +0100 Subject: [PATCH] enable tests --- .github/workflows/ci.yml | 5 +-- .../internal/provider/datasource/resources.go | 33 +++++++++---------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ee1bfee8..30b8daae 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,6 +15,7 @@ on: - 'README.md' branches: - main + - feature/add-filtering-support-for-twingate_resources-datasource # Ensures only 1 action runs per PR and previous is canceled on new trigger concurrency: @@ -118,7 +119,7 @@ jobs: name: Matrix Acceptance Tests needs: build runs-on: ubuntu-latest - if: "!github.event.pull_request.head.repo.fork" +# if: "!github.event.pull_request.head.repo.fork" timeout-minutes: 15 strategy: fail-fast: false @@ -169,7 +170,7 @@ jobs: cleanup: name: Cleanup - if: "!github.event.pull_request.head.repo.fork" +# if: "!github.event.pull_request.head.repo.fork" needs: tests-acceptance runs-on: ubuntu-latest timeout-minutes: 15 diff --git a/twingate/internal/provider/datasource/resources.go b/twingate/internal/provider/datasource/resources.go index 673459c6..23a190f4 100644 --- a/twingate/internal/provider/datasource/resources.go +++ b/twingate/internal/provider/datasource/resources.go @@ -150,7 +150,7 @@ func (d *resources) Schema(ctx context.Context, req datasource.SchemaRequest, re } } -//nolint:funlen,cyclop +//nolint:cyclop func (d *resources) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { var data resourcesModel @@ -161,53 +161,38 @@ func (d *resources) Read(ctx context.Context, req datasource.ReadRequest, resp * return } - var ( - name, filter string - countOptionalAttributes int - ) + var name, filter string if data.Name.ValueString() != "" { - countOptionalAttributes++ - name = data.Name.ValueString() } if data.NameRegexp.ValueString() != "" { - countOptionalAttributes++ - name = data.NameRegexp.ValueString() filter = attr.FilterByRegexp } if data.NameContains.ValueString() != "" { - countOptionalAttributes++ - name = data.NameContains.ValueString() filter = attr.FilterByContains } if data.NameExclude.ValueString() != "" { - countOptionalAttributes++ - name = data.NameExclude.ValueString() filter = attr.FilterByExclude } if data.NamePrefix.ValueString() != "" { - countOptionalAttributes++ - name = data.NamePrefix.ValueString() filter = attr.FilterByPrefix } if data.NameSuffix.ValueString() != "" { - countOptionalAttributes++ - name = data.NameSuffix.ValueString() filter = attr.FilterBySuffix } - if countOptionalAttributes != 1 { + if countOptionalAttributes(data.Name, data.NameRegexp, data.NameContains, data.NameExclude, data.NamePrefix, data.NameSuffix) != 1 { addErr(&resp.Diagnostics, ErrResourcesDatasourceShouldSetOneOptionalNameAttribute, TwingateResources) return @@ -226,3 +211,15 @@ func (d *resources) Read(ctx context.Context, req datasource.ReadRequest, resp * // Save data into Terraform state resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) } + +func countOptionalAttributes(attributes ...types.String) int { + var count int + + for _, attr := range attributes { + if attr.ValueString() != "" { + count++ + } + } + + return count +}