-
Notifications
You must be signed in to change notification settings - Fork 34
Macro example using nested Jinja2 transforms #47
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
Draft
ogenstad
wants to merge
1
commit into
main
Choose a base branch
from
pog-nested-transforms-macro
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| query device_startup_interface ($device: String, $interface: String) { | ||
| InfraInterface(device__name__value: $device, name__value: $interface) { | ||
| edges { | ||
| node { | ||
| id | ||
| name { | ||
| value | ||
| } | ||
| description { | ||
| value | ||
| } | ||
| enabled { | ||
| value | ||
| } | ||
| mtu { | ||
| value | ||
| } | ||
| role { | ||
| value | ||
| } | ||
| ... on InfraInterfaceL3 { | ||
| ip_addresses { | ||
| edges { | ||
| node { | ||
| address { | ||
| value | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| {% macro render_interfaces(device_interfaces) %} | ||
| {% set ns = namespace(loopback_intf_name=none, loopback_ip=none, management_intf_name=none, management_ip=none) %} | ||
| {% for intf in device_interfaces %} | ||
| {% if intf.node.role.value == "loopback" %} | ||
| {% set ns.loopback_intf_name = intf.node.name.value %} | ||
| {% set ns.loopback_ip = intf.node.ip_addresses.edges[0].node.address.value.split('/')[0] %} | ||
| {% elif intf.node.role.value == "management" %} | ||
| {% set ns.management_intf_name = intf.node.name.value %} | ||
| {% set ns.management_ip = intf.node.ip_addresses.edges[0].node.address.value.split('/')[0] %} | ||
| {% endif %} | ||
| {% endfor %} | ||
| {% for intf in device_interfaces %} | ||
| {% if intf.node.name.value != ns.management_intf_name and intf.node.name.value != ns.loopback_intf_name %} | ||
| interface {{ intf.node.name.value }} | ||
| {% if intf.node["description"]["value"] %} | ||
| description {{ intf.node["description"]["value"] }} | ||
| {% else %} | ||
| description role: {{ intf.node.role.value }} | ||
| {% endif %} | ||
| {% if "mtu" in intf.node and intf.node["mtu"]["value"] %} | ||
| mtu {{ intf.node["mtu"]["value"] }} | ||
| {% endif %} | ||
| {% if not intf.node["enabled"]["value"] %} | ||
| shutdown | ||
| {% endif %} | ||
| {% if intf.node["ip_addresses"] %} | ||
| {% for ip in intf.node["ip_addresses"]["edges"] %} | ||
| ip address {{ ip.node["address"]["value"] }} | ||
| no switchport | ||
| {% if intf.node.role.value == "peer" or intf.node.role.value == "backbone" %} | ||
| ip ospf network point-to-point | ||
| {% endif %} | ||
| {% endfor %} | ||
| {% endif %} | ||
| ! | ||
| {% endif %} | ||
| {% endfor %} | ||
| ! | ||
| interface {{ ns.management_intf_name }} | ||
| {% for intf in device_interfaces %} | ||
| {% if intf.node.name.value == ns.management_intf_name %} | ||
| {% for ip in intf["ip_addresses"] %} | ||
| ip address {{ ip["address"]["value"] }} | ||
| {% endfor %} | ||
| {% endif %} | ||
| {% endfor %} | ||
| ! | ||
| interface {{ ns.loopback_intf_name }} | ||
| {% for intf in device_interfaces %} | ||
| {% if intf.node.name.value == ns.loopback_intf_name %} | ||
| {% for ip in intf["ip_addresses"] %} | ||
| ip address {{ ip["address"]["value"] }} | ||
| {% endfor %} | ||
| {% endif %} | ||
| {% endfor %} | ||
| ! | ||
| {%- endmacro -%} | ||
| {%- if data is defined %} | ||
| {{ render_interfaces(data.InfraInterface.edges) }} | ||
| {% endif %} | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without this conditional if-statement all the rendering of the base template fails because
datais not defined. Another approach to this would be to place all macros in a separate file. Though in that case this stand-alone interface template would only import the macro and then render the macro.