Skip to content

Commit 3555739

Browse files
committed
get_ip, get_public_ip and get_private_ip
1 parent 3e6dc13 commit 3555739

File tree

1 file changed

+29
-32
lines changed

1 file changed

+29
-32
lines changed

upcloud_api/server.py

Lines changed: 29 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
from __future__ import unicode_literals
22
from __future__ import absolute_import
33

4-
import itertools
54
from time import sleep
65

7-
from upcloud_api import Storage, IPAddress, UpCloudAPIError, OperatingSystems
6+
from upcloud_api import Storage, IPAddress, OperatingSystems
87
from upcloud_api.utils import try_it_n_times
98

109

@@ -392,48 +391,46 @@ def to_dict(self):
392391
del fields['cloud_manager']
393392
return fields
394393

395-
def get_public_ip(self, addr_family='IPv4', strict=False):
394+
def get_ip(self, access='public', addr_family=None, strict=None):
396395
"""
397-
Return a server's public IP.
396+
Return the server's IP address.
398397
399398
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'
410402
"""
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'")
413408

414409
if not hasattr(self, 'ip_addresses'):
415410
self.populate()
416411

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+
]
425417

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:
429422
return ip_addr.address
430423

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)
434430

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)
437434

438435
def _wait_for_state_change(self, target_states, update_interval=10):
439436
"""

0 commit comments

Comments
 (0)