Skip to content

Commit

Permalink
Add all_ips method to VM entities
Browse files Browse the repository at this point in the history
abstractmethod and implementations for subclasses of VM
OVIRT and vmware in particular are a real pain in the ass when it comes to addresses
With no reasonable way to determine what's a local network IP and what's a public network IP
Provide a property that just returns all the available addresses, allowing the caller to process them

Changed GCE's properties, as `ip` was returning an internal IP, and `ip_external` should have been `ip`
  • Loading branch information
mshriver committed Jun 5, 2019
1 parent 992941f commit d49d038
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 16 deletions.
10 changes: 9 additions & 1 deletion wrapanapi/entities/vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,19 @@ def is_stopping(self):
@abstractproperty
def ip(self):
"""
Returns IP address of the VM/instance
Returns: (string) externally reachable IP address of the VM/instance (when possible)
Should refresh if necessary to get most up-to-date info
"""

@abstractproperty
def all_ips(self):
"""
Returns: (list) All ip addresses available on the VM
For consistency on platforms that return link-local and restricted network addresses
"""

@abstractproperty
def creation_time(self):
"""
Expand Down
8 changes: 8 additions & 0 deletions wrapanapi/systems/ec2.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,14 @@ def ip(self):
self.refresh()
return self.raw.public_ip_address

@property
def all_ips(self):
""" Wrapping self.ip to meet abstractproperty requirement
Returns: (list) the addresses assigned to the machine
"""
return [self.ip]

@property
def type(self):
return self.raw.instance_type
Expand Down
24 changes: 19 additions & 5 deletions wrapanapi/systems/google.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,29 @@ def _get_state(self):
return self._api_state_to_vmstate(self.raw['status'])

@property
def ip(self):
def ip_internal(self):
self.refresh()
return self.raw.get('networkInterfaces')[0].get('networkIP')
try:
return self.raw.get('networkInterfaces')[0].get('networkIP')
except IndexError:
return None

@property
def ip_external(self):
def ip(self):
self.refresh()
access_configs = self.raw.get('networkInterfaces')[0].get('accessConfigs')[0]
return access_configs.get('natIP')
try:
access_configs = self.raw.get('networkInterfaces', [{}])[0].get('accessConfigs', [])[0]
return access_configs.get('natIP')
except IndexError:
return None

@property
def all_ips(self):
""" Wrapping self.ip and self.ip_internal to meet abstractproperty requirement
Returns: (list) the addresses assigned to the machine
"""
return [self.ip, self.ip_internal]

@property
def type(self):
Expand Down
12 changes: 12 additions & 0 deletions wrapanapi/systems/msazure.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@ def ip(self):

return public_ip.ip_address

@property
def all_ips(self):
""" Wrapping self.ip to meet abstractproperty requirement
TODO: Actually fetch the various addresses on non-primary interfaces
non-public addresses are not necessary for testing at this time, so not implementing
Returns: (list) the public IPv4 address in a list
"""
return [self.ip]

@property
def type(self):
return self.raw.hardware_profile.vm_size
Expand Down
9 changes: 9 additions & 0 deletions wrapanapi/systems/openstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ def ip(self):
if nic['OS-EXT-IPS:type'] == 'floating':
return str(nic['addr'])

@property
def all_ips(self):
""" Get all the IPs on the machine
Returns: (list) the addresses assigned to the machine
"""
# raw.networks is dict of network: [ip, ip] key:value pairs
return [ip for nets in self.raw.networks.values() for ip in nets]

@property
def flavor(self):
if not self._flavor:
Expand Down
26 changes: 16 additions & 10 deletions wrapanapi/systems/rhevm.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def uuid(self):
@property
def creation_time(self):
"""
Returns creation time of VM/instance
Returns creation time of VM
"""
self.refresh()
return self.raw.creation_time.astimezone(pytz.UTC)
Expand Down Expand Up @@ -252,16 +252,22 @@ def _get_state(self):
@property
def ip(self):
"""
Returns IPv4 or global IPv6 address of the VM/instance
Returns IPv4 or global IPv6 address of the VM
If there are multiple IP's on the VM, just returns the first non-link-local
"""
link_local_prefix = 'fe80::'
potentials = []
for ip in self.all_ips:
if link_local_prefix not in ip[:len(link_local_prefix)]:
return ip
return None
if not ip.startswith('fe80::'):
potentials.append(ip)
return potentials[0] if potentials else None

@property
def all_ips(self):
""" Return all of the IPs
Returns: (list) the addresses assigned to the machine
"""
ips = []
rep_dev_service = self.api.reported_devices_service()
for dev in rep_dev_service.list():
Expand All @@ -271,7 +277,7 @@ def all_ips(self):

def start(self):
"""
Starts the VM/instance. Blocks until task completes.
Starts the VM. Blocks until task completes.
Returns: True if vm action has been initiated properly
"""
Expand All @@ -287,7 +293,7 @@ def start(self):

def stop(self):
"""
Stops the VM/instance. Blocks until task completes.
Stops the VM. Blocks until task completes.
Returns: True if vm action has been initiated properly
"""
Expand All @@ -303,7 +309,7 @@ def stop(self):

def restart(self):
"""
Restarts the VM/instance. Blocks until task completes.
Restarts the VM. Blocks until task completes.
Returns: True if vm action has been initiated properly
"""
Expand All @@ -312,7 +318,7 @@ def restart(self):

def suspend(self):
"""
Suspends the VM/instance. Blocks until task completes.
Suspends the VM. Blocks until task completes.
Returns: True if vm action has been initiated properly
"""
Expand Down
5 changes: 5 additions & 0 deletions wrapanapi/systems/scvmm.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ def ip(self):
ip = data.translate(None, '{}')
return ip if ip else None

@property
def all_ips(self):
""" wrap self.ip to meet abstractproperty """
return [self.ip]

@property
def creation_time(self):
self.refresh()
Expand Down
7 changes: 7 additions & 0 deletions wrapanapi/systems/virtualcenter.py
Original file line number Diff line number Diff line change
Expand Up @@ -640,6 +640,13 @@ def ip(self):
# TypeError: ip address wasn't a string
return None

@property
def all_ips(self):
"""vmware is limited, can't get more than one IP for a vm summary
wrapper for API consistency
"""
return [self.ip]

@property
def creation_time(self):
"""Detect the vm_creation_time either via uptime if non-zero, or by last boot time
Expand Down

0 comments on commit d49d038

Please sign in to comment.