Skip to content

Commit

Permalink
chore: Add ShowByID filtering generation (#3227)
Browse files Browse the repository at this point in the history
<!-- Feel free to delete comments as you fill this in -->

<!-- summary of changes -->
## Changes
- Added ShowByID filtering options to `implement_functions` template
- New filed in `Operation` struct with Filtering options

To generate filtering options, add filtering options in
`ShowByIDOperation`:
```go
).ShowByIdOperation(
	g.ShowByIDLikeFiltering,
	g.ShowByIDExtendedInFiltering,
)
```
## Test Plan
<!-- detail ways in which this PR has been tested or needs to be tested
-->
Generation for `Like` and `ExtendedIn` for SchemaObjectidentifier
(Secrets):
* [x] acceptance tests 
* [x] integration tests 
* [x] unit tests 
---
Generation for `Like` and `In` for SchemaObjectIdentifier (Streamlits):
* [x] acceptance tests 
* [x] integration tests 
* [x] unit tests 
---
Generation for `Like` for AccountObjectIdentifier (Connections):
* [x] acceptance tests 
* [x] integration tests 
* [x] unit tests 
---
Generation for `ApplicationName` for DatabaseObjectIdentifier
(Application Roles):
* [x] acceptance tests 
* [x] integration tests 
* [x] unit tests 

## Notes 
- `WithIn` and `WithLike` are mostly used in `ShowByID()` 
- no objects use `Limit` and `StartsWith` in `ShowByID()`
- `application_roles` is the only `DatabaseObejctIdentifier` with a
`_def` file
- `application_roles` does not implement any of the common filtering
options e.g. `OptionalLike()` or `OptionalIn()`
- `application_roles` implement `WithApplicationName` filtering option
that has been added
  • Loading branch information
sfc-gh-fbudzynski authored Dec 9, 2024
1 parent 246547f commit 548ec42
Show file tree
Hide file tree
Showing 15 changed files with 185 additions and 37 deletions.
4 changes: 3 additions & 1 deletion pkg/sdk/application_roles_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ var ApplicationRolesDef = g.NewInterface(
Identifier("ApplicationName", g.KindOfT[AccountObjectIdentifier](), g.IdentifierOptions()).
OptionalLimitFrom().
WithValidation(g.ValidIdentifier, "ApplicationName"),
).ShowByIdOperation()
).ShowByIdOperationWithFiltering(
g.ShowByIDApplicationNameFiltering,
)
3 changes: 2 additions & 1 deletion pkg/sdk/application_roles_impl_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ func (v *applicationRoles) Show(ctx context.Context, request *ShowApplicationRol
}

func (v *applicationRoles) ShowByID(ctx context.Context, id DatabaseObjectIdentifier) (*ApplicationRole, error) {
request := NewShowApplicationRoleRequest().WithApplicationName(id.DatabaseId())
request := NewShowApplicationRoleRequest().
WithApplicationName(id.DatabaseId())
applicationRoles, err := v.Show(ctx, request)
if err != nil {
return nil, err
Expand Down
4 changes: 3 additions & 1 deletion pkg/sdk/connections_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,6 @@ var ConnectionDef = g.NewInterface(
Show().
SQL("CONNECTIONS").
OptionalLike(),
).ShowByIdOperation()
).ShowByIdOperationWithFiltering(
g.ShowByIDLikeFiltering,
)
7 changes: 3 additions & 4 deletions pkg/sdk/connections_impl_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@ func (v *connections) Show(ctx context.Context, request *ShowConnectionRequest)
}

