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: Add ShowByID filtering generation #3227

Merged
merged 19 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
f4d01dd
Adding generating ShowByID filtering options from def file
sfc-gh-fbudzynski Nov 25, 2024
a7366ec
Moved show by id filtering to another file
sfc-gh-fbudzynski Nov 26, 2024
bc0fb66
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Nov 26, 2024
5cb03d1
Remove unused definition in template
sfc-gh-fbudzynski Nov 26, 2024
8f2a301
change showByID Filtering to diference types of filtering based on ne…
sfc-gh-fbudzynski Nov 26, 2024
1599afb
linter adjustemnts
sfc-gh-fbudzynski Nov 26, 2024
ebeffac
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Nov 28, 2024
dcbae83
add filtering type
sfc-gh-fbudzynski Nov 28, 2024
151741b
first steps into implementing interface
sfc-gh-fbudzynski Nov 28, 2024
93ab8ff
refactor
sfc-gh-fbudzynski Dec 2, 2024
947902d
refactor cleanup
sfc-gh-fbudzynski Dec 2, 2024
4633831
test generator run for streamlits
sfc-gh-fbudzynski Dec 2, 2024
04bc3ce
Merge branch 'main' of github.com:Snowflake-Labs/terraform-provider-s…
sfc-gh-fbudzynski Dec 2, 2024
aaf81f1
rephrase the TODO message
sfc-gh-fbudzynski Dec 2, 2024
6370828
regenerate secrets back to In
sfc-gh-fbudzynski Dec 2, 2024
29b2ad8
enum identifierKind
sfc-gh-fbudzynski Dec 5, 2024
b02da54
showbyid generation with examples
sfc-gh-fbudzynski Dec 5, 2024
9cd65c3
showbyid fix for generating nofiltering
sfc-gh-fbudzynski Dec 5, 2024
9d51aa9
Merge branch 'main' into sdk-generator-show-by-id
sfc-gh-fbudzynski Dec 5, 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
7 changes: 7 additions & 0 deletions pkg/sdk/poc/generator/interface.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package generator

import "strings"

// 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 All @@ -25,3 +27,8 @@ func NewInterface(name string, nameSingular string, identifierKind string, opera
func (i *Interface) NameLowerCased() string {
return startingWithLowerCase(i.Name)
}

// ObjectIdentifierKind returns the level of the object identifier (e.g. for DatabaseObjectIdentifier, it returns "Database")
func (i *Interface) ObjectIdentifierKind() string {
return strings.Replace(i.IdentifierKind, "ObjectIdentifier", "", 1)
}
13 changes: 9 additions & 4 deletions pkg/sdk/poc/generator/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type Operation struct {
DescribeKind *DescriptionMappingKind
// DescribeMapping is a definition of mapping needed by Operation kind of OperationKindDescribe
DescribeMapping *Mapping
// ShowByIDFiltering defines a kind of filterings performed in ShowByID operation
ShowByIDFiltering []ShowByIDFiltering
}

