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

feat: Add identifier parsers #2957

Merged
merged 12 commits into from
Aug 5, 2024
45 changes: 45 additions & 0 deletions pkg/acceptance/helpers/grant_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package helpers

import (
"context"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/stretchr/testify/require"
)

type GrantClient struct {
context *TestClientContext
ids *IdsGenerator
}

func NewGrantClient(context *TestClientContext, idsGenerator *IdsGenerator) *GrantClient {
return &GrantClient{
context: context,
ids: idsGenerator,
}
}

func (c *GrantClient) client() sdk.Grants {
return c.context.client.Grants
}

func (c *GrantClient) GrantOnSchemaToAccountRole(t *testing.T, schemaId sdk.DatabaseObjectIdentifier, accountRoleId sdk.AccountObjectIdentifier, privileges ...sdk.SchemaPrivilege) {
t.Helper()
ctx := context.Background()

err := c.client().GrantPrivilegesToAccountRole(
ctx,
&sdk.AccountRoleGrantPrivileges{
SchemaPrivileges: privileges,
},
&sdk.AccountRoleGrantOn{
Schema: &sdk.GrantOnSchema{
Schema: &schemaId,
},
},
accountRoleId,
new(sdk.GrantPrivilegesToAccountRoleOptions),
)
require.NoError(t, err)
}
2 changes: 2 additions & 0 deletions pkg/acceptance/helpers/test_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type TestClient struct {
ExternalVolume *ExternalVolumeClient
FailoverGroup *FailoverGroupClient
FileFormat *FileFormatClient
Grant *GrantClient
MaskingPolicy *MaskingPolicyClient
MaterializedView *MaterializedViewClient
NetworkPolicy *NetworkPolicyClient
Expand Down Expand Up @@ -77,6 +78,7 @@ func NewTestClient(c *sdk.Client, database string, schema string, warehouse stri
ExternalVolume: NewExternalVolumeClient(context, idsGenerator),
FailoverGroup: NewFailoverGroupClient(context, idsGenerator),
FileFormat: NewFileFormatClient(context, idsGenerator),
Grant: NewGrantClient(context, idsGenerator),
MaskingPolicy: NewMaskingPolicyClient(context, idsGenerator),
MaterializedView: NewMaterializedViewClient(context, idsGenerator),
NetworkPolicy: NewNetworkPolicyClient(context, idsGenerator),
Expand Down
6 changes: 5 additions & 1 deletion pkg/datasources/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ func ReadDatabase(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("is_current", database.IsCurrent); err != nil {
return err
}
if err := d.Set("origin", database.Origin); err != nil {
var origin string
if database.Origin != nil {
origin = database.Origin.FullyQualifiedName()
}
if err := d.Set("origin", origin); err != nil {
return err
}
if err := d.Set("retention_time", database.RetentionTime); err != nil {
Expand Down
6 changes: 3 additions & 3 deletions pkg/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func DecodeSnowflakeID(id string) sdk.ObjectIdentifier {
// The following configuration { "some_identifier": "db.name" } will be parsed as an object called "name" that lives
// inside database called "db", not a database called "db.name". In this case quotes should be used.
func DecodeSnowflakeParameterID(identifier string) (sdk.ObjectIdentifier, error) {
parts, err := ParseIdentifierString(identifier)
parts, err := sdk.ParseIdentifierString(identifier)
if err != nil {
return nil, err
}
Expand All @@ -126,7 +126,7 @@ func DecodeSnowflakeParameterID(identifier string) (sdk.ObjectIdentifier, error)
// DecodeSnowflakeAccountIdentifier decodes account identifier (usually passed as one of the parameter in tf configuration) into sdk.AccountIdentifier.
// Check more in https://docs.snowflake.com/en/sql-reference/sql/create-account#required-parameters.
func DecodeSnowflakeAccountIdentifier(identifier string) (sdk.AccountIdentifier, error) {
parts, err := ParseIdentifierString(identifier)
parts, err := sdk.ParseIdentifierString(identifier)
if err != nil {
return sdk.AccountIdentifier{}, err
}
Expand Down Expand Up @@ -166,7 +166,7 @@ func ConcatSlices[T any](slices ...[]T) []T {
// TODO(SNOW-999049): address during identifiers rework
func ParseRootLocation(location string) (sdk.SchemaObjectIdentifier, string, error) {
location = strings.TrimPrefix(location, "@")
parts, err := parseIdentifierStringWithOpts(location, func(r *csv.Reader) {
parts, err := sdk.ParseIdentifierStringWithOpts(location, func(r *csv.Reader) {
r.Comma = '.'
r.LazyQuotes = true
})
Expand Down
149 changes: 0 additions & 149 deletions pkg/helpers/identifier_string_parser.go

This file was deleted.

Loading
Loading