|
1 | 1 | from __future__ import unicode_literals |
2 | 2 | from __future__ import absolute_import |
3 | 3 |
|
4 | | -import itertools |
5 | 4 | from time import sleep |
6 | 5 |
|
7 | | -from upcloud_api import Storage, IPAddress, UpCloudAPIError, OperatingSystems |
| 6 | +from upcloud_api import Storage, IPAddress, OperatingSystems |
8 | 7 | from upcloud_api.utils import try_it_n_times |
9 | 8 |
|
10 | 9 |
|
@@ -392,48 +391,46 @@ def to_dict(self): |
392 | 391 | del fields['cloud_manager'] |
393 | 392 | return fields |
394 | 393 |
|
395 | | - def get_public_ip(self, addr_family='IPv4', strict=False): |
| 394 | + def get_ip(self, access='public', addr_family=None, strict=None): |
396 | 395 | """ |
397 | | - Return a server's public IP. |
| 396 | + Return the server's IP address. |
398 | 397 |
|
399 | 398 | Params: |
400 | | - - addr_family: prefer IPv4 (default) or IPv6. |
401 | | - - strict mode (false/off by default): only return IP if it belongs to |
402 | | - addr_family (IPv4 or IPv6). |
403 | | -
|
404 | | - Tries to fetch Server data from API if ip_addresses not set. |
405 | | -
|
406 | | - New in 3.4: |
407 | | - - possibility to specify which protocol is preferred via addr_family, |
408 | | - instead of always preferring IPv4. |
409 | | - - strict mode |
| 399 | + - addr_family: IPv4, IPv6 or None. None prefers IPv4 but will |
| 400 | + return IPv6 if IPv4 addr was not available. |
| 401 | + - access: 'public' or 'private' |
410 | 402 | """ |
411 | | - if addr_family not in ['IPv4', 'IPv6']: |
412 | | - raise Exception("`addr_family` must be 'IPv4' or 'IPv6'") |
| 403 | + if addr_family not in ['IPv4', 'IPv6', None]: |
| 404 | + raise Exception("`addr_family` must be 'IPv4', 'IPv6' or None") |
| 405 | + |
| 406 | + if access not in ['private', 'public']: |
| 407 | + raise Exception("`access` must be 'public' or 'private'") |
413 | 408 |
|
414 | 409 | if not hasattr(self, 'ip_addresses'): |
415 | 410 | self.populate() |
416 | 411 |
|
417 | | - # server can have several public IPs |
418 | | - public_ip_addrs = [] |
419 | | - for ip_addr in self.ip_addresses: |
420 | | - if ip_addr.access == 'public': |
421 | | - public_ip_addrs.append(ip_addr) |
422 | | - |
423 | | - if not public_ip_addrs: |
424 | | - return None |
| 412 | + # server can have several public or private IPs |
| 413 | + ip_addrs = [ |
| 414 | + ip_addr for ip_addr in self.ip_addresses |
| 415 | + if ip_addr.access == access |
| 416 | + ] |
425 | 417 |
|
426 | | - # prefer addr_family |
427 | | - for ip_addr in public_ip_addrs: |
428 | | - if ip_addr.family == addr_family: |
| 418 | + # prefer addr_family (or IPv4 if none given) |
| 419 | + preferred_family = addr_family if addr_family else 'IPv4' |
| 420 | + for ip_addr in ip_addrs: |
| 421 | + if ip_addr.family == preferred_family: |
429 | 422 | return ip_addr.address |
430 | 423 |
|
431 | | - # strict mode: either find addr_family or don't |
432 | | - if strict: |
433 | | - return None |
| 424 | + # any IP (of the right access) will do if available and addr_family is None |
| 425 | + return ip_addrs[0].address if ip_addrs and not addr_family else None |
| 426 | + |
| 427 | + def get_public_ip(self, addr_family=None, *args, **kwargs): |
| 428 | + """Alias for get_ip('public')""" |
| 429 | + return self.get_ip('public', addr_family, *args, **kwargs) |
434 | 430 |
|
435 | | - # not stict mode: any public IP will do if addr_family didn't match |
436 | | - return public_ip_addrs[0].address |
| 431 | + def get_private_ip(self, addr_family=None, *args, **kwargs): |
| 432 | + """Alias for get_ip('private')""" |
| 433 | + return self.get_ip('private', addr_family, *args, **kwargs) |
437 | 434 |
|
438 | 435 | def _wait_for_state_change(self, target_states, update_interval=10): |
439 | 436 | """ |
|
0 commit comments