Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Add support for usage tracking to data sources #3224

Merged
merged 4 commits into from
Nov 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/datasources/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ 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"

"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"
Expand Down
7 changes: 3 additions & 4 deletions pkg/datasources/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -62,11 +60,13 @@ func Tasks() *schema.Resource {
return &schema.Resource{
ReadContext: TrackingReadWrapper(datasources.Tasks, ReadTasks),
Schema: tasksSchema,
Description: "Data source used to get details of filtered tasks. Filtering is aligned with the current possibilities for [SHOW TASKS](https://docs.snowflake.com/en/sql-reference/sql/show-tasks) query. The results of SHOW and SHOW PARAMETERS IN are encapsulated in one output collection `tasks`.",
}
}

func ReadTasks(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
client := meta.(*provider.Context).Client
req := sdk.NewShowTaskRequest()

handleLike(d, &req.Like)
if err := handleExtendedIn(d, &req.In); err != nil {
Expand Down Expand Up @@ -107,6 +107,5 @@ func ReadTasks(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagn
return diag.FromErr(err)
}

d.SetId(fmt.Sprintf(`%v|%v`, databaseName, schemaName))
return diag.FromErr(d.Set("tasks", tasks))
return nil
}
4 changes: 2 additions & 2 deletions pkg/internal/tracking/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

it was supposed to go to a test package :sadeg:

return Metadata{
SchemaVersion: CurrentSchemaVersion,
Version: version,
Expand Down
2 changes: 1 addition & 1 deletion pkg/internal/tracking/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
6 changes: 3 additions & 3 deletions pkg/internal/tracking/query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion pkg/sdk/testint/client_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{SchemaVersion: "1", Version: "v1.13.1002-rc-test", Resource: resources.Database.String(), Operation: tracking.CreateOperation}

assertQueryMetadata := func(t *testing.T, queryId string) {
t.Helper()
Expand Down
2 changes: 1 addition & 1 deletion pkg/sdk/testint/dynamic_table_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
2 changes: 1 addition & 1 deletion pkg/sdk/testint/materialized_views_gen_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion pkg/sdk/testint/views_gen_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
Loading