func (v *connections) ShowByID(ctx context.Context, id AccountObjectIdentifier) (*Connection, error) {
connections, err := v.Show(ctx, NewShowConnectionRequest().WithLike(
Like{
Pattern: String(id.Name()),
}))
request := NewShowConnectionRequest().
WithLike(Like{Pattern: String(id.Name())})
connections, err := v.Show(ctx, request)
if err != nil {
return nil, err
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/sdk/poc/generator/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,9 @@ 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 the prefix "Database")
func (i *Interface) ObjectIdentifierPrefix() idPrefix {
// return strings.Replace(i.IdentifierKind, "ObjectIdentifier", "", 1)
return identifierStringToPrefix(i.IdentifierKind)
}
19 changes: 15 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,10 @@ func addDescriptionMapping(op *Operation, from, to *Field) {
op.DescribeMapping = newMapping("convert", from, to)
}

func (i *Interface) newNoSqlOperation(kind string) *Interface {
func newNoSqlOperation(kind string) *Operation {
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 @@ -161,7 +162,17 @@ func (i *Interface) ShowOperation(doc string, dbRepresentation *dbStruct, resour
}

func (i *Interface) ShowByIdOperation() *Interface {
return i.newNoSqlOperation(string(OperationKindShowByID))
op := newNoSqlOperation(string(OperationKindShowByID))
i.Operations = append(i.Operations, op)
return i
}

func (i *Interface) ShowByIdOperationWithFiltering(filter ShowByIDFilteringKind, filtering ...ShowByIDFilteringKind) *Interface {
op := newNoSqlOperation(string(OperationKindShowByID))
op.ObjectInterface = i
op.withFiltering(append([]ShowByIDFilteringKind{filter}, filtering...)...)
i.Operations = append(i.Operations, op)
return i
}

func (i *Interface) DescribeOperation(describeKind DescriptionMappingKind, doc string, dbRepresentation *dbStruct, resourceRepresentation *plainStruct, queryStruct *QueryStruct) *Interface {
Expand Down
113 changes: 113 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,113 @@
package generator

import (
"fmt"
"log"
)

type ShowByIDFilteringKind uint

const (
ShowByIDLikeFiltering ShowByIDFilteringKind = iota
ShowByIDInFiltering
ShowByIDExtendedInFiltering
ShowByIDApplicationNameFiltering
ShowByIDNoFiltering
)

type idPrefix string

const (
AccountIdentifierPrefix idPrefix = "Account"
DatabaseIdentifierPrefix idPrefix = "Database"
SchemaIdentifierPrefix idPrefix = "Schema"
)

func identifierStringToPrefix(s string) idPrefix {
switch s {
case "AccountObjectIdentifier":
return AccountIdentifierPrefix
case "DatabaseObjectIdentifier":
return DatabaseIdentifierPrefix
case "SchemaObjectIdentifier":
return SchemaIdentifierPrefix
default:
return ""
}
}

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)
}

func newShowByIDFiltering(name, kind, args string) ShowByIDFiltering {
return &showByIDFilter{
Name: name,
Kind: kind,
Args: args,
}
}

func newShowByIDNoFiltering() ShowByIDFiltering {
return newShowByIDFiltering("NoFiltering", "", "")
}

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

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

func newShowByIDExtendedInFiltering(identifierKind idPrefix) ShowByIDFiltering {
return newShowByIDFiltering("In", "ExtendedIn", fmt.Sprintf("In: In{%[1]v: id.%[1]vId()}", identifierKind))
}

type showByIDApplicationFilter struct {
showByIDFilter
}

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

func newShowByIDApplicationFiltering() ShowByIDFiltering {
return &showByIDApplicationFilter{
showByIDFilter: showByIDFilter{
Name: "ApplicationName",
Kind: "",
Args: "id.DatabaseId()",
},
}
}

func (s *Operation) withFiltering(filtering ...ShowByIDFilteringKind) *Operation {
for _, filteringKind := range filtering {
switch filteringKind {
case ShowByIDInFiltering:
s.ShowByIDFiltering = append(s.ShowByIDFiltering, newShowByIDInFiltering(s.ObjectInterface.ObjectIdentifierPrefix()))
case ShowByIDExtendedInFiltering:
s.ShowByIDFiltering = append(s.ShowByIDFiltering, newShowByIDExtendedInFiltering(s.ObjectInterface.ObjectIdentifierPrefix()))
case ShowByIDLikeFiltering:
s.ShowByIDFiltering = append(s.ShowByIDFiltering, newShowByIDLikeFiltering())
case ShowByIDApplicationNameFiltering:
s.ShowByIDFiltering = append(s.ShowByIDFiltering, newShowByIDApplicationFiltering())
case ShowByIDNoFiltering:
s.ShowByIDFiltering = []ShowByIDFiltering{newShowByIDNoFiltering()}
default:
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 @@ -14,8 +15,13 @@
}
{{ else if eq .Name "ShowByID" }}
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()
{{- range .ShowByIDFiltering }}
{{- if not (eq .Name "NoFiltering") -}}.
{{ .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"),
)
).ShowByIdOperationWithFiltering(
g.ShowByIDLikeFiltering,
g.ShowByIDExtendedInFiltering,
).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"),
)
2 changes: 0 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.

7 changes: 4 additions & 3 deletions pkg/sdk/secrets_impl_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ func (v *secrets) Show(ctx context.Context, request *ShowSecretRequest) ([]Secre
}

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())})
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
Expand All @@ -78,8 +80,7 @@ func (r *CreateWithOAuthClientCredentialsFlowSecretRequest) toOpts() *CreateWith
IfNotExists: r.IfNotExists,
name: r.name,
ApiIntegration: r.ApiIntegration,

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

if r.OauthScopes != nil {
Expand Down
3 changes: 3 additions & 0 deletions pkg/sdk/session_policies_def.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ var SessionPoliciesDef = g.NewInterface(
Show().
SQL("SESSION POLICIES"),
).
ShowByIdOperationWithFiltering(
g.ShowByIDNoFiltering,
).
DescribeOperation(
g.DescriptionMappingKindSingleValue,
"https://docs.snowflake.com/en/sql-reference/sql/desc-session-policy",
Expand Down
4 changes: 2 additions & 2 deletions pkg/sdk/session_policies_impl_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ func (v *sessionPolicies) Show(ctx context.Context, request *ShowSessionPolicyRe
}

func (v *sessionPolicies) ShowByID(ctx context.Context, id SchemaObjectIdentifier) (*SessionPolicy, error) {
sessionPolicies, err := v.Show(ctx, NewShowSessionPolicyRequest())
request := NewShowSessionPolicyRequest()
sessionPolicies, err := v.Show(ctx, request)
if err != nil {
return nil, err
}

return collections.FindFirst(sessionPolicies, func(r SessionPolicy) bool { return r.Name == id.Name() })
}

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(
).ShowByIdOperationWithFiltering(
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

0 comments on commit 548ec42

Please sign in to comment.