-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add identifier parsers (#2957)
Changes: - Add and test identifier parsers for object and account identifiers with different separators (only dot was added as identifier parser, description provided below). The encoder/parser for resource identifiers was in the end implemented as simple string join/split by pipe, because of the following: - Identifiers that have only one part that is represented by the `sdk.ObjectIdentifier` should be encoded as `FullyQualifiedName` and parsed with one of the newly introduced parsers. This should improve a bit the support for tools that generate import commands (or import blocks) because the resource identifier is the same as the Snowflake one. - More complicated identifiers having more parts should use the provided encoder/parser to create or parse resource identifiers into parts. This could cause problems for things that may contain pipe, but we should generally avoid it or document it (can be done in the usage part of identifier rework). Also, CSV reader/writer cannot be used for resource identifiers, because this would make identifiers more complex. For example, a fully qualified identifier with quotes would have to be imported as `terraform import snowflake_table.test_table '"""database_name"".""schema_name"".table_name"""'`. To have this as an identifier would also mean creating a state upgrader for every resource (or general one that could be reused) that would transform the identifiers into CSV-compliant ones. Notes: - Implementation with the use of `identifeirParsingFunc` was added because initially, I had two parsers (one with dot as csv.Comma and the other one with pipe) and later on removed the one with the pipe as csv.Comma (I can merge the unexported parsers with exported ones or leave it as is if we would like to introduce another internal parser). - ExternalObjectIdentifier and AccountIdentifier parsers were created with certain assumptions (described in code). ## Test Plan <!-- detail ways in which this PR has been tested or needs to be tested --> * [x] unit tests ## References <!-- issues documentation links, etc --> * SNOW-1495051
- Loading branch information
1 parent
0d90cc9
commit 824ec52
Showing
29 changed files
with
832 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package helpers | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type GrantClient struct { | ||
context *TestClientContext | ||
ids *IdsGenerator | ||
} | ||
|
||
func NewGrantClient(context *TestClientContext, idsGenerator *IdsGenerator) *GrantClient { | ||
return &GrantClient{ | ||
context: context, | ||
ids: idsGenerator, | ||
} | ||
} | ||
|
||
func (c *GrantClient) client() sdk.Grants { | ||
return c.context.client.Grants | ||
} | ||
|
||
func (c *GrantClient) GrantOnSchemaToAccountRole(t *testing.T, schemaId sdk.DatabaseObjectIdentifier, accountRoleId sdk.AccountObjectIdentifier, privileges ...sdk.SchemaPrivilege) { | ||
t.Helper() | ||
ctx := context.Background() | ||
|
||
err := c.client().GrantPrivilegesToAccountRole( | ||
ctx, | ||
&sdk.AccountRoleGrantPrivileges{ | ||
SchemaPrivileges: privileges, | ||
}, | ||
&sdk.AccountRoleGrantOn{ | ||
Schema: &sdk.GrantOnSchema{ | ||
Schema: &schemaId, | ||
}, | ||
}, | ||
accountRoleId, | ||
new(sdk.GrantPrivilegesToAccountRoleOptions), | ||
) | ||
require.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package helpers | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
const ResourceIdDelimiter = '|' | ||
|
||
func ParseResourceIdentifier(identifier string) []string { | ||
if identifier == "" { | ||
return make([]string, 0) | ||
} | ||
return strings.Split(identifier, string(ResourceIdDelimiter)) | ||
} | ||
|
||
func EncodeResourceIdentifier(parts ...string) string { | ||
return strings.Join(parts, string(ResourceIdDelimiter)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package helpers | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_Encoding_And_Parsing_Of_ResourceIdentifier(t *testing.T) { | ||
testCases := []struct { | ||
Input []string | ||
Expected string | ||
ExpectedAfterDecoding []string | ||
}{ | ||
{Input: []string{sdk.NewSchemaObjectIdentifier("a", "b", "c").FullyQualifiedName(), "info"}, Expected: `"a"."b"."c"|info`}, | ||
{Input: []string{}, Expected: ``}, | ||
{Input: []string{"", "", ""}, Expected: `||`}, | ||
{Input: []string{"a", "b", "c"}, Expected: `a|b|c`}, | ||
// If one of the parts contains a separator sign (pipe in this case), | ||
// we can end up with more parts than we started with. | ||
{Input: []string{"a", "b", "c|d"}, Expected: `a|b|c|d`, ExpectedAfterDecoding: []string{"a", "b", "c", "d"}}, | ||
} | ||
|
||
for _, testCase := range testCases { | ||
t.Run(fmt.Sprintf(`Encoding and parsing %s resource identifier`, testCase.Input), func(t *testing.T) { | ||
encodedIdentifier := EncodeResourceIdentifier(testCase.Input...) | ||
assert.Equal(t, testCase.Expected, encodedIdentifier) | ||
|
||
parsedIdentifier := ParseResourceIdentifier(encodedIdentifier) | ||
if testCase.ExpectedAfterDecoding != nil { | ||
assert.Equal(t, testCase.ExpectedAfterDecoding, parsedIdentifier) | ||
} else { | ||
assert.Equal(t, testCase.Input, parsedIdentifier) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.