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 20 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
2 changes: 1 addition & 1 deletion pkg/sdk/api_integrations_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,4 @@ var ApiIntegrationsDef = g.NewInterface(
SQL("API INTEGRATION").
Name().
WithValidation(g.ValidIdentifier, "name"),
)
).HelperMethodID()
8 changes: 4 additions & 4 deletions pkg/sdk/api_integrations_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,6 @@ type ApiIntegration struct {
CreatedOn time.Time
}

func (v *ApiIntegration) ID() AccountObjectIdentifier {
return NewAccountObjectIdentifier(v.Name)
}

// DescribeApiIntegrationOptions is based on https://docs.snowflake.com/en/sql-reference/sql/desc-integration.
type DescribeApiIntegrationOptions struct {
describe bool `ddl:"static" sql:"DESCRIBE"`
Expand All @@ -154,3 +150,7 @@ type ApiIntegrationProperty struct {
Value string
Default string
}

func (v *ApiIntegration) ID() AccountObjectIdentifier {
return NewAccountObjectIdentifier(v.Name)
}
2 changes: 1 addition & 1 deletion pkg/sdk/notification_integrations_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,4 +200,4 @@ var NotificationIntegrationsDef = g.NewInterface(
SQL("NOTIFICATION INTEGRATION").
Name().
WithValidation(g.ValidIdentifier, "name"),
)
).HelperMethodID()
8 changes: 4 additions & 4 deletions pkg/sdk/notification_integrations_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,6 @@ type NotificationIntegration struct {
CreatedOn time.Time
}

func (v *NotificationIntegration) ID() AccountObjectIdentifier {
return NewAccountObjectIdentifier(v.Name)
}

// DescribeNotificationIntegrationOptions is based on https://docs.snowflake.com/en/sql-reference/sql/desc-integration.
type DescribeNotificationIntegrationOptions struct {
describe bool `ddl:"static" sql:"DESCRIBE"`
Expand All @@ -185,3 +181,7 @@ type NotificationIntegrationProperty struct {
Value string
Default string
}

func (v *NotificationIntegration) ID() AccountObjectIdentifier {
return NewAccountObjectIdentifier(v.Name)
}
115 changes: 115 additions & 0 deletions pkg/sdk/poc/generator/helper_methods.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package generator

sfc-gh-jmichalak marked this conversation as resolved.
Show resolved Hide resolved
import (
"fmt"
"log"
)

type objectIdentifier string

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

func identifierStringToObjectIdentifier(s string) objectIdentifier {
switch s {
case "AccountObjectIdentifier":
return AccountObjectIdentifier
case "DatabaseObjectIdentifier":
return DatabaseObjectIdentifier
case "SchemaObjectIdentifier":
return SchemaObjectIdentifier
case "SchemaObjectIdentifierWithArguments":
return SchemaObjectIdentifierWithArguments
default:
return ""
}
}

type HelperMethod struct {
Name string
StructName string
ReturnValue string
ReturnType string
}

func newHelperMethod(name, structName, returnValue string, returnType string) *HelperMethod {
return &HelperMethod{
Name: name,
StructName: structName,
ReturnValue: returnValue,
ReturnType: returnType,
}
}

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

func newIDHelperMethod(structName string, objectIdentifier objectIdentifier) *HelperMethod {
var args string
fields := requiredFieldsForIDMethodMapping[objectIdentifier]
for _, field := range fields {
args += fmt.Sprintf("v.%v, ", field)
}
returnValue := fmt.Sprintf("New%v(%v)", objectIdentifier, args)
return newHelperMethod("ID", structName, returnValue, string(objectIdentifier))
}

func newObjectTypeHelperMethod(structName string) *HelperMethod {
returnValue := fmt.Sprintf("ObjectType%v", structName)
return newHelperMethod("ObjectType", structName, returnValue, "ObjectType")
}

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

for _, name := range names {
if _, ok := fieldNames[name]; !ok {
sfc-gh-jmichalak marked this conversation as resolved.
Show resolved Hide resolved
return false
}
}
return true
}

func hasRequiredFieldsForIDMethod(operations []*Operation, structName string, requiredFields ...string) bool {
for _, op := range operations {
if op.Name != string(OperationKindShow) {
continue
}
for _, field := range op.HelperStructs {
if field.Name == structName {
return containsFieldNames(field.Fields, requiredFields...)
}
}
log.Printf("WARNING: Struct: '%s' not found in '%s' operation. Couldn't generate ID() helper method.", structName, OperationKindShow)
}
log.Printf("WARNING: Operation: '%s' not found. Couldn't generate ID() helper method.", OperationKindShow)
return false
}

