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: Use stage from sdk #2427

Merged
merged 6 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
70 changes: 42 additions & 28 deletions pkg/datasources/stages.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package datasources

import (
"context"
"database/sql"
"errors"
"fmt"
"log"

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

Expand Down Expand Up @@ -56,42 +57,55 @@ var stagesSchema = map[string]*schema.Schema{

func Stages() *schema.Resource {
return &schema.Resource{
Read: ReadStages,
Schema: stagesSchema,
ReadContext: ReadStages,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i didnt know about this ReadContext function. we could actually use this for all resources, as we are using context.Background()

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is one of the points I added to our discussion on our resource-designing conventions. I'm using it because Read is deprecated and ReadContext or ReadWithoutTimeout should be used instead.

Schema: stagesSchema,
}
}

func ReadStages(d *schema.ResourceData, meta interface{}) error {
func ReadStages(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
db := meta.(*sql.DB)
databaseName := d.Get("database").(string)
schemaName := d.Get("schema").(string)

currentStages, err := snowflake.ListStages(databaseName, schemaName, db)
if errors.Is(err, sql.ErrNoRows) {
// If not found, mark resource to be removed from state file during apply or refresh
log.Printf("[DEBUG] stages in schema (%s) not found", d.Id())
d.SetId("")
return nil
} else if err != nil {
log.Printf("[DEBUG] unable to parse stages in schema (%s)", d.Id())
client := sdk.NewClientFromDB(db)
stages, err := client.Stages.Show(ctx, sdk.NewShowStageRequest().WithIn(
&sdk.In{
Schema: sdk.NewDatabaseObjectIdentifier(databaseName, schemaName),
},
))
if err != nil {
d.SetId("")
return nil
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Warning,
Summary: "Failed to query stages",
Detail: fmt.Sprintf("DatabaseName: %s, SchemaName: %s, Err: %s", databaseName, schemaName, err),
},
}
}

stages := []map[string]interface{}{}

for _, stage := range currentStages {
stageMap := map[string]interface{}{}

stageMap["name"] = stage.Name
stageMap["database"] = stage.DatabaseName
stageMap["schema"] = stage.SchemaName
stageMap["comment"] = stage.Comment
stageMap["storage_integration"] = stage.StorageIntegration
stagesList := make([]map[string]any, len(stages))
for i, stage := range stages {
stagesList[i] = map[string]any{
"name": stage.Name,
"database": stage.DatabaseName,
"schema": stage.SchemaName,
"comment": stage.Comment,
"storage_integration": stage.StorageIntegration,
}
}

stages = append(stages, stageMap)
if err := d.Set("stages", stagesList); err != nil {
return diag.Diagnostics{
diag.Diagnostic{
Severity: diag.Error,
Summary: "Failed to set stages",
Detail: fmt.Sprintf("Err: %s", err),
},
}
}

d.SetId(fmt.Sprintf(`%v|%v`, databaseName, schemaName))
return d.Set("stages", stages)
d.SetId(helpers.EncodeSnowflakeID(databaseName, schemaName))

return nil
}
69 changes: 45 additions & 24 deletions pkg/datasources/stages_acceptance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,75 @@ import (
"strings"
"testing"

acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance"
"github.com/hashicorp/terraform-plugin-testing/tfversion"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAcc_Stages(t *testing.T) {
databaseName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
schemaName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
storageIntegrationName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
stageName := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))
resource.ParallelTest(t, resource.TestCase{
Providers: providers(),
CheckDestroy: nil,
comment := strings.ToUpper(acctest.RandStringFromCharSet(10, acctest.CharSetAlpha))

resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories,
PreCheck: func() { acc.TestAccPreCheck(t) },
TerraformVersionChecks: []tfversion.TerraformVersionCheck{
tfversion.RequireAbove(tfversion.Version1_5_0),
},
Steps: []resource.TestStep{
{
Config: stages(databaseName, schemaName, stageName),
Config: stages(databaseName, schemaName, storageIntegrationName, stageName, comment),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.snowflake_stages.t", "database", databaseName),
resource.TestCheckResourceAttr("data.snowflake_stages.t", "schema", schemaName),
resource.TestCheckResourceAttrSet("data.snowflake_stages.t", "stages.#"),
resource.TestCheckResourceAttr("data.snowflake_stages.t", "stages.#", "1"),
resource.TestCheckResourceAttr("data.snowflake_stages.t", "stages.0.name", stageName),
resource.TestCheckResourceAttr("data.snowflake_stages.test", "database", databaseName),
resource.TestCheckResourceAttr("data.snowflake_stages.test", "schema", schemaName),
resource.TestCheckResourceAttr("data.snowflake_stages.test", "stages.#", "1"),
resource.TestCheckResourceAttr("data.snowflake_stages.test", "stages.0.name", stageName),
resource.TestCheckResourceAttr("data.snowflake_stages.test", "stages.0.storage_integration", storageIntegrationName),
resource.TestCheckResourceAttr("data.snowflake_stages.test", "stages.0.comment", comment),
),
},
},
})
}

func stages(databaseName string, schemaName string, stageName string) string {
func stages(databaseName string, schemaName string, storageIntegrationName string, stageName string, comment string) string {
return fmt.Sprintf(`
resource "snowflake_database" "test" {
name = "%s"
}

resource snowflake_database "d" {
name = "%v"
resource "snowflake_schema" "test"{
name = "%s"
database = snowflake_database.test.name
}

resource snowflake_schema "s"{
name = "%v"
database = snowflake_database.d.name
resource "snowflake_storage_integration" "test" {
name = "%s"
storage_allowed_locations = ["s3://foo/"]
storage_provider = "S3"

storage_aws_role_arn = "arn:aws:iam::000000000001:/role/test"
}

resource snowflake_stage "t"{
name = "%v"
database = snowflake_schema.s.database
schema = snowflake_schema.s.name
resource "snowflake_stage" "test"{
name = "%s"
database = snowflake_schema.test.database
schema = snowflake_schema.test.name
url = "s3://foo/"
storage_integration = snowflake_storage_integration.test.name
comment = "%s"
}

data snowflake_stages "t" {
database = snowflake_stage.t.database
schema = snowflake_stage.t.schema
depends_on = [snowflake_stage.t]
data "snowflake_stages" "test" {
depends_on = [snowflake_storage_integration.test, snowflake_stage.test]

database = snowflake_stage.test.database
schema = snowflake_stage.test.schema
}
`, databaseName, schemaName, stageName)
`, databaseName, schemaName, storageIntegrationName, stageName, comment)
}
Loading
Loading