Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add filtering support for twingate resources datasource #42

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
b12299a
added new attributes for resources datasource
vmanilo Dec 17, 2023
858c510
added optional name attributes for resources datasource
vmanilo Dec 17, 2023
e203748
added feature branch
vmanilo Dec 17, 2023
ea6a201
fix test
vmanilo Dec 18, 2023
8347c58
remove feature branch
vmanilo Dec 18, 2023
605b1df
enable tests
vmanilo Dec 23, 2023
ac7dbfe
remove feature branch
vmanilo Dec 23, 2023
d0fb2ba
refactore
vmanilo Dec 24, 2023
1361403
updated docs
vmanilo Jan 1, 2024
f1954f2
Merge remote-tracking branch 'upstream/main' into feature/add-filteri…
vmanilo Jan 3, 2024
118a6a7
update resources datasource: allow to list all resources
vmanilo Jan 3, 2024
c1f33ba
Merge branch 'main' into feature/add-filtering-support-for-twingate_r…
vmanilo Jan 7, 2024
5043500
Merge branch 'main' into feature/add-filtering-support-for-twingate_r…
vmanilo Jan 10, 2024
e36c65c
Merge branch 'main' into feature/add-filtering-support-for-twingate_r…
vmanilo Jan 17, 2024
e846b3e
Merge branch 'main' into feature/add-filtering-support-for-twingate_r…
vmanilo Jan 20, 2024
e39fa34
fix docs
vmanilo Jan 20, 2024
5572f68
Merge branch 'main' into feature/add-filtering-support-for-twingate_r…
vmanilo Jan 27, 2024
fa232b6
Merge branch 'main' into feature/add-filtering-support-for-twingate_r…
vmanilo Feb 1, 2024
3dae7f2
Merge branch 'main' into feature/add-filtering-support-for-twingate_r…
vmanilo Feb 16, 2024
5234500
Merge branch 'main' into feature/add-filtering-support-for-twingate_r…
twingate-blee Feb 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update resources datasource: allow to list all resources
  • Loading branch information
vmanilo committed Jan 3, 2024
commit 118a6a715304ec7fd964bf6a446c478519daac02
2 changes: 1 addition & 1 deletion docs/data-sources/resources.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ data "twingate_resources" "foo" {

### Optional

- `name` (String) Returns only resources that exactly match this name.
- `name` (String) Returns only resources that exactly match this name. If no options are passed it will return all service accounts. Only one option can be used at a time.
- `name_contains` (String) Match when the value exist in the name of the resource.
- `name_exclude` (String) Match when the value does not exist in the name of the resource.
- `name_prefix` (String) The name of the resource must start with the value.
Expand Down
4 changes: 4 additions & 0 deletions twingate/internal/client/query/groups-read.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ type StringFilterOperationInput struct {
}

func NewStringFilterOperationInput(name, filter string) *StringFilterOperationInput {
if filter == "" && name == "" {
return nil
}

var stringFilter StringFilterOperationInput

switch filter {
Expand Down
4 changes: 2 additions & 2 deletions twingate/internal/provider/datasource/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func (d *resources) Schema(ctx context.Context, req datasource.SchemaRequest, re
},
attr.Name: schema.StringAttribute{
Optional: true,
Description: "Returns only resources that exactly match this name.",
Description: "Returns only resources that exactly match this name. If no options are passed it will return all service accounts. Only one option can be used at a time.",
},
attr.Name + attr.FilterByRegexp: schema.StringAttribute{
Optional: true,
Expand Down Expand Up @@ -192,7 +192,7 @@ func (d *resources) Read(ctx context.Context, req datasource.ReadRequest, resp *
filter = attr.FilterBySuffix
}

if countOptionalAttributes(data.Name, data.NameRegexp, data.NameContains, data.NameExclude, data.NamePrefix, data.NameSuffix) != 1 {
if countOptionalAttributes(data.Name, data.NameRegexp, data.NameContains, data.NameExclude, data.NamePrefix, data.NameSuffix) > 1 {
addErr(&resp.Diagnostics, ErrResourcesDatasourceShouldSetOneOptionalNameAttribute, TwingateResources)

return
Expand Down
41 changes: 41 additions & 0 deletions twingate/internal/test/acctests/datasource/resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,44 @@ func testDatasourceTwingateResourcesFilter(resourceName, networkName, name1, nam
}
`, resourceName, networkName, name1, name2, name, filter)
}

func TestAccDatasourceTwingateResourcesWithoutFilters(t *testing.T) {
t.Parallel()

prefix := test.Prefix()
resourceName := test.RandomResourceName()
networkName := test.RandomName()
theDatasource := "data.twingate_resources." + resourceName

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acctests.ProviderFactories,
PreCheck: func() { acctests.PreCheck(t) },
CheckDestroy: acctests.CheckTwingateResourceDestroy,
Steps: []resource.TestStep{
{
Config: testDatasourceTwingateResourcesAll(resourceName, networkName, prefix+"_test_app"),
Check: acctests.ComposeTestCheckFunc(
testCheckResourceAttrNotEqual(theDatasource, resourcesLen, "0"),
),
},
},
})
}

func testDatasourceTwingateResourcesAll(resourceName, networkName, name string) string {
return fmt.Sprintf(`
resource "twingate_remote_network" "%[2]s" {
name = "%[2]s"
}

resource "twingate_resource" "%[1]s" {
name = "%[3]s"
address = "acc-test.com"
remote_network_id = twingate_remote_network.%[2]s.id
}

data "twingate_resources" "%[1]s" {
depends_on = [twingate_resource.%[1]s]
}
`, resourceName, networkName, name)
}