// HelperMethodID adds a helper method "ID()" to the interface file that returns the ObjectIdentifier of the object
func (i *Interface) HelperMethodID() *Interface {
Copy link
Collaborator

Choose a reason for hiding this comment

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

ObjectMethodID?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

renamed to ResourceIDHelperMethod because in ShowOperation we provide the plainStruct representing resource as resourceRepresentation

It could be named to something more suitable tho

sfc-gh-jmichalak marked this conversation as resolved.
Show resolved Hide resolved
identifierKind := identifierStringToObjectIdentifier(i.IdentifierKind)
requiredFields := requiredFieldsForIDMethodMapping[identifierKind]
if !hasRequiredFieldsForIDMethod(i.Operations, i.NameSingular, requiredFields...) {
log.Printf("WARNING: Struct '%s' does not contain needed fields to build ID() helper method. Create the method manually in _ext file or add missing one of required fields: %v.\n", i.NameSingular, requiredFields)
return i
}
i.HelperMethods = append(i.HelperMethods, newIDHelperMethod(i.NameSingular, identifierKind))
return i
}

// HelperMethodObjectType adds a helper method "ObjectType()" to the interface file that returns the ObjectType for the struct
func (i *Interface) HelperMethodObjectType() *Interface {
i.HelperMethods = append(i.HelperMethods, newObjectTypeHelperMethod(i.NameSingular))
return i
}
2 changes: 2 additions & 0 deletions pkg/sdk/poc/generator/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ type Interface struct {
Operations []*Operation
// IdentifierKind keeps identifier of the underlying object (e.g. DatabaseObjectIdentifier)
IdentifierKind string
// HelperMethods contains helper methods for the Interface file (i.e. ID(), ObjectType())
HelperMethods []*HelperMethod
}

func NewInterface(name string, nameSingular string, identifierKind string, operations ...*Operation) *Interface {
Expand Down
9 changes: 9 additions & 0 deletions pkg/sdk/poc/generator/template_executors.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ func GenerateInterface(writer io.Writer, def *Interface) {
generateOptionsStruct(writer, o)
}
}
for _, m := range def.HelperMethods {
if m != nil {
generateHelperMethods(writer, m)
}
}
}

func generateHelperMethods(writer io.Writer, hm *HelperMethod) {
printTo(writer, HelperMethodTemplate, hm)
}

func generateOptionsStruct(writer io.Writer, operation *Operation) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/sdk/poc/generator/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ var (
structTemplateContent string
StructTemplate, _ = template.New("structTemplate").Parse(structTemplateContent)

//go:embed templates/helper_method.tmpl
helperMethodTemplateContent string
HelperMethodTemplate, _ = template.New("helperMethodTemplate").Parse(helperMethodTemplateContent)

//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/helper_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.HelperMethod*/ -}}

func (v *{{ .StructName }}) {{ .Name }}() {{ .ReturnType }} {
return {{ .ReturnValue }}
}
3 changes: 2 additions & 1 deletion pkg/sdk/secrets_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,5 @@ var SecretsDef = g.NewInterface(
SQL("SECRET").
Name().
WithValidation(g.ValidIdentifier, "name"),
)
).HelperMethodID().
HelperMethodObjectType()
27 changes: 19 additions & 8 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,14 +162,6 @@ type Secret struct {
OwnerRoleType string
}

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

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

// DescribeSecretOptions is based on https://docs.snowflake.com/en/sql-reference/sql/desc-secret.
type DescribeSecretOptions struct {
describe bool `ddl:"static" sql:"DESCRIBE"`
Expand Down Expand Up @@ -193,3 +196,11 @@ type SecretDetails struct {
OauthScopes []string
IntegrationName *string
}

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

func (v *Secret) ObjectType() ObjectType {
return ObjectTypeSecret
}
2 changes: 1 addition & 1 deletion pkg/sdk/streamlits_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,4 @@ var StreamlitsDef = g.NewInterface(
SQL("STREAMLIT").
Name().
WithValidation(g.ValidIdentifier, "name"),
)
).HelperMethodID()
11 changes: 9 additions & 2 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 @@ -104,6 +109,7 @@ type DescribeStreamlitOptions struct {
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 +123,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 @@ -131,6 +138,6 @@ type StreamlitDetail struct {
ExternalAccessSecrets string
}

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