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: Use new identifier with arguments in function, external function and procedure grants #3002

Merged
merged 21 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 17 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
1 change: 1 addition & 0 deletions docs/resources/grant_privileges_to_share.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ resource "snowflake_grant_privileges_to_share" "example" {

- `on_all_tables_in_schema` (String) The fully qualified identifier for the schema for which the specified privilege will be granted for all tables.
- `on_database` (String) The fully qualified name of the database on which privileges will be granted.
- `on_function` (String) The fully qualified name of the function on which privileges will be granted.
- `on_schema` (String) The fully qualified name of the schema on which privileges will be granted.
- `on_table` (String) The fully qualified name of the table on which privileges will be granted.
- `on_tag` (String) The fully qualified name of the tag on which privileges will be granted.
Expand Down
57 changes: 57 additions & 0 deletions pkg/acceptance/helpers/external_function_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package helpers

import (
"context"
"testing"

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

type ExternalFunctionClient struct {
context *TestClientContext
ids *IdsGenerator
}

func NewExternalFunctionClient(context *TestClientContext, idsGenerator *IdsGenerator) *ExternalFunctionClient {
return &ExternalFunctionClient{
context: context,
ids: idsGenerator,
}
}

func (c *ExternalFunctionClient) client() sdk.ExternalFunctions {
return c.context.client.ExternalFunctions
}

func (c *ExternalFunctionClient) Create(t *testing.T, apiIntegrationId sdk.AccountObjectIdentifier, arguments ...sdk.DataType) *sdk.ExternalFunction {
t.Helper()
return c.CreateWithIdentifier(t, apiIntegrationId, c.ids.RandomSchemaObjectIdentifierWithArguments(arguments...))
}

func (c *ExternalFunctionClient) CreateWithIdentifier(t *testing.T, apiIntegrationId sdk.AccountObjectIdentifier, id sdk.SchemaObjectIdentifierWithArguments) *sdk.ExternalFunction {
t.Helper()
ctx := context.Background()
argumentRequests := make([]sdk.ExternalFunctionArgumentRequest, len(id.ArgumentDataTypes()))
for i, argumentDataType := range id.ArgumentDataTypes() {
argumentRequests[i] = *sdk.NewExternalFunctionArgumentRequest(c.ids.Alpha(), argumentDataType)
}
err := c.client().Create(ctx,
sdk.NewCreateExternalFunctionRequest(
id.SchemaObjectId(),
sdk.DataTypeVariant,
&apiIntegrationId,
"https://xyz.execute-api.us-west-2.amazonaws.com/production/remote_echo",
).WithArguments(argumentRequests),
)
require.NoError(t, err)

t.Cleanup(func() {
require.NoError(t, c.context.client.Functions.Drop(ctx, sdk.NewDropFunctionRequest(id).WithIfExists(true)))
})

externalFunction, err := c.client().ShowByID(ctx, id)
require.NoError(t, err)

return externalFunction
}
75 changes: 75 additions & 0 deletions pkg/acceptance/helpers/function_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package helpers

import (
"context"
"testing"

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

type FunctionClient struct {
context *TestClientContext
ids *IdsGenerator
}

func NewFunctionClient(context *TestClientContext, idsGenerator *IdsGenerator) *FunctionClient {
return &FunctionClient{
context: context,
ids: idsGenerator,
}
}

func (c *FunctionClient) client() sdk.Functions {
return c.context.client.Functions
}

func (c *FunctionClient) Create(t *testing.T, arguments ...sdk.DataType) *sdk.Function {
t.Helper()
return c.CreateWithIdentifier(t, c.ids.RandomSchemaObjectIdentifierWithArguments(arguments...))
}

func (c *FunctionClient) CreateWithIdentifier(t *testing.T, id sdk.SchemaObjectIdentifierWithArguments) *sdk.Function {
t.Helper()

return c.CreateWithRequest(t, id,
sdk.NewCreateForSQLFunctionRequest(
id.SchemaObjectId(),
*sdk.NewFunctionReturnsRequest().WithResultDataType(*sdk.NewFunctionReturnsResultDataTypeRequest(sdk.DataTypeInt)),
"SELECT 1",
),
)
}

func (c *FunctionClient) CreateSecure(t *testing.T, arguments ...sdk.DataType) *sdk.Function {
t.Helper()
id := c.ids.RandomSchemaObjectIdentifierWithArguments(arguments...)

return c.CreateWithRequest(t, id,
sdk.NewCreateForSQLFunctionRequest(
id.SchemaObjectId(),
*sdk.NewFunctionReturnsRequest().WithResultDataType(*sdk.NewFunctionReturnsResultDataTypeRequest(sdk.DataTypeInt)),
"SELECT 1",
).WithSecure(true),
)
}

func (c *FunctionClient) CreateWithRequest(t *testing.T, id sdk.SchemaObjectIdentifierWithArguments, req *sdk.CreateForSQLFunctionRequest) *sdk.Function {
t.Helper()
ctx := context.Background()
argumentRequests := make([]sdk.FunctionArgumentRequest, len(id.ArgumentDataTypes()))
for i, argumentDataType := range id.ArgumentDataTypes() {
argumentRequests[i] = *sdk.NewFunctionArgumentRequest(c.ids.Alpha(), argumentDataType)
}
err := c.client().CreateForSQL(ctx, req.WithArguments(argumentRequests))
require.NoError(t, err)

t.Cleanup(func() {
require.NoError(t, c.context.client.Functions.Drop(ctx, sdk.NewDropFunctionRequest(id).WithIfExists(true)))
})

function, err := c.client().ShowByID(ctx, id)
require.NoError(t, err)

return function
}
55 changes: 55 additions & 0 deletions pkg/acceptance/helpers/procedure_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package helpers

import (
"context"
"testing"

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

type ProcedureClient struct {
context *TestClientContext
ids *IdsGenerator
}

func NewProcedureClient(context *TestClientContext, idsGenerator *IdsGenerator) *ProcedureClient {
return &ProcedureClient{
context: context,
ids: idsGenerator,
}
}

func (c *ProcedureClient) client() sdk.Procedures {
return c.context.client.Procedures
}

func (c *ProcedureClient) Create(t *testing.T, arguments ...sdk.DataType) *sdk.Procedure {
t.Helper()
return c.CreateWithIdentifier(t, c.ids.RandomSchemaObjectIdentifierWithArguments(arguments...))
}

func (c *ProcedureClient) CreateWithIdentifier(t *testing.T, id sdk.SchemaObjectIdentifierWithArguments) *sdk.Procedure {
t.Helper()
ctx := context.Background()
argumentRequests := make([]sdk.ProcedureArgumentRequest, len(id.ArgumentDataTypes()))
for i, argumentDataType := range id.ArgumentDataTypes() {
argumentRequests[i] = *sdk.NewProcedureArgumentRequest(c.ids.Alpha(), argumentDataType)
}
err := c.client().CreateForSQL(ctx,
sdk.NewCreateForSQLProcedureRequest(
id.SchemaObjectId(),
*sdk.NewProcedureSQLReturnsRequest().WithResultDataType(*sdk.NewProcedureReturnsResultDataTypeRequest(sdk.DataTypeInt)),
`BEGIN RETURN 1; END`).WithArguments(argumentRequests),
)
require.NoError(t, err)

t.Cleanup(func() {
require.NoError(t, c.context.client.Procedures.Drop(ctx, sdk.NewDropProcedureRequest(id).WithIfExists(true)))
})

procedure, err := c.client().ShowByID(ctx, id)
require.NoError(t, err)

return procedure
}
1 change: 0 additions & 1 deletion pkg/acceptance/helpers/share_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ 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
sfc-gh-asawicki marked this conversation as resolved.
Show resolved Hide resolved
return c.CreateShareWithIdentifier(t, c.ids.RandomAccountObjectIdentifier())
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/acceptance/helpers/test_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@ type TestClient struct {
DataMetricFunctionReferences *DataMetricFunctionReferencesClient
DynamicTable *DynamicTableClient
ExternalAccessIntegration *ExternalAccessIntegrationClient
ExternalFunction *ExternalFunctionClient
ExternalVolume *ExternalVolumeClient
FailoverGroup *FailoverGroupClient
FileFormat *FileFormatClient
Function *FunctionClient
Grant *GrantClient
MaskingPolicy *MaskingPolicyClient
MaterializedView *MaterializedViewClient
Expand All @@ -35,6 +37,7 @@ type TestClient struct {
Parameter *ParameterClient
PasswordPolicy *PasswordPolicyClient
Pipe *PipeClient
Procedure *ProcedureClient
ProjectionPolicy *ProjectionPolicyClient
PolicyReferences *PolicyReferencesClient
ResourceMonitor *ResourceMonitorClient
Expand Down Expand Up @@ -83,9 +86,11 @@ func NewTestClient(c *sdk.Client, database string, schema string, warehouse stri
DataMetricFunctionReferences: NewDataMetricFunctionReferencesClient(context),
DynamicTable: NewDynamicTableClient(context, idsGenerator),
ExternalAccessIntegration: NewExternalAccessIntegrationClient(context, idsGenerator),
ExternalFunction: NewExternalFunctionClient(context, idsGenerator),
ExternalVolume: NewExternalVolumeClient(context, idsGenerator),
FailoverGroup: NewFailoverGroupClient(context, idsGenerator),
FileFormat: NewFileFormatClient(context, idsGenerator),
Function: NewFunctionClient(context, idsGenerator),
Grant: NewGrantClient(context, idsGenerator),
MaskingPolicy: NewMaskingPolicyClient(context, idsGenerator),
MaterializedView: NewMaterializedViewClient(context, idsGenerator),
Expand All @@ -94,6 +99,7 @@ func NewTestClient(c *sdk.Client, database string, schema string, warehouse stri
Parameter: NewParameterClient(context),
PasswordPolicy: NewPasswordPolicyClient(context, idsGenerator),
Pipe: NewPipeClient(context, idsGenerator),
Procedure: NewProcedureClient(context, idsGenerator),
ProjectionPolicy: NewProjectionPolicyClient(context, idsGenerator),
PolicyReferences: NewPolicyReferencesClient(context),
ResourceMonitor: NewResourceMonitorClient(context, idsGenerator),
Expand Down
6 changes: 3 additions & 3 deletions pkg/datasources/procedures.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,10 @@ func ReadContextProcedures(ctx context.Context, d *schema.ResourceData, meta int

req := sdk.NewShowProcedureRequest()
if databaseName != "" {
req.WithIn(&sdk.In{Database: sdk.NewAccountObjectIdentifier(databaseName)})
req.WithIn(sdk.In{Database: sdk.NewAccountObjectIdentifier(databaseName)})
}
if schemaName != "" {
req.WithIn(&sdk.In{Schema: sdk.NewDatabaseObjectIdentifier(databaseName, schemaName)})
req.WithIn(sdk.In{Schema: sdk.NewDatabaseObjectIdentifier(databaseName, schemaName)})
}
procedures, err := client.Procedures.Show(ctx, req)
if err != nil {
Expand All @@ -103,7 +103,7 @@ func ReadContextProcedures(ctx context.Context, d *schema.ResourceData, meta int
procedureMap["database"] = procedure.CatalogName
procedureMap["schema"] = procedure.SchemaName
procedureMap["comment"] = procedure.Description
procedureSignatureMap, err := parseArguments(procedure.Arguments)
procedureSignatureMap, err := parseArguments(procedure.ArgumentsRaw)
if err != nil {
return diag.FromErr(err)
}
Expand Down
Loading
Loading