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: Generate ID and ObjectType Show Object Methods #3292

Open
wants to merge 45 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
cea5dd3
init for interface helper methods
sfc-gh-fbudzynski Dec 9, 2024
d47db72
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Dec 10, 2024
4750632
enum
sfc-gh-fbudzynski Dec 10, 2024
e051890
refactor
sfc-gh-fbudzynski Dec 10, 2024
86d55bd
doc to helper methods
sfc-gh-fbudzynski Dec 10, 2024
cf927ef
generated for streamlits
sfc-gh-fbudzynski Dec 10, 2024
495e236
generated for notification integrations
sfc-gh-fbudzynski Dec 10, 2024
c9cb8d7
checks for missing fields to create ID() method
sfc-gh-fbudzynski Dec 10, 2024
8fdc1dd
small fix
sfc-gh-fbudzynski Dec 12, 2024
a3784c0
slight refactor and generated some examples
sfc-gh-fbudzynski Dec 12, 2024
a1d1067
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Dec 12, 2024
1c5c00d
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Dec 12, 2024
590cb48
ff
sfc-gh-fbudzynski Dec 12, 2024
b0725ae
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Dec 12, 2024
78a01e3
log change
sfc-gh-fbudzynski Dec 12, 2024
d5d1aa6
ref
sfc-gh-fbudzynski Dec 12, 2024
6b4be76
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Dec 12, 2024
e1a27df
ref
sfc-gh-fbudzynski Dec 12, 2024
38b8a3e
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Dec 16, 2024
9f29c42
ref
sfc-gh-fbudzynski Dec 16, 2024
1310f63
Merge branch 'dev' of github.com:Snowflake-Labs/terraform-provider-sn…
sfc-gh-fbudzynski Dec 16, 2024
d610978
refacotred to be on Operatin level
sfc-gh-fbudzynski Dec 16, 2024
72fc399
rename
sfc-gh-fbudzynski Dec 16, 2024
475a570
small ref
sfc-gh-fbudzynski Dec 16, 2024
388abfc
rename
sfc-gh-fbudzynski Dec 16, 2024
546e125
self rev
sfc-gh-fbudzynski Dec 16, 2024
64fea51
self rev
sfc-gh-fbudzynski Dec 16, 2024
3f44299
self rev
sfc-gh-fbudzynski Dec 16, 2024
8987603
self rev
sfc-gh-fbudzynski Dec 16, 2024
05914c3
self rev
sfc-gh-fbudzynski Dec 16, 2024
3954232
rev
sfc-gh-fbudzynski Dec 18, 2024
05c44e5
def fixes
sfc-gh-fbudzynski Dec 18, 2024
9e757be
order fixed
sfc-gh-fbudzynski Dec 18, 2024
8183ff7
rename...
sfc-gh-fbudzynski Dec 18, 2024
268e2d8
lint
sfc-gh-fbudzynski Dec 18, 2024
c60dec5
ref
sfc-gh-fbudzynski Dec 18, 2024
df0563b
ref
sfc-gh-fbudzynski Dec 18, 2024
a8ff21d
ref
sfc-gh-fbudzynski Dec 18, 2024
a9d50cb
ref
sfc-gh-fbudzynski Dec 18, 2024
57ebbc3
ref
sfc-gh-fbudzynski Dec 18, 2024
560cc8b
small ref
sfc-gh-fbudzynski Dec 19, 2024
891ce2b
removed methods from operation
sfc-gh-fbudzynski Dec 19, 2024
2d9930e
Merge branch 'dev' of github.com:Snowflake-Labs/terraform-provider-sn…
sfc-gh-fbudzynski Dec 19, 2024
5f5e263
merge
sfc-gh-fbudzynski Dec 19, 2024
cae24f6
Merge branch 'dev' into sdk-helper-methods
sfc-gh-fbudzynski Dec 20, 2024
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
27 changes: 26 additions & 1 deletion pkg/sdk/poc/generator/interface.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
package generator

import "fmt"

type objectIdentifierKind string

