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: Schema SDK upgrade #2945

Merged
merged 5 commits into from
Jul 24, 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
7 changes: 4 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ jobs:
run: mkdir $HOME/.snowflake && echo "${{ secrets.SNOWFLAKE_CONFIG_FILE }}" > $HOME/.snowflake/config

- run: make test
if: steps.create_config.conclusion == 'success'
if: ${{ !cancelled() && steps.create_config.conclusion == 'success' }}
env:
SNOWFLAKE_BUSINESS_CRITICAL_ACCOUNT: ${{ secrets.SNOWFLAKE_BUSINESS_CRITICAL_ACCOUNT }}
TEST_SF_TF_AWS_EXTERNAL_BUCKET_URL: ${{ secrets.TEST_SF_TF_AWS_EXTERNAL_BUCKET_URL }}
Expand All @@ -57,14 +57,15 @@ jobs:
TEST_SF_TF_GCS_EXTERNAL_BUCKET_URL: ${{ secrets.TEST_SF_TF_GCS_EXTERNAL_BUCKET_URL }}

- name: Setup Terraform
if: steps.create_config.conclusion == 'success'
if: ${{ !cancelled() && steps.create_config.conclusion == 'success' }}
uses: hashicorp/setup-terraform@v3
id: setup_terraform
with:
terraform_version: 1.7.4
terraform_wrapper: false

- run: make test-acceptance
if: steps.create_config.conclusion == 'success'
if: ${{ !cancelled() && steps.setup_terraform.conclusion == 'success' }}
env:
SNOWFLAKE_BUSINESS_CRITICAL_ACCOUNT: ${{ secrets.SNOWFLAKE_BUSINESS_CRITICAL_ACCOUNT }}
TEST_SF_TF_AWS_EXTERNAL_BUCKET_URL: ${{ secrets.TEST_SF_TF_AWS_EXTERNAL_BUCKET_URL }}
Expand Down
11 changes: 11 additions & 0 deletions pkg/acceptance/helpers/parameter_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ func (c *ParameterClient) ShowWarehouseParameters(t *testing.T, id sdk.AccountOb
return params
}

func (c *ParameterClient) ShowSchemaParameters(t *testing.T, id sdk.DatabaseObjectIdentifier) []*sdk.Parameter {
t.Helper()
params, err := c.client().ShowParameters(context.Background(), &sdk.ShowParametersOptions{
In: &sdk.ParametersIn{
Schema: id,
},
})
require.NoError(t, err)
return params
}

func (c *ParameterClient) ShowUserParameters(t *testing.T, id sdk.AccountObjectIdentifier) []*sdk.Parameter {
t.Helper()
params, err := c.client().ShowParameters(context.Background(), &sdk.ShowParametersOptions{
Expand Down
7 changes: 6 additions & 1 deletion pkg/acceptance/helpers/schema_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ func (c *SchemaClient) CreateSchemaWithName(t *testing.T, name string) (*sdk.Sch
}

func (c *SchemaClient) CreateSchemaWithIdentifier(t *testing.T, id sdk.DatabaseObjectIdentifier) (*sdk.Schema, func()) {
t.Helper()
return c.CreateSchemaWithOpts(t, id, nil)
}

func (c *SchemaClient) CreateSchemaWithOpts(t *testing.T, id sdk.DatabaseObjectIdentifier, opts *sdk.CreateSchemaOptions) (*sdk.Schema, func()) {
t.Helper()
ctx := context.Background()

err := c.client().Create(ctx, id, nil)
err := c.client().Create(ctx, id, opts)
require.NoError(t, err)
schema, err := c.client().ShowByID(ctx, id)
require.NoError(t, err)
Expand Down
6 changes: 2 additions & 4 deletions pkg/resources/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,13 +142,11 @@ func ReadSchema(d *schema.ResourceData, meta interface{}) error {
values := map[string]any{
"name": s.Name,
"database": s.DatabaseName,
"comment": s.Comment,
// reset the options before reading back from the DB
"is_transient": false,
"is_managed": false,
}
if s.Comment != nil {
values["comment"] = *s.Comment
}

for k, v := range values {
if err := d.Set(k, v); err != nil {
Expand Down Expand Up @@ -184,7 +182,7 @@ func UpdateSchema(d *schema.ResourceData, meta interface{}) error {
newId := sdk.NewDatabaseObjectIdentifier(id.DatabaseName(), d.Get("name").(string))

err := client.Schemas.Alter(ctx, id, &sdk.AlterSchemaOptions{
NewName: newId,
NewName: sdk.Pointer(newId),
})
if err != nil {
return fmt.Errorf("error updating schema name on %v err = %w", d.Id(), err)
Expand Down
33 changes: 33 additions & 0 deletions pkg/schemas/schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package schemas

import (
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

var SchemaDescribeSchema = map[string]*schema.Schema{
"created_on": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"kind": {
Type: schema.TypeString,
Computed: true,
},
}

func SchemaDescriptionToSchema(description []sdk.SchemaDetails) []map[string]any {
result := make([]map[string]any, len(description))
for i, row := range description {
result[i] = map[string]any{
"created_on": row.CreatedOn.String(),
"name": row.Name,
"kind": row.Kind,
}
}
return result
}
9 changes: 6 additions & 3 deletions pkg/schemas/schema_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions pkg/schemas/schema_parameters.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package schemas

import (
"slices"
"strings"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

var (
ShowSchemaParametersSchema = make(map[string]*schema.Schema)
schemaParameters = []sdk.AccountParameter{
sdk.AccountParameterDataRetentionTimeInDays,
sdk.AccountParameterMaxDataExtensionTimeInDays,
sdk.AccountParameterExternalVolume,
sdk.AccountParameterCatalog,
sdk.AccountParameterReplaceInvalidCharacters,
sdk.AccountParameterDefaultDDLCollation,
sdk.AccountParameterStorageSerializationPolicy,
sdk.AccountParameterLogLevel,
sdk.AccountParameterTraceLevel,
sdk.AccountParameterSuspendTaskAfterNumFailures,
sdk.AccountParameterTaskAutoRetryAttempts,
sdk.AccountParameterUserTaskManagedInitialWarehouseSize,
sdk.AccountParameterUserTaskTimeoutMs,
sdk.AccountParameterUserTaskMinimumTriggerIntervalInSeconds,
sdk.AccountParameterQuotedIdentifiersIgnoreCase,
sdk.AccountParameterEnableConsoleOutput,
sdk.AccountParameterPipeExecutionPaused,
}
)

func init() {
for _, param := range schemaParameters {
ShowSchemaParametersSchema[strings.ToLower(string(param))] = ParameterListSchema
}
}

func SchemaParametersToSchema(parameters []*sdk.Parameter) map[string]any {
schemaParametersValue := make(map[string]any)
for _, param := range parameters {
if slices.Contains(schemaParameters, sdk.AccountParameter(param.Key)) {
schemaParametersValue[strings.ToLower(param.Key)] = []map[string]any{ParameterToSchema(param)}
}
}
return schemaParametersValue
}
1 change: 1 addition & 0 deletions pkg/sdk/parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -1018,6 +1018,7 @@ const (
ParameterTypeObject ParameterType = "OBJECT"
ParameterTypeWarehouse ParameterType = "WAREHOUSE"
ParameterTypeDatabase ParameterType = "DATABASE"
ParameterTypeSchema ParameterType = "SCHEMA"
)

type Parameter struct {
Expand Down
Loading
Loading