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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable passing a prefix for IP addresses #75

Merged
merged 1 commit into from May 28, 2015
Merged

Enable passing a prefix for IP addresses #75

merged 1 commit into from May 28, 2015

Conversation

mfalesni
Copy link
Contributor

I hope tests say all, intended to replace https://github.com/RedHatQE/cfme_tests/blob/master/utils/randomness.py#L5

btw. what's wrong on map(str, prefix_ipv4 or []) ? Shorter than the list comprehension 馃槃

@elyezer
Copy link
Contributor

elyezer commented May 27, 2015

We may need a prefix for ipv6 too. With that said I'm in favor of changing the argument name to prefix and use that for both, ipv4 and ipv6 generation.

A ValueError could be raised if the prefix type is not a list. I will be one person that will try to pass a string instead.

@mfalesni
Copy link
Contributor Author

@elyezer I can rename it, but I am not entirely familiar with ipv6, so someone else will probably have to do it 馃槂

@elyezer
Copy link
Contributor

elyezer commented May 27, 2015

@mfalesni the ipv6 generation is pretty straight forward, and expecting a list you can decrease the number of random generation parts then join all together, the same approach for ipv4 you've done.

@omaciel
Copy link
Owner

omaciel commented May 27, 2015

@elyezer @mfalesni does it make sense for both of you to work on this new feature? :)

@mfalesni
Copy link
Contributor Author

Ok, will take a look on ipv6 too.

@elyezer
Copy link
Contributor

elyezer commented May 27, 2015

What I thought would be something like:

max_prefix_len = 7 if not ipv6 else 3 - int(ip3)
prefix = [str(field) for field in prefix_ipv4 or []]
prefix_len = len(prefix)
if prefix_len > max_prefix_len:
    raise ValueError('Prefix {} would lead to no randomness at all'.format(repr(prefix)))

if ipv6:
    ipaddr = u':'.join('{0:x}'.format(
        random.randint(0, 2**16 - 1)
    ) for i in range(8 - prefix_len))
    if prefix is not None:
        ipaddr = '{}:{}'.format(':'.join(prefix), ipaddr)
else:
    rng = 3 if ip3 else 4
    ipaddr = u".".join([str(random.randrange(0, 255, 1)) for _ in range(rng - prefix_len)])
    if prefix is not None:
        ipaddr = '{}.{}'.format('.'.join(prefix), ipaddr)
    if ip3:
        ipaddr = ipaddr + u".0"

We can improve the prefix logic in order to check after generating the random part.

@mfalesni
Copy link
Contributor Author

@elyezer Just pushed something.

"""Generates a random IP address.

You can also specify an IP address prefix if you are interested eg. in
Copy link
Owner

Choose a reason for hiding this comment

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

I'd remove the eg. here and add a , right before the etc as such: generation, etc..

@elyezer
Copy link
Contributor

elyezer commented May 27, 2015

APT

:returns: An IP address.
:rtype: str
:raises: ``ValueError`` if ``prefix`` is too long either to fit into
the IP address or disabling randomness at all.
Copy link
Owner

Choose a reason for hiding this comment

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

My previous comment may have gotten squashed, but: what does disabling randomness at all means?

Copy link
Contributor

Choose a reason for hiding this comment

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

How about:

:raises: ``ValueError`` if ``prefix`` defines a full IP addess or has no elements.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@omaciel If you specify a prefix with length of 4, then there is no randomness involved. Maybe the phrase should be written differently, I'll gladly accept an advice.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Ichimonji10 the :raises: cannot be so simply written because these limits also apply for ipv6 which has 8 fields therefore max. 7 fields of prefix.

Copy link
Contributor

Choose a reason for hiding this comment

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

@mfalesni I've updated the suggested docstring. Have a second look.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Ichimonji10 Why no elements? If the prefix is empty, then nothing special happens ...

Copy link
Contributor

Choose a reason for hiding this comment

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

@mfalesni The code will not be orthogonal if you allow an empty prefix to be specified. A user can specify an emtpy prefix either by passing in None or by passing in an empty iterable such as [] or (). Non-orthogonal code is harder to understand and more complex.

Now, if you do want to allow empty prefixes, that's fine. It makes a lot of sense, actually. But in that case, disallow None and instead write this:

def gen_ipaddr(ip3=False, ipv6=False, prefix=()):

If you do that, then there is only one way to specify an empty prefix: by passing in an empty iterable.

@mfalesni
Copy link
Contributor Author

@Ichimonji10 @elyezer @omaciel Updated according to the comments.

@omaciel
Copy link
Owner

omaciel commented May 28, 2015

ACK

1 similar comment
@elyezer
Copy link
Contributor

elyezer commented May 28, 2015

ACK

omaciel added a commit that referenced this pull request May 28, 2015
Enable passing a prefix for IP addresses
@omaciel omaciel merged commit f2f62a4 into omaciel:master May 28, 2015
@omaciel
Copy link
Owner

omaciel commented May 28, 2015

Thank you @mfalesni

10:00:18 omaciel: mfalesni++
10:00:19 robottelo: mfalesni has 1 point
10:00:56 elyezer: mfalesni++
10:00:57 robottelo: mfalesni has 2 points
10:00:59 omaciel: elyezer: ++

@mfalesni mfalesni deleted the ip-addr-prefix branch May 28, 2015 14:04
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.

None yet

4 participants