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

Update implementation.py to check for .exploded attribute #108

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

palmersample
Copy link

When generating dynamic testbed objects, implementation.py expects an ipaddress object to be passed as the IP address. This change checks for the existence of the exploded attribute for each IP / host before assigning that attribute, simplifying dynamic testbed creation using string values for addresses

In addition, the IOS XE implementation has been updated to properly generate an IPv6 URL in the format [xxxx:yyyy....]:{port} to validate functionality. If this passes scrutiny and code style checks, another PR will be generated to update other implementations.

@palmersample palmersample requested a review from a team as a code owner April 27, 2023 21:01
Comment on lines 102 to 107
if 'host' in self.connection_info:
ip = self.connection_info['host']
else:
ip = self.connection_info['ip'].exploded
ip = self.connection_info['ip']
if hasattr(ip, 'exploded'):
ip = ip.exploded
Copy link
Contributor

Choose a reason for hiding this comment

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

To use an address that is not an IPAddress object, you can pass the host key instead.

For this change I suggest the following code:

ip = self.connection_info['ip']
if isinstance(ip, (ipaddress.IPv4Address, ipaddress.IPv6Address)):
   ip = ip.exploded

Copy link
Author

Choose a reason for hiding this comment

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

Minor changes to proposed solution (for your review):

  1. Reduce scope of imports - only ip_address, IPv4Address, and IPv6Address from the ipaddress package.
  2. IP logic to check if the ip is NOT an instance of IPv4 or IPv6 - if this is true, generate a new ip_address object. This allows testing of IPv6Address and subsequent URL fixup.

Example change from IOS XE follows:

            ip = self.connection_info['ip']
            if not isinstance(ip, (IPv4Address, IPv6Address)):
                ip = ip_address(ip)

            # Properly format IPv6 URL if a v6 address is provided
            if isinstance(ip, IPv6Address):
                ip = f"[{ip.exploded}]"
            else:
                ip = ip.exploded

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