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: apply identifier conventions to grants #3008

Merged
merged 11 commits into from
Aug 23, 2024
84 changes: 70 additions & 14 deletions pkg/datasources/grants.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,35 +396,59 @@ func buildOptsForGrantsTo(grantsTo map[string]any) (*sdk.ShowGrantOptions, error
opts := new(sdk.ShowGrantOptions)

if application := grantsTo["application"].(string); application != "" {
applicationId, err := sdk.ParseAccountObjectIdentifier(application)
if err != nil {
return nil, err
}
opts.To = &sdk.ShowGrantsTo{
Application: sdk.NewAccountObjectIdentifier(application),
Application: applicationId,
}
}
if applicationRole := grantsTo["application_role"].(string); applicationRole != "" {
applicationRoleId, err := sdk.ParseDatabaseObjectIdentifier(applicationRole)
if err != nil {
return nil, err
}
opts.To = &sdk.ShowGrantsTo{
ApplicationRole: sdk.NewDatabaseObjectIdentifierFromFullyQualifiedName(applicationRole),
ApplicationRole: applicationRoleId,
}
}
if accountRole := grantsTo["account_role"].(string); accountRole != "" {
accountRoleId, err := sdk.ParseAccountObjectIdentifier(accountRole)
if err != nil {
return nil, err
}
opts.To = &sdk.ShowGrantsTo{
Role: sdk.NewAccountObjectIdentifier(accountRole),
Role: accountRoleId,
}
}
if databaseRole := grantsTo["database_role"].(string); databaseRole != "" {
databaseRoleId, err := sdk.ParseDatabaseObjectIdentifier(databaseRole)
if err != nil {
return nil, err
}
opts.To = &sdk.ShowGrantsTo{
DatabaseRole: sdk.NewDatabaseObjectIdentifierFromFullyQualifiedName(databaseRole),
DatabaseRole: databaseRoleId,
}
}
if user := grantsTo["user"].(string); user != "" {
userId, err := sdk.ParseAccountObjectIdentifier(user)
if err != nil {
return nil, err
}
opts.To = &sdk.ShowGrantsTo{
User: sdk.NewAccountObjectIdentifier(user),
User: userId,
}
}
if share := grantsTo["share"]; share != nil && len(share.([]any)) > 0 {
shareMap := share.([]any)[0].(map[string]any)
sharedId, err := sdk.ParseAccountObjectIdentifier(shareMap["share_name"].(string))
if err != nil {
return nil, err
}
opts.To = &sdk.ShowGrantsTo{
Share: &sdk.ShowGrantsToShare{
Name: sdk.NewAccountObjectIdentifier(shareMap["share_name"].(string)),
Name: sharedId,
},
}
// TODO [SNOW-1284382]: Uncomment after SHOW GRANTS TO SHARE <share_name> IN APPLICATION PACKAGE <app_package_name> syntax starts working.
Expand All @@ -439,23 +463,39 @@ func buildOptsForGrantsOf(grantsOf map[string]any) (*sdk.ShowGrantOptions, error
opts := new(sdk.ShowGrantOptions)

if accountRole := grantsOf["account_role"].(string); accountRole != "" {
accountRoleId, err := sdk.ParseAccountObjectIdentifier(accountRole)
if err != nil {
return nil, err
}
opts.Of = &sdk.ShowGrantsOf{
Role: sdk.NewAccountObjectIdentifier(accountRole),
Role: accountRoleId,
}
}
if databaseRole := grantsOf["database_role"].(string); databaseRole != "" {
databaseRoleId, err := sdk.ParseDatabaseObjectIdentifier(databaseRole)
if err != nil {
return nil, err
}
opts.Of = &sdk.ShowGrantsOf{
DatabaseRole: sdk.NewDatabaseObjectIdentifierFromFullyQualifiedName(databaseRole),
DatabaseRole: databaseRoleId,
}
}
if applicationRole := grantsOf["application_role"].(string); applicationRole != "" {
applicationRoleId, err := sdk.ParseDatabaseObjectIdentifier(applicationRole)
if err != nil {
return nil, err
}
opts.Of = &sdk.ShowGrantsOf{
ApplicationRole: sdk.NewDatabaseObjectIdentifierFromFullyQualifiedName(applicationRole),
ApplicationRole: applicationRoleId,
}
}
if share := grantsOf["share"].(string); share != "" {
shareId, err := sdk.ParseAccountObjectIdentifier(share)
if err != nil {
return nil, err
}
opts.Of = &sdk.ShowGrantsOf{
Share: sdk.NewAccountObjectIdentifier(share),
Share: shareId,
}
}
return opts, nil
Expand All @@ -466,13 +506,21 @@ func buildOptsForFutureGrantsIn(futureGrantsIn map[string]any) (*sdk.ShowGrantOp
opts.Future = sdk.Bool(true)

if db := futureGrantsIn["database"].(string); db != "" {
databaseId, err := sdk.ParseAccountObjectIdentifier(db)
if err != nil {
return nil, err
}
opts.In = &sdk.ShowGrantsIn{
Database: sdk.Pointer(sdk.NewAccountObjectIdentifier(db)),
Database: sdk.Pointer(databaseId),
}
}
if sc := futureGrantsIn["schema"].(string); sc != "" {
schemaId, err := sdk.ParseDatabaseObjectIdentifier(sc)
if err != nil {
return nil, err
}
opts.In = &sdk.ShowGrantsIn{
Schema: sdk.Pointer(sdk.NewDatabaseObjectIdentifierFromFullyQualifiedName(sc)),
Schema: sdk.Pointer(schemaId),
}
}
return opts, nil
Expand All @@ -483,13 +531,21 @@ func buildOptsForFutureGrantsTo(futureGrantsTo map[string]any) (*sdk.ShowGrantOp
opts.Future = sdk.Bool(true)

if accountRole := futureGrantsTo["account_role"].(string); accountRole != "" {
accountRoleId, err := sdk.ParseAccountObjectIdentifier(accountRole)
if err != nil {
return nil, err
}
opts.To = &sdk.ShowGrantsTo{
Role: sdk.NewAccountObjectIdentifier(accountRole),
Role: accountRoleId,
}
}
if databaseRole := futureGrantsTo["database_role"].(string); databaseRole != "" {
databaseRoleId, err := sdk.ParseDatabaseObjectIdentifier(databaseRole)
if err != nil {
return nil, err
}
opts.To = &sdk.ShowGrantsTo{
DatabaseRole: sdk.NewDatabaseObjectIdentifierFromFullyQualifiedName(databaseRole),
DatabaseRole: databaseRoleId,
}
}
return opts, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/helpers/resource_identifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func Test_Encoding_And_Parsing_Of_ResourceIdentifier_Identifier(t *testing.T) {

for _, testCase := range testCases {
t.Run(fmt.Sprintf(`Encoding and parsing %s resource identifier`, testCase.Input), func(t *testing.T) {
switch typedInput := any(testCase.Input).(type) {
switch typedInput := testCase.Input.(type) {
case []sdk.AccountObjectIdentifier:
encodedIdentifier := EncodeResourceIdentifier(typedInput...)
assert.Equal(t, testCase.Expected, encodedIdentifier)
Expand Down
3 changes: 1 addition & 2 deletions pkg/resources/account_role.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func AccountRole() *schema.Resource {
),

Importer: &schema.ResourceImporter{
StateContext: ImportName,
StateContext: ImportName[sdk.AccountObjectIdentifier],
},
}
}
Expand All @@ -67,7 +67,6 @@ func CreateAccountRole(ctx context.Context, d *schema.ResourceData, meta any) di
if err != nil {
return diag.FromErr(err)
}

req := sdk.NewCreateRoleRequest(id)

if v, ok := d.GetOk("comment"); ok {
Expand Down
2 changes: 1 addition & 1 deletion pkg/resources/api_authentication_integration_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ func handleApiAuthCreate(d *schema.ResourceData) (commonApiAuthCreate, error) {
func handleApiAuthImport(d *schema.ResourceData, integration *sdk.SecurityIntegration,
properties []sdk.SecurityIntegrationProperty,
) error {
if _, err := ImportName(context.Background(), d, nil); err != nil {
if _, err := ImportName[sdk.AccountObjectIdentifier](context.Background(), d, nil); err != nil {
return err
}

Expand Down
50 changes: 42 additions & 8 deletions pkg/resources/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package resources

import (
"context"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"strings"

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

"github.com/hashicorp/go-cty/cty"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
Expand Down Expand Up @@ -48,14 +49,47 @@ func ctyValToSliceString(valueElems []cty.Value) []string {
return elems
}

func ImportName(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
id, err := sdk.ParseAccountObjectIdentifier(d.Id())
if err != nil {
return nil, err
}
func ImportName[T sdk.AccountObjectIdentifier | sdk.DatabaseObjectIdentifier | sdk.SchemaObjectIdentifier](ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
switch any(new(T)).(type) {
case *sdk.AccountObjectIdentifier:
id, err := sdk.ParseAccountObjectIdentifier(d.Id())
if err != nil {
return nil, err
}

if err := d.Set("name", id.Name()); err != nil {
return nil, err
}
case *sdk.DatabaseObjectIdentifier:
id, err := sdk.ParseDatabaseObjectIdentifier(d.Id())
if err != nil {
return nil, err
}

if err := d.Set("name", id.Name()); err != nil {
return nil, err
}

if err := d.Set("database", id.DatabaseName()); err != nil {
return nil, err
}
case *sdk.SchemaObjectIdentifier:
id, err := sdk.ParseSchemaObjectIdentifier(d.Id())
if err != nil {
return nil, err
}

if err := d.Set("name", id.Name()); err != nil {
return nil, err
}

if err := d.Set("database", id.DatabaseName()); err != nil {
return nil, err
}

if err := d.Set("name", id.Name()); err != nil {
return nil, err
if err := d.Set("schema", id.SchemaName()); err != nil {
return nil, err
}
}

return []*schema.ResourceData{d}, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/resources/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func Database() *schema.Resource {

Schema: helpers.MergeMaps(databaseSchema, databaseParametersSchema),
Importer: &schema.ResourceImporter{
StateContext: ImportName,
StateContext: ImportName[sdk.AccountObjectIdentifier],
},

CustomizeDiff: customdiff.All(
Expand Down
2 changes: 1 addition & 1 deletion pkg/resources/database_old.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func DatabaseOld() *schema.Resource {

Schema: databaseOldSchema,
Importer: &schema.ResourceImporter{
StateContext: ImportName,
StateContext: ImportName[sdk.AccountObjectIdentifier],
},
}
}
Expand Down
Loading
Loading