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

chore: Random ids rework part4 #2837

Merged
merged 13 commits into from
May 29, 2024
Merged
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/acceptance/helpers/table_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (c *TableClient) DropTableFunc(t *testing.T, id sdk.SchemaObjectIdentifier)

return func() {
// to prevent error when schema was removed before the table
_, err := c.context.client.Schemas.ShowByID(ctx, sdk.NewDatabaseObjectIdentifier(id.DatabaseName(), id.SchemaName()))
_, err := c.context.client.Schemas.ShowByID(ctx, id.SchemaId())
if errors.Is(err, sdk.ErrObjectNotExistOrAuthorized) {
return
}
Expand Down
25 changes: 13 additions & 12 deletions pkg/datasources/alerts_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (

acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
)

func TestAcc_Alerts(t *testing.T) {
name := acc.TestClient().Ids.Alpha()
alertId := acc.TestClient().Ids.RandomSchemaObjectIdentifier()

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
Expand All @@ -22,33 +23,33 @@ func TestAcc_Alerts(t *testing.T) {
CheckDestroy: nil,
Steps: []resource.TestStep{
{
Config: alertsResourceConfig(name) + alertsDatasourceConfigNoOptionals(),
Config: alertsResourceConfig(alertId) + alertsDatasourceConfigNoOptionals(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.snowflake_alerts.test_datasource_alert", "alerts.#"),
),
},
{
Config: alertsResourceConfig(name) + alertsDatasourceConfigDbOnly(),
Config: alertsResourceConfig(alertId) + alertsDatasourceConfigDbOnly(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.snowflake_alerts.test_datasource_alert", "alerts.#"),
),
},
{
Config: alertsResourceConfig(name) + alertsDatasourceConfigDbAndSchema(),
Config: alertsResourceConfig(alertId) + alertsDatasourceConfigDbAndSchema(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.snowflake_alerts.test_datasource_alert", "alerts.#"),
resource.TestCheckResourceAttr("data.snowflake_alerts.test_datasource_alert", "alerts.0.name", name),
resource.TestCheckResourceAttr("data.snowflake_alerts.test_datasource_alert", "alerts.0.name", alertId.Name()),
),
},
{
Config: alertsResourceConfig(name) + alertsDatasourceConfigAllOptionals(name),
Config: alertsResourceConfig(alertId) + alertsDatasourceConfigAllOptionals(alertId),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.snowflake_alerts.test_datasource_alert", "alerts.#"),
resource.TestCheckResourceAttr("data.snowflake_alerts.test_datasource_alert", "alerts.0.name", name),
resource.TestCheckResourceAttr("data.snowflake_alerts.test_datasource_alert", "alerts.0.name", alertId.Name()),
),
},
{
Config: alertsResourceConfig(name) + alertsDatasourceConfigSchemaOnly(),
Config: alertsResourceConfig(alertId) + alertsDatasourceConfigSchemaOnly(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrSet("data.snowflake_alerts.test_datasource_alert", "alerts.#"),
),
Expand All @@ -57,7 +58,7 @@ func TestAcc_Alerts(t *testing.T) {
})
}

func alertsResourceConfig(name string) string {
func alertsResourceConfig(alertId sdk.SchemaObjectIdentifier) string {
return fmt.Sprintf(`
resource "snowflake_alert" "test_resource_alert" {
name = "%s"
Expand All @@ -72,7 +73,7 @@ resource "snowflake_alert" "test_resource_alert" {
interval = "60"
}
}
`, name, acc.TestDatabaseName, acc.TestSchemaName, acc.TestWarehouseName)
`, alertId.Name(), alertId.DatabaseName(), alertId.SchemaName(), acc.TestWarehouseName)
}

func alertsDatasourceConfigNoOptionals() string {
Expand All @@ -98,14 +99,14 @@ data "snowflake_alerts" "test_datasource_alert" {
`, acc.TestDatabaseName, acc.TestSchemaName)
}

func alertsDatasourceConfigAllOptionals(name string) string {
func alertsDatasourceConfigAllOptionals(alertId sdk.SchemaObjectIdentifier) string {
return fmt.Sprintf(`
data "snowflake_alerts" "test_datasource_alert" {
database = "%s"
schema = "%s"
pattern = "%s"
}
`, acc.TestDatabaseName, acc.TestSchemaName, name)
`, alertId.DatabaseName(), alertId.SchemaName(), alertId.Name())
}

func alertsDatasourceConfigSchemaOnly() string {
Expand Down
8 changes: 4 additions & 4 deletions pkg/resources/dynamic_table_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ func TestAcc_DynamicTable_issue2173(t *testing.T) {
tableName := dynamicTableName + "_table"
tableId := acc.TestClient().Ids.NewSchemaObjectIdentifier(tableName)
query := fmt.Sprintf(`select "ID" from %s`, tableId.FullyQualifiedName())
otherSchema := acc.TestClient().Ids.Alpha()
otherSchemaId := sdk.NewDatabaseObjectIdentifier(acc.TestDatabaseName, otherSchema)
otherSchemaId := acc.TestClient().Ids.RandomDatabaseObjectIdentifier()
otherSchemaName := otherSchemaId.Name()
newDynamicTableId := acc.TestClient().Ids.NewSchemaObjectIdentifierInSchema(dynamicTableName, otherSchemaId)
m := func() map[string]config.Variable {
return map[string]config.Variable{
Expand All @@ -173,7 +173,7 @@ func TestAcc_DynamicTable_issue2173(t *testing.T) {
"query": config.StringVariable(query),
"comment": config.StringVariable("Terraform acceptance test for GH issue 2173"),
"table_name": config.StringVariable(tableName),
"other_schema": config.StringVariable(otherSchema),
"other_schema": config.StringVariable(otherSchemaName),
}
}

Expand All @@ -192,7 +192,7 @@ func TestAcc_DynamicTable_issue2173(t *testing.T) {
PreApply: []plancheck.PlanCheck{plancheck.ExpectNonEmptyPlan()},
},
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("snowflake_schema.other_schema", "name", otherSchema),
resource.TestCheckResourceAttr("snowflake_schema.other_schema", "name", otherSchemaName),
resource.TestCheckResourceAttr("snowflake_table.t", "name", tableName),
),
},
Expand Down
2 changes: 1 addition & 1 deletion pkg/resources/dynamic_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func init() {
}
ctx := context.Background()
dynamicTables, err := client.DynamicTables.Show(ctx, sdk.NewShowDynamicTableRequest().WithIn(&sdk.In{
Schema: sdk.NewDatabaseObjectIdentifier(acc.TestDatabaseName, acc.TestSchemaName),
Schema: acc.TestClient().Ids.SchemaId(),
}))
if err != nil {
return fmt.Errorf("error getting dynamic tables during sweep: %w", err)
Expand Down
5 changes: 3 additions & 2 deletions pkg/resources/external_function_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,8 @@ func TestAcc_ExternalFunction_complete(t *testing.T) {
}

func TestAcc_ExternalFunction_migrateFromVersion085(t *testing.T) {
name := acc.TestClient().Ids.Alpha()
id := acc.TestClient().Ids.RandomSchemaObjectIdentifierWithArguments([]sdk.DataType{sdk.DataTypeVARCHAR, sdk.DataTypeVARCHAR})
name := id.Name()
resourceName := "snowflake_external_function.f"

resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -261,7 +262,7 @@ func TestAcc_ExternalFunction_migrateFromVersion085(t *testing.T) {
PreApply: []plancheck.PlanCheck{plancheck.ExpectEmptyPlan()},
},
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "id", sdk.NewSchemaObjectIdentifierWithArguments(acc.TestDatabaseName, acc.TestSchemaName, name, []sdk.DataType{sdk.DataTypeVARCHAR, sdk.DataTypeVARCHAR}).FullyQualifiedName()),
resource.TestCheckResourceAttr(resourceName, "id", id.FullyQualifiedName()),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "database", acc.TestDatabaseName),
resource.TestCheckResourceAttr(resourceName, "schema", acc.TestSchemaName),
Expand Down
5 changes: 3 additions & 2 deletions pkg/resources/function_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,8 @@ func TestAcc_Function_complex(t *testing.T) {

// proves issue https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2490
func TestAcc_Function_migrateFromVersion085(t *testing.T) {
name := acc.TestClient().Ids.Alpha()
id := acc.TestClient().Ids.RandomSchemaObjectIdentifierWithArguments([]sdk.DataType{sdk.DataTypeVARCHAR})
name := id.Name()
comment := random.Comment()
resourceName := "snowflake_function.f"

Expand Down Expand Up @@ -220,7 +221,7 @@ func TestAcc_Function_migrateFromVersion085(t *testing.T) {
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
Config: functionConfig(acc.TestDatabaseName, acc.TestSchemaName, name, comment),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "id", sdk.NewSchemaObjectIdentifierWithArguments(acc.TestDatabaseName, acc.TestSchemaName, name, []sdk.DataType{sdk.DataTypeVARCHAR}).FullyQualifiedName()),
resource.TestCheckResourceAttr(resourceName, "id", id.FullyQualifiedName()),
resource.TestCheckResourceAttr(resourceName, "name", name),
resource.TestCheckResourceAttr(resourceName, "database", acc.TestDatabaseName),
resource.TestCheckResourceAttr(resourceName, "schema", acc.TestSchemaName),
Expand Down
22 changes: 10 additions & 12 deletions pkg/resources/grant_database_role_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (

acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/hashicorp/terraform-plugin-testing/config"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/tfversion"
Expand Down Expand Up @@ -128,19 +127,18 @@ func TestAcc_GrantDatabaseRole_accountRole(t *testing.T) {

// proves https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2410 is fixed
func TestAcc_GrantDatabaseRole_share(t *testing.T) {
databaseName := acc.TestClient().Ids.Alpha()
databaseRoleName := acc.TestClient().Ids.Alpha()
databaseRoleId := sdk.NewDatabaseObjectIdentifier(databaseName, databaseRoleName).FullyQualifiedName()
databaseId := acc.TestClient().Ids.RandomAccountObjectIdentifier()
databaseRoleId := acc.TestClient().Ids.RandomDatabaseObjectIdentifierInDatabase(databaseId)
shareId := acc.TestClient().Ids.RandomAccountObjectIdentifier()
shareName := shareId.Name()
resourceName := "snowflake_grant_database_role.test"

configVariables := func() config.Variables {
return config.Variables{
"database": config.StringVariable(databaseName),
"database_role_name": config.StringVariable(databaseRoleName),
"share_name": config.StringVariable(shareName),
"database": config.StringVariable(databaseId.Name()),
"database_role_name": config.StringVariable(databaseRoleId.Name()),
"share_name": config.StringVariable(shareId.Name()),
}
}
resourceName := "snowflake_grant_database_role.test"
resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() { acc.TestAccPreCheck(t) },
Expand All @@ -153,9 +151,9 @@ func TestAcc_GrantDatabaseRole_share(t *testing.T) {
ConfigDirectory: config.StaticDirectory("testdata/TestAcc_GrantDatabaseRole/share"),
ConfigVariables: configVariables(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "database_role_name", databaseRoleId),
resource.TestCheckResourceAttr(resourceName, "share_name", shareName),
resource.TestCheckResourceAttr(resourceName, "id", fmt.Sprintf(`%v|%v|%v`, databaseRoleId, "SHARE", shareId.FullyQualifiedName())),
resource.TestCheckResourceAttr(resourceName, "database_role_name", databaseRoleId.FullyQualifiedName()),
resource.TestCheckResourceAttr(resourceName, "share_name", shareId.Name()),
resource.TestCheckResourceAttr(resourceName, "id", fmt.Sprintf(`%v|%v|%v`, databaseRoleId.FullyQualifiedName(), "SHARE", shareId.FullyQualifiedName())),
),
},
// test import
Expand Down
Loading
Loading