type Mapping struct {
Expand Down Expand Up @@ -85,11 +87,11 @@ func addDescriptionMapping(op *Operation, from, to *Field) {
op.DescribeMapping = newMapping("convert", from, to)
}

func (i *Interface) newNoSqlOperation(kind string) *Interface {
func (i *Interface) newNoSqlOperation(kind string) *Operation {
sfc-gh-asawicki marked this conversation as resolved.
Show resolved Hide resolved
operation := newOperation(kind, "placeholder").
withOptionsStruct(nil)
i.Operations = append(i.Operations, operation)
return i
return operation
}

func (i *Interface) newSimpleOperation(kind string, doc string, queryStruct *QueryStruct, helperStructs ...IntoField) *Interface {
Expand Down Expand Up @@ -160,8 +162,11 @@ func (i *Interface) ShowOperation(doc string, dbRepresentation *dbStruct, resour
return i
}

func (i *Interface) ShowByIdOperation() *Interface {
return i.newNoSqlOperation(string(OperationKindShowByID))
func (i *Interface) ShowByIdOperation(filtering ...ShowByIDFilteringKind) *Interface {
op := i.newNoSqlOperation(string(OperationKindShowByID))
op.ObjectInterface = i
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The operation.ObjectInterface is being set here, because without it operation.ObjectInterface.ObjectIdentifierKind() can't be used before executing the template. It results in nil pointer dereference, same as forInterface.NameLowerCased().

I wanted to use it before the template execution to invoke the filtering just by calling {{ .WithFiltering }} in the template.

I'm open for discussion on this one.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm fine with assigning it in the constructor (newNoSqlOperation in this case), it's not bad and imo could be added to other constructors that are Interface methods

op.withFiltering(filtering...)
return i
}

func (i *Interface) DescribeOperation(describeKind DescriptionMappingKind, doc string, dbRepresentation *dbStruct, resourceRepresentation *plainStruct, queryStruct *QueryStruct) *Interface {
Expand Down
81 changes: 81 additions & 0 deletions pkg/sdk/poc/generator/show_by_id_filtering.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package generator

import (
"fmt"
"log"
)

type ShowByIDFilteringKind uint

const (
// Enables filtering with: Like
ShowByIDLikeFiltering ShowByIDFilteringKind = iota
// Enables filtering with: In
// Based on the identifier Kind
ShowByIDInFiltering
// Enables filtering with: In
// Based on the identifier Kind
ShowByIDExtendedInFiltering
sfc-gh-jmichalak marked this conversation as resolved.
Show resolved Hide resolved
// Enables filtering with: Limit
ShowByIDLimitFiltering
)

type ShowByIDFiltering interface {
WithFiltering() string
}

type showByIDFilter struct {
Name string
Kind string
Args string
}

func (s *showByIDFilter) WithFiltering() string {
return fmt.Sprintf("With%s(%s{%s})", s.Name, s.Kind, s.Args)
}

var filteringMapping = map[ShowByIDFilteringKind]func(string) ShowByIDFiltering{
ShowByIDLikeFiltering: newShowByIDLikeFiltering,
ShowByIDInFiltering: newShowByIDInFiltering,
ShowByIDExtendedInFiltering: newShowByIDExtendedInFiltering,
ShowByIDLimitFiltering: newShowByIDLimitFiltering,
}

func newShowByIDFiltering(name, kind, args string, identifierKind *string) ShowByIDFiltering {
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about using a variadic function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

will be done by formatting arguments in the create function (as discussed verbally). This way the identifier kind is removed from newShowByIDFiltering() function and there is no need for variadic anymore.

filter := &showByIDFilter{
Name: name,
Kind: kind,
Args: args,
}
if identifierKind != nil {
filter.Args = fmt.Sprintf(args, *identifierKind)
}
return filter
}

func newShowByIDLikeFiltering(string) ShowByIDFiltering {
return newShowByIDFiltering("Like", "Like", "Pattern: String(id.Name())", nil)
}

func newShowByIDInFiltering(identifierKind string) ShowByIDFiltering {
return newShowByIDFiltering("In", "In", "%[1]v: id.%[1]vId()", &identifierKind)
}

func newShowByIDExtendedInFiltering(identifierKind string) ShowByIDFiltering {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we have an enum for identifierKind?

return newShowByIDFiltering("In", "In", "In: In{%[1]v: id.%[1]vId()}", &identifierKind)
}

func newShowByIDLimitFiltering(string) ShowByIDFiltering {
return newShowByIDFiltering("Limit", "LimitFrom", "Rows: Int(1)", nil)
}

func (s *Operation) withFiltering(filtering ...ShowByIDFilteringKind) *Operation {
for _, filteringKind := range filtering {
if filter, ok := filteringMapping[filteringKind]; ok {
s.ShowByIDFiltering = append(s.ShowByIDFiltering, filter(s.ObjectInterface.ObjectIdentifierKind()))
} else {
log.Println("No showByID filtering found for kind:", filteringKind)
}
}
return s
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{{- /*gotype: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator.Interface*/ -}}

{{ $impl := .NameLowerCased }}

{{ range .Operations }}
{{ if and (eq .Name "Show") .ShowMapping }}
func (v *{{ $impl }}) Show(ctx context.Context, request *{{ .OptsField.DtoDecl }}) ([]{{ .ShowMapping.To.Name }}, error) {
Expand All @@ -13,9 +14,17 @@
return resultList, nil
}
{{ else if eq .Name "ShowByID" }}
{{ $idkind := .ObjectInterface.ObjectIdentifierKind }}
func (v *{{ $impl }}) ShowByID(ctx context.Context, id {{ .ObjectInterface.IdentifierKind }}) (*{{ .ObjectInterface.NameSingular }}, error) {
// TODO: adjust request if e.g. LIKE is supported for the resource
{{ $impl }}, err := v.Show(ctx, NewShow{{ .ObjectInterface.NameSingular }}Request())
request := NewShow{{ .ObjectInterface.NameSingular }}Request()
{{- if not .ShowByIDFiltering }}
sfc-gh-asawicki marked this conversation as resolved.
Show resolved Hide resolved
// TODO: adjust request with resource supported filtering e.g. for LIKE use 'ShowByIDLikeFiltering' in 'ShowByIdOperation()' inside the definition file.
{{ else }}
{{- range .ShowByIDFiltering }}.
{{ .WithFiltering }}
{{- end }}
{{- end }}
{{ $impl }}, err := v.Show(ctx, request)
if err != nil {
return nil, err
}
Expand Down
26 changes: 14 additions & 12 deletions pkg/sdk/secrets_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,15 +241,17 @@ var SecretsDef = g.NewInterface(
SQL("SECRETS").
OptionalLike().
OptionalExtendedIn(),
).ShowByIdOperation().
DescribeOperation(
g.DescriptionMappingKindSingleValue,
"https://docs.snowflake.com/en/sql-reference/sql/desc-secret",
secretDetailsDbRow,
secretDetails,
g.NewQueryStruct("DescribeSecret").
Describe().
SQL("SECRET").
Name().
WithValidation(g.ValidIdentifier, "name"),
)
).DescribeOperation(
g.DescriptionMappingKindSingleValue,
"https://docs.snowflake.com/en/sql-reference/sql/desc-secret",
secretDetailsDbRow,
secretDetails,
g.NewQueryStruct("DescribeSecret").
Describe().
SQL("SECRET").
Name().
WithValidation(g.ValidIdentifier, "name"),
).ShowByIdOperation(
sfc-gh-asawicki marked this conversation as resolved.
Show resolved Hide resolved
g.ShowByIDLikeFiltering,
g.ShowByIDExtendedInFiltering,
)
2 changes: 1 addition & 1 deletion pkg/sdk/secrets_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ type Secrets interface {
Alter(ctx context.Context, request *AlterSecretRequest) error
Drop(ctx context.Context, request *DropSecretRequest) error
Show(ctx context.Context, request *ShowSecretRequest) ([]Secret, error)
ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Secret, error)
Describe(ctx context.Context, id SchemaObjectIdentifier) (*SecretDetails, error)
ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Secret, error)
}

// CreateWithOAuthClientCredentialsFlowSecretOptions is based on https://docs.snowflake.com/en/sql-reference/sql/create-secret.
Expand Down
23 changes: 12 additions & 11 deletions pkg/sdk/secrets_impl_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,6 @@ func (v *secrets) Show(ctx context.Context, request *ShowSecretRequest) ([]Secre
return resultList, nil
}

func (v *secrets) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Secret, error) {
request := NewShowSecretRequest().WithIn(ExtendedIn{In: In{Schema: id.SchemaId()}}).WithLike(Like{String(id.Name())})
secrets, err := v.Show(ctx, request)
if err != nil {
return nil, err
}
return collections.FindFirst(secrets, func(r Secret) bool { return r.Name == id.Name() })
}

func (v *secrets) Describe(ctx context.Context, id SchemaObjectIdentifier) (*SecretDetails, error) {
opts := &DescribeSecretOptions{
name: id,
Expand All @@ -72,14 +63,24 @@ func (v *secrets) Describe(ctx context.Context, id SchemaObjectIdentifier) (*Sec
return result.convert(), nil
}

func (v *secrets) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Secret, error) {
request := NewShowSecretRequest().
WithLike(Like{Pattern: String(id.Name())}).
WithIn(ExtendedIn{In: In{Schema: id.SchemaId()}})
secrets, err := v.Show(ctx, request)
if err != nil {
return nil, err
}
return collections.FindFirst(secrets, func(r Secret) bool { return r.Name == id.Name() })
}

func (r *CreateWithOAuthClientCredentialsFlowSecretRequest) toOpts() *CreateWithOAuthClientCredentialsFlowSecretOptions {
opts := &CreateWithOAuthClientCredentialsFlowSecretOptions{
OrReplace: r.OrReplace,
IfNotExists: r.IfNotExists,
name: r.name,
ApiIntegration: r.ApiIntegration,

Comment: r.Comment,
Comment: r.Comment,
}

if r.OauthScopes != nil {
Expand Down
5 changes: 4 additions & 1 deletion pkg/sdk/streamlits_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ var StreamlitsDef = g.NewInterface(
OptionalLike().
OptionalIn().
OptionalLimit(),
).ShowByIdOperation().DescribeOperation(
).ShowByIdOperation(
sfc-gh-asawicki marked this conversation as resolved.
Show resolved Hide resolved
g.ShowByIDLikeFiltering,
g.ShowByIDInFiltering,
).DescribeOperation(
g.DescriptionMappingKindSingleValue,
"https://docs.snowflake.com/en/sql-reference/sql/desc-streamlit",
g.DbStruct("streamlitsDetailRow").
Expand Down
9 changes: 5 additions & 4 deletions pkg/sdk/streamlits_impl_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ func (v *streamlits) Show(ctx context.Context, request *ShowStreamlitRequest) ([
}

func (v *streamlits) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Streamlit, error) {
request := NewShowStreamlitRequest().WithIn(In{Schema: id.SchemaId()}).WithLike(Like{String(id.Name())})
request := NewShowStreamlitRequest().
WithLike(Like{Pattern: String(id.Name())}).
WithIn(In{Schema: id.SchemaId()})
streamlits, err := v.Show(ctx, request)
if err != nil {
return nil, err
Expand Down Expand Up @@ -92,9 +94,8 @@ func (r *AlterStreamlitRequest) toOpts() *AlterStreamlitOptions {
RootLocation: r.Set.RootLocation,
MainFile: r.Set.MainFile,
QueryWarehouse: r.Set.QueryWarehouse,

Comment: r.Set.Comment,
Title: r.Set.Title,
Comment: r.Set.Comment,
Title: r.Set.Title,
}

if r.Set.ExternalAccessIntegrations != nil {
Expand Down
Loading