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 9 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
2 changes: 1 addition & 1 deletion pkg/datasources/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ func ReadSecrets(ctx context.Context, d *schema.ResourceData, meta any) diag.Dia
req := sdk.NewShowSecretRequest()

handleLike(d, &req.Like)
err := handleExtendedIn(d, &req.ExtendedIn)
err := handleExtendedIn(d, &req.In)
if err != nil {
return diag.FromErr(err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/sdk/poc/generator/keyword_builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func (v *QueryStruct) OptionalIn() *QueryStruct {
}

func (v *QueryStruct) OptionalExtendedIn() *QueryStruct {
return v.PredefinedQueryStructField("ExtendedIn", "*ExtendedIn", KeywordOptions().SQL("IN"))
return v.PredefinedQueryStructField("In", "*ExtendedIn", KeywordOptions().SQL("IN"))
}

func (v *QueryStruct) OptionalStartsWith() *QueryStruct {
Expand Down
1 change: 1 addition & 0 deletions pkg/sdk/poc/generator/operation.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ func (i *Interface) ShowOperation(doc string, dbRepresentation *dbStruct, resour

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
}
Expand Down
87 changes: 61 additions & 26 deletions pkg/sdk/poc/generator/show_by_id_filtering.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package generator

type ShowByIDFiltering struct {
Kind string
Args string
IdentifierBased bool
}
import (
"fmt"
"log"
)

type ShowByIDFilteringKind uint

Expand All @@ -14,32 +13,68 @@ const (
// Enables filtering with: In
// Based on the identifier Kind
ShowByIDInFiltering
// Enables filtering with: ExtendedIn
// 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 _, f := range filtering {
switch f {
case ShowByIDLikeFiltering:
s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{
Kind: "Like",
Args: "Pattern: String(id.Name())",
IdentifierBased: false,
})
case ShowByIDInFiltering:
s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{
Kind: "In",
Args: "%[1]v: id.%[1]vId()",
IdentifierBased: true,
})
case ShowByIDExtendedInFiltering:
s.ShowByIDFiltering = append(s.ShowByIDFiltering, ShowByIDFiltering{
Kind: "ExtendedIn",
Args: "In: In{%[1]v: id.%[1]vId()}",
IdentifierBased: true,
})
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@
func (v *{{ $impl }}) ShowByID(ctx context.Context, id {{ .ObjectInterface.IdentifierKind }}) (*{{ .ObjectInterface.NameSingular }}, error) {
request := NewShow{{ .ObjectInterface.NameSingular }}Request()
{{- if not .ShowByIDFiltering }}
sfc-gh-asawicki marked this conversation as resolved.
Show resolved Hide resolved
// TODO: adjust request if e.g. LIKE is supported for the resource
// TODO: adjust request with resource supported filtering e.g. for LIKE use 'ShowByIDLikeFiltering' in 'ShowByIdOperation()' inside the definition file.
{{ else }}
{{- range .ShowByIDFiltering -}}
.{{- if .IdentifierBased }}
With{{ .Kind }}( {{ .Kind }} { {{ printf .Args $idkind }} })
{{- else }}
With{{ .Kind }}( {{ .Kind }} { {{ .Args }} })
{{- end }}
{{- range .ShowByIDFiltering }}.
{{ .WithFiltering }}
{{- end }}
{{- end }}
{{ $impl }}, err := v.Show(ctx, request)
Expand Down
4 changes: 2 additions & 2 deletions pkg/sdk/secrets_dto_builders_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/sdk/secrets_dto_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ type DropSecretRequest struct {
}

type ShowSecretRequest struct {
Like *Like
ExtendedIn *ExtendedIn
Like *Like
In *ExtendedIn
}

type DescribeSecretRequest struct {
Expand Down
8 changes: 4 additions & 4 deletions pkg/sdk/secrets_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,10 @@ type DropSecretOptions struct {

// ShowSecretOptions is based on https://docs.snowflake.com/en/sql-reference/sql/show-secrets.
type ShowSecretOptions struct {
show bool `ddl:"static" sql:"SHOW"`
secrets bool `ddl:"static" sql:"SECRETS"`
Like *Like `ddl:"keyword" sql:"LIKE"`
ExtendedIn *ExtendedIn `ddl:"keyword" sql:"IN"`
show bool `ddl:"static" sql:"SHOW"`
secrets bool `ddl:"static" sql:"SECRETS"`
Like *Like `ddl:"keyword" sql:"LIKE"`
In *ExtendedIn `ddl:"keyword" sql:"IN"`
}
type secretDBRow struct {
CreatedOn time.Time `db:"created_on"`
Expand Down
2 changes: 1 addition & 1 deletion pkg/sdk/secrets_gen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func TestSecrets_Show(t *testing.T) {

t.Run("show with in", func(t *testing.T) {
opts := defaultOpts()
opts.ExtendedIn = &ExtendedIn{
opts.In = &ExtendedIn{
In: In{
Account: Bool(true),
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/sdk/secrets_impl_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (v *secrets) Describe(ctx context.Context, id SchemaObjectIdentifier) (*Sec
func (v *secrets) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*Secret, error) {
request := NewShowSecretRequest().
WithLike(Like{Pattern: String(id.Name())}).
WithExtendedIn(ExtendedIn{In: In{Schema: id.SchemaId()}})
WithIn(ExtendedIn{In: In{Schema: id.SchemaId()}})
secrets, err := v.Show(ctx, request)
if err != nil {
return nil, err
Expand Down Expand Up @@ -193,8 +193,8 @@ func (r *DropSecretRequest) toOpts() *DropSecretOptions {

func (r *ShowSecretRequest) toOpts() *ShowSecretOptions {
opts := &ShowSecretOptions{
Like: r.Like,
ExtendedIn: r.ExtendedIn,
Like: r.Like,
In: r.In,
}
return opts
}
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
24 changes: 12 additions & 12 deletions pkg/sdk/testint/secrets_gen_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -594,15 +594,15 @@ func TestInt_Secrets(t *testing.T) {
secret, secretCleanup := testClientHelper().Secret.CreateWithOAuthClientCredentialsFlow(t, id, integrationId, []sdk.ApiIntegrationScope{{Scope: "foo"}})
t.Cleanup(secretCleanup)

returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}}))
returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}}))
require.NoError(t, err)
require.Contains(t, returnedSecrets, *secret)

returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}}))
returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}}))
require.NoError(t, err)
require.Contains(t, returnedSecrets, *secret)

returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}}))
returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}}))
require.NoError(t, err)
require.Contains(t, returnedSecrets, *secret)
})
Expand All @@ -612,15 +612,15 @@ func TestInt_Secrets(t *testing.T) {
secret, secretCleanup := testClientHelper().Secret.CreateWithOAuthAuthorizationCodeFlow(t, id, integrationId, "foo", refreshTokenExpiryTime)
t.Cleanup(secretCleanup)

returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}}))
returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}}))
require.NoError(t, err)
require.Contains(t, returnedSecrets, *secret)

returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}}))
returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}}))
require.NoError(t, err)
require.Contains(t, returnedSecrets, *secret)

returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}}))
returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}}))
require.NoError(t, err)
require.Contains(t, returnedSecrets, *secret)
})
Expand All @@ -640,21 +640,21 @@ func TestInt_Secrets(t *testing.T) {
secretGenericString, secretCleanupWithGenericString := testClientHelper().Secret.CreateWithGenericString(t, testClientHelper().Ids.RandomSchemaObjectIdentifier(), "foo")
t.Cleanup(secretCleanupWithGenericString)

returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}}))
returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}}))
require.NoError(t, err)
require.Contains(t, returnedSecrets, *secretOAuthClientCredentials)
require.Contains(t, returnedSecrets, *secretOAuthAuthorizationCode)
require.Contains(t, returnedSecrets, *secretBasicAuthentication)
require.Contains(t, returnedSecrets, *secretGenericString)

returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}}))
returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}}))
require.NoError(t, err)
require.Contains(t, returnedSecrets, *secretOAuthClientCredentials)
require.Contains(t, returnedSecrets, *secretOAuthAuthorizationCode)
require.Contains(t, returnedSecrets, *secretBasicAuthentication)
require.Contains(t, returnedSecrets, *secretGenericString)

returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}}))
returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}}))
require.NoError(t, err)
require.Contains(t, returnedSecrets, *secretOAuthClientCredentials)
require.Contains(t, returnedSecrets, *secretOAuthAuthorizationCode)
Expand All @@ -667,15 +667,15 @@ func TestInt_Secrets(t *testing.T) {
secret, secretCleanup := testClientHelper().Secret.CreateWithGenericString(t, id, "foo")
t.Cleanup(secretCleanup)

returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}}))
returnedSecrets, err := client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Account: sdk.Pointer(true)}}))
require.NoError(t, err)
require.Contains(t, returnedSecrets, *secret)

returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}}))
returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Database: id.DatabaseId()}}))
require.NoError(t, err)
require.Contains(t, returnedSecrets, *secret)

returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithExtendedIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}}))
returnedSecrets, err = client.Secrets.Show(ctx, sdk.NewShowSecretRequest().WithIn(sdk.ExtendedIn{In: sdk.In{Schema: id.SchemaId()}}))
require.NoError(t, err)
require.Contains(t, returnedSecrets, *secret)
})
Expand Down
Loading