Add --afi filter option to report bgp command#2133
Merged
Conversation
There was a problem hiding this comment.
Hey - I've found 1 issue, and left some high level feedback:
- In the BGP AFI filtering loop, you recompute
[a.lower() for a in parsed_args.afi]for every AFI; consider precomputing a lowercased set/list of AFIs once before the loop to avoid repeated work and make the intent clearer. - For the new
--afiargument, you may want to leveragechoices(or a small validator) in argparse to restrict values to known address families and catch invalid input early instead of silently filtering everything out.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- In the BGP AFI filtering loop, you recompute `[a.lower() for a in parsed_args.afi]` for every AFI; consider precomputing a lowercased set/list of AFIs once before the loop to avoid repeated work and make the intent clearer.
- For the new `--afi` argument, you may want to leverage `choices` (or a small validator) in argparse to restrict values to known address families and catch invalid input early instead of silently filtering everything out.
## Individual Comments
### Comment 1
<location path="osism/commands/report.py" line_range="402-407" />
<code_context>
bgp_data = json.loads(bgp_result.stdout)
for afi, afi_data in bgp_data.items():
+ if parsed_args.afi and afi.lower() not in [a.lower() for a in parsed_args.afi]:
+ continue
peers = afi_data.get("peers", {})
</code_context>
<issue_to_address>
**suggestion:** Avoid recomputing the lowercased AFI list on every loop iteration by normalizing once up front.
This recreates a lowercased AFI list on every iteration of `bgp_data.items()`. To avoid repeated work and clarify intent, normalize the filter once before the loop, e.g.
```python
afi_filter = None
if parsed_args.afi:
afi_filter = {a.lower() for a in parsed_args.afi}
for afi, afi_data in bgp_data.items():
if afi_filter is not None and afi.lower() not in afi_filter:
continue
...
```
This keeps normalization centralized and the loop leaner.
```suggestion
bgp_data = json.loads(bgp_result.stdout)
afi_filter = None
if parsed_args.afi:
afi_filter = {a.lower() for a in parsed_args.afi}
for afi, afi_data in bgp_data.items():
if afi_filter is not None and afi.lower() not in afi_filter:
continue
peers = afi_data.get("peers", {})
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
AI-assisted: Claude Code Signed-off-by: Christian Berendt <berendt@osism.tech>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
AI-assisted: Claude Code