From fbc9eaa0f8449a7cd789b41f134f8c29d73f9121 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Cie=C5=9Blak?= Date: Tue, 26 Nov 2024 12:57:17 +0100 Subject: [PATCH 1/3] Add support for usage tracking to data sources --- pkg/datasources/accounts.go | 16 ++-- pkg/datasources/alerts.go | 14 ++-- pkg/datasources/common.go | 12 +++ pkg/datasources/connections.go | 4 +- pkg/datasources/cortex_search_services.go | 13 +-- pkg/datasources/current_account.go | 16 ++-- pkg/datasources/current_role.go | 12 +-- pkg/datasources/database.go | 30 +++---- pkg/datasources/database_role.go | 16 ++-- pkg/datasources/database_roles.go | 4 +- pkg/datasources/databases.go | 4 +- pkg/datasources/dynamic_tables.go | 13 +-- pkg/datasources/external_functions.go | 4 +- pkg/datasources/external_tables.go | 12 +-- pkg/datasources/failover_groups.go | 14 ++-- pkg/datasources/file_formats.go | 14 ++-- pkg/datasources/functions.go | 4 +- pkg/datasources/grants.go | 4 +- pkg/datasources/masking_policies.go | 4 +- pkg/datasources/materialized_views.go | 12 +-- pkg/datasources/network_policies.go | 4 +- pkg/datasources/parameters.go | 18 ++-- pkg/datasources/pipes.go | 14 ++-- pkg/datasources/procedures.go | 4 +- pkg/datasources/resource_monitors.go | 4 +- pkg/datasources/role.go | 14 ++-- pkg/datasources/roles.go | 4 +- pkg/datasources/row_access_policies.go | 4 +- pkg/datasources/schemas.go | 4 +- pkg/datasources/secrets.go | 4 +- pkg/datasources/security_integrations.go | 4 +- pkg/datasources/sequences.go | 14 ++-- pkg/datasources/shares.go | 14 ++-- pkg/datasources/stages.go | 4 +- pkg/datasources/storage_integrations.go | 18 ++-- pkg/datasources/streamlits.go | 4 +- pkg/datasources/streams.go | 4 +- .../system_generate_scim_access_token.go | 12 ++- .../system_get_aws_sns_iam_policy.go | 14 ++-- .../system_get_privatelink_config.go | 28 ++++--- .../system_get_snowflake_platform_info.go | 19 +++-- pkg/datasources/tables.go | 12 +-- pkg/datasources/tags.go | 4 +- pkg/datasources/tasks.go | 12 +-- .../usage_tracking_acceptance_test.go | 84 +++++++++++++++++++ pkg/datasources/users.go | 4 +- pkg/datasources/views.go | 4 +- pkg/datasources/warehouses.go | 4 +- pkg/internal/tracking/context.go | 51 +++++++---- pkg/internal/tracking/context_test.go | 6 +- pkg/internal/tracking/query_test.go | 11 +-- pkg/provider/datasources/datasources.go | 63 ++++++++++++++ pkg/resources/common.go | 12 +-- .../usage_tracking_acceptance_test.go | 12 ++- pkg/sdk/testint/client_integration_test.go | 2 +- 55 files changed, 496 insertions(+), 216 deletions(-) create mode 100644 pkg/datasources/usage_tracking_acceptance_test.go create mode 100644 pkg/provider/datasources/datasources.go diff --git a/pkg/datasources/accounts.go b/pkg/datasources/accounts.go index 7f3079074e..87c706152c 100644 --- a/pkg/datasources/accounts.go +++ b/pkg/datasources/accounts.go @@ -4,6 +4,9 @@ import ( "context" "log" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/snowflakeroles" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" @@ -110,19 +113,18 @@ var accountsSchema = map[string]*schema.Schema{ // Accounts Snowflake Accounts resource. func Accounts() *schema.Resource { return &schema.Resource{ - Read: ReadAccounts, - Schema: accountsSchema, + ReadContext: TrackingReadWrapper(datasources.Accounts, ReadAccounts), + Schema: accountsSchema, } } // ReadAccounts lists accounts. -func ReadAccounts(d *schema.ResourceData, meta interface{}) error { +func ReadAccounts(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client - ctx := context.Background() ok, err := client.ContextFunctions.IsRoleInSession(ctx, snowflakeroles.Orgadmin) if err != nil { - return err + return diag.FromErr(err) } if !ok { log.Printf("[DEBUG] ORGADMIN role is not in current session, cannot read accounts") @@ -136,7 +138,7 @@ func ReadAccounts(d *schema.ResourceData, meta interface{}) error { } accounts, err := client.Accounts.Show(ctx, opts) if err != nil { - return err + return diag.FromErr(err) } d.SetId("accounts") accountsFlatten := []map[string]interface{}{} @@ -161,7 +163,7 @@ func ReadAccounts(d *schema.ResourceData, meta interface{}) error { accountsFlatten = append(accountsFlatten, m) } if err := d.Set("accounts", accountsFlatten); err != nil { - return err + return diag.FromErr(err) } return nil } diff --git a/pkg/datasources/alerts.go b/pkg/datasources/alerts.go index 554a7a7cd1..ac0d643528 100644 --- a/pkg/datasources/alerts.go +++ b/pkg/datasources/alerts.go @@ -4,6 +4,9 @@ import ( "context" "log" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" @@ -76,15 +79,14 @@ var alertsSchema = map[string]*schema.Schema{ // Alerts Snowflake Roles resource. func Alerts() *schema.Resource { return &schema.Resource{ - Read: ReadAlerts, - Schema: alertsSchema, + ReadContext: TrackingReadWrapper(datasources.Alerts, ReadAlerts), + Schema: alertsSchema, } } // ReadAlerts Reads the database metadata information. -func ReadAlerts(d *schema.ResourceData, meta interface{}) error { +func ReadAlerts(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client - ctx := context.Background() d.SetId("alerts_read") @@ -114,7 +116,7 @@ func ReadAlerts(d *schema.ResourceData, meta interface{}) error { if err != nil { log.Printf("[DEBUG] failed to list alerts in schema (%s)", d.Id()) d.SetId("") - return err + return diag.FromErr(err) } alerts := make([]map[string]any, 0, len(listAlerts)) @@ -127,7 +129,7 @@ func ReadAlerts(d *schema.ResourceData, meta interface{}) error { } if err := d.Set("alerts", alerts); err != nil { - return err + return diag.FromErr(err) } return nil } diff --git a/pkg/datasources/common.go b/pkg/datasources/common.go index 6f8ab20169..96662064c8 100644 --- a/pkg/datasources/common.go +++ b/pkg/datasources/common.go @@ -1,8 +1,13 @@ package datasources import ( + "context" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/tracking" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" "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" ) @@ -147,3 +152,10 @@ func handleExtendedIn(d *schema.ResourceData, setField **sdk.ExtendedIn) error { } return nil } + +func TrackingReadWrapper(datasourceName datasources.Datasource, readImplementation schema.ReadContextFunc) schema.ReadContextFunc { + return func(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { + ctx = tracking.NewContext(ctx, tracking.NewVersionedDatasourceMetadata(datasourceName)) + return readImplementation(ctx, d, meta) + } +} diff --git a/pkg/datasources/connections.go b/pkg/datasources/connections.go index ef03ffa7f1..bc3c59c378 100644 --- a/pkg/datasources/connections.go +++ b/pkg/datasources/connections.go @@ -3,6 +3,8 @@ package datasources import ( "context" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/schemas" @@ -34,7 +36,7 @@ var connectionsSchema = map[string]*schema.Schema{ func Connections() *schema.Resource { return &schema.Resource{ - ReadContext: ReadConnections, + ReadContext: TrackingReadWrapper(datasources.Connections, ReadConnections), Schema: connectionsSchema, Description: "Datasource used to get details of filtered connections. Filtering is aligned with the current possibilities for [SHOW CONNECTIONS](https://docs.snowflake.com/en/sql-reference/sql/show-connections) query. The results of SHOW is encapsulated in one output collection `connections`.", } diff --git a/pkg/datasources/cortex_search_services.go b/pkg/datasources/cortex_search_services.go index aa58a98b4b..b657d9f156 100644 --- a/pkg/datasources/cortex_search_services.go +++ b/pkg/datasources/cortex_search_services.go @@ -4,6 +4,9 @@ import ( "context" "log" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" @@ -108,13 +111,13 @@ var cortexSearchServicesSchema = map[string]*schema.Schema{ // CortexSearchServices Snowflake Cortex search services resource. func CortexSearchServices() *schema.Resource { return &schema.Resource{ - Read: ReadCortexSearchServices, - Schema: cortexSearchServicesSchema, + ReadContext: TrackingReadWrapper(datasources.CortexSearchServices, ReadCortexSearchServices), + Schema: cortexSearchServicesSchema, } } // ReadCortexSearchServices Reads the cortex search services metadata information. -func ReadCortexSearchServices(d *schema.ResourceData, meta interface{}) error { +func ReadCortexSearchServices(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client request := sdk.NewShowCortexSearchServiceRequest() @@ -167,7 +170,7 @@ func ReadCortexSearchServices(d *schema.ResourceData, meta interface{}) error { if err != nil { log.Printf("[DEBUG] snowflake_cortex_search_services.go: %v", err) d.SetId("") - return err + return diag.FromErr(err) } d.SetId("cortex_search_services") records := make([]map[string]any, 0, len(dts)) @@ -181,7 +184,7 @@ func ReadCortexSearchServices(d *schema.ResourceData, meta interface{}) error { records = append(records, record) } if err := d.Set("cortex_search_services", records); err != nil { - return err + return diag.FromErr(err) } return nil } diff --git a/pkg/datasources/current_account.go b/pkg/datasources/current_account.go index d6a0d1d291..c3ba23e1fa 100644 --- a/pkg/datasources/current_account.go +++ b/pkg/datasources/current_account.go @@ -5,6 +5,9 @@ import ( "fmt" "log" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -33,15 +36,14 @@ var currentAccountSchema = map[string]*schema.Schema{ // CurrentAccount the Snowflake current account resource. func CurrentAccount() *schema.Resource { return &schema.Resource{ - Read: ReadCurrentAccount, - Schema: currentAccountSchema, + ReadContext: TrackingReadWrapper(datasources.CurrentAccount, ReadCurrentAccount), + Schema: currentAccountSchema, } } // ReadCurrentAccount read the current snowflake account information. -func ReadCurrentAccount(d *schema.ResourceData, meta interface{}) error { +func ReadCurrentAccount(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client - ctx := context.Background() current, err := client.ContextFunctions.CurrentSessionDetails(ctx) if err != nil { @@ -53,11 +55,11 @@ func ReadCurrentAccount(d *schema.ResourceData, meta interface{}) error { d.SetId(fmt.Sprintf("%s.%s", current.Account, current.Region)) accountErr := d.Set("account", current.Account) if accountErr != nil { - return accountErr + return diag.FromErr(accountErr) } regionErr := d.Set("region", current.Region) if regionErr != nil { - return regionErr + return diag.FromErr(regionErr) } url, err := current.AccountURL() if err != nil { @@ -67,7 +69,7 @@ func ReadCurrentAccount(d *schema.ResourceData, meta interface{}) error { urlErr := d.Set("url", url) if urlErr != nil { - return urlErr + return diag.FromErr(urlErr) } return nil } diff --git a/pkg/datasources/current_role.go b/pkg/datasources/current_role.go index 254bb70dbc..e078773590 100644 --- a/pkg/datasources/current_role.go +++ b/pkg/datasources/current_role.go @@ -4,6 +4,9 @@ import ( "context" "log" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -19,14 +22,13 @@ var currentRoleSchema = map[string]*schema.Schema{ func CurrentRole() *schema.Resource { return &schema.Resource{ - Read: ReadCurrentRole, - Schema: currentRoleSchema, + ReadContext: TrackingReadWrapper(datasources.CurrentRole, ReadCurrentRole), + Schema: currentRoleSchema, } } -func ReadCurrentRole(d *schema.ResourceData, meta interface{}) error { +func ReadCurrentRole(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client - ctx := context.Background() role, err := client.ContextFunctions.CurrentRole(ctx) if err != nil { @@ -38,7 +40,7 @@ func ReadCurrentRole(d *schema.ResourceData, meta interface{}) error { d.SetId(helpers.EncodeSnowflakeID(role)) err = d.Set("name", role.Name()) if err != nil { - return err + return diag.FromErr(err) } return nil } diff --git a/pkg/datasources/database.go b/pkg/datasources/database.go index 0be0368121..d04595cad8 100644 --- a/pkg/datasources/database.go +++ b/pkg/datasources/database.go @@ -3,6 +3,9 @@ package datasources import ( "context" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" @@ -54,52 +57,51 @@ var databaseSchema = map[string]*schema.Schema{ // Database the Snowflake Database resource. func Database() *schema.Resource { return &schema.Resource{ - Read: ReadDatabase, - Schema: databaseSchema, + ReadContext: TrackingReadWrapper(datasources.Database, ReadDatabase), + Schema: databaseSchema, } } // ReadDatabase read the database meta-data information. -func ReadDatabase(d *schema.ResourceData, meta interface{}) error { +func ReadDatabase(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client - ctx := context.Background() name := d.Get("name").(string) id := sdk.NewAccountObjectIdentifier(name) database, err := client.Databases.ShowByID(ctx, id) if err != nil { - return err + return diag.FromErr(err) } d.SetId(helpers.EncodeResourceIdentifier(database.ID())) if err := d.Set("name", database.Name); err != nil { - return err + return diag.FromErr(err) } if err := d.Set("comment", database.Comment); err != nil { - return err + return diag.FromErr(err) } if err := d.Set("owner", database.Owner); err != nil { - return err + return diag.FromErr(err) } if err := d.Set("is_default", database.IsDefault); err != nil { - return err + return diag.FromErr(err) } if err := d.Set("is_current", database.IsCurrent); err != nil { - return err + return diag.FromErr(err) } var origin string if database.Origin != nil { origin = database.Origin.FullyQualifiedName() } if err := d.Set("origin", origin); err != nil { - return err + return diag.FromErr(err) } if err := d.Set("retention_time", database.RetentionTime); err != nil { - return err + return diag.FromErr(err) } if err := d.Set("created_on", database.CreatedOn.String()); err != nil { - return err + return diag.FromErr(err) } if err := d.Set("options", database.Options); err != nil { - return err + return diag.FromErr(err) } return nil } diff --git a/pkg/datasources/database_role.go b/pkg/datasources/database_role.go index 4379954ec3..dc33ac89fa 100644 --- a/pkg/datasources/database_role.go +++ b/pkg/datasources/database_role.go @@ -4,6 +4,9 @@ import ( "context" "log" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" @@ -36,34 +39,33 @@ var databaseRoleSchema = map[string]*schema.Schema{ // DatabaseRole Snowflake Database Role resource. func DatabaseRole() *schema.Resource { return &schema.Resource{ - Read: ReadDatabaseRole, - Schema: databaseRoleSchema, + ReadContext: TrackingReadWrapper(datasources.DatabaseRole, ReadDatabaseRole), + Schema: databaseRoleSchema, } } // ReadDatabaseRole Reads the database role metadata information. -func ReadDatabaseRole(d *schema.ResourceData, meta interface{}) error { +func ReadDatabaseRole(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client databaseName := d.Get("database").(string) roleName := d.Get("name").(string) - ctx := context.Background() dbObjId := sdk.NewDatabaseObjectIdentifier(databaseName, roleName) databaseRole, err := client.DatabaseRoles.ShowByID(ctx, dbObjId) if err != nil { log.Printf("[DEBUG] unable to show database role %s in db (%s)", roleName, databaseName) d.SetId("") - return err + return diag.FromErr(err) } err = d.Set("comment", databaseRole.Comment) if err != nil { - return err + return diag.FromErr(err) } err = d.Set("owner", databaseRole.Owner) if err != nil { - return err + return diag.FromErr(err) } d.SetId("database_role_read") diff --git a/pkg/datasources/database_roles.go b/pkg/datasources/database_roles.go index 615951e890..570b75fd60 100644 --- a/pkg/datasources/database_roles.go +++ b/pkg/datasources/database_roles.go @@ -3,6 +3,8 @@ package datasources import ( "context" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/schemas" @@ -64,7 +66,7 @@ var databaseRolesSchema = map[string]*schema.Schema{ func DatabaseRoles() *schema.Resource { return &schema.Resource{ - ReadContext: ReadDatabaseRoles, + ReadContext: TrackingReadWrapper(datasources.DatabaseRoles, ReadDatabaseRoles), Schema: databaseRolesSchema, Description: "Datasource used to get details of filtered database roles. Filtering is aligned with the current possibilities for [SHOW DATABASE ROLES](https://docs.snowflake.com/en/sql-reference/sql/show-database-roles) query (`like` and `limit` are supported). The results of SHOW is encapsulated in show_output collection.", } diff --git a/pkg/datasources/databases.go b/pkg/datasources/databases.go index a9a0f6d2d1..21fb414aed 100644 --- a/pkg/datasources/databases.go +++ b/pkg/datasources/databases.go @@ -3,6 +3,8 @@ package datasources import ( "context" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/schemas" @@ -91,7 +93,7 @@ var databasesSchema = map[string]*schema.Schema{ func Databases() *schema.Resource { return &schema.Resource{ - ReadContext: ReadDatabases, + ReadContext: TrackingReadWrapper(datasources.Databases, ReadDatabases), Schema: databasesSchema, Description: "Datasource used to get details of filtered databases. Filtering is aligned with the current possibilities for [SHOW DATABASES](https://docs.snowflake.com/en/sql-reference/sql/show-databases) query (`like`, `starts_with`, and `limit` are all supported). The results of SHOW, DESCRIBE, and SHOW PARAMETERS IN are encapsulated in one output collection.", } diff --git a/pkg/datasources/dynamic_tables.go b/pkg/datasources/dynamic_tables.go index 4431201cc4..8b5468ef00 100644 --- a/pkg/datasources/dynamic_tables.go +++ b/pkg/datasources/dynamic_tables.go @@ -4,6 +4,9 @@ import ( "context" "log" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" @@ -194,13 +197,13 @@ var dynamicTablesSchema = map[string]*schema.Schema{ // DynamicTables Snowflake Dynamic Tables resource. func DynamicTables() *schema.Resource { return &schema.Resource{ - Read: ReadDynamicTables, - Schema: dynamicTablesSchema, + ReadContext: TrackingReadWrapper(datasources.DynamicTables, ReadDynamicTables), + Schema: dynamicTablesSchema, } } // ReadDynamicTables Reads the dynamic tables metadata information. -func ReadDynamicTables(d *schema.ResourceData, meta interface{}) error { +func ReadDynamicTables(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client request := sdk.NewShowDynamicTableRequest() if v, ok := d.GetOk("like"); ok { @@ -252,7 +255,7 @@ func ReadDynamicTables(d *schema.ResourceData, meta interface{}) error { if err != nil { log.Printf("[DEBUG] snowflake_dynamic_tables.go: %v", err) d.SetId("") - return err + return diag.FromErr(err) } d.SetId("dynamic_tables") records := make([]map[string]any, 0, len(dts)) @@ -287,7 +290,7 @@ func ReadDynamicTables(d *schema.ResourceData, meta interface{}) error { records = append(records, record) } if err := d.Set("records", records); err != nil { - return err + return diag.FromErr(err) } return nil } diff --git a/pkg/datasources/external_functions.go b/pkg/datasources/external_functions.go index 01e5b956e6..9a85bd08c3 100644 --- a/pkg/datasources/external_functions.go +++ b/pkg/datasources/external_functions.go @@ -5,6 +5,8 @@ import ( "fmt" "strings" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" @@ -59,7 +61,7 @@ var externalFunctionsSchema = map[string]*schema.Schema{ func ExternalFunctions() *schema.Resource { return &schema.Resource{ - ReadContext: ReadContextExternalFunctions, + ReadContext: TrackingReadWrapper(datasources.ExternalFunctions, ReadContextExternalFunctions), Schema: externalFunctionsSchema, } } diff --git a/pkg/datasources/external_tables.go b/pkg/datasources/external_tables.go index 429fac4a3c..7c8c40ff52 100644 --- a/pkg/datasources/external_tables.go +++ b/pkg/datasources/external_tables.go @@ -4,6 +4,9 @@ import ( "context" "log" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers" @@ -54,14 +57,13 @@ var externalTablesSchema = map[string]*schema.Schema{ func ExternalTables() *schema.Resource { return &schema.Resource{ - Read: ReadExternalTables, - Schema: externalTablesSchema, + ReadContext: TrackingReadWrapper(datasources.ExternalTables, ReadExternalTables), + Schema: externalTablesSchema, } } -func ReadExternalTables(d *schema.ResourceData, meta interface{}) error { +func ReadExternalTables(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client - ctx := context.Background() databaseName := d.Get("database").(string) schemaName := d.Get("schema").(string) @@ -86,5 +88,5 @@ func ReadExternalTables(d *schema.ResourceData, meta interface{}) error { d.SetId(helpers.EncodeSnowflakeID(schemaId)) - return d.Set("external_tables", externalTablesObjects) + return diag.FromErr(d.Set("external_tables", externalTablesObjects)) } diff --git a/pkg/datasources/failover_groups.go b/pkg/datasources/failover_groups.go index f1bdf428af..85c50fd2c0 100644 --- a/pkg/datasources/failover_groups.go +++ b/pkg/datasources/failover_groups.go @@ -3,6 +3,9 @@ package datasources import ( "context" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" @@ -117,15 +120,14 @@ var failoverGroupsSchema = map[string]*schema.Schema{ // FailoverGroups Snowflake FailoverGroups resource. func FailoverGroups() *schema.Resource { return &schema.Resource{ - Read: ReadFailoverGroups, - Schema: failoverGroupsSchema, + ReadContext: TrackingReadWrapper(datasources.FailoverGroups, ReadFailoverGroups), + Schema: failoverGroupsSchema, } } // ReadFailoverGroups lists failover groups. -func ReadFailoverGroups(d *schema.ResourceData, meta interface{}) error { +func ReadFailoverGroups(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client - ctx := context.Background() inAccount := d.Get("in_account").(string) opts := sdk.ShowFailoverGroupOptions{} @@ -134,7 +136,7 @@ func ReadFailoverGroups(d *schema.ResourceData, meta interface{}) error { } failoverGroups, err := client.FailoverGroups.Show(ctx, &opts) if err != nil { - return err + return diag.FromErr(err) } d.SetId("failover_groups") failoverGroupsFlatten := []map[string]interface{}{} @@ -173,7 +175,7 @@ func ReadFailoverGroups(d *schema.ResourceData, meta interface{}) error { failoverGroupsFlatten = append(failoverGroupsFlatten, m) } if err := d.Set("failover_groups", failoverGroupsFlatten); err != nil { - return err + return diag.FromErr(err) } return nil } diff --git a/pkg/datasources/file_formats.go b/pkg/datasources/file_formats.go index 0bb77aa79e..465ea03c4a 100644 --- a/pkg/datasources/file_formats.go +++ b/pkg/datasources/file_formats.go @@ -4,6 +4,9 @@ import ( "context" "fmt" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" @@ -56,14 +59,13 @@ var fileFormatsSchema = map[string]*schema.Schema{ func FileFormats() *schema.Resource { return &schema.Resource{ - Read: ReadFileFormats, - Schema: fileFormatsSchema, + ReadContext: TrackingReadWrapper(datasources.FileFormats, ReadFileFormats), + Schema: fileFormatsSchema, } } -func ReadFileFormats(d *schema.ResourceData, meta interface{}) error { +func ReadFileFormats(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client - ctx := context.Background() databaseName := d.Get("database").(string) schemaName := d.Get("schema").(string) @@ -75,7 +77,7 @@ func ReadFileFormats(d *schema.ResourceData, meta interface{}) error { }) if err != nil { d.SetId("") - return err + return diag.FromErr(err) } fileFormats := []map[string]interface{}{} @@ -93,5 +95,5 @@ func ReadFileFormats(d *schema.ResourceData, meta interface{}) error { } d.SetId(fmt.Sprintf(`%v|%v`, databaseName, schemaName)) - return d.Set("file_formats", fileFormats) + return diag.FromErr(d.Set("file_formats", fileFormats)) } diff --git a/pkg/datasources/functions.go b/pkg/datasources/functions.go index b54213442c..480ed24b6f 100644 --- a/pkg/datasources/functions.go +++ b/pkg/datasources/functions.go @@ -4,6 +4,8 @@ import ( "context" "fmt" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers" @@ -64,7 +66,7 @@ var functionsSchema = map[string]*schema.Schema{ func Functions() *schema.Resource { return &schema.Resource{ - ReadContext: ReadContextFunctions, + ReadContext: TrackingReadWrapper(datasources.Functions, ReadContextFunctions), Schema: functionsSchema, } } diff --git a/pkg/datasources/grants.go b/pkg/datasources/grants.go index 87ea97752f..e4e24e68e7 100644 --- a/pkg/datasources/grants.go +++ b/pkg/datasources/grants.go @@ -4,6 +4,8 @@ import ( "context" "fmt" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" @@ -320,7 +322,7 @@ var grantsSchema = map[string]*schema.Schema{ func Grants() *schema.Resource { return &schema.Resource{ - ReadContext: ReadGrants, + ReadContext: TrackingReadWrapper(datasources.Grants, ReadGrants), Schema: grantsSchema, } } diff --git a/pkg/datasources/masking_policies.go b/pkg/datasources/masking_policies.go index 0435c1907b..670e29b760 100644 --- a/pkg/datasources/masking_policies.go +++ b/pkg/datasources/masking_policies.go @@ -3,6 +3,8 @@ package datasources import ( "context" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/schemas" @@ -117,7 +119,7 @@ var maskingPoliciesSchema = map[string]*schema.Schema{ func MaskingPolicies() *schema.Resource { return &schema.Resource{ - ReadContext: ReadMaskingPolicies, + ReadContext: TrackingReadWrapper(datasources.MaskingPolicies, ReadMaskingPolicies), Schema: maskingPoliciesSchema, Description: "Datasource used to get details of filtered masking policies. Filtering is aligned with the current possibilities for [SHOW MASKING POLICIES](https://docs.snowflake.com/en/sql-reference/sql/show-masking-policies) query. The results of SHOW and DESCRIBE are encapsulated in one output collection `masking_policies`.", } diff --git a/pkg/datasources/materialized_views.go b/pkg/datasources/materialized_views.go index ca5f929570..18f3b796e7 100644 --- a/pkg/datasources/materialized_views.go +++ b/pkg/datasources/materialized_views.go @@ -4,6 +4,9 @@ import ( "context" "log" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers" @@ -52,17 +55,16 @@ var materializedViewsSchema = map[string]*schema.Schema{ func MaterializedViews() *schema.Resource { return &schema.Resource{ - Read: ReadMaterializedViews, - Schema: materializedViewsSchema, + ReadContext: TrackingReadWrapper(datasources.MaterializedViews, ReadMaterializedViews), + Schema: materializedViewsSchema, } } -func ReadMaterializedViews(d *schema.ResourceData, meta interface{}) error { +func ReadMaterializedViews(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client databaseName := d.Get("database").(string) schemaName := d.Get("schema").(string) - ctx := context.Background() schemaId := sdk.NewDatabaseObjectIdentifier(databaseName, schemaName) extractedMaterializedViews, err := client.MaterializedViews.Show(ctx, sdk.NewShowMaterializedViewRequest().WithIn( &sdk.In{Schema: schemaId}, @@ -85,5 +87,5 @@ func ReadMaterializedViews(d *schema.ResourceData, meta interface{}) error { } d.SetId(helpers.EncodeSnowflakeID(databaseName, schemaName)) - return d.Set("materialized_views", materializedViews) + return diag.FromErr(d.Set("materialized_views", materializedViews)) } diff --git a/pkg/datasources/network_policies.go b/pkg/datasources/network_policies.go index c67db55b2f..6aa615d136 100644 --- a/pkg/datasources/network_policies.go +++ b/pkg/datasources/network_policies.go @@ -3,6 +3,8 @@ package datasources import ( "context" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/schemas" @@ -52,7 +54,7 @@ var networkPoliciesSchema = map[string]*schema.Schema{ func NetworkPolicies() *schema.Resource { return &schema.Resource{ - ReadContext: ReadNetworkPolicies, + ReadContext: TrackingReadWrapper(datasources.NetworkPolicies, ReadNetworkPolicies), Schema: networkPoliciesSchema, Description: "Datasource used to get details of filtered network policies. Filtering is aligned with the current possibilities for [SHOW NETWORK POLICIES](https://docs.snowflake.com/en/sql-reference/sql/show-network-policies) query (`like` is supported). The results of SHOW and DESCRIBE are encapsulated in one output collection.", } diff --git a/pkg/datasources/parameters.go b/pkg/datasources/parameters.go index 9ed876e404..8c88eb1222 100644 --- a/pkg/datasources/parameters.go +++ b/pkg/datasources/parameters.go @@ -5,6 +5,9 @@ import ( "fmt" "strings" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" @@ -79,14 +82,13 @@ var parametersSchema = map[string]*schema.Schema{ func Parameters() *schema.Resource { return &schema.Resource{ - Read: ReadParameters, - Schema: parametersSchema, + ReadContext: TrackingReadWrapper(datasources.Parameters, ReadParameters), + Schema: parametersSchema, } } -func ReadParameters(d *schema.ResourceData, meta interface{}) error { +func ReadParameters(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client - ctx := context.Background() p, ok := d.GetOk("pattern") pattern := "" if ok { @@ -107,7 +109,7 @@ func ReadParameters(d *schema.ResourceData, meta interface{}) error { case "SESSION": user := d.Get("user").(string) if user == "" { - return fmt.Errorf("user is required when parameter_type is set to SESSION") + return diag.FromErr(fmt.Errorf("user is required when parameter_type is set to SESSION")) } opts.In.User = sdk.NewAccountObjectIdentifier(user) case "OBJECT": @@ -125,12 +127,12 @@ func ReadParameters(d *schema.ResourceData, meta interface{}) error { case sdk.ObjectTypeTable: opts.In.Table = sdk.NewSchemaObjectIdentifierFromFullyQualifiedName(objectName) default: - return fmt.Errorf("object_type %s is not supported", objectType) + return diag.FromErr(fmt.Errorf("object_type %s is not supported", objectType)) } } parameters, err = client.Parameters.ShowParameters(ctx, &opts) if err != nil { - return fmt.Errorf("error listing parameters: %w", err) + return diag.FromErr(fmt.Errorf("error listing parameters: %w", err)) } d.SetId("parameters") @@ -146,5 +148,5 @@ func ReadParameters(d *schema.ResourceData, meta interface{}) error { params = append(params, paramMap) } - return d.Set("parameters", params) + return diag.FromErr(d.Set("parameters", params)) } diff --git a/pkg/datasources/pipes.go b/pkg/datasources/pipes.go index d157251187..9cca5a20b2 100644 --- a/pkg/datasources/pipes.go +++ b/pkg/datasources/pipes.go @@ -5,6 +5,9 @@ import ( "fmt" "log" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" @@ -57,14 +60,13 @@ var pipesSchema = map[string]*schema.Schema{ func Pipes() *schema.Resource { return &schema.Resource{ - Read: ReadPipes, - Schema: pipesSchema, + ReadContext: TrackingReadWrapper(datasources.Pipes, ReadPipes), + Schema: pipesSchema, } } -func ReadPipes(d *schema.ResourceData, meta interface{}) error { +func ReadPipes(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client - ctx := context.Background() databaseName := d.Get("database").(string) schemaName := d.Get("schema").(string) @@ -77,7 +79,7 @@ func ReadPipes(d *schema.ResourceData, meta interface{}) error { if err != nil { log.Printf("[DEBUG] unable to parse pipes in schema (%s)", d.Id()) d.SetId("") - return err + return diag.FromErr(err) } pipes := make([]map[string]any, 0, len(extractedPipes)) @@ -94,5 +96,5 @@ func ReadPipes(d *schema.ResourceData, meta interface{}) error { } d.SetId(fmt.Sprintf(`%v|%v`, databaseName, schemaName)) - return d.Set("pipes", pipes) + return diag.FromErr(d.Set("pipes", pipes)) } diff --git a/pkg/datasources/procedures.go b/pkg/datasources/procedures.go index 28a1ddc563..a20c338a68 100644 --- a/pkg/datasources/procedures.go +++ b/pkg/datasources/procedures.go @@ -6,6 +6,8 @@ import ( "regexp" "strings" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" @@ -65,7 +67,7 @@ var proceduresSchema = map[string]*schema.Schema{ func Procedures() *schema.Resource { return &schema.Resource{ - ReadContext: ReadContextProcedures, + ReadContext: TrackingReadWrapper(datasources.Procedures, ReadContextProcedures), Schema: proceduresSchema, } } diff --git a/pkg/datasources/resource_monitors.go b/pkg/datasources/resource_monitors.go index 0af5724842..8a3825034f 100644 --- a/pkg/datasources/resource_monitors.go +++ b/pkg/datasources/resource_monitors.go @@ -3,6 +3,8 @@ package datasources import ( "context" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/schemas" @@ -39,7 +41,7 @@ var resourceMonitorsSchema = map[string]*schema.Schema{ func ResourceMonitors() *schema.Resource { return &schema.Resource{ - ReadContext: ReadResourceMonitors, + ReadContext: TrackingReadWrapper(datasources.ResourceMonitors, ReadResourceMonitors), Schema: resourceMonitorsSchema, Description: "Datasource used to get details of filtered resource monitors. Filtering is aligned with the current possibilities for [SHOW RESOURCE MONITORS](https://docs.snowflake.com/en/sql-reference/sql/show-resource-monitors) query (`like` is supported). The results of SHOW is encapsulated in show_output collection.", } diff --git a/pkg/datasources/role.go b/pkg/datasources/role.go index 4bf1ddbb1b..4f2ccb5898 100644 --- a/pkg/datasources/role.go +++ b/pkg/datasources/role.go @@ -4,6 +4,9 @@ import ( "context" "log" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" @@ -28,7 +31,7 @@ var roleSchema = map[string]*schema.Schema{ // Role Snowflake Role resource. func Role() *schema.Resource { return &schema.Resource{ - Read: ReadRole, + ReadContext: TrackingReadWrapper(datasources.Role, ReadRole), Schema: roleSchema, DeprecationMessage: "This resource is deprecated and will be removed in a future major version release. Please use snowflake_roles instead.", Importer: &schema.ResourceImporter{ @@ -38,13 +41,12 @@ func Role() *schema.Resource { } // ReadRole Reads the database metadata information. -func ReadRole(d *schema.ResourceData, meta interface{}) error { +func ReadRole(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client - ctx := context.Background() roleId, err := sdk.ParseAccountObjectIdentifier(d.Get("name").(string)) if err != nil { - return err + return diag.FromErr(err) } role, err := client.Roles.ShowByID(ctx, roleId) @@ -56,10 +58,10 @@ func ReadRole(d *schema.ResourceData, meta interface{}) error { d.SetId(helpers.EncodeResourceIdentifier(role.ID())) if err := d.Set("name", role.Name); err != nil { - return err + return diag.FromErr(err) } if err := d.Set("comment", role.Comment); err != nil { - return err + return diag.FromErr(err) } return nil } diff --git a/pkg/datasources/roles.go b/pkg/datasources/roles.go index 6af7cf88cf..7278988f7d 100644 --- a/pkg/datasources/roles.go +++ b/pkg/datasources/roles.go @@ -4,6 +4,8 @@ import ( "context" "fmt" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/schemas" @@ -47,7 +49,7 @@ var rolesSchema = map[string]*schema.Schema{ func Roles() *schema.Resource { return &schema.Resource{ - ReadContext: ReadRoles, + ReadContext: TrackingReadWrapper(datasources.Roles, ReadRoles), Schema: rolesSchema, Description: "Datasource used to get details of filtered roles. Filtering is aligned with the current possibilities for [SHOW ROLES](https://docs.snowflake.com/en/sql-reference/sql/show-roles) query (`like` and `in_class` are all supported). The results of SHOW are encapsulated in one output collection.", } diff --git a/pkg/datasources/row_access_policies.go b/pkg/datasources/row_access_policies.go index eb6196b584..22d8607422 100644 --- a/pkg/datasources/row_access_policies.go +++ b/pkg/datasources/row_access_policies.go @@ -3,6 +3,8 @@ package datasources import ( "context" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/schemas" @@ -113,7 +115,7 @@ var rowAccessPoliciesSchema = map[string]*schema.Schema{ func RowAccessPolicies() *schema.Resource { return &schema.Resource{ - ReadContext: ReadRowAccessPolicies, + ReadContext: TrackingReadWrapper(datasources.RowAccessPolicies, ReadRowAccessPolicies), Schema: rowAccessPoliciesSchema, Description: "Datasource used to get details of filtered row access policies. Filtering is aligned with the current possibilities for [SHOW ROW ACCESS POLICIES](https://docs.snowflake.com/en/sql-reference/sql/show-row-access-policies) query. The results of SHOW and DESCRIBE are encapsulated in one output collection `row_access_policies`.", } diff --git a/pkg/datasources/schemas.go b/pkg/datasources/schemas.go index 07c5be6d67..bab420e5e3 100644 --- a/pkg/datasources/schemas.go +++ b/pkg/datasources/schemas.go @@ -3,6 +3,8 @@ package datasources import ( "context" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/schemas" @@ -127,7 +129,7 @@ var schemasSchema = map[string]*schema.Schema{ func Schemas() *schema.Resource { return &schema.Resource{ - ReadContext: ReadSchemas, + ReadContext: TrackingReadWrapper(datasources.Schemas, ReadSchemas), Schema: schemasSchema, Description: "Datasource used to get details of filtered schemas. Filtering is aligned with the current possibilities for [SHOW SCHEMAS](https://docs.snowflake.com/en/sql-reference/sql/show-schemas) query. The results of SHOW, DESCRIBE, and SHOW PARAMETERS IN are encapsulated in one output collection.", } diff --git a/pkg/datasources/secrets.go b/pkg/datasources/secrets.go index 41101c1ae4..c40ec7aa6e 100644 --- a/pkg/datasources/secrets.go +++ b/pkg/datasources/secrets.go @@ -3,6 +3,8 @@ package datasources import ( "context" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/schemas" @@ -96,7 +98,7 @@ var secretsSchema = map[string]*schema.Schema{ func Secrets() *schema.Resource { return &schema.Resource{ - ReadContext: ReadSecrets, + ReadContext: TrackingReadWrapper(datasources.Secrets, ReadSecrets), Schema: secretsSchema, Description: "Datasource used to get details of filtered secrets. Filtering is aligned with the current possibilities for [SHOW SECRETS](https://docs.snowflake.com/en/sql-reference/sql/show-secrets) query. The results of SHOW and DESCRIBE are encapsulated in one output collection `secrets`.", } diff --git a/pkg/datasources/security_integrations.go b/pkg/datasources/security_integrations.go index c020cc78c2..6418f6e4fc 100644 --- a/pkg/datasources/security_integrations.go +++ b/pkg/datasources/security_integrations.go @@ -3,6 +3,8 @@ package datasources import ( "context" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/schemas" @@ -52,7 +54,7 @@ var securityIntegrationsSchema = map[string]*schema.Schema{ func SecurityIntegrations() *schema.Resource { return &schema.Resource{ - ReadContext: ReadSecurityIntegrations, + ReadContext: TrackingReadWrapper(datasources.SecurityIntegrations, ReadSecurityIntegrations), Schema: securityIntegrationsSchema, Description: "Datasource used to get details of filtered security integrations. Filtering is aligned with the current possibilities for [SHOW SECURITY INTEGRATIONS](https://docs.snowflake.com/en/sql-reference/sql/show-integrations) query (only `like` is supported). The results of SHOW and DESCRIBE are encapsulated in one output collection `security_integrations`.", } diff --git a/pkg/datasources/sequences.go b/pkg/datasources/sequences.go index d59078266d..e9c6b7b53e 100644 --- a/pkg/datasources/sequences.go +++ b/pkg/datasources/sequences.go @@ -4,6 +4,9 @@ import ( "context" "fmt" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" @@ -51,14 +54,13 @@ var sequencesSchema = map[string]*schema.Schema{ func Sequences() *schema.Resource { return &schema.Resource{ - Read: ReadSequences, - Schema: sequencesSchema, + ReadContext: TrackingReadWrapper(datasources.Sequences, ReadSequences), + Schema: sequencesSchema, } } -func ReadSequences(d *schema.ResourceData, meta interface{}) error { +func ReadSequences(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client - ctx := context.Background() databaseName := d.Get("database").(string) schemaName := d.Get("schema").(string) @@ -67,7 +69,7 @@ func ReadSequences(d *schema.ResourceData, meta interface{}) error { }) seqs, err := client.Sequences.Show(ctx, req) if err != nil { - return err + return diag.FromErr(err) } sequences := []map[string]interface{}{} for _, seq := range seqs { @@ -81,5 +83,5 @@ func ReadSequences(d *schema.ResourceData, meta interface{}) error { } d.SetId(fmt.Sprintf(`%v|%v`, databaseName, schemaName)) - return d.Set("sequences", sequences) + return diag.FromErr(d.Set("sequences", sequences)) } diff --git a/pkg/datasources/shares.go b/pkg/datasources/shares.go index afe4902450..721debb9ed 100644 --- a/pkg/datasources/shares.go +++ b/pkg/datasources/shares.go @@ -3,6 +3,9 @@ package datasources import ( "context" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" @@ -55,18 +58,17 @@ var sharesSchema = map[string]*schema.Schema{ // Shares Snowflake Shares resource. func Shares() *schema.Resource { return &schema.Resource{ - Read: ReadShares, - Schema: sharesSchema, + ReadContext: TrackingReadWrapper(datasources.Shares, ReadShares), + Schema: sharesSchema, } } // ReadShares Reads the database metadata information. -func ReadShares(d *schema.ResourceData, meta interface{}) error { +func ReadShares(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client d.SetId("shares_read") pattern := d.Get("pattern").(string) - ctx := context.Background() var opts sdk.ShowShareOptions if pattern != "" { opts.Like = &sdk.Like{ @@ -75,7 +77,7 @@ func ReadShares(d *schema.ResourceData, meta interface{}) error { } shares, err := client.Shares.Show(ctx, &opts) if err != nil { - return err + return diag.FromErr(err) } sharesFlatten := []map[string]interface{}{} for _, share := range shares { @@ -93,7 +95,7 @@ func ReadShares(d *schema.ResourceData, meta interface{}) error { } if err := d.Set("shares", sharesFlatten); err != nil { - return err + return diag.FromErr(err) } return nil } diff --git a/pkg/datasources/stages.go b/pkg/datasources/stages.go index 65d665fbf6..695e785776 100644 --- a/pkg/datasources/stages.go +++ b/pkg/datasources/stages.go @@ -4,6 +4,8 @@ import ( "context" "fmt" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers" @@ -58,7 +60,7 @@ var stagesSchema = map[string]*schema.Schema{ func Stages() *schema.Resource { return &schema.Resource{ - ReadContext: ReadStages, + ReadContext: TrackingReadWrapper(datasources.Stages, ReadStages), Schema: stagesSchema, } } diff --git a/pkg/datasources/storage_integrations.go b/pkg/datasources/storage_integrations.go index 5a523e065b..2e17b29701 100644 --- a/pkg/datasources/storage_integrations.go +++ b/pkg/datasources/storage_integrations.go @@ -4,6 +4,9 @@ import ( "context" "fmt" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" @@ -42,25 +45,24 @@ var storageIntegrationsSchema = map[string]*schema.Schema{ func StorageIntegrations() *schema.Resource { return &schema.Resource{ - Read: ReadStorageIntegrations, - Schema: storageIntegrationsSchema, + ReadContext: TrackingReadWrapper(datasources.StorageIntegrations, ReadStorageIntegrations), + Schema: storageIntegrationsSchema, } } -func ReadStorageIntegrations(d *schema.ResourceData, meta interface{}) error { +func ReadStorageIntegrations(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client - ctx := context.Background() account, err := client.ContextFunctions.CurrentAccount(ctx) if err != nil { d.SetId("") - return fmt.Errorf("[DEBUG] unable to retrieve current account") + return diag.FromErr(fmt.Errorf("[DEBUG] unable to retrieve current account")) } region, err := client.ContextFunctions.CurrentRegion(ctx) if err != nil { d.SetId("") - return fmt.Errorf("[DEBUG] unable to retrieve current region") + return diag.FromErr(fmt.Errorf("[DEBUG] unable to retrieve current region")) } d.SetId(fmt.Sprintf("%s.%s", account, region)) @@ -68,7 +70,7 @@ func ReadStorageIntegrations(d *schema.ResourceData, meta interface{}) error { storageIntegrations, err := client.StorageIntegrations.Show(ctx, sdk.NewShowStorageIntegrationRequest()) if err != nil { d.SetId("") - return fmt.Errorf("unable to retrieve storage integrations in account (%s), err = %w", d.Id(), err) + return diag.FromErr(fmt.Errorf("unable to retrieve storage integrations in account (%s), err = %w", d.Id(), err)) } storageIntegrationMaps := make([]map[string]any, len(storageIntegrations)) @@ -82,5 +84,5 @@ func ReadStorageIntegrations(d *schema.ResourceData, meta interface{}) error { } } - return d.Set("storage_integrations", storageIntegrationMaps) + return diag.FromErr(d.Set("storage_integrations", storageIntegrationMaps)) } diff --git a/pkg/datasources/streamlits.go b/pkg/datasources/streamlits.go index 4fed061263..889a23548f 100644 --- a/pkg/datasources/streamlits.go +++ b/pkg/datasources/streamlits.go @@ -3,6 +3,8 @@ package datasources import ( "context" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/schemas" @@ -100,7 +102,7 @@ var streamlitsSchema = map[string]*schema.Schema{ func Streamlits() *schema.Resource { return &schema.Resource{ - ReadContext: ReadStreamlits, + ReadContext: TrackingReadWrapper(datasources.Streamlits, ReadStreamlits), Schema: streamlitsSchema, Description: "Datasource used to get details of filtered streamlits. Filtering is aligned with the current possibilities for [SHOW STREAMLITS](https://docs.snowflake.com/en/sql-reference/sql/show-streamlits) query (only `like` is supported). The results of SHOW and DESCRIBE are encapsulated in one output collection `streamlits`.", } diff --git a/pkg/datasources/streams.go b/pkg/datasources/streams.go index d992f2d669..4323fb19d2 100644 --- a/pkg/datasources/streams.go +++ b/pkg/datasources/streams.go @@ -3,6 +3,8 @@ package datasources import ( "context" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/schemas" @@ -52,7 +54,7 @@ var streamsSchema = map[string]*schema.Schema{ func Streams() *schema.Resource { return &schema.Resource{ - ReadContext: ReadStreams, + ReadContext: TrackingReadWrapper(datasources.Streams, ReadStreams), Schema: streamsSchema, Description: "Datasource used to get details of filtered streams. Filtering is aligned with the current possibilities for [SHOW STREAMS](https://docs.snowflake.com/en/sql-reference/sql/show-streams) query. The results of SHOW and DESCRIBE are encapsulated in one output collection `streams`.", } diff --git a/pkg/datasources/system_generate_scim_access_token.go b/pkg/datasources/system_generate_scim_access_token.go index 5630b58cae..f40eb00306 100644 --- a/pkg/datasources/system_generate_scim_access_token.go +++ b/pkg/datasources/system_generate_scim_access_token.go @@ -1,10 +1,14 @@ package datasources import ( + "context" "database/sql" "errors" "log" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" @@ -28,13 +32,13 @@ var systemGenerateSCIMAccesstokenSchema = map[string]*schema.Schema{ func SystemGenerateSCIMAccessToken() *schema.Resource { return &schema.Resource{ - Read: ReadSystemGenerateSCIMAccessToken, - Schema: systemGenerateSCIMAccesstokenSchema, + ReadContext: TrackingReadWrapper(datasources.SystemGenerateScimAccessToken, ReadSystemGenerateSCIMAccessToken), + Schema: systemGenerateSCIMAccesstokenSchema, } } // ReadSystemGetAWSSNSIAMPolicy implements schema.ReadFunc. -func ReadSystemGenerateSCIMAccessToken(d *schema.ResourceData, meta interface{}) error { +func ReadSystemGenerateSCIMAccessToken(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client db := client.GetConn().DB @@ -57,5 +61,5 @@ func ReadSystemGenerateSCIMAccessToken(d *schema.ResourceData, meta interface{}) } d.SetId(integrationName) - return d.Set("access_token", accessToken.Token) + return diag.FromErr(d.Set("access_token", accessToken.Token)) } diff --git a/pkg/datasources/system_get_aws_sns_iam_policy.go b/pkg/datasources/system_get_aws_sns_iam_policy.go index 2f27b43736..a772d370ad 100644 --- a/pkg/datasources/system_get_aws_sns_iam_policy.go +++ b/pkg/datasources/system_get_aws_sns_iam_policy.go @@ -1,10 +1,14 @@ package datasources import ( + "context" "database/sql" "errors" "log" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/snowflake" @@ -26,13 +30,13 @@ var systemGetAWSSNSIAMPolicySchema = map[string]*schema.Schema{ func SystemGetAWSSNSIAMPolicy() *schema.Resource { return &schema.Resource{ - Read: ReadSystemGetAWSSNSIAMPolicy, - Schema: systemGetAWSSNSIAMPolicySchema, + ReadContext: TrackingReadWrapper(datasources.SystemGetAwsSnsIamPolicy, ReadSystemGetAWSSNSIAMPolicy), + Schema: systemGetAWSSNSIAMPolicySchema, } } // ReadSystemGetAWSSNSIAMPolicy implements schema.ReadFunc. -func ReadSystemGetAWSSNSIAMPolicy(d *schema.ResourceData, meta interface{}) error { +func ReadSystemGetAWSSNSIAMPolicy(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client db := client.GetConn().DB awsSNSTopicArn := d.Get("aws_sns_topic_arn").(string) @@ -47,9 +51,9 @@ func ReadSystemGetAWSSNSIAMPolicy(d *schema.ResourceData, meta interface{}) erro return nil } if err != nil { - return err + return diag.FromErr(err) } d.SetId(awsSNSTopicArn) - return d.Set("aws_sns_topic_policy_json", policy.Policy) + return diag.FromErr(d.Set("aws_sns_topic_policy_json", policy.Policy)) } diff --git a/pkg/datasources/system_get_privatelink_config.go b/pkg/datasources/system_get_privatelink_config.go index 947519f433..ae13316ad7 100644 --- a/pkg/datasources/system_get_privatelink_config.go +++ b/pkg/datasources/system_get_privatelink_config.go @@ -1,10 +1,14 @@ package datasources import ( + "context" "database/sql" "errors" "log" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/snowflake" @@ -69,13 +73,13 @@ var systemGetPrivateLinkConfigSchema = map[string]*schema.Schema{ func SystemGetPrivateLinkConfig() *schema.Resource { return &schema.Resource{ - Read: ReadSystemGetPrivateLinkConfig, - Schema: systemGetPrivateLinkConfigSchema, + ReadContext: TrackingReadWrapper(datasources.SystemGetPrivateLinkConfig, ReadSystemGetPrivateLinkConfig), + Schema: systemGetPrivateLinkConfigSchema, } } // ReadSystemGetPrivateLinkConfig implements schema.ReadFunc. -func ReadSystemGetPrivateLinkConfig(d *schema.ResourceData, meta interface{}) error { +func ReadSystemGetPrivateLinkConfig(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client db := client.GetConn().DB @@ -100,56 +104,56 @@ func ReadSystemGetPrivateLinkConfig(d *schema.ResourceData, meta interface{}) er d.SetId(config.AccountName) accNameErr := d.Set("account_name", config.AccountName) if accNameErr != nil { - return accNameErr + return diag.FromErr(accNameErr) } accURLErr := d.Set("account_url", config.AccountURL) if accURLErr != nil { - return accURLErr + return diag.FromErr(accURLErr) } ocspURLErr := d.Set("ocsp_url", config.OCSPURL) if ocspURLErr != nil { - return ocspURLErr + return diag.FromErr(ocspURLErr) } if config.AwsVpceID != "" { awsVpceIDErr := d.Set("aws_vpce_id", config.AwsVpceID) if awsVpceIDErr != nil { - return awsVpceIDErr + return diag.FromErr(awsVpceIDErr) } } if config.AzurePrivateLinkServiceID != "" { azurePlsIDErr := d.Set("azure_pls_id", config.AzurePrivateLinkServiceID) if azurePlsIDErr != nil { - return azurePlsIDErr + return diag.FromErr(azurePlsIDErr) } } if config.InternalStage != "" { intStgErr := d.Set("internal_stage", config.InternalStage) if intStgErr != nil { - return intStgErr + return diag.FromErr(intStgErr) } } if config.SnowsightURL != "" { snowSigURLErr := d.Set("snowsight_url", config.SnowsightURL) if snowSigURLErr != nil { - return snowSigURLErr + return diag.FromErr(snowSigURLErr) } } if config.RegionlessSnowsightURL != "" { reglssSnowURLErr := d.Set("regionless_snowsight_url", config.RegionlessSnowsightURL) if reglssSnowURLErr != nil { - return reglssSnowURLErr + return diag.FromErr(reglssSnowURLErr) } } if config.RegionlessAccountURL != "" { reglssAccURLErr := d.Set("regionless_account_url", config.RegionlessAccountURL) if reglssAccURLErr != nil { - return reglssAccURLErr + return diag.FromErr(reglssAccURLErr) } } diff --git a/pkg/datasources/system_get_snowflake_platform_info.go b/pkg/datasources/system_get_snowflake_platform_info.go index 793e37f76e..e9f54ac4ce 100644 --- a/pkg/datasources/system_get_snowflake_platform_info.go +++ b/pkg/datasources/system_get_snowflake_platform_info.go @@ -7,6 +7,9 @@ import ( "fmt" "log" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/snowflake" @@ -30,13 +33,13 @@ var systemGetSnowflakePlatformInfoSchema = map[string]*schema.Schema{ func SystemGetSnowflakePlatformInfo() *schema.Resource { return &schema.Resource{ - Read: ReadSystemGetSnowflakePlatformInfo, - Schema: systemGetSnowflakePlatformInfoSchema, + ReadContext: TrackingReadWrapper(datasources.SystemGetSnowflakePlatformInfo, ReadSystemGetSnowflakePlatformInfo), + Schema: systemGetSnowflakePlatformInfoSchema, } } // ReadSystemGetSnowflakePlatformInfo implements schema.ReadFunc. -func ReadSystemGetSnowflakePlatformInfo(d *schema.ResourceData, meta interface{}) error { +func ReadSystemGetSnowflakePlatformInfo(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client db := client.GetConn().DB @@ -48,7 +51,7 @@ func ReadSystemGetSnowflakePlatformInfo(d *schema.ResourceData, meta interface{} // If not found, mark resource to be removed from state file during apply or refresh d.SetId("") log.Println("[DEBUG] current_account failed to decode") - return fmt.Errorf("error current_account err = %w", err) + return diag.FromErr(fmt.Errorf("error current_account err = %w", err)) } d.SetId(fmt.Sprintf("%s.%s", acc.Account, acc.Region)) @@ -57,22 +60,22 @@ func ReadSystemGetSnowflakePlatformInfo(d *schema.ResourceData, meta interface{} if errors.Is(err, sql.ErrNoRows) { // If not found, mark resource to be removed from state file during apply or refresh log.Println("[DEBUG] system_get_snowflake_platform_info not found") - return fmt.Errorf("error system_get_snowflake_platform_info err = %w", err) + return diag.FromErr(fmt.Errorf("error system_get_snowflake_platform_info err = %w", err)) } info, err := rawInfo.GetStructuredConfig() if err != nil { log.Println("[DEBUG] system_get_snowflake_platform_info failed to decode") d.SetId("") - return fmt.Errorf("error system_get_snowflake_platform_info err = %w", err) + return diag.FromErr(fmt.Errorf("error system_get_snowflake_platform_info err = %w", err)) } if err := d.Set("azure_vnet_subnet_ids", info.AzureVnetSubnetIds); err != nil { - return fmt.Errorf("error system_get_snowflake_platform_info err = %w", err) + return diag.FromErr(fmt.Errorf("error system_get_snowflake_platform_info err = %w", err)) } if err := d.Set("aws_vpc_ids", info.AwsVpcIds); err != nil { - return fmt.Errorf("error system_get_snowflake_platform_info err = %w", err) + return diag.FromErr(fmt.Errorf("error system_get_snowflake_platform_info err = %w", err)) } return nil diff --git a/pkg/datasources/tables.go b/pkg/datasources/tables.go index 09944aeeda..fac59da574 100644 --- a/pkg/datasources/tables.go +++ b/pkg/datasources/tables.go @@ -4,6 +4,9 @@ import ( "context" "log" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers" @@ -52,14 +55,13 @@ var tablesSchema = map[string]*schema.Schema{ func Tables() *schema.Resource { return &schema.Resource{ - Read: ReadTables, - Schema: tablesSchema, + ReadContext: TrackingReadWrapper(datasources.Tables, ReadTables), + Schema: tablesSchema, } } -func ReadTables(d *schema.ResourceData, meta interface{}) error { +func ReadTables(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client - ctx := context.Background() databaseName := d.Get("database").(string) schemaName := d.Get("schema").(string) @@ -91,5 +93,5 @@ func ReadTables(d *schema.ResourceData, meta interface{}) error { } d.SetId(helpers.EncodeSnowflakeID(databaseName, schemaName)) - return d.Set("tables", tables) + return diag.FromErr(d.Set("tables", tables)) } diff --git a/pkg/datasources/tags.go b/pkg/datasources/tags.go index 05b551fc0b..6fee2d84d6 100644 --- a/pkg/datasources/tags.go +++ b/pkg/datasources/tags.go @@ -3,6 +3,8 @@ package datasources import ( "context" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/schemas" @@ -36,7 +38,7 @@ var tagsSchema = map[string]*schema.Schema{ func Tags() *schema.Resource { return &schema.Resource{ - ReadContext: ReadTags, + ReadContext: TrackingReadWrapper(datasources.Tags, ReadTags), Schema: tagsSchema, Description: "Datasource used to get details of filtered tags. Filtering is aligned with the current possibilities for [SHOW TAGS](https://docs.snowflake.com/en/sql-reference/sql/show-tags) query. The results of SHOW are encapsulated in one output collection `tags`.", } diff --git a/pkg/datasources/tasks.go b/pkg/datasources/tasks.go index 5af820fa7c..604667f1e4 100644 --- a/pkg/datasources/tasks.go +++ b/pkg/datasources/tasks.go @@ -5,6 +5,9 @@ import ( "fmt" "log" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" @@ -57,14 +60,13 @@ var tasksSchema = map[string]*schema.Schema{ func Tasks() *schema.Resource { return &schema.Resource{ - Read: ReadTasks, - Schema: tasksSchema, + ReadContext: TrackingReadWrapper(datasources.Tasks, ReadTasks), + Schema: tasksSchema, } } -func ReadTasks(d *schema.ResourceData, meta interface{}) error { +func ReadTasks(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { client := meta.(*provider.Context).Client - ctx := context.Background() databaseName := d.Get("database").(string) schemaName := d.Get("schema").(string) @@ -91,5 +93,5 @@ func ReadTasks(d *schema.ResourceData, meta interface{}) error { } d.SetId(fmt.Sprintf(`%v|%v`, databaseName, schemaName)) - return d.Set("tasks", tasks) + return diag.FromErr(d.Set("tasks", tasks)) } diff --git a/pkg/datasources/usage_tracking_acceptance_test.go b/pkg/datasources/usage_tracking_acceptance_test.go new file mode 100644 index 0000000000..a2146f7bed --- /dev/null +++ b/pkg/datasources/usage_tracking_acceptance_test.go @@ -0,0 +1,84 @@ +package datasources_test + +import ( + "fmt" + "strings" + "testing" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/testenvs" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/collections" + "github.com/hashicorp/terraform-plugin-testing/terraform" + + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/helpers" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/tracking" + + acc "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/assert/resourceassert" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/config" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/acceptance/bettertestspoc/config/model" + "github.com/hashicorp/terraform-plugin-testing/tfversion" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" +) + +func TestAcc_CompleteUsageTracking(t *testing.T) { + _ = testenvs.GetOrSkipTest(t, testenvs.EnableAcceptance) + acc.TestAccPreCheck(t) + + id := acc.TestClient().Ids.RandomDatabaseObjectIdentifier() + schemaModel := model.Schema("test", id.DatabaseName(), id.Name()) + + assertQueryMetadataExists := func(t *testing.T, query string) resource.TestCheckFunc { + t.Helper() + return func(state *terraform.State) error { + queryHistory := acc.TestClient().InformationSchema.GetQueryHistory(t, 100) + expectedMetadata := tracking.NewVersionedDatasourceMetadata(datasources.Schemas) + if _, err := collections.FindFirst(queryHistory, func(history helpers.QueryHistory) bool { + metadata, err := tracking.ParseMetadata(history.QueryText) + return err == nil && + expectedMetadata == metadata && + strings.Contains(history.QueryText, query) + }); err != nil { + return fmt.Errorf("query history does not contain query metadata: %v with query containing: %s", expectedMetadata, query) + } + return nil + } + } + + resource.Test(t, resource.TestCase{ + ProtoV6ProviderFactories: acc.TestAccProtoV6ProviderFactories, + TerraformVersionChecks: []tfversion.TerraformVersionCheck{ + tfversion.RequireAbove(tfversion.Version1_5_0), + }, + PreCheck: func() { acc.TestAccPreCheck(t) }, + Steps: []resource.TestStep{ + { + Config: config.FromModel(t, schemaModel) + schemaDatasourceConfigWithDependency(schemaModel.ResourceReference(), id), + Check: assert.AssertThat(t, + resourceassert.SchemaResource(t, schemaModel.ResourceReference()). + HasNameString(id.Name()), + assert.Check(assertQueryMetadataExists(t, fmt.Sprintf(`SHOW SCHEMAS LIKE '%s' IN DATABASE "%s"`, id.Name(), id.DatabaseName()))), + // SHOW PARAMETERS IN SCHEMA "acc_test_db_AT_1AB7E1DE_1A10_89C3_C13C_899754A250B6"."FPGDHEAT_1AB7E1DE_1A10_89C3_C13C_899754A250B6" --terraform_provider_usage_tracking {"json_schema_version":"1","version":"v0.99.0","datasource":"snowflake_schemas","operation":"read"} + assert.Check(assertQueryMetadataExists(t, fmt.Sprintf(`SHOW PARAMETERS IN SCHEMA %s`, id.FullyQualifiedName()))), + assert.Check(assertQueryMetadataExists(t, fmt.Sprintf(`DESCRIBE SCHEMA %s`, id.FullyQualifiedName()))), + ), + }, + }, + }) +} + +func schemaDatasourceConfigWithDependency(schemaResourceReference string, id sdk.DatabaseObjectIdentifier) string { + return fmt.Sprintf(` +data "snowflake_schemas" "test" { + depends_on = [ %[1]s ] + in { + database = "%[2]s" + } + like = "%[3]s" +} +`, schemaResourceReference, id.DatabaseName(), id.Name()) +} diff --git a/pkg/datasources/users.go b/pkg/datasources/users.go index a2e576a2ac..5afe984886 100644 --- a/pkg/datasources/users.go +++ b/pkg/datasources/users.go @@ -3,6 +3,8 @@ package datasources import ( "context" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/schemas" @@ -91,7 +93,7 @@ var usersSchema = map[string]*schema.Schema{ func Users() *schema.Resource { return &schema.Resource{ - ReadContext: ReadUsers, + ReadContext: TrackingReadWrapper(datasources.Users, ReadUsers), Schema: usersSchema, Description: "Datasource used to get details of filtered users. Filtering is aligned with the current possibilities for [SHOW USERS](https://docs.snowflake.com/en/sql-reference/sql/show-users) query. The results of SHOW, DESCRIBE, and SHOW PARAMETERS IN are encapsulated in one output collection. Important note is that when querying users you don't have permissions to, the querying options are limited. You won't get almost any field in `show_output` (only empty or default values), the DESCRIBE command cannot be called, so you have to set `with_describe = false`. Only `parameters` output is not affected by the lack of privileges.", } diff --git a/pkg/datasources/views.go b/pkg/datasources/views.go index fa99c1becf..12a1bcdf14 100644 --- a/pkg/datasources/views.go +++ b/pkg/datasources/views.go @@ -3,6 +3,8 @@ package datasources import ( "context" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/schemas" @@ -106,7 +108,7 @@ var viewsSchema = map[string]*schema.Schema{ func Views() *schema.Resource { return &schema.Resource{ - ReadContext: ReadViews, + ReadContext: TrackingReadWrapper(datasources.Views, ReadViews), Schema: viewsSchema, Description: "Datasource used to get details of filtered views. Filtering is aligned with the current possibilities for [SHOW VIEWS](https://docs.snowflake.com/en/sql-reference/sql/show-views) query (only `like` is supported). The results of SHOW and DESCRIBE are encapsulated in one output collection `views`.", } diff --git a/pkg/datasources/warehouses.go b/pkg/datasources/warehouses.go index e52c09e9aa..9c42fb5e07 100644 --- a/pkg/datasources/warehouses.go +++ b/pkg/datasources/warehouses.go @@ -3,6 +3,8 @@ package datasources import ( "context" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/schemas" @@ -66,7 +68,7 @@ var warehousesSchema = map[string]*schema.Schema{ func Warehouses() *schema.Resource { return &schema.Resource{ - ReadContext: ReadWarehouses, + ReadContext: TrackingReadWrapper(datasources.Warehouses, ReadWarehouses), Schema: warehousesSchema, Description: "Datasource used to get details of filtered warehouses. Filtering is aligned with the current possibilities for [SHOW WAREHOUSES](https://docs.snowflake.com/en/sql-reference/sql/show-warehouses) query (only `like` is supported). The results of SHOW, DESCRIBE, and SHOW PARAMETERS IN are encapsulated in one output collection.", } diff --git a/pkg/internal/tracking/context.go b/pkg/internal/tracking/context.go index 9519bf1bb4..4f4045960c 100644 --- a/pkg/internal/tracking/context.go +++ b/pkg/internal/tracking/context.go @@ -4,12 +4,14 @@ import ( "context" "errors" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" ) const ( - ProviderVersion string = "v0.99.0" // TODO(SNOW-1814934): Currently hardcoded, make it computed - MetadataPrefix string = "terraform_provider_usage_tracking" + CurrentSchemaVersion string = "1" + ProviderVersion string = "v0.99.0" // TODO(SNOW-1814934): Currently hardcoded, make it computed + MetadataPrefix string = "terraform_provider_usage_tracking" ) type key struct{} @@ -28,18 +30,23 @@ const ( ) type Metadata struct { - Version string `json:"version,omitempty"` - Resource string `json:"resource,omitempty"` - Operation Operation `json:"operation,omitempty"` + SchemaVersion string `json:"json_schema_version,omitempty"` + Version string `json:"version,omitempty"` + Resource string `json:"resource,omitempty"` + Datasource string `json:"datasource,omitempty"` + Operation Operation `json:"operation,omitempty"` } func (m Metadata) validate() error { errs := make([]error, 0) + if m.SchemaVersion == "" { + errs = append(errs, errors.New("schema version for metadata should not be empty")) + } if m.Version == "" { - errs = append(errs, errors.New("version for metadata should not be empty")) + errs = append(errs, errors.New("provider version for metadata should not be empty")) } - if m.Resource == "" { - errs = append(errs, errors.New("resource name for metadata should not be empty")) + if m.Resource == "" && m.Datasource == "" { + errs = append(errs, errors.New("either resource or data source name for metadata should be specified")) } if m.Operation == "" { errs = append(errs, errors.New("operation for metadata should not be empty")) @@ -47,19 +54,31 @@ func (m Metadata) validate() error { return errors.Join(errs...) } -func NewMetadata(version string, resource resources.Resource, operation Operation) Metadata { +// NewTestMetadata is a helper constructor that is used only for testing purposes +func NewTestMetadata(version string, resource resources.Resource, operation Operation) Metadata { + return Metadata{ + SchemaVersion: CurrentSchemaVersion, + Version: version, + Resource: resource.String(), + Operation: operation, + } +} + +func NewVersionedResourceMetadata(resource resources.Resource, operation Operation) Metadata { return Metadata{ - Version: version, - Resource: resource.String(), - Operation: operation, + SchemaVersion: CurrentSchemaVersion, + Version: ProviderVersion, + Resource: resource.String(), + Operation: operation, } } -func NewVersionedMetadata(resource resources.Resource, operation Operation) Metadata { +func NewVersionedDatasourceMetadata(datasource datasources.Datasource) Metadata { return Metadata{ - Version: ProviderVersion, - Resource: resource.String(), - Operation: operation, + SchemaVersion: CurrentSchemaVersion, + Version: ProviderVersion, + Datasource: datasource.String(), + Operation: ReadOperation, } } diff --git a/pkg/internal/tracking/context_test.go b/pkg/internal/tracking/context_test.go index 96e38f75a3..cc929b4f64 100644 --- a/pkg/internal/tracking/context_test.go +++ b/pkg/internal/tracking/context_test.go @@ -4,13 +4,15 @@ import ( "context" "testing" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" + "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources" "github.com/stretchr/testify/require" ) func Test_Context(t *testing.T) { - metadata := NewMetadata("123", resources.Account, CreateOperation) - newMetadata := NewMetadata("321", resources.Database, UpdateOperation) + metadata := NewTestMetadata("123", resources.Account, CreateOperation) + newMetadata := NewVersionedDatasourceMetadata(datasources.Databases) ctx := context.Background() // no metadata in context diff --git a/pkg/internal/tracking/query_test.go b/pkg/internal/tracking/query_test.go index 6d46162186..43948b94cf 100644 --- a/pkg/internal/tracking/query_test.go +++ b/pkg/internal/tracking/query_test.go @@ -10,7 +10,7 @@ import ( ) func TestAppendMetadata(t *testing.T) { - metadata := NewMetadata("123", resources.Account, CreateOperation) + metadata := NewTestMetadata("123", resources.Account, CreateOperation) sql := "SELECT 1" bytes, err := json.Marshal(metadata) @@ -24,7 +24,7 @@ func TestAppendMetadata(t *testing.T) { } func TestParseMetadata(t *testing.T) { - metadata := NewMetadata("123", resources.Account, CreateOperation) + metadata := NewTestMetadata("123", resources.Account, CreateOperation) bytes, err := json.Marshal(metadata) require.NoError(t, err) sql := fmt.Sprintf("SELECT 1 --%s %s", MetadataPrefix, string(bytes)) @@ -38,8 +38,9 @@ func TestParseInvalidMetadataKeys(t *testing.T) { sql := fmt.Sprintf(`SELECT 1 --%s {"key": "value"}`, MetadataPrefix) parsedMetadata, err := ParseMetadata(sql) - require.ErrorContains(t, err, "version for metadata should not be empty") - require.ErrorContains(t, err, "resource name for metadata should not be empty") + require.ErrorContains(t, err, "schema version for metadata should not be empty") + require.ErrorContains(t, err, "provider version for metadata should not be empty") + require.ErrorContains(t, err, "either resource or data source name for metadata should be specified") require.ErrorContains(t, err, "operation for metadata should not be empty") require.Equal(t, Metadata{}, parsedMetadata) } @@ -53,7 +54,7 @@ func TestParseInvalidMetadataJson(t *testing.T) { } func TestParseMetadataFromInvalidSqlCommentPrefix(t *testing.T) { - metadata := NewMetadata("123", resources.Account, CreateOperation) + metadata := NewTestMetadata("123", resources.Account, CreateOperation) sql := "SELECT 1" bytes, err := json.Marshal(metadata) diff --git a/pkg/provider/datasources/datasources.go b/pkg/provider/datasources/datasources.go new file mode 100644 index 0000000000..56ea68b1c3 --- /dev/null +++ b/pkg/provider/datasources/datasources.go @@ -0,0 +1,63 @@ +package datasources + +type datasource string + +const ( + Accounts datasource = "snowflake_accounts" + Alerts datasource = "snowflake_alerts" + Connections datasource = "snowflake_connections" + CortexSearchServices datasource = "snowflake_cortex_search_services" + CurrentAccount datasource = "snowflake_current_account" + CurrentRole datasource = "snowflake_current_role" + Database datasource = "snowflake_database" + DatabaseRole datasource = "snowflake_database_role" + DatabaseRoles datasource = "snowflake_database_roles" + Databases datasource = "snowflake_databases" + DynamicTables datasource = "snowflake_dynamic_tables" + ExternalFunctions datasource = "snowflake_external_functions" + ExternalTables datasource = "snowflake_external_tables" + FailoverGroups datasource = "snowflake_failover_groups" + FileFormats datasource = "snowflake_file_formats" + Functions datasource = "snowflake_functions" + Grants datasource = "snowflake_grants" + MaskingPolicies datasource = "snowflake_masking_policies" + MaterializedViews datasource = "snowflake_materialized_views" + NetworkPolicies datasource = "snowflake_network_policies" + Parameters datasource = "snowflake_parameters" + Pipes datasource = "snowflake_pipes" + Procedures datasource = "snowflake_procedures" + ResourceMonitors datasource = "snowflake_resource_monitors" + Role datasource = "snowflake_role" + Roles datasource = "snowflake_roles" + RowAccessPolicies datasource = "snowflake_row_access_policies" + Schemas datasource = "snowflake_schemas" + Secrets datasource = "snowflake_secrets" + SecurityIntegrations datasource = "snowflake_security_integrations" + Sequences datasource = "snowflake_sequences" + Shares datasource = "snowflake_shares" + Stages datasource = "snowflake_stages" + StorageIntegrations datasource = "snowflake_storage_integrations" + Streams datasource = "snowflake_streams" + Streamlits datasource = "snowflake_streamlits" + SystemGenerateScimAccessToken datasource = "snowflake_system_generate_scim_access_token" + SystemGetAwsSnsIamPolicy datasource = "snowflake_system_get_aws_sns_iam_policy" + SystemGetPrivateLinkConfig datasource = "snowflake_system_get_privatelink_config" + SystemGetSnowflakePlatformInfo datasource = "snowflake_system_get_snowflake_platform_info" + Tables datasource = "snowflake_tables" + Tags datasource = "snowflake_tags" + Tasks datasource = "snowflake_tasks" + Users datasource = "snowflake_users" + Views datasource = "snowflake_views" + Warehouses datasource = "snowflake_warehouses" +) + +type Datasource interface { + xxxProtected() + String() string +} + +func (r datasource) xxxProtected() {} + +func (r datasource) String() string { + return string(r) +} diff --git a/pkg/resources/common.go b/pkg/resources/common.go index 36a1da648a..643524f9d9 100644 --- a/pkg/resources/common.go +++ b/pkg/resources/common.go @@ -108,42 +108,42 @@ func ImportName[T sdk.AccountObjectIdentifier | sdk.DatabaseObjectIdentifier | s func TrackingImportWrapper(resourceName resources.Resource, importImplementation schema.StateContextFunc) schema.StateContextFunc { return func(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) { - ctx = tracking.NewContext(ctx, tracking.NewVersionedMetadata(resourceName, tracking.ImportOperation)) + ctx = tracking.NewContext(ctx, tracking.NewVersionedResourceMetadata(resourceName, tracking.ImportOperation)) return importImplementation(ctx, d, meta) } } func TrackingCreateWrapper(resourceName resources.Resource, createImplementation schema.CreateContextFunc) schema.CreateContextFunc { return func(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { - ctx = tracking.NewContext(ctx, tracking.NewVersionedMetadata(resourceName, tracking.CreateOperation)) + ctx = tracking.NewContext(ctx, tracking.NewVersionedResourceMetadata(resourceName, tracking.CreateOperation)) return createImplementation(ctx, d, meta) } } func TrackingReadWrapper(resourceName resources.Resource, readImplementation schema.ReadContextFunc) schema.ReadContextFunc { return func(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { - ctx = tracking.NewContext(ctx, tracking.NewVersionedMetadata(resourceName, tracking.ReadOperation)) + ctx = tracking.NewContext(ctx, tracking.NewVersionedResourceMetadata(resourceName, tracking.ReadOperation)) return readImplementation(ctx, d, meta) } } func TrackingUpdateWrapper(resourceName resources.Resource, updateImplementation schema.UpdateContextFunc) schema.UpdateContextFunc { return func(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { - ctx = tracking.NewContext(ctx, tracking.NewVersionedMetadata(resourceName, tracking.UpdateOperation)) + ctx = tracking.NewContext(ctx, tracking.NewVersionedResourceMetadata(resourceName, tracking.UpdateOperation)) return updateImplementation(ctx, d, meta) } } func TrackingDeleteWrapper(resourceName resources.Resource, deleteImplementation schema.DeleteContextFunc) schema.DeleteContextFunc { return func(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { - ctx = tracking.NewContext(ctx, tracking.NewVersionedMetadata(resourceName, tracking.DeleteOperation)) + ctx = tracking.NewContext(ctx, tracking.NewVersionedResourceMetadata(resourceName, tracking.DeleteOperation)) return deleteImplementation(ctx, d, meta) } } func TrackingCustomDiffWrapper(resourceName resources.Resource, customdiffImplementation schema.CustomizeDiffFunc) schema.CustomizeDiffFunc { return func(ctx context.Context, diff *schema.ResourceDiff, meta any) error { - ctx = tracking.NewContext(ctx, tracking.NewVersionedMetadata(resourceName, tracking.CustomDiffOperation)) + ctx = tracking.NewContext(ctx, tracking.NewVersionedResourceMetadata(resourceName, tracking.CustomDiffOperation)) return customdiffImplementation(ctx, diff, meta) } } diff --git a/pkg/resources/usage_tracking_acceptance_test.go b/pkg/resources/usage_tracking_acceptance_test.go index a50dbc855f..fcd866afcd 100644 --- a/pkg/resources/usage_tracking_acceptance_test.go +++ b/pkg/resources/usage_tracking_acceptance_test.go @@ -38,14 +38,12 @@ func TestAcc_CompleteUsageTracking(t *testing.T) { t.Helper() return func(state *terraform.State) error { queryHistory := acc.TestClient().InformationSchema.GetQueryHistory(t, 60) - expectedMetadata := tracking.NewVersionedMetadata(resources.Schema, operation) + expectedMetadata := tracking.NewVersionedResourceMetadata(resources.Schema, operation) if _, err := collections.FindFirst(queryHistory, func(history helpers.QueryHistory) bool { - if metadata, err := tracking.ParseMetadata(history.QueryText); err == nil { - if expectedMetadata == metadata && strings.Contains(history.QueryText, query) { - return true - } - } - return false + metadata, err := tracking.ParseMetadata(history.QueryText) + return err == nil && + expectedMetadata == metadata && + strings.Contains(history.QueryText, query) }); err != nil { return fmt.Errorf("query history does not contain query metadata: %v with query containing: %s", expectedMetadata, query) } diff --git a/pkg/sdk/testint/client_integration_test.go b/pkg/sdk/testint/client_integration_test.go index 8cacd6b540..7d98396c0f 100644 --- a/pkg/sdk/testint/client_integration_test.go +++ b/pkg/sdk/testint/client_integration_test.go @@ -12,7 +12,7 @@ import ( func TestInt_Client_AdditionalMetadata(t *testing.T) { client := testClient(t) - metadata := tracking.NewMetadata("v1.13.1002-rc-test", resources.Database, tracking.CreateOperation) + metadata := tracking.NewTestMetadata("v1.13.1002-rc-test", resources.Database, tracking.CreateOperation) assertQueryMetadata := func(t *testing.T, queryId string) { t.Helper() From 62c747673a3f13adda2598133c8a5b6f1c13ea40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Cie=C5=9Blak?= Date: Tue, 26 Nov 2024 14:28:24 +0100 Subject: [PATCH 2/3] Changes after review --- pkg/datasources/common.go | 2 +- pkg/datasources/tasks.go | 2 -- pkg/internal/tracking/context.go | 4 ++-- pkg/internal/tracking/context_test.go | 2 +- pkg/internal/tracking/query_test.go | 6 +++--- pkg/sdk/testint/client_integration_test.go | 2 +- pkg/sdk/testint/dynamic_table_integration_test.go | 2 +- pkg/sdk/testint/materialized_views_gen_integration_test.go | 2 +- pkg/sdk/testint/views_gen_integration_test.go | 2 +- 9 files changed, 11 insertions(+), 13 deletions(-) diff --git a/pkg/datasources/common.go b/pkg/datasources/common.go index 7f4b27d566..9b4f354dda 100644 --- a/pkg/datasources/common.go +++ b/pkg/datasources/common.go @@ -2,10 +2,10 @@ package datasources import ( "context" + "fmt" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/tracking" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/datasources" - "fmt" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" diff --git a/pkg/datasources/tasks.go b/pkg/datasources/tasks.go index 69ba8b7fa7..356556dbf7 100644 --- a/pkg/datasources/tasks.go +++ b/pkg/datasources/tasks.go @@ -9,8 +9,6 @@ import ( "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/provider" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/resources" "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/schemas" - "github.com/hashicorp/terraform-plugin-sdk/v2/diag" - "github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" ) diff --git a/pkg/internal/tracking/context.go b/pkg/internal/tracking/context.go index 4f4045960c..8fdbf8c03f 100644 --- a/pkg/internal/tracking/context.go +++ b/pkg/internal/tracking/context.go @@ -54,8 +54,8 @@ func (m Metadata) validate() error { return errors.Join(errs...) } -// NewTestMetadata is a helper constructor that is used only for testing purposes -func NewTestMetadata(version string, resource resources.Resource, operation Operation) Metadata { +// newTestMetadata is a helper constructor that is used only for testing purposes +func newTestMetadata(version string, resource resources.Resource, operation Operation) Metadata { return Metadata{ SchemaVersion: CurrentSchemaVersion, Version: version, diff --git a/pkg/internal/tracking/context_test.go b/pkg/internal/tracking/context_test.go index cc929b4f64..18bb38d588 100644 --- a/pkg/internal/tracking/context_test.go +++ b/pkg/internal/tracking/context_test.go @@ -11,7 +11,7 @@ import ( ) func Test_Context(t *testing.T) { - metadata := NewTestMetadata("123", resources.Account, CreateOperation) + metadata := newTestMetadata("123", resources.Account, CreateOperation) newMetadata := NewVersionedDatasourceMetadata(datasources.Databases) ctx := context.Background() diff --git a/pkg/internal/tracking/query_test.go b/pkg/internal/tracking/query_test.go index d40261477c..83696b928b 100644 --- a/pkg/internal/tracking/query_test.go +++ b/pkg/internal/tracking/query_test.go @@ -47,7 +47,7 @@ func TestTrimMetadata(t *testing.T) { } func TestAppendMetadata(t *testing.T) { - metadata := NewTestMetadata("123", resources.Account, CreateOperation) + metadata := newTestMetadata("123", resources.Account, CreateOperation) sql := "SELECT 1" bytes, err := json.Marshal(metadata) @@ -61,7 +61,7 @@ func TestAppendMetadata(t *testing.T) { } func TestParseMetadata(t *testing.T) { - metadata := NewTestMetadata("123", resources.Account, CreateOperation) + metadata := newTestMetadata("123", resources.Account, CreateOperation) bytes, err := json.Marshal(metadata) require.NoError(t, err) sql := fmt.Sprintf("SELECT 1 --%s %s", MetadataPrefix, string(bytes)) @@ -91,7 +91,7 @@ func TestParseInvalidMetadataJson(t *testing.T) { } func TestParseMetadataFromInvalidSqlCommentPrefix(t *testing.T) { - metadata := NewTestMetadata("123", resources.Account, CreateOperation) + metadata := newTestMetadata("123", resources.Account, CreateOperation) sql := "SELECT 1" bytes, err := json.Marshal(metadata) diff --git a/pkg/sdk/testint/client_integration_test.go b/pkg/sdk/testint/client_integration_test.go index 7d98396c0f..1f0a3875df 100644 --- a/pkg/sdk/testint/client_integration_test.go +++ b/pkg/sdk/testint/client_integration_test.go @@ -12,7 +12,7 @@ import ( func TestInt_Client_AdditionalMetadata(t *testing.T) { client := testClient(t) - metadata := tracking.NewTestMetadata("v1.13.1002-rc-test", resources.Database, tracking.CreateOperation) + metadata := tracking.Metadata{Version: "v1.13.1002-rc-test", Resource: resources.Database.String(), Operation: tracking.CreateOperation} assertQueryMetadata := func(t *testing.T, queryId string) { t.Helper() diff --git a/pkg/sdk/testint/dynamic_table_integration_test.go b/pkg/sdk/testint/dynamic_table_integration_test.go index b7025d4e04..8eb130486c 100644 --- a/pkg/sdk/testint/dynamic_table_integration_test.go +++ b/pkg/sdk/testint/dynamic_table_integration_test.go @@ -56,7 +56,7 @@ func TestInt_DynamicTableCreateAndDrop(t *testing.T) { t.Run("create with usage tracking comment", func(t *testing.T) { id := testClientHelper().Ids.RandomSchemaObjectIdentifier() plainQuery := fmt.Sprintf("SELECT id FROM %s", tableTest.ID().FullyQualifiedName()) - query, err := tracking.AppendMetadata(plainQuery, tracking.NewVersionedMetadata(resources.DynamicTable, tracking.CreateOperation)) + query, err := tracking.AppendMetadata(plainQuery, tracking.NewVersionedResourceMetadata(resources.DynamicTable, tracking.CreateOperation)) require.NoError(t, err) err = client.DynamicTables.Create(ctx, sdk.NewCreateDynamicTableRequest(id, testClientHelper().Ids.WarehouseId(), sdk.TargetLag{ diff --git a/pkg/sdk/testint/materialized_views_gen_integration_test.go b/pkg/sdk/testint/materialized_views_gen_integration_test.go index 6ef58925e9..6cad7bcb93 100644 --- a/pkg/sdk/testint/materialized_views_gen_integration_test.go +++ b/pkg/sdk/testint/materialized_views_gen_integration_test.go @@ -115,7 +115,7 @@ func TestInt_MaterializedViews(t *testing.T) { t.Run("create materialized view: with usage tracking comment", func(t *testing.T) { id := testClientHelper().Ids.RandomSchemaObjectIdentifier() plainQuery := fmt.Sprintf("SELECT id FROM %s", table.ID().FullyQualifiedName()) - query, err := tracking.AppendMetadata(plainQuery, tracking.NewVersionedMetadata(resources.MaterializedView, tracking.CreateOperation)) + query, err := tracking.AppendMetadata(plainQuery, tracking.NewVersionedResourceMetadata(resources.MaterializedView, tracking.CreateOperation)) require.NoError(t, err) view := createMaterializedViewWithRequest(t, sdk.NewCreateMaterializedViewRequest(id, query)) diff --git a/pkg/sdk/testint/views_gen_integration_test.go b/pkg/sdk/testint/views_gen_integration_test.go index 3682a3ee09..df8db37ab8 100644 --- a/pkg/sdk/testint/views_gen_integration_test.go +++ b/pkg/sdk/testint/views_gen_integration_test.go @@ -172,7 +172,7 @@ func TestInt_Views(t *testing.T) { t.Run("create view: with usage tracking comment", func(t *testing.T) { id := testClientHelper().Ids.RandomSchemaObjectIdentifier() plainQuery := "SELECT NULL AS TYPE" - query, err := tracking.AppendMetadata(plainQuery, tracking.NewVersionedMetadata(resources.View, tracking.CreateOperation)) + query, err := tracking.AppendMetadata(plainQuery, tracking.NewVersionedResourceMetadata(resources.View, tracking.CreateOperation)) require.NoError(t, err) request := sdk.NewCreateViewRequest(id, query) From 0685049a81c2323738b9a8ef7ac945e21512def7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Cie=C5=9Blak?= Date: Tue, 26 Nov 2024 16:09:00 +0100 Subject: [PATCH 3/3] Fix test --- pkg/sdk/testint/client_integration_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/sdk/testint/client_integration_test.go b/pkg/sdk/testint/client_integration_test.go index 1f0a3875df..b670fbfe34 100644 --- a/pkg/sdk/testint/client_integration_test.go +++ b/pkg/sdk/testint/client_integration_test.go @@ -12,7 +12,7 @@ import ( func TestInt_Client_AdditionalMetadata(t *testing.T) { client := testClient(t) - metadata := tracking.Metadata{Version: "v1.13.1002-rc-test", Resource: resources.Database.String(), Operation: tracking.CreateOperation} + metadata := tracking.Metadata{SchemaVersion: "1", Version: "v1.13.1002-rc-test", Resource: resources.Database.String(), Operation: tracking.CreateOperation} assertQueryMetadata := func(t *testing.T, queryId string) { t.Helper()