const (
AccountObjectIdentifier objectIdentifierKind = "AccountObjectIdentifier"
DatabaseObjectIdentifier objectIdentifierKind = "DatabaseObjectIdentifier"
SchemaObjectIdentifier objectIdentifierKind = "SchemaObjectIdentifier"
SchemaObjectIdentifierWithArguments objectIdentifierKind = "SchemaObjectIdentifierWithArguments"
)

func toObjectIdentifierKind(s string) (objectIdentifierKind, error) {
switch s {
case "AccountObjectIdentifier":
return AccountObjectIdentifier, nil
case "DatabaseObjectIdentifier":
return DatabaseObjectIdentifier, nil
case "SchemaObjectIdentifier":
return SchemaObjectIdentifier, nil
case "SchemaObjectIdentifierWithArguments":
return SchemaObjectIdentifierWithArguments, nil
default:
return "", fmt.Errorf("invalid string identifier type: %s", s)
}
}

// Interface groups operations for particular object or objects family (e.g. DATABASE ROLE)
type Interface struct {
// Name is the interface's name, e.g. "DatabaseRoles"
Expand Down Expand Up @@ -28,6 +54,5 @@ func (i *Interface) NameLowerCased() string {

// ObjectIdentifierKind returns the level of the object identifier (e.g. for DatabaseObjectIdentifier, it returns the prefix "Database")
func (i *Interface) ObjectIdentifierPrefix() idPrefix {
// return strings.Replace(i.IdentifierKind, "ObjectIdentifier", "", 1)
return identifierStringToPrefix(i.IdentifierKind)
}
11 changes: 8 additions & 3 deletions pkg/sdk/poc/generator/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ func (s *Operation) withHelperStructs(helperStructs ...*Field) *Operation {
return s
}

func (s *Operation) withObjectInterface(objectInterface *Interface) *Operation {
s.ObjectInterface = objectInterface
return s
}

func addShowMapping(op *Operation, from, to *Field) {
op.ShowMapping = newMapping("convert", from, to)
}
Expand Down Expand Up @@ -170,9 +175,9 @@ func (i *Interface) ShowByIdOperationWithNoFiltering() *Interface {

// ShowByIdOperationWithFiltering adds a ShowByID operation to the interface with filtering. Should be used for objects that implement filtering options e.g. Like or In.
func (i *Interface) ShowByIdOperationWithFiltering(filter ShowByIDFilteringKind, filtering ...ShowByIDFilteringKind) *Interface {
op := newNoSqlOperation(string(OperationKindShowByID))
op.ObjectInterface = i
op.withFiltering(append([]ShowByIDFilteringKind{filter}, filtering...)...)
op := newNoSqlOperation(string(OperationKindShowByID)).
withObjectInterface(i).
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

withFiltering(append(filtering, filter)...)
i.Operations = append(i.Operations, op)
return i
}
Expand Down
60 changes: 60 additions & 0 deletions pkg/sdk/poc/generator/show_object_methods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package generator

import (
"log"
"slices"
)

type ShowObjectIdMethod struct {
StructName string
IdentifierKind objectIdentifierKind
Args []string
}

func newShowObjectIDMethod(structName string, idType objectIdentifierKind) *ShowObjectIdMethod {
return &ShowObjectIdMethod{
StructName: structName,
IdentifierKind: idType,
Args: idTypeParts[idType],
}
}

var idTypeParts map[objectIdentifierKind][]string = map[objectIdentifierKind][]string{
AccountObjectIdentifier: {"Name"},
DatabaseObjectIdentifier: {"DatabaseName", "Name"},
SchemaObjectIdentifier: {"DatabaseName", "SchemaName", "Name"},
}

func checkRequiredFieldsForIdMethod(structName string, helperStructs []*Field, idKind objectIdentifierKind) bool {
if requiredFields, ok := idTypeParts[idKind]; ok {
for _, field := range helperStructs {
if field.Name == structName {
return containsFieldNames(field.Fields, requiredFields...)
}
}
}
log.Printf("[WARN] no required fields mapping defined for identifier %s", idKind)
return false
}

func containsFieldNames(fields []*Field, names ...string) bool {
fieldNames := []string{}
for _, field := range fields {
fieldNames = append(fieldNames, field.Name)
}

for _, name := range names {
if !slices.Contains(fieldNames, name) {
return false
}
}
return true
}

type ShowObjectTypeMethod struct {
StructName string
}

func newShowObjectTypeMethod(structName string) *ShowObjectTypeMethod {
return &ShowObjectTypeMethod{StructName: structName}
}
50 changes: 50 additions & 0 deletions pkg/sdk/poc/generator/show_object_methods_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package generator

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestIdentifierStringToObjectIdentifier(t *testing.T) {
tests := []struct {
input string
expected objectIdentifierKind
}{
{"AccountObjectIdentifier", AccountObjectIdentifier},
{"DatabaseObjectIdentifier", DatabaseObjectIdentifier},
{"SchemaObjectIdentifier", SchemaObjectIdentifier},
{"SchemaObjectIdentifierWithArguments", SchemaObjectIdentifierWithArguments},
}

for _, test := range tests {
t.Run(test.input, func(t *testing.T) {
result, err := toObjectIdentifierKind(test.input)
require.NoError(t, err)
require.Equal(t, test.expected, result)
})
}
}

func TestIdentifierStringToObjectIdentifier_Invalid(t *testing.T) {
tests := []struct {
input string
err string
}{
{"accountobjectidentifier", "invalid string identifier type: accountobjectidentifier"},
{"Account", "invalid string identifier type: Account"},
{"databaseobjectidentifier", "invalid string identifier type: databaseobjectidentifier"},
{"Database", "invalid string identifier type: Database"},
{"schemaobjectidentifier", "invalid string identifier type: schemaobjectidentifier"},
{"Schema", "invalid string identifier type: Schema"},
{"schemaobjectidentifierwitharguments", "invalid string identifier type: schemaobjectidentifierwitharguments"},
{"schemawitharguemnts", "invalid string identifier type: schemawitharguemnts"},
}

for _, tc := range tests {
t.Run(tc.input, func(t *testing.T) {
_, err := toObjectIdentifierKind(tc.input)
require.ErrorContains(t, err, tc.err)
})
}
}
20 changes: 20 additions & 0 deletions pkg/sdk/poc/generator/template_executors.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,29 @@ func GenerateInterface(writer io.Writer, def *Interface) {
if o.OptsField != nil {
generateOptionsStruct(writer, o)
}

if o.Name == string(OperationKindShow) {
idKind, err := toObjectIdentifierKind(def.IdentifierKind)
if err != nil {
log.Printf("[WARN] for showObjectIdMethod: %v", err)
}
if checkRequiredFieldsForIdMethod(def.NameSingular, o.HelperStructs, idKind) {
generateShowObjectIdMethod(writer, newShowObjectIDMethod(def.NameSingular, idKind))
}

generateShowObjectTypeMethod(writer, newShowObjectTypeMethod(def.NameSingular))
}
}
}

func generateShowObjectIdMethod(writer io.Writer, m *ShowObjectIdMethod) {
printTo(writer, ShowObjectIdMethodTemplate, m)
}

func generateShowObjectTypeMethod(writer io.Writer, m *ShowObjectTypeMethod) {
printTo(writer, ShowObjectTypeMethodTemplate, m)
}

func generateOptionsStruct(writer io.Writer, operation *Operation) {
printTo(writer, OperationStructTemplate, operation)

Expand Down
12 changes: 12 additions & 0 deletions pkg/sdk/poc/generator/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ var (
structTemplateContent string
StructTemplate, _ = template.New("structTemplate").Parse(structTemplateContent)

//go:embed templates/show_object_method.tmpl
showObjectMethodTemplateContent string
ShowObjectMethodTemplate, _ = template.New("helperMethodTemplate").Parse(showObjectMethodTemplateContent)

//go:embed templates/show_object_id_method.tmpl
showObjectIdMethodTemplateContent string
ShowObjectIdMethodTemplate, _ = template.New("showObjectIdMethodTemplate").Parse(showObjectIdMethodTemplateContent)

//go:embed templates/show_object_type_method.tmpl
showObjectTypeMethodTemplateContent string
ShowObjectTypeMethodTemplate, _ = template.New("showObjectTypeMethodTemplate").Parse(showObjectTypeMethodTemplateContent)

//go:embed templates/dto_declarations.tmpl
dtoDeclarationsTemplateContent string
DtoTemplate, _ = template.New("dtoTemplate").Parse(dtoDeclarationsTemplateContent)
Expand Down
5 changes: 5 additions & 0 deletions pkg/sdk/poc/generator/templates/show_object_id_method.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{- /*gotype: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator.ShowObjectIdMethod*/ -}}

func (v *{{ .StructName }}) ID() {{ .IdentifierKind }} {
return New{{ .IdentifierKind }}({{ range .Args }}v.{{ . }}, {{ end }})
}
6 changes: 6 additions & 0 deletions pkg/sdk/poc/generator/templates/show_object_method.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{- /*gotype: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator.ShowObjectMethod*/ -}}

func (v *{{ .StructName }}) ObjectType() ObjectType {
return ObjectType{{ .StructName }}
}

5 changes: 5 additions & 0 deletions pkg/sdk/poc/generator/templates/show_object_type_method.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{{- /*gotype: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator.ShowObjectTypeMethod*/ -}}

func (v *{{ .StructName }}) ObjectType() ObjectType {
return ObjectType{{ .StructName }}
}
17 changes: 14 additions & 3 deletions pkg/sdk/secrets_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ type CreateWithOAuthClientCredentialsFlowSecretOptions struct {
OauthScopes *OauthScopesList `ddl:"parameter,parentheses" sql:"OAUTH_SCOPES"`
Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"`
}

type ApiIntegrationScope struct {
Scope string `ddl:"keyword,single_quotes"`
}

type OauthScopesList struct {
OauthScopesList []ApiIntegrationScope `ddl:"list,must_parentheses"`
}
Expand Down Expand Up @@ -85,30 +87,37 @@ type AlterSecretOptions struct {
Set *SecretSet `ddl:"keyword" sql:"SET"`
Unset *SecretUnset `ddl:"keyword"`
}

type SecretSet struct {
Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"`
SetForFlow *SetForFlow `ddl:"keyword"`
}

type SetForFlow struct {
SetForOAuthClientCredentials *SetForOAuthClientCredentials `ddl:"keyword"`
SetForOAuthAuthorization *SetForOAuthAuthorization `ddl:"keyword"`
SetForBasicAuthentication *SetForBasicAuthentication `ddl:"keyword"`
SetForGenericString *SetForGenericString `ddl:"keyword"`
}

type SetForOAuthClientCredentials struct {
OauthScopes *OauthScopesList `ddl:"parameter,parentheses" sql:"OAUTH_SCOPES"`
}

type SetForOAuthAuthorization struct {
OauthRefreshToken *string `ddl:"parameter,single_quotes" sql:"OAUTH_REFRESH_TOKEN"`
OauthRefreshTokenExpiryTime *string `ddl:"parameter,single_quotes" sql:"OAUTH_REFRESH_TOKEN_EXPIRY_TIME"`
}

type SetForBasicAuthentication struct {
Username *string `ddl:"parameter,single_quotes" sql:"USERNAME"`
Password *string `ddl:"parameter,single_quotes" sql:"PASSWORD"`
}

type SetForGenericString struct {
SecretString *string `ddl:"parameter,single_quotes" sql:"SECRET_STRING"`
}

type SecretUnset struct {
Comment *bool `ddl:"keyword" sql:"SET COMMENT = NULL"`
}
Expand All @@ -128,6 +137,7 @@ type ShowSecretOptions struct {
Like *Like `ddl:"keyword" sql:"LIKE"`
In *ExtendedIn `ddl:"keyword" sql:"IN"`
}

type secretDBRow struct {
CreatedOn time.Time `db:"created_on"`
Name string `db:"name"`
Expand All @@ -139,6 +149,7 @@ type secretDBRow struct {
OauthScopes sql.NullString `db:"oauth_scopes"`
OwnerRoleType string `db:"owner_role_type"`
}

type Secret struct {
CreatedOn time.Time
Name string
Expand All @@ -151,11 +162,11 @@ type Secret struct {
OwnerRoleType string
}

func (s *Secret) ID() SchemaObjectIdentifier {
return NewSchemaObjectIdentifier(s.DatabaseName, s.SchemaName, s.Name)
func (v *Secret) ID() SchemaObjectIdentifier {
return NewSchemaObjectIdentifier(v.DatabaseName, v.SchemaName, v.Name)
}

func (s *Secret) ObjectType() ObjectType {
func (v *Secret) ObjectType() ObjectType {
return ObjectTypeSecret
}

Expand Down
15 changes: 11 additions & 4 deletions pkg/sdk/streamlits_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type CreateStreamlitOptions struct {
Title *string `ddl:"parameter,single_quotes" sql:"TITLE"`
Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"`
}

type ExternalAccessIntegrations struct {
ExternalAccessIntegrations []AccountObjectIdentifier `ddl:"list,must_parentheses"`
}
Expand All @@ -42,6 +43,7 @@ type AlterStreamlitOptions struct {
Unset *StreamlitUnset `ddl:"list,no_parentheses" sql:"UNSET"`
RenameTo *SchemaObjectIdentifier `ddl:"identifier" sql:"RENAME TO"`
}

type StreamlitSet struct {
RootLocation *string `ddl:"parameter,single_quotes" sql:"ROOT_LOCATION"`
MainFile *string `ddl:"parameter,single_quotes" sql:"MAIN_FILE"`
Expand All @@ -50,6 +52,7 @@ type StreamlitSet struct {
Comment *string `ddl:"parameter,single_quotes" sql:"COMMENT"`
Title *string `ddl:"parameter,single_quotes" sql:"TITLE"`
}

type StreamlitUnset struct {
QueryWarehouse *bool `ddl:"keyword" sql:"QUERY_WAREHOUSE"`
Comment *bool `ddl:"keyword" sql:"COMMENT"`
Expand All @@ -73,6 +76,7 @@ type ShowStreamlitOptions struct {
In *In `ddl:"keyword" sql:"IN"`
Limit *LimitFrom `ddl:"keyword" sql:"LIMIT"`
}

type streamlitsRow struct {
CreatedOn string `db:"created_on"`
Name string `db:"name"`
Expand All @@ -85,6 +89,7 @@ type streamlitsRow struct {
UrlId string `db:"url_id"`
OwnerRoleType string `db:"owner_role_type"`
}

type Streamlit struct {
CreatedOn string
Name string
Expand All @@ -98,12 +103,17 @@ type Streamlit struct {
OwnerRoleType string
}

func (v *Streamlit) ID() SchemaObjectIdentifier {
return NewSchemaObjectIdentifier(v.DatabaseName, v.SchemaName, v.Name)
}

// DescribeStreamlitOptions is based on https://docs.snowflake.com/en/sql-reference/sql/desc-streamlit.
type DescribeStreamlitOptions struct {
describe bool `ddl:"static" sql:"DESCRIBE"`
streamlit bool `ddl:"static" sql:"STREAMLIT"`
name SchemaObjectIdentifier `ddl:"identifier"`
}

type streamlitsDetailRow struct {
Name string `db:"name"`
Title sql.NullString `db:"title"`
Expand All @@ -117,6 +127,7 @@ type streamlitsDetailRow struct {
ExternalAccessIntegrations string `db:"external_access_integrations"`
ExternalAccessSecrets string `db:"external_access_secrets"`
}

type StreamlitDetail struct {
Name string
Title string
Expand All @@ -130,7 +141,3 @@ type StreamlitDetail struct {
ExternalAccessIntegrations []string
ExternalAccessSecrets string
}

func (s *Streamlit) ID() SchemaObjectIdentifier {
return NewSchemaObjectIdentifier(s.DatabaseName, s.SchemaName, s.Name)
}
Loading