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

bug: Jinja computed attribute failure if related node is not found #5768

Open
BeArchiTek opened this issue Feb 17, 2025 · 5 comments
Open

bug: Jinja computed attribute failure if related node is not found #5768

BeArchiTek opened this issue Feb 17, 2025 · 5 comments
Labels
group/backend Issue related to the backend (API Server, Git Agent) type/bug Something isn't working as expected

Comments

@BeArchiTek
Copy link
Contributor

Component

API Server / GraphQL

Infrahub version

1.1.6

Current Behavior

When a computed_attribute (in jinja2) is using an optional related node, and this is one is not set, the API reply with a cryptic "Unable to find the peer"

Image

The schema used for this :

  - name: BGPPeerGroup
    namespace: Routing
    menu_placement: RoutingBGPSession
    uniqueness_constraints:
      - ["name__value"]
    human_friendly_id:
      - name__value
    order_by:
      - name__value
    display_labels:
      - description__value
    attributes:
      - name: name
        kind: Text
        optional: false
        order_weight: 1000
      - name: description
        kind: Text
        computed_attribute:
          kind: Jinja2
          jinja2_template: "{% if remote_as__asn__value %}.{{ remote_as__asn__value }}{% endif %}.{{ address_family__value }}"
        read_only: true
        order_weight: 1100
      - name: address_family
        kind: Dropdown
        choices:
          - name: "ipv4"
            label: IPv4
            description: "Policy applies to IPv4 addresses."
            color: "#E6E6FA"  # lavender
          - name: "ipv6"
            label: IPv6
            description: "Policy applies to IPv6 addresses."
            color: "#E6E6FA"  # lavender
        default_value: "ipv4"
        order_weight: 1150
    relationships:
      - name: local_as
        identifier: bgppeergroup__local_as
        peer: RoutingAutonomousSystem
        optional: true
        cardinality: one
        kind: Attribute
      - name: remote_as
        identifier: bgppeergroup__remote_as
        peer: RoutingAutonomousSystem
        optional: true
        cardinality: one
        kind: Attribute

Expected Behavior

The API should provide a better error for the non-creation of the node.

Steps to Reproduce

1- load the schema above in infrahub as well as this one :

  - name: AutonomousSystem
    namespace: Routing
    uniqueness_constraints:
      - ["asn__value"]
    human_friendly_id:
      - "asn__value"
    order_by:
      - asn__value
    display_labels:
      - asn__value
    attributes:
      - name: name
        kind: Text
        computed_attribute:
          kind: Jinja2
          jinja2_template: "AS{{ asn__value }}"
        read_only: true
        description: "Name of the Autonomous System"
        order_weight: 1000
      - name: asn
        kind: Number
        description: "Autonomous System Number"
        order_weight: 1050

3 - try creating a RoutingBGPPeerGroup without any remote_as

Additional Information

A better if/else test seems to be

jinja2_template: "{% if remote_as__asn__value is defined and remote_as__asn__value %}.{{ remote_as__asn__value }}{% endif %}.{{ address_family__value }}"

But I find strange we can't directly use the relationship name like : if remote_as

@BeArchiTek BeArchiTek added the type/bug Something isn't working as expected label Feb 17, 2025
@BeArchiTek BeArchiTek changed the title bug: Jinja aomputed attribute failure if related node is not found bug: Jinja computed attribute failure if related node is not found Feb 17, 2025
@BeArchiTek BeArchiTek added the group/backend Issue related to the backend (API Server, Git Agent) label Feb 17, 2025
@ogenstad
Copy link
Contributor

The error would come from the fact that the attribute is marked as mandatory. It could of course return a more user friendly error, but based on the above it seems like you actually want it to return a blank string or undefined value for situations like this?

@BeArchiTek
Copy link
Contributor Author

The compute_attribute is mandatory but notremote_as, I would then expect to only have the existing piece which would be the address_family_value

@ogenstad
Copy link
Contributor

What about if it looks like this:

jinja2_template: "{{ remote_as__asn__value }}"

Would you say that this should result in an error or an empty string?

@BeArchiTek
Copy link
Contributor Author

I would expect an error if there is not default_value. For example something like

jinja2_template: "{{ remote_as__asn__value | default_value("XXX") }}"

But then we could use default_value(omitted) which would result in an empty string I guess 🤔

@ogenstad
Copy link
Contributor

I was thinking along the same lines.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
group/backend Issue related to the backend (API Server, Git Agent) type/bug Something isn't working as expected
Projects
None yet
Development

No branches or pull requests

2 participants