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

Validate Assigned Types #342

Closed
milesstoetzner opened this issue Aug 23, 2024 · 3 comments
Closed

Validate Assigned Types #342

milesstoetzner opened this issue Aug 23, 2024 · 3 comments
Assignees

Comments

@milesstoetzner
Copy link

Following #341, it would be great if there is a validation that, e.g., an interface type can not be assigned to a node template or artifact definition.

The following simple example does not throw a validation error even tho an interface type is assigned to a node template and artifact definition.

apiVersion: unfurl/v1alpha1
kind: Ensemble
spec:
  service_template:
    interface_types:
      my_interface_type:
        derived_from: tosca.interfaces.Root

    topology_template:
      node_templates:
        my_app:
          type: my_interface_type # <--- invalid
          
          artifacts:
            my_artifact:
              type: my_interface_type # <--- invalid
              file: my_artifact_file

I tried this on Unfurl v1.0.0 and v1.1.0.

Best regards
Miles

@aszs
Copy link
Member

aszs commented Oct 12, 2024

yes it should but unfortunately unless we require types to derive from the root tosca types (like tosca.interfaces.Root or tosca.nodes.Root) it would be hard to check that. I'm thinking maybe issue a warning if the type isn't derived from one of those and do the validation check when they are.
More generally, my focus has been on the Python DSL -- it can do far more sophisticated validation then the fairly primitive YAML parser and works with any IDE that supports Python. For example, I just ran unfurl --home '' export --format python i342.yaml on your example above and this is the python it generated:

import tosca
from tosca import ArtifactEntity, Interface, Node


class my_interface_type(tosca.interfaces.Root):
    pass


my_app: Node = my_interface_type(
    "my_app",
)
__my_app_my_artifact: ArtifactEntity = my_interface_type(
    "my_artifact",
    file="my_artifact_file",
)
my_app.my_artifact = __my_app_my_artifact  # type: ignore[attr-defined]

Running mypy on that will find all the validation errors and if you open it in an IDE VS-Code it will highlight the errors as you type them. I think it's a much better experience than writing in YAML.

@aszs aszs self-assigned this Oct 12, 2024
@milesstoetzner
Copy link
Author

Interesting that the Python DSL would catch this.

unless we require types to derive from the root tosca types ... it would be hard to check that

I don't think that is even required.
We already know if a type is for an artifact or interface etc simply by the fact if its defined in aritfact_types or interface_types etc.
So when parsing the YAML one could attach a corresponding metadata to the type and do the checks later based on that.

@aszs
Copy link
Member

aszs commented Oct 14, 2024

We already know if a type is for an artifact or interface etc simply by the fact if its defined in aritfact_types or interface_types etc.

Good point -- that does make validation a lot easier! So yeah, I'll add it soon...

aszs added a commit that referenced this issue Oct 25, 2024
…entity type (fixes #342 but disabled for now unless UNFURL_VALIDATION_MODE env var includes "types").

Enable this in unit tests.
@aszs aszs closed this as completed in 9380917 Nov 12, 2024
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

No branches or pull requests

2 participants