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: Identifier with arguments for procedure and external function #2987

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
}
56 changes: 56 additions & 0 deletions pkg/acceptance/helpers/function_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
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()
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,
sdk.NewCreateForSQLFunctionRequest(
id.SchemaObjectId(),
*sdk.NewFunctionReturnsRequest().WithResultDataType(*sdk.NewFunctionReturnsResultDataTypeRequest(sdk.DataTypeInt)),
"SELECT 1",
).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
}
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