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: basic object tracking part 2 #3214

Merged
merged 3 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 28 additions & 9 deletions pkg/acceptance/helpers/information_schema_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"testing"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/collections"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"github.com/stretchr/testify/require"
)
Expand All @@ -25,20 +27,37 @@ func (c *InformationSchemaClient) client() *sdk.Client {
return c.context.client
}

func (c *InformationSchemaClient) GetQueryTextByQueryId(t *testing.T, queryId string) string {
type QueryHistory struct {
QueryId string
QueryText string
QueryTag string
}

func (c *InformationSchemaClient) GetQueryHistory(t *testing.T, limit int) []QueryHistory {
t.Helper()
result, err := c.client().QueryUnsafe(context.Background(), fmt.Sprintf("SELECT QUERY_TEXT FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY(RESULT_LIMIT => 20)) WHERE QUERY_ID = '%s'", queryId))
result, err := c.client().QueryUnsafe(context.Background(), fmt.Sprintf("SELECT * FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY(RESULT_LIMIT => %d))", limit))
require.NoError(t, err)
require.Len(t, result, 1)
require.NotNil(t, result[0]["QUERY_TEXT"])
return (*result[0]["QUERY_TEXT"]).(string)
return collections.Map(result, func(m map[string]*any) QueryHistory {
require.NotNil(t, m["QUERY_ID"])
require.NotNil(t, m["QUERY_TEXT"])
require.NotNil(t, m["QUERY_TAG"])
return QueryHistory{
QueryId: (*m["QUERY_ID"]).(string),
QueryText: (*m["QUERY_TEXT"]).(string),
QueryTag: (*m["QUERY_TAG"]).(string),
}
})
}

func (c *InformationSchemaClient) GetQueryTagByQueryId(t *testing.T, queryId string) string {
func (c *InformationSchemaClient) GetQueryHistoryByQueryId(t *testing.T, limit int, queryId string) QueryHistory {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can agree on a default limit here and avoid specifying it in each call.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I would leave it as a value to specify as I changed this a lot during testing and we should aim for as little number as possible

t.Helper()
result, err := c.client().QueryUnsafe(context.Background(), fmt.Sprintf("SELECT QUERY_TAG FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY(RESULT_LIMIT => 20)) WHERE QUERY_ID = '%s'", queryId))
result, err := c.client().QueryUnsafe(context.Background(), fmt.Sprintf("SELECT QUERY_TEXT FROM TABLE(INFORMATION_SCHEMA.QUERY_HISTORY(RESULT_LIMIT => %d)) WHERE QUERY_ID = '%s'", limit, queryId))
require.NoError(t, err)
require.Len(t, result, 1)
require.NotNil(t, result[0]["QUERY_TAG"])
return (*result[0]["QUERY_TAG"]).(string)
require.NotNil(t, result[0]["QUERY_TEXT"])
return QueryHistory{
QueryId: (*result[0]["QUERY_ID"]).(string),
QueryText: (*result[0]["QUERY_TEXT"]).(string),
QueryTag: (*result[0]["QUERY_TAG"]).(string),
}
}
9 changes: 9 additions & 0 deletions pkg/provider/resources/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ const (
ExternalVolume resource = "snowflake_external_volume"
FailoverGroup resource = "snowflake_failover_group"
FileFormat resource = "snowflake_file_format"
GrantApplicationRole resource = "snowflake_grant_application_role"
GrantAccountRole resource = "snowflake_grant_account_role"
GrantDatabaseRole resource = "snowflake_grant_database_role"
GrantOwnership resource = "snowflake_grant_ownership"
GrantPrivilegesToAccountRole resource = "snowflake_grant_privileges_to_account_role"
GrantPrivilegesToDatabaseRole resource = "snowflake_grant_privileges_to_database_role"
GrantPrivilegesToShare resource = "snowflake_grant_privileges_to_share"
Function resource = "snowflake_function"
LegacyServiceUser resource = "snowflake_legacy_service_user"
ManagedAccount resource = "snowflake_managed_account"
Expand Down Expand Up @@ -64,7 +71,9 @@ const (
Table resource = "snowflake_table"
Tag resource = "snowflake_tag"
TagAssociation resource = "snowflake_tag_association"
TagMaskingPolicyAssociation resource = "snowflake_tag_masking_policy_association"
Task resource = "snowflake_task"
UnsafeExecute resource = "snowflake_unsafe_execute"
User resource = "snowflake_user"
View resource = "snowflake_view"
Warehouse resource = "snowflake_warehouse"
Expand Down
6 changes: 4 additions & 2 deletions pkg/resources/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strings"
"time"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources"

"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/internal/util"
Expand Down Expand Up @@ -216,9 +218,9 @@ func Account() *schema.Resource {
Update: UpdateAccount,
Delete: DeleteAccount,

CustomizeDiff: customdiff.All(
CustomizeDiff: TrackingCustomDiffWrapper(resources.Account, customdiff.All(
ComputedIfAnyAttributeChanged(accountSchema, FullyQualifiedNameAttributeName, "name"),
),
)),

Schema: accountSchema,
Importer: &schema.ResourceImporter{
Expand Down
16 changes: 9 additions & 7 deletions pkg/resources/account_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"errors"
"fmt"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources"

"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/schemas"
Expand Down Expand Up @@ -43,19 +45,19 @@ func AccountRole() *schema.Resource {
return &schema.Resource{
Schema: accountRoleSchema,

CreateContext: CreateAccountRole,
ReadContext: ReadAccountRole,
DeleteContext: DeleteAccountRole,
UpdateContext: UpdateAccountRole,
CreateContext: TrackingCreateWrapper(resources.AccountRole, CreateAccountRole),
ReadContext: TrackingReadWrapper(resources.AccountRole, ReadAccountRole),
DeleteContext: TrackingDeleteWrapper(resources.AccountRole, DeleteAccountRole),
UpdateContext: TrackingUpdateWrapper(resources.AccountRole, UpdateAccountRole),
Description: "The resource is used for role management, where roles can be assigned privileges and, in turn, granted to users and other roles. When granted to roles they can create hierarchies of privilege structures. For more details, refer to the [official documentation](https://docs.snowflake.com/en/user-guide/security-access-control-overview).",

CustomizeDiff: customdiff.All(
CustomizeDiff: TrackingCustomDiffWrapper(resources.AccountRole, customdiff.All(
ComputedIfAnyAttributeChanged(accountRoleSchema, ShowOutputAttributeName, "comment", "name"),
ComputedIfAnyAttributeChanged(accountRoleSchema, FullyQualifiedNameAttributeName, "name"),
),
)),

Importer: &schema.ResourceImporter{
StateContext: ImportName[sdk.AccountObjectIdentifier],
StateContext: TrackingImportWrapper(resources.AccountRole, ImportName[sdk.AccountObjectIdentifier]),
},
}
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/resources/alert.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"strings"
"time"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources"

"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/internal/util"
Expand Down Expand Up @@ -110,14 +112,14 @@ var alertSchema = map[string]*schema.Schema{
// Alert returns a pointer to the resource representing an alert.
func Alert() *schema.Resource {
return &schema.Resource{
CreateContext: CreateAlert,
ReadContext: ReadAlert,
UpdateContext: UpdateAlert,
DeleteContext: DeleteAlert,
CreateContext: TrackingCreateWrapper(resources.Alert, CreateAlert),
ReadContext: TrackingReadWrapper(resources.Alert, ReadAlert),
UpdateContext: TrackingUpdateWrapper(resources.Alert, UpdateAlert),
DeleteContext: TrackingDeleteWrapper(resources.Alert, DeleteAlert),

Schema: alertSchema,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
StateContext: TrackingImportWrapper(resources.Alert, schema.ImportStatePassthroughContext),
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"reflect"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/collections"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/logging"
Expand Down Expand Up @@ -36,24 +38,24 @@ var apiAuthAuthorizationCodeGrantSchema = func() map[string]*schema.Schema {

func ApiAuthenticationIntegrationWithAuthorizationCodeGrant() *schema.Resource {
return &schema.Resource{
CreateContext: CreateContextApiAuthenticationIntegrationWithAuthorizationCodeGrant,
ReadContext: ReadContextApiAuthenticationIntegrationWithAuthorizationCodeGrant(true),
UpdateContext: UpdateContextApiAuthenticationIntegrationWithAuthorizationCodeGrant,
DeleteContext: DeleteContextApiAuthenticationIntegrationWithAuthorizationCodeGrant,
CreateContext: TrackingCreateWrapper(resources.ApiAuthenticationIntegrationWithAuthorizationCodeGrant, CreateContextApiAuthenticationIntegrationWithAuthorizationCodeGrant),
ReadContext: TrackingReadWrapper(resources.ApiAuthenticationIntegrationWithAuthorizationCodeGrant, ReadContextApiAuthenticationIntegrationWithAuthorizationCodeGrant(true)),
UpdateContext: TrackingUpdateWrapper(resources.ApiAuthenticationIntegrationWithAuthorizationCodeGrant, UpdateContextApiAuthenticationIntegrationWithAuthorizationCodeGrant),
DeleteContext: TrackingDeleteWrapper(resources.ApiAuthenticationIntegrationWithAuthorizationCodeGrant, DeleteContextApiAuthenticationIntegrationWithAuthorizationCodeGrant),
Description: "Resource used to manage api authentication security integration objects with authorization code grant. For more information, check [security integrations documentation](https://docs.snowflake.com/en/sql-reference/sql/create-security-integration-api-auth).",

CustomizeDiff: customdiff.All(
CustomizeDiff: TrackingCustomDiffWrapper(resources.ApiAuthenticationIntegrationWithAuthorizationCodeGrant, customdiff.All(
ForceNewIfChangeToEmptyString("oauth_token_endpoint"),
ForceNewIfChangeToEmptyString("oauth_authorization_endpoint"),
ForceNewIfChangeToEmptyString("oauth_client_auth_method"),
ComputedIfAnyAttributeChanged(apiAuthAuthorizationCodeGrantSchema, ShowOutputAttributeName, "enabled", "comment"),
ComputedIfAnyAttributeChanged(apiAuthAuthorizationCodeGrantSchema, DescribeOutputAttributeName, "enabled", "comment", "oauth_access_token_validity", "oauth_refresh_token_validity",
"oauth_client_id", "oauth_client_auth_method", "oauth_authorization_endpoint",
"oauth_token_endpoint", "oauth_allowed_scopes"),
),
)),
Schema: apiAuthAuthorizationCodeGrantSchema,
Importer: &schema.ResourceImporter{
StateContext: ImportApiAuthenticationWithAuthorizationCodeGrant,
StateContext: TrackingImportWrapper(resources.ApiAuthenticationIntegrationWithAuthorizationCodeGrant, ImportApiAuthenticationWithAuthorizationCodeGrant),
},
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"reflect"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/collections"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/logging"
Expand All @@ -31,22 +33,22 @@ var apiAuthClientCredentialsSchema = func() map[string]*schema.Schema {

func ApiAuthenticationIntegrationWithClientCredentials() *schema.Resource {
return &schema.Resource{
CreateContext: CreateContextApiAuthenticationIntegrationWithClientCredentials,
ReadContext: ReadContextApiAuthenticationIntegrationWithClientCredentials(true),
UpdateContext: UpdateContextApiAuthenticationIntegrationWithClientCredentials,
DeleteContext: DeleteContextApiAuthenticationIntegrationWithClientCredentials,
CreateContext: TrackingCreateWrapper(resources.ApiAuthenticationIntegrationWithClientCredentials, CreateContextApiAuthenticationIntegrationWithClientCredentials),
ReadContext: TrackingReadWrapper(resources.ApiAuthenticationIntegrationWithClientCredentials, ReadContextApiAuthenticationIntegrationWithClientCredentials(true)),
UpdateContext: TrackingUpdateWrapper(resources.ApiAuthenticationIntegrationWithClientCredentials, UpdateContextApiAuthenticationIntegrationWithClientCredentials),
DeleteContext: TrackingDeleteWrapper(resources.ApiAuthenticationIntegrationWithClientCredentials, DeleteContextApiAuthenticationIntegrationWithClientCredentials),
Description: "Resource used to manage api authentication security integration objects with client credentials. For more information, check [security integrations documentation](https://docs.snowflake.com/en/sql-reference/sql/create-security-integration-api-auth).",

Schema: apiAuthClientCredentialsSchema,
CustomizeDiff: customdiff.All(
CustomizeDiff: TrackingCustomDiffWrapper(resources.ApiAuthenticationIntegrationWithClientCredentials, customdiff.All(
ForceNewIfChangeToEmptyString("oauth_token_endpoint"),
ForceNewIfChangeToEmptyString("oauth_client_auth_method"),
ComputedIfAnyAttributeChanged(apiAuthClientCredentialsSchema, ShowOutputAttributeName, "enabled", "comment"),
ComputedIfAnyAttributeChanged(apiAuthClientCredentialsSchema, DescribeOutputAttributeName, "enabled", "comment", "oauth_access_token_validity", "oauth_refresh_token_validity",
"oauth_client_id", "oauth_client_auth_method", "oauth_token_endpoint", "oauth_allowed_scopes"),
),
)),
Importer: &schema.ResourceImporter{
StateContext: ImportApiAuthenticationWithClientCredentials,
StateContext: TrackingImportWrapper(resources.ApiAuthenticationIntegrationWithClientCredentials, ImportApiAuthenticationWithClientCredentials),
},
}
}
Expand Down
16 changes: 9 additions & 7 deletions pkg/resources/api_authentication_integration_with_jwt_bearer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"reflect"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/collections"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/logging"
Expand Down Expand Up @@ -34,24 +36,24 @@ var apiAuthJwtBearerSchema = func() map[string]*schema.Schema {

func ApiAuthenticationIntegrationWithJwtBearer() *schema.Resource {
return &schema.Resource{
CreateContext: CreateContextApiAuthenticationIntegrationWithJwtBearer,
ReadContext: ReadContextApiAuthenticationIntegrationWithJwtBearer(true),
UpdateContext: UpdateContextApiAuthenticationIntegrationWithJwtBearer,
DeleteContext: DeleteContextApiAuthenticationIntegrationWithJwtBearer,
CreateContext: TrackingCreateWrapper(resources.ApiAuthenticationIntegrationWithJwtBearer, CreateContextApiAuthenticationIntegrationWithJwtBearer),
ReadContext: TrackingReadWrapper(resources.ApiAuthenticationIntegrationWithJwtBearer, ReadContextApiAuthenticationIntegrationWithJwtBearer(true)),
UpdateContext: TrackingUpdateWrapper(resources.ApiAuthenticationIntegrationWithJwtBearer, UpdateContextApiAuthenticationIntegrationWithJwtBearer),
DeleteContext: TrackingDeleteWrapper(resources.ApiAuthenticationIntegrationWithJwtBearer, DeleteContextApiAuthenticationIntegrationWithJwtBearer),
Description: "Resource used to manage api authentication security integration objects with jwt bearer. For more information, check [security integrations documentation](https://docs.snowflake.com/en/sql-reference/sql/create-security-integration-api-auth).",

Schema: apiAuthJwtBearerSchema,
CustomizeDiff: customdiff.All(
CustomizeDiff: TrackingCustomDiffWrapper(resources.ApiAuthenticationIntegrationWithJwtBearer, customdiff.All(
ForceNewIfChangeToEmptyString("oauth_token_endpoint"),
ForceNewIfChangeToEmptyString("oauth_authorization_endpoint"),
ForceNewIfChangeToEmptyString("oauth_client_auth_method"),
ComputedIfAnyAttributeChanged(apiAuthJwtBearerSchema, ShowOutputAttributeName, "enabled", "comment"),
ComputedIfAnyAttributeChanged(apiAuthJwtBearerSchema, DescribeOutputAttributeName, "enabled", "comment", "oauth_access_token_validity", "oauth_refresh_token_validity",
"oauth_client_id", "oauth_client_auth_method", "oauth_authorization_endpoint",
"oauth_token_endpoint", "oauth_assertion_issuer"),
"oauth_token_endpoint", "oauth_assertion_issuer")),
),
Importer: &schema.ResourceImporter{
StateContext: ImportApiAuthenticationWithJwtBearer,
StateContext: TrackingImportWrapper(resources.ApiAuthenticationIntegrationWithJwtBearer, ImportApiAuthenticationWithJwtBearer),
},
}
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/resources/authentication_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"fmt"
"reflect"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/helpers"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/collections"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/internal/logging"
Expand Down Expand Up @@ -108,15 +110,15 @@ var authenticationPolicySchema = map[string]*schema.Schema{
// AuthenticationPolicy returns a pointer to the resource representing an authentication policy.
func AuthenticationPolicy() *schema.Resource {
return &schema.Resource{
CreateContext: CreateContextAuthenticationPolicy,
ReadContext: ReadContextAuthenticationPolicy,
UpdateContext: UpdateContextAuthenticationPolicy,
DeleteContext: DeleteContextAuthenticationPolicy,
CreateContext: TrackingCreateWrapper(resources.AuthenticationPolicy, CreateContextAuthenticationPolicy),
ReadContext: TrackingReadWrapper(resources.AuthenticationPolicy, ReadContextAuthenticationPolicy),
UpdateContext: TrackingUpdateWrapper(resources.AuthenticationPolicy, UpdateContextAuthenticationPolicy),
DeleteContext: TrackingDeleteWrapper(resources.AuthenticationPolicy, DeleteContextAuthenticationPolicy),
Description: "Resource used to manage authentication policy objects. For more information, check [authentication policy documentation](https://docs.snowflake.com/en/sql-reference/sql/create-authentication-policy).",

Schema: authenticationPolicySchema,
Importer: &schema.ResourceImporter{
StateContext: ImportAuthenticationPolicy,
StateContext: TrackingImportWrapper(resources.AuthenticationPolicy, ImportAuthenticationPolicy),
},
}
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/resources/cortex_search_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"log"
"time"

"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/provider/resources"

"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/schemas"
Expand Down Expand Up @@ -81,10 +83,10 @@ var cortexSearchServiceSchema = map[string]*schema.Schema{
// CortexSearchService returns a pointer to the resource representing a Cortex search service.
func CortexSearchService() *schema.Resource {
return &schema.Resource{
CreateContext: CreateCortexSearchService,
ReadContext: ReadCortexSearchService,
UpdateContext: UpdateCortexSearchService,
DeleteContext: DeleteCortexSearchService,
CreateContext: TrackingCreateWrapper(resources.CortexSearchService, CreateCortexSearchService),
ReadContext: TrackingReadWrapper(resources.CortexSearchService, ReadCortexSearchService),
UpdateContext: TrackingUpdateWrapper(resources.CortexSearchService, UpdateCortexSearchService),
DeleteContext: TrackingDeleteWrapper(resources.CortexSearchService, DeleteCortexSearchService),

Schema: cortexSearchServiceSchema,
Importer: &schema.ResourceImporter{
Expand Down
Loading
Loading