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

Conversation

sfc-gh-fbudzynski
Copy link
Collaborator

Changes

  • added generating ID() and ObjectType() helper methods

Examples

Generated examples for:
SchemaObjectIdentifier

  • secrets
  • streamlits

AccountObjectIdentifier

  • api_integrations
  • notificatoin_integrations

@sfc-gh-fbudzynski sfc-gh-fbudzynski changed the base branch from main to dev December 16, 2024 09:15
@sfc-gh-fbudzynski sfc-gh-fbudzynski marked this pull request as ready for review December 16, 2024 09:19
@sfc-gh-fbudzynski sfc-gh-fbudzynski marked this pull request as draft December 16, 2024 09:20
@sfc-gh-fbudzynski sfc-gh-fbudzynski marked this pull request as ready for review December 16, 2024 09:37
Copy link

Integration tests failure for 38b8a3e322265c1b53426416e494dade4b2d54b5

Copy link

Integration tests failure for 9f29c422c3fb2175fcdcd555a6b47c010a02a993

pkg/sdk/poc/generator/helper_methods.go Outdated Show resolved Hide resolved
pkg/sdk/poc/generator/helper_methods.go Outdated Show resolved Hide resolved
}

// 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

pkg/sdk/poc/generator/helper_methods.go Outdated Show resolved Hide resolved
Copy link

Integration tests failure for 89876035c5ca299ff39b58ac321972b4651ad1b2

Copy link

Integration tests failure for 05914c304c19eb9003b8e365d06f13e15e5728e5

@@ -150,6 +150,7 @@ var ApiIntegrationsDef = g.NewInterface(
Show().
SQL("API INTEGRATIONS").
OptionalLike(),
g.ResourceIDHelperMethod,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Resource is bad, because it's coupled with TF resource, and SDK should not be coupled with TF. My proposals are g.ShowObjectIdMethod and g.ObjectIdMethod. I like the former more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

g.ShowObjectIdMethod and g.ShowObjectTypeMethod seems fine to me as well

func (s *Secret) ID() SchemaObjectIdentifier {
return NewSchemaObjectIdentifier(s.DatabaseName, s.SchemaName, s.Name)
func (v *Secret) ID() SchemaObjectIdentifier {
return NewSchemaObjectIdentifier(v.Name, v.DatabaseName, v.SchemaName)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Wrong order.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

fixed

@@ -241,6 +241,8 @@ var SecretsDef = g.NewInterface(
SQL("SECRETS").
OptionalLike().
OptionalExtendedIn(),
g.ResourceIDHelperMethod,
g.ResourceObjectTypeHelperMethod,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similarly to the comment above: g.ShowObjectTypeMethod?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

seems good

@@ -21,9 +21,18 @@ func GenerateInterface(writer io.Writer, def *Interface) {
if o.OptsField != nil {
generateOptionsStruct(writer, o)
}
if o.ResourceHelperMethods != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: this check is not necessary - when a slice is nil, for ... range does not iterate.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

op.ObjectInterface = i
op.withFiltering(append([]ShowByIDFilteringKind{filter}, filtering...)...)
op := newNoSqlOperation(string(OperationKindShowByID)).
withObjectInterface(i).
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

}
}

var requiredFieldsForIDMethodMapping map[objectIdentifier][]string = map[objectIdentifier][]string{
Copy link
Collaborator

Choose a reason for hiding this comment

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

var idTypeParts = map[objectIdentifier][]string{ , wdyt?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

seems all right

SchemaObjectIdentifierWithArguments objectIdentifier = "SchemaObjectIdentifierWithArguments"
)

func identifierStringToObjectIdentifier(s string) objectIdentifier {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: pls add basic unit tests.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

objectIdentifier := identifierStringToObjectIdentifier(identifierString)
requiredFields, ok := requiredFieldsForIDMethodMapping[objectIdentifier]
if !ok {
log.Printf("WARNING: No required fields mapping defined for identifier %s", objectIdentifier)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: we us [WARN] prefix for such logs.

SchemaObjectIdentifier: {"Name", "DatabaseName", "SchemaName"},
}

func hasRequiredFieldsForIDMethod(structName string, helperStructs []*Field, requiredFields ...string) bool {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: we should receive requiredFields ...objectIdentifier and convert to string inside.

return newResourceHelperMethod("ObjectType", structName, "ObjectType"+structName, "ObjectType")
}

func (s *Operation) withResourceHelperMethods(structName string, helperMethods ...ResourceHelperMethodKind) *Operation {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit: in general, it's easier to read a file when upstream functions are below functions that call them. So, this function should be moved higher than newResourceIDHelperMethod and newResourceObjectTypeHelperMethod.

Copy link

Integration tests failure for 268e2d8590a4659e699c66f75ca75841698daac8

Copy link

Integration tests failure for 9e757be8daa86a0ea39820a52d7e183208c76a74

@sfc-gh-fbudzynski sfc-gh-fbudzynski changed the title chore: Generate ID and ObjectType Methods chore: Generate ID and ObjectType Show Object Methods Dec 18, 2024
Copy link

Integration tests failure for c60dec58a0f43da3e9bf4126eb358889ca0c101e

Copy link

Integration tests failure for a9d50cb6fd168a1818d4e2038ff268f7edc1c413

Copy link

Integration tests failure for 57ebbc34fb17754d7e0e1ab7df9a62ad02ea4dfe

Copy link

Integration tests failure for 560cc8b84fbd42697bf5d898308ea91743b01918

@@ -40,6 +40,8 @@ type Operation struct {
DescribeMapping *Mapping
// ShowByIDFiltering defines a kind of filterings performed in ShowByID operation
ShowByIDFiltering []ShowByIDFiltering
// ShowObjectMethods contains helper methods for the Interface file (i.e. ID(), ObjectType())
ShowObjectMethods []*ShowObjectMethod
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this should not be a configurable perk but rather a permanent feature for each compatible object. Because of that let's:

  • remove this ShowObjectMethods (and all added modifications from this file)
  • for each generated show operation let's add a conditional generation (if show output contains all needed fields, then generate ID() func, and always generate objectType func)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

@@ -0,0 +1,6 @@
{{- /*gotype: github.com/Snowflake-Labs/terraform-provider-snowflake/pkg/sdk/poc/generator.ResourceHelperMethod*/ -}}
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's have dedicated templates for both functions and let's move more logic there (e.g. for ObjectType method we should require only structName, for identifier we should require id type and args and handle it inside the template)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

done

Copy link

Integration tests failure for 5f5e26326b475687677b11f7a579267e8b173113

Copy link

Integration tests failure for cae24f6f5b8bbf68767e342ef07bbee9df13c0ce

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants