diff --git a/pkg/sdk/application_roles_def.go b/pkg/sdk/application_roles_def.go index b26ea5da7d..bb439d1817 100644 --- a/pkg/sdk/application_roles_def.go +++ b/pkg/sdk/application_roles_def.go @@ -60,4 +60,6 @@ var ApplicationRolesDef = g.NewInterface( Identifier("ApplicationName", g.KindOfT[AccountObjectIdentifier](), g.IdentifierOptions()). OptionalLimitFrom(). WithValidation(g.ValidIdentifier, "ApplicationName"), -).ShowByIdOperation() +).ShowByIdOperationWithFiltering( + g.ShowByIDApplicationNameFiltering, +) diff --git a/pkg/sdk/application_roles_impl_gen.go b/pkg/sdk/application_roles_impl_gen.go index a02e940f3d..b61491940a 100644 --- a/pkg/sdk/application_roles_impl_gen.go +++ b/pkg/sdk/application_roles_impl_gen.go @@ -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 diff --git a/pkg/sdk/connections_def.go b/pkg/sdk/connections_def.go index cad11042c9..d320728cdb 100644 --- a/pkg/sdk/connections_def.go +++ b/pkg/sdk/connections_def.go @@ -105,4 +105,6 @@ var ConnectionDef = g.NewInterface( Show(). SQL("CONNECTIONS"). OptionalLike(), -).ShowByIdOperation() +).ShowByIdOperationWithFiltering( + g.ShowByIDLikeFiltering, +) diff --git a/pkg/sdk/connections_impl_gen.go b/pkg/sdk/connections_impl_gen.go index a1fb403c4c..ae3121870f 100644 --- a/pkg/sdk/connections_impl_gen.go +++ b/pkg/sdk/connections_impl_gen.go @@ -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 } diff --git a/pkg/sdk/poc/generator/interface.go b/pkg/sdk/poc/generator/interface.go index e5674b1e00..98c213367e 100644 --- a/pkg/sdk/poc/generator/interface.go +++ b/pkg/sdk/poc/generator/interface.go @@ -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) +} diff --git a/pkg/sdk/poc/generator/operation.go b/pkg/sdk/poc/generator/operation.go index 8c8bd84342..c8d0c7e422 100644 --- a/pkg/sdk/poc/generator/operation.go +++ b/pkg/sdk/poc/generator/operation.go @@ -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 { @@ -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 { @@ -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 { diff --git a/pkg/sdk/poc/generator/show_by_id_filtering.go b/pkg/sdk/poc/generator/show_by_id_filtering.go new file mode 100644 index 0000000000..c80ec3aef3 --- /dev/null +++ b/pkg/sdk/poc/generator/show_by_id_filtering.go @@ -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 +} diff --git a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl index 5b79463dbb..60b0cc7e0d 100644 --- a/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl +++ b/pkg/sdk/poc/generator/templates/sub_templates/implementation_functions.tmpl @@ -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) { @@ -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 } diff --git a/pkg/sdk/secrets_def.go b/pkg/sdk/secrets_def.go index 5ac89932a4..2598c8f497 100644 --- a/pkg/sdk/secrets_def.go +++ b/pkg/sdk/secrets_def.go @@ -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"), +) diff --git a/pkg/sdk/secrets_dto_builders_gen.go b/pkg/sdk/secrets_dto_builders_gen.go index 08982900a0..1d80cc5bfd 100644 --- a/pkg/sdk/secrets_dto_builders_gen.go +++ b/pkg/sdk/secrets_dto_builders_gen.go @@ -2,8 +2,6 @@ package sdk -import () - func NewCreateWithOAuthClientCredentialsFlowSecretRequest( name SchemaObjectIdentifier, ApiIntegration AccountObjectIdentifier, diff --git a/pkg/sdk/secrets_impl_gen.go b/pkg/sdk/secrets_impl_gen.go index c758ad5aad..8e65e3463a 100644 --- a/pkg/sdk/secrets_impl_gen.go +++ b/pkg/sdk/secrets_impl_gen.go @@ -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 @@ -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 { diff --git a/pkg/sdk/session_policies_def.go b/pkg/sdk/session_policies_def.go index 65f8870cf4..3b172f5562 100644 --- a/pkg/sdk/session_policies_def.go +++ b/pkg/sdk/session_policies_def.go @@ -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", diff --git a/pkg/sdk/session_policies_impl_gen.go b/pkg/sdk/session_policies_impl_gen.go index 93a0293b93..c06b9adc3b 100644 --- a/pkg/sdk/session_policies_impl_gen.go +++ b/pkg/sdk/session_policies_impl_gen.go @@ -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() }) } diff --git a/pkg/sdk/streamlits_def.go b/pkg/sdk/streamlits_def.go index bbe422424a..e7bf14bb13 100644 --- a/pkg/sdk/streamlits_def.go +++ b/pkg/sdk/streamlits_def.go @@ -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"). diff --git a/pkg/sdk/streamlits_impl_gen.go b/pkg/sdk/streamlits_impl_gen.go index 0544bc8cfe..ac20497773 100644 --- a/pkg/sdk/streamlits_impl_gen.go +++ b/pkg/sdk/streamlits_impl_gen.go @@ -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 @@ -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 {