-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Rework users datasource (#3030)
User datasource reworked and aligned with our V1 guidelines, additionally: - added a way to reuse show_output and parameters assertions in the datasource tests - added init to add Sensitive to certain fields in generated and not generated output schemas - added describe output to user (manually like for a few other objects) References: #2902
- Loading branch information
1 parent
ad657eb
commit 751239b
Showing
14 changed files
with
1,744 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,73 @@ | ||
data "snowflake_users" "current" { | ||
pattern = "user1" | ||
} | ||
# Simple usage | ||
data "snowflake_users" "simple" { | ||
} | ||
|
||
output "simple_output" { | ||
value = data.snowflake_users.simple.users | ||
} | ||
|
||
# Filtering (like) | ||
data "snowflake_users" "like" { | ||
like = "user-name" | ||
} | ||
|
||
output "like_output" { | ||
value = data.snowflake_users.like.users | ||
} | ||
|
||
# Filtering (starts_with) | ||
data "snowflake_users" "starts_with" { | ||
starts_with = "user-" | ||
} | ||
|
||
output "starts_with_output" { | ||
value = data.snowflake_users.starts_with.users | ||
} | ||
|
||
# Filtering (limit) | ||
data "snowflake_users" "limit" { | ||
limit { | ||
rows = 10 | ||
from = "user-" | ||
} | ||
} | ||
|
||
output "limit_output" { | ||
value = data.snowflake_users.limit.users | ||
} | ||
|
||
# Without additional data (to limit the number of calls make for every found user) | ||
data "snowflake_users" "only_show" { | ||
# with_describe is turned on by default and it calls DESCRIBE USER for every user found and attaches its output to users.*.describe_output field | ||
with_describe = false | ||
|
||
# with_parameters is turned on by default and it calls SHOW PARAMETERS FOR USER for every user found and attaches its output to users.*.parameters field | ||
with_parameters = false | ||
} | ||
|
||
output "only_show_output" { | ||
value = data.snowflake_users.only_show.users | ||
} | ||
|
||
# Ensure the number of users is equal to at least one element (with the use of postcondition) | ||
data "snowflake_users" "assert_with_postcondition" { | ||
starts_with = "user-name" | ||
lifecycle { | ||
postcondition { | ||
condition = length(self.users) > 0 | ||
error_message = "there should be at least one user" | ||
} | ||
} | ||
} | ||
|
||
# Ensure the number of users is equal to at exactly one element (with the use of check block) | ||
check "user_check" { | ||
data "snowflake_users" "assert_with_check_block" { | ||
like = "user-name" | ||
} | ||
|
||
assert { | ||
condition = length(data.snowflake_users.assert_with_check_block.users) == 1 | ||
error_message = "users filtered by '${data.snowflake_users.assert_with_check_block.like}' returned ${length(data.snowflake_users.assert_with_check_block.users)} users where one was expected" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...eptance/bettertestspoc/assert/resourceparametersassert/users_datasource_parameters_ext.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package resourceparametersassert | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert" | ||
) | ||
|
||
// UsersDatasourceParameters is a temporary workaround to have better parameter assertions in data source acceptance tests. | ||
func UsersDatasourceParameters(t *testing.T, name string) *UserResourceParametersAssert { | ||
t.Helper() | ||
|
||
u := UserResourceParametersAssert{ | ||
ResourceAssert: assert.NewDatasourceAssert("data."+name, "parameters", "users.0."), | ||
} | ||
u.AddAssertion(assert.ValueSet("parameters.#", "1")) | ||
return &u | ||
} |
25 changes: 25 additions & 0 deletions
25
pkg/acceptance/bettertestspoc/assert/resourceshowoutputassert/user_show_output_ext.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package resourceshowoutputassert | ||
|
||
import ( | ||
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert" | ||
) | ||
|
||
func (u *UserShowOutputAssert) HasCreatedOnNotEmpty() *UserShowOutputAssert { | ||
u.AddAssertion(assert.ResourceShowOutputValuePresent("created_on")) | ||
return u | ||
} | ||
|
||
func (u *UserShowOutputAssert) HasDaysToExpiryNotEmpty() *UserShowOutputAssert { | ||
u.AddAssertion(assert.ResourceShowOutputValuePresent("days_to_expiry")) | ||
return u | ||
} | ||
|
||
func (u *UserShowOutputAssert) HasMinsToUnlockNotEmpty() *UserShowOutputAssert { | ||
u.AddAssertion(assert.ResourceShowOutputValuePresent("mins_to_unlock")) | ||
return u | ||
} | ||
|
||
func (u *UserShowOutputAssert) HasMinsToBypassMfaNotEmpty() *UserShowOutputAssert { | ||
u.AddAssertion(assert.ResourceShowOutputValuePresent("mins_to_bypass_mfa")) | ||
return u | ||
} |
18 changes: 18 additions & 0 deletions
18
...ptance/bettertestspoc/assert/resourceshowoutputassert/users_datasource_show_output_ext.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package resourceshowoutputassert | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert" | ||
) | ||
|
||
// UsersDatasourceShowOutput is a temporary workaround to have better show output assertions in data source acceptance tests. | ||
func UsersDatasourceShowOutput(t *testing.T, name string) *UserShowOutputAssert { | ||
t.Helper() | ||
|
||
u := UserShowOutputAssert{ | ||
ResourceAssert: assert.NewDatasourceAssert("data."+name, "show_output", "users.0."), | ||
} | ||
u.AddAssertion(assert.ValueSet("show_output.#", "1")) | ||
return &u | ||
} |
10 changes: 10 additions & 0 deletions
10
pkg/datasources/testdata/TestAcc_Users/without_user/test.tf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
data "snowflake_users" "test" { | ||
like = "non-existing-user" | ||
|
||
lifecycle { | ||
postcondition { | ||
condition = length(self.users) > 0 | ||
error_message = "there should be at least one user" | ||
} | ||
} | ||
} |
Oops, something went wrong.