Skip to content

Commit

Permalink
chore: Random ids rework part3 (#2833)
Browse files Browse the repository at this point in the history
Continuation of ids rework:
- Do not use `random.String()` for the identifiers
- Do not use `random.StringN()` for the identifiers
- Do not use `random.UUID()` for the identifiers
- Do not use `random.AlphanumericN()` for the identifiers
- Do not use `random.Alpha()` for the identifiers
- Do not use `random.StringRange()` for the identifiers
  • Loading branch information
sfc-gh-asawicki authored May 27, 2024
1 parent b576f29 commit 36ead85
Show file tree
Hide file tree
Showing 69 changed files with 625 additions and 801 deletions.
5 changes: 2 additions & 3 deletions pkg/acceptance/helpers/database_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ func (c *DatabaseClient) CreateDatabase(t *testing.T) (*sdk.Database, func()) {
return c.CreateDatabaseWithOptions(t, c.ids.RandomAccountObjectIdentifier(), &sdk.CreateDatabaseOptions{})
}

// TODO [SNOW-955520]: we have to control the name
func (c *DatabaseClient) CreateDatabaseWithName(t *testing.T, name string) (*sdk.Database, func()) {
func (c *DatabaseClient) CreateDatabaseWithIdentifier(t *testing.T, id sdk.AccountObjectIdentifier) (*sdk.Database, func()) {
t.Helper()
return c.CreateDatabaseWithOptions(t, sdk.NewAccountObjectIdentifier(name), &sdk.CreateDatabaseOptions{})
return c.CreateDatabaseWithOptions(t, id, &sdk.CreateDatabaseOptions{})
}

func (c *DatabaseClient) CreateDatabaseWithOptions(t *testing.T, id sdk.AccountObjectIdentifier, opts *sdk.CreateDatabaseOptions) (*sdk.Database, func()) {
Expand Down
3 changes: 1 addition & 2 deletions pkg/acceptance/helpers/database_role_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"errors"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -33,7 +32,7 @@ func (c *DatabaseRoleClient) CreateDatabaseRole(t *testing.T) (*sdk.DatabaseRole

func (c *DatabaseRoleClient) CreateDatabaseRoleInDatabase(t *testing.T, databaseId sdk.AccountObjectIdentifier) (*sdk.DatabaseRole, func()) {
t.Helper()
return c.CreateDatabaseRoleInDatabaseWithName(t, databaseId, random.String())
return c.CreateDatabaseRoleInDatabaseWithName(t, databaseId, c.ids.Alpha())
}

func (c *DatabaseRoleClient) CreateDatabaseRoleWithName(t *testing.T, name string) (*sdk.DatabaseRole, func()) {
Expand Down
5 changes: 2 additions & 3 deletions pkg/acceptance/helpers/dynamic_table_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,11 @@ func (c *DynamicTableClient) client() sdk.DynamicTables {

func (c *DynamicTableClient) CreateDynamicTable(t *testing.T, tableId sdk.SchemaObjectIdentifier) (*sdk.DynamicTable, func()) {
t.Helper()
return c.CreateDynamicTableWithOptions(t, c.ids.SchemaId(), random.AlphaN(12), c.ids.WarehouseId(), tableId)
return c.CreateDynamicTableWithOptions(t, c.ids.RandomSchemaObjectIdentifier(), c.ids.WarehouseId(), tableId)
}

func (c *DynamicTableClient) CreateDynamicTableWithOptions(t *testing.T, schemaId sdk.DatabaseObjectIdentifier, name string, warehouseId sdk.AccountObjectIdentifier, tableId sdk.SchemaObjectIdentifier) (*sdk.DynamicTable, func()) {
func (c *DynamicTableClient) CreateDynamicTableWithOptions(t *testing.T, id sdk.SchemaObjectIdentifier, warehouseId sdk.AccountObjectIdentifier, tableId sdk.SchemaObjectIdentifier) (*sdk.DynamicTable, func()) {
t.Helper()
id := sdk.NewSchemaObjectIdentifierInSchema(schemaId, name)
targetLag := sdk.TargetLag{
MaximumDuration: sdk.String("2 minutes"),
}
Expand Down
34 changes: 29 additions & 5 deletions pkg/acceptance/helpers/ids_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ func (c *IdsGenerator) AccountIdentifierWithLocator() sdk.AccountIdentifier {
return sdk.NewAccountIdentifierFromAccountLocator(c.context.client.GetAccountLocator())
}

func (c *IdsGenerator) NewSchemaObjectIdentifier(name string) sdk.SchemaObjectIdentifier {
return sdk.NewSchemaObjectIdentifierInSchema(c.SchemaId(), name)
}

func (c *IdsGenerator) RandomAccountObjectIdentifier() sdk.AccountObjectIdentifier {
return sdk.NewAccountObjectIdentifier(c.Alpha())
}
Expand All @@ -49,14 +45,42 @@ func (c *IdsGenerator) RandomAccountObjectIdentifierContaining(part string) sdk.
return sdk.NewAccountObjectIdentifier(c.AlphaContaining(part))
}

func (c *IdsGenerator) NewDatabaseObjectIdentifier(name string) sdk.DatabaseObjectIdentifier {
return sdk.NewDatabaseObjectIdentifier(c.DatabaseId().Name(), name)
}

func (c *IdsGenerator) RandomDatabaseObjectIdentifier() sdk.DatabaseObjectIdentifier {
return sdk.NewDatabaseObjectIdentifier(c.DatabaseId().Name(), c.Alpha())
return c.RandomDatabaseObjectIdentifierInDatabase(c.DatabaseId())
}

func (c *IdsGenerator) RandomDatabaseObjectIdentifierInDatabase(databaseId sdk.AccountObjectIdentifier) sdk.DatabaseObjectIdentifier {
return sdk.NewDatabaseObjectIdentifier(databaseId.Name(), c.Alpha())
}

func (c *IdsGenerator) RandomDatabaseObjectIdentifierWithPrefix(prefix string) sdk.DatabaseObjectIdentifier {
return sdk.NewDatabaseObjectIdentifier(c.DatabaseId().Name(), c.AlphaWithPrefix(prefix))
}

func (c *IdsGenerator) NewSchemaObjectIdentifier(name string) sdk.SchemaObjectIdentifier {
return sdk.NewSchemaObjectIdentifierInSchema(c.SchemaId(), name)
}

func (c *IdsGenerator) NewSchemaObjectIdentifierInSchema(name string, schemaId sdk.DatabaseObjectIdentifier) sdk.SchemaObjectIdentifier {
return sdk.NewSchemaObjectIdentifierInSchema(schemaId, name)
}

func (c *IdsGenerator) RandomSchemaObjectIdentifier() sdk.SchemaObjectIdentifier {
return c.RandomSchemaObjectIdentifierInSchema(c.SchemaId())
}

func (c *IdsGenerator) RandomSchemaObjectIdentifierWithPrefix(prefix string) sdk.SchemaObjectIdentifier {
return sdk.NewSchemaObjectIdentifierInSchema(c.SchemaId(), c.AlphaWithPrefix(prefix))
}

func (c *IdsGenerator) RandomSchemaObjectIdentifierWithArguments(arguments []sdk.DataType) sdk.SchemaObjectIdentifier {
return sdk.NewSchemaObjectIdentifierWithArguments(c.SchemaId().DatabaseName(), c.SchemaId().Name(), c.Alpha(), arguments)
}

func (c *IdsGenerator) RandomSchemaObjectIdentifierInSchema(schemaId sdk.DatabaseObjectIdentifier) sdk.SchemaObjectIdentifier {
return sdk.NewSchemaObjectIdentifierInSchema(schemaId, c.Alpha())
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/acceptance/helpers/masking_policy_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -35,11 +34,11 @@ func (c *MaskingPolicyClient) CreateMaskingPolicyInSchema(t *testing.T, schemaId
t.Helper()
signature := []sdk.TableColumnSignature{
{
Name: random.String(),
Name: c.ids.Alpha(),
Type: sdk.DataTypeVARCHAR,
},
{
Name: random.String(),
Name: c.ids.Alpha(),
Type: sdk.DataTypeVARCHAR,
},
}
Expand Down
19 changes: 11 additions & 8 deletions pkg/acceptance/helpers/random/random_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ func Comment() string {
return gofakeit.Sentence(10)
}

func Password() string {
return StringN(12)
}

// AdminName returns admin name acceptable by Snowflake:
// 090088 (22000): ADMIN_NAME can only contain letters, numbers and underscores.
// 090089 (22000): ADMIN_NAME must start with a letter.
func AdminName() string {
return AlphaN(1) + AlphanumericN(11)
}

func Bool() bool {
return gofakeit.Bool()
}
Expand All @@ -33,11 +44,3 @@ func AlphanumericN(num int) string {
func AlphaN(num int) string {
return gofakeit.Password(true, true, false, false, false, num)
}

func StringRange(min, max int) string {
return gofakeit.Password(true, true, true, true, false, IntRange(min, max))
}

func IntRange(min, max int) int {
return gofakeit.IntRange(min, max)
}
5 changes: 2 additions & 3 deletions pkg/acceptance/helpers/role_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ func (c *RoleClient) CreateRole(t *testing.T) (*sdk.Role, func()) {
return c.CreateRoleWithRequest(t, sdk.NewCreateRoleRequest(c.ids.RandomAccountObjectIdentifier()))
}

// TODO [SNOW-955520]: we have to control the name
func (c *RoleClient) CreateRoleWithName(t *testing.T, name string) (*sdk.Role, func()) {
func (c *RoleClient) CreateRoleWithIdentifier(t *testing.T, id sdk.AccountObjectIdentifier) (*sdk.Role, func()) {
t.Helper()
return c.CreateRoleWithRequest(t, sdk.NewCreateRoleRequest(sdk.NewAccountObjectIdentifier(name)))
return c.CreateRoleWithRequest(t, sdk.NewCreateRoleRequest(id))
}

func (c *RoleClient) CreateRoleGrantedToCurrentUser(t *testing.T) (*sdk.Role, func()) {
Expand Down
15 changes: 7 additions & 8 deletions pkg/acceptance/helpers/schema_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -32,23 +31,23 @@ func (c *SchemaClient) CreateSchema(t *testing.T) (*sdk.Schema, func()) {

func (c *SchemaClient) CreateSchemaInDatabase(t *testing.T, databaseId sdk.AccountObjectIdentifier) (*sdk.Schema, func()) {
t.Helper()
return c.CreateSchemaInDatabaseWithIdentifier(t, databaseId, random.AlphaN(12))
return c.CreateSchemaWithIdentifier(t, c.ids.RandomDatabaseObjectIdentifierInDatabase(databaseId))
}

func (c *SchemaClient) CreateSchemaWithName(t *testing.T, name string) (*sdk.Schema, func()) {
t.Helper()
return c.CreateSchemaInDatabaseWithIdentifier(t, c.ids.DatabaseId(), name)
return c.CreateSchemaWithIdentifier(t, c.ids.NewDatabaseObjectIdentifier(name))
}

func (c *SchemaClient) CreateSchemaInDatabaseWithIdentifier(t *testing.T, databaseId sdk.AccountObjectIdentifier, name string) (*sdk.Schema, func()) {
func (c *SchemaClient) CreateSchemaWithIdentifier(t *testing.T, id sdk.DatabaseObjectIdentifier) (*sdk.Schema, func()) {
t.Helper()
ctx := context.Background()
schemaID := sdk.NewDatabaseObjectIdentifier(databaseId.Name(), name)
err := c.client().Create(ctx, schemaID, nil)

err := c.client().Create(ctx, id, nil)
require.NoError(t, err)
schema, err := c.client().ShowByID(ctx, sdk.NewDatabaseObjectIdentifier(databaseId.Name(), name))
schema, err := c.client().ShowByID(ctx, id)
require.NoError(t, err)
return schema, c.DropSchemaFunc(t, schemaID)
return schema, c.DropSchemaFunc(t, id)
}

func (c *SchemaClient) DropSchemaFunc(t *testing.T, id sdk.DatabaseObjectIdentifier) func() {
Expand Down
8 changes: 3 additions & 5 deletions pkg/acceptance/helpers/share_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/stretchr/testify/require"
)
Expand All @@ -28,13 +27,12 @@ func (c *ShareClient) client() sdk.Shares {
func (c *ShareClient) CreateShare(t *testing.T) (*sdk.Share, func()) {
t.Helper()
// TODO(SNOW-1058419): Try with identifier containing dot during identifiers rework
return c.CreateShareWithName(t, random.AlphanumericN(12))
return c.CreateShareWithIdentifier(t, c.ids.RandomAccountObjectIdentifier())
}

// TODO [SNOW-955520]: we have to control the name
func (c *ShareClient) CreateShareWithName(t *testing.T, name string) (*sdk.Share, func()) {
func (c *ShareClient) CreateShareWithIdentifier(t *testing.T, id sdk.AccountObjectIdentifier) (*sdk.Share, func()) {
t.Helper()
return c.CreateShareWithOptions(t, sdk.NewAccountObjectIdentifier(name), &sdk.CreateShareOptions{})
return c.CreateShareWithOptions(t, id, &sdk.CreateShareOptions{})
}

func (c *ShareClient) CreateShareWithOptions(t *testing.T, id sdk.AccountObjectIdentifier, opts *sdk.CreateShareOptions) (*sdk.Share, func()) {
Expand Down
21 changes: 16 additions & 5 deletions pkg/acceptance/helpers/table_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"fmt"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers/random"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/stretchr/testify/require"
)
Expand All @@ -33,20 +32,32 @@ func (c *TableClient) CreateTable(t *testing.T) (*sdk.Table, func()) {
return c.CreateTableInSchema(t, c.ids.SchemaId())
}

func (c *TableClient) CreateTableWithName(t *testing.T, name string) (*sdk.Table, func()) {
t.Helper()

columns := []sdk.TableColumnRequest{
*sdk.NewTableColumnRequest("id", sdk.DataTypeNumber),
}
return c.CreateTableWithIdAndColumns(t, c.ids.NewSchemaObjectIdentifier(name), columns)
}

func (c *TableClient) CreateTableInSchema(t *testing.T, schemaId sdk.DatabaseObjectIdentifier) (*sdk.Table, func()) {
t.Helper()

columns := []sdk.TableColumnRequest{
*sdk.NewTableColumnRequest("id", sdk.DataTypeNumber),
}
name := random.StringRange(8, 28)
return c.CreateTableWithColumns(t, schemaId, name, columns)
return c.CreateTableWithIdAndColumns(t, c.ids.RandomSchemaObjectIdentifierInSchema(schemaId), columns)
}

func (c *TableClient) CreateTableWithColumns(t *testing.T, schemaId sdk.DatabaseObjectIdentifier, name string, columns []sdk.TableColumnRequest) (*sdk.Table, func()) {
func (c *TableClient) CreateTableWithColumns(t *testing.T, columns []sdk.TableColumnRequest) (*sdk.Table, func()) {
t.Helper()

id := sdk.NewSchemaObjectIdentifierInSchema(schemaId, name)
return c.CreateTableWithIdAndColumns(t, c.ids.RandomSchemaObjectIdentifier(), columns)
}

func (c *TableClient) CreateTableWithIdAndColumns(t *testing.T, id sdk.SchemaObjectIdentifier, columns []sdk.TableColumnRequest) (*sdk.Table, func()) {
t.Helper()
ctx := context.Background()

dbCreateRequest := sdk.NewCreateTableRequest(id, columns)
Expand Down
5 changes: 2 additions & 3 deletions pkg/acceptance/helpers/user_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ func (c *UserClient) CreateUser(t *testing.T) (*sdk.User, func()) {
return c.CreateUserWithOptions(t, c.ids.RandomAccountObjectIdentifier(), &sdk.CreateUserOptions{})
}

// TODO [SNOW-955520]: we have to control the name
func (c *UserClient) CreateUserWithName(t *testing.T, name string) (*sdk.User, func()) {
func (c *UserClient) CreateUserWithPrefix(t *testing.T, prefix string) (*sdk.User, func()) {
t.Helper()
return c.CreateUserWithOptions(t, sdk.NewAccountObjectIdentifier(name), &sdk.CreateUserOptions{})
return c.CreateUserWithOptions(t, c.ids.RandomAccountObjectIdentifierWithPrefix(prefix), &sdk.CreateUserOptions{})
}

func (c *UserClient) CreateUserWithOptions(t *testing.T, id sdk.AccountObjectIdentifier, opts *sdk.CreateUserOptions) (*sdk.User, func()) {
Expand Down
6 changes: 4 additions & 2 deletions pkg/resources/dynamic_table_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,14 @@ func TestAcc_DynamicTable_basic(t *testing.T) {

// TestAcc_DynamicTable_issue2173 proves https://github.com/Snowflake-Labs/terraform-provider-snowflake/issues/2173 issue.
func TestAcc_DynamicTable_issue2173(t *testing.T) {
dynamicTableName := acc.TestClient().Ids.Alpha()
dynamicTableId := acc.TestClient().Ids.RandomSchemaObjectIdentifier()
dynamicTableName := dynamicTableId.Name()
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)
newDynamicTableId := acc.TestClient().Ids.NewSchemaObjectIdentifierInSchema(dynamicTableName, otherSchemaId)
m := func() map[string]config.Variable {
return map[string]config.Variable{
"name": config.StringVariable(dynamicTableName),
Expand Down Expand Up @@ -196,7 +198,7 @@ func TestAcc_DynamicTable_issue2173(t *testing.T) {
},
{
PreConfig: func() {
acc.TestClient().DynamicTable.CreateDynamicTableWithOptions(t, otherSchemaId, dynamicTableName, acc.TestClient().Ids.WarehouseId(), tableId)
acc.TestClient().DynamicTable.CreateDynamicTableWithOptions(t, newDynamicTableId, acc.TestClient().Ids.WarehouseId(), tableId)
},
ConfigDirectory: config.TestStepDirectory(),
ConfigVariables: m(),
Expand Down
Loading

0 comments on commit 36ead85

Please sign in to comment.