Skip to content

Commit

Permalink
Fixed azure add/remove floating ip functions
Browse files Browse the repository at this point in the history
  • Loading branch information
nuwang committed Nov 28, 2017
1 parent ef51ea4 commit fe01b83
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 32 deletions.
6 changes: 4 additions & 2 deletions cloudbridge/cloud/providers/azure/azure_client.py
Expand Up @@ -444,17 +444,19 @@ def get_nic(self, name):
return self.network_management_client. \
network_interfaces.get(self.resource_group, name)

def create_nic(self, nic_name, params):
def update_nic(self, nic_name, params):
async_nic_creation = self.network_management_client. \
network_interfaces.create_or_update(
self.resource_group,
nic_name,
params
)
nic_info = async_nic_creation.result()

return nic_info

def create_nic(self, nic_name, params):
return self.update_nic(nic_name, params)

def get_public_ip(self, name):
return self.network_management_client. \
public_ip_addresses.get(self.resource_group, name)
Expand Down
48 changes: 18 additions & 30 deletions cloudbridge/cloud/providers/azure/resources.py
Expand Up @@ -972,7 +972,7 @@ def __init__(self, provider, floating_ip):

@property
def id(self):
return self._ip.name
return self._ip.id

@property
def resource_id(self):
Expand Down Expand Up @@ -1405,44 +1405,32 @@ def _deprovision(self, private_key_path):
sftp.execute('sudo waagent -deprovision -force')
sftp.close()

def add_floating_ip(self, ip_address):
def add_floating_ip(self, floating_ip):
"""
Attaches public ip to the instance
:param ip_address:
:return:
"""
try:
ip_addresses = [ip for ip in
self._provider.azure_client.list_floating_ips()
if ip.ip_address and ip.ip_address == ip_address]
if len(ip_addresses) > 0:
"""
Add an elastic IP address to this instance.
"""
nic = self._provider.azure_client.get_nic(self._nic_ids[0])

nic.ip_configurations[0].public_ip_address = {
'id': ip_addresses[0].id
}
self._provider.azure_client.create_nic(self._nic_ids[0], nic)
return True
return False
except CloudError as cloudError:
log.exception(cloudError.message)
return False
fip = (floating_ip if isinstance(floating_ip, AzureFloatingIP) else
self._provider.networking.floating_ips.get(floating_ip))

nic = self._provider.azure_client.get_nic(self._nic_ids[0])

def remove_floating_ip(self, ip_address=None):
nic.ip_configurations[0].public_ip_address = {
'id': fip.id
}
self._provider.azure_client.update_nic(self._nic_ids[0], nic)

def remove_floating_ip(self, floating_ip):
"""
Remove a public IP address from this instance.
"""
try:
nic = self._provider.azure_client.get_nic(self._nic_ids[0])
nic.ip_configurations[0].public_ip_address = None
self._provider.azure_client.create_nic(self._nic_ids[0], nic)
return True
except CloudError as cloudError:
log.exception(cloudError.message)
return False
nic = self._provider.azure_client.get_nic(self._nic_ids[0])
for ip_config in nic.ip_configurations:
if ip_config.public_ip_address.id == floating_ip.id:
nic.ip_configurations[0].public_ip_address = None
self._provider.azure_client.update_nic(self._nic_ids[0],
nic)

def add_vm_firewall(self, fw):
'''
Expand Down

0 comments on commit fe01b83

Please sign in to comment.