Skip to content

Commit

Permalink
Remove resource identifier csv parser
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-jcieslak committed Jul 24, 2024
1 parent 393d34c commit ca4f1f0
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 255 deletions.
55 changes: 18 additions & 37 deletions pkg/helpers/identifier_string_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package helpers
import (
"encoding/csv"
"fmt"
"github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk"
"strings"

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

const (
Expand All @@ -23,6 +24,9 @@ func parseIdentifierStringWithOpts(identifier string, opts func(*csv.Reader)) ([
if err != nil {
return nil, fmt.Errorf("unable to read identifier: %s, err = %w", identifier, err)
}
if lines == nil {
return make([]string, 0), nil
}
if len(lines) != 1 {
return nil, fmt.Errorf("incompatible identifier: %s", identifier)
}
Expand All @@ -35,14 +39,15 @@ func ParseIdentifierString(identifier string) ([]string, error) {
})
}

func EncodeResourceIdentifier(parts ...string) string {
return strings.Join(parts, string(ResourceIdDelimiter))
func ParseResourceIdentifier(identifier string) []string {
if identifier == "" {
return make([]string, 0)
}
return strings.Split(identifier, string(ResourceIdDelimiter))
}

func ParseResourceIdentifier(identifier string) ([]string, error) {
return parseIdentifierStringWithOpts(identifier, func(r *csv.Reader) {
r.Comma = ResourceIdDelimiter
})
func EncodeResourceIdentifier(parts ...string) string {
return strings.Join(parts, string(ResourceIdDelimiter))
}

func parseAccountObjectIdentifier(identifier string, parser identifierParsingFunc) (sdk.AccountObjectIdentifier, error) {
Expand All @@ -60,10 +65,6 @@ func ParseAccountObjectIdentifier(identifier string) (sdk.AccountObjectIdentifie
return parseAccountObjectIdentifier(identifier, ParseIdentifierString)
}

func ParseAccountObjectResourceIdentifier(identifier string) (sdk.AccountObjectIdentifier, error) {
return parseAccountObjectIdentifier(identifier, ParseResourceIdentifier)
}

func parseDatabaseObjectIdentifier(identifier string, parser identifierParsingFunc, delimiter rune) (sdk.DatabaseObjectIdentifier, error) {
parts, err := parser(identifier)
if err != nil {
Expand All @@ -79,10 +80,6 @@ func ParseDatabaseObjectIdentifier(identifier string) (sdk.DatabaseObjectIdentif
return parseDatabaseObjectIdentifier(identifier, ParseIdentifierString, IdDelimiter)
}

func ParseDatabaseObjectResourceIdentifier(identifier string) (sdk.DatabaseObjectIdentifier, error) {
return parseDatabaseObjectIdentifier(identifier, ParseResourceIdentifier, ResourceIdDelimiter)
}

func parseSchemaObjectIdentifier(identifier string, parser identifierParsingFunc, delimiter rune) (sdk.SchemaObjectIdentifier, error) {
parts, err := parser(identifier)
if err != nil {
Expand All @@ -98,10 +95,6 @@ func ParseSchemaObjectIdentifier(identifier string) (sdk.SchemaObjectIdentifier,
return parseSchemaObjectIdentifier(identifier, ParseIdentifierString, IdDelimiter)
}

func ParseSchemaObjectResourceIdentifier(identifier string) (sdk.SchemaObjectIdentifier, error) {
return parseSchemaObjectIdentifier(identifier, ParseResourceIdentifier, ResourceIdDelimiter)
}

func parseTableColumnObjectIdentifier(identifier string, parser identifierParsingFunc, delimiter rune) (sdk.TableColumnIdentifier, error) {
parts, err := parser(identifier)
if err != nil {
Expand All @@ -117,10 +110,6 @@ func ParseTableColumnIdentifier(identifier string) (sdk.TableColumnIdentifier, e
return parseTableColumnObjectIdentifier(identifier, ParseIdentifierString, IdDelimiter)
}

func ParseTableColumnResourceIdentifier(identifier string) (sdk.TableColumnIdentifier, error) {
return parseTableColumnObjectIdentifier(identifier, ParseResourceIdentifier, ResourceIdDelimiter)
}

func parseAccountIdentifier(identifier string, parser identifierParsingFunc, delimiter rune) (sdk.AccountIdentifier, error) {
parts, err := parser(identifier)
if err != nil {
Expand All @@ -136,16 +125,6 @@ func ParseAccountIdentifier(identifier string) (sdk.AccountIdentifier, error) {
return parseAccountIdentifier(identifier, ParseIdentifierString, IdDelimiter)
}

func ParseAccountResourceIdentifier(identifier string) (sdk.AccountIdentifier, error) {
return parseAccountIdentifier(identifier, ParseResourceIdentifier, ResourceIdDelimiter)
}

// parseExternalObjectIdentifier is implemented with an assumption that the identifier consists of three parts, because:
// - After identifier rework, we expect account identifiers to always have two parts "<organization_name>.<account_name>".
// - So far, the only external things that we referred to with external identifiers had only one part (not including the account identifier),
// meaning it will always be represented as sdk.AccountObjectIdentifier. Documentation also doesn't describe any case where
// account identifier would be used as part of the identifier that would refer to the "lower level" object.
// Reference: https://docs.snowflake.com/en/user-guide/admin-account-identifier#where-are-account-identifiers-used.
func parseExternalObjectIdentifier(identifier string, parser identifierParsingFunc, delimiter rune) (sdk.ExternalObjectIdentifier, error) {
parts, err := parser(identifier)
if err != nil {
Expand All @@ -157,10 +136,12 @@ func parseExternalObjectIdentifier(identifier string, parser identifierParsingFu
return sdk.NewExternalObjectIdentifier(sdk.NewAccountIdentifier(parts[0], parts[1]), sdk.NewAccountObjectIdentifier(parts[2])), nil
}

// ParseExternalObjectIdentifier is implemented with an assumption that the identifier consists of three parts, because:
// - After identifier rework, we expect account identifiers to always have two parts "<organization_name>.<account_name>".
// - So far, the only external things that we referred to with external identifiers had only one part (not including the account identifier),
// meaning it will always be represented as sdk.AccountObjectIdentifier. Documentation also doesn't describe any case where
// account identifier would be used as part of the identifier that would refer to the "lower level" object.
// Reference: https://docs.snowflake.com/en/user-guide/admin-account-identifier#where-are-account-identifiers-used.
func ParseExternalObjectIdentifier(identifier string) (sdk.ExternalObjectIdentifier, error) {
return parseExternalObjectIdentifier(identifier, ParseIdentifierString, IdDelimiter)
}

func ParseExternalObjectResourceIdentifier(identifier string) (sdk.ExternalObjectIdentifier, error) {
return parseExternalObjectIdentifier(identifier, ParseResourceIdentifier, ResourceIdDelimiter)
}
Loading

0 comments on commit ca4f1f0

Please sign in to comment.