Skip to content

Commit

Permalink
Merge pull request #599 from QualiSystems/bug/rollback_should_clear_n…
Browse files Browse the repository at this point in the history
…sg_artifacts

fixed bug where VM nsgs would not get deleted during rollback, nor in…
  • Loading branch information
nahumtimerman authored Mar 14, 2019
2 parents c64b95c + dba1969 commit 2978606
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
31 changes: 29 additions & 2 deletions package/cloudshell/cp/azure/domain/services/network_service.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import time

import azure
from azure.mgmt.network.models import NetworkInterface, NetworkInterfaceIPConfiguration, VirtualNetwork, RouteTable, Route
from azure.mgmt.network.models import NetworkInterface, NetworkInterfaceIPConfiguration, VirtualNetwork, RouteTable, \
Route
from retrying import retry

from cloudshell.cp.azure.common.helpers.ip_allocation_helper import is_static_allocation, to_azure_type
Expand Down Expand Up @@ -117,7 +118,8 @@ def create_network_for_vm(self,

@retry(stop_max_attempt_number=5, wait_fixed=2000, retry_on_exception=retry_if_connection_error)
def create_nic(self, interface_name, group_name, network_client, public_ip_address, region,
subnet, private_ip_allocation_method, tags, logger, reservation_id, cloudshell_session, network_security_group=None):
subnet, private_ip_allocation_method, tags, logger, reservation_id, cloudshell_session,
network_security_group=None):
"""
The method creates or updates network interface.
Parameter
Expand Down Expand Up @@ -444,3 +446,28 @@ def get_virtual_network_by_tag(self, virtual_networks, tag_key, tag_value):
if network and self.tags_service.try_find_tag(
tags_list=network.tags, tag_key=tag_key) == tag_value),
None)

@retry(stop_max_attempt_number=5, wait_fixed=2000, retry_on_exception=retry_if_connection_error)
def delete_nsg_artifacts_associated_with_vm(self, network_client, resource_group_name, vm_name):
"""
:param azure.mgmt.network.network_management_client.NetworkManagementClient network_client:
:param str resource_group_name:
:param str vm_name:
"""

network_security_groups = network_client.network_security_groups.list(resource_group_name)
for nsg in network_security_groups:
if vm_name in nsg.name:
# rollback vm nsg
poller = network_client.network_security_groups.delete(resource_group_name,
nsg.name)
poller.wait()

if 'sandbox_all_subnets' in nsg.name:
for rule in nsg.security_rules:
if vm_name in rule.name:
# rollback inbound ports
poller = network_client.security_rules.delete(resource_group_name,
nsg.name,
rule.name)
poller.wait()
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,11 @@ def _rollback_deployed_resources(self, logger, compute_client, network_client, g
except:
logger.exception('Failed to released ips from pool')

self.network_service.delete_nsg_artifacts_associated_with_vm(
network_client=network_client,
resource_group_name=group_name,
vm_name=vm_name)

def _get_public_ip_address(self, network_client, azure_vm_deployment_model, group_name, ip_name,
cancellation_context, logger):
"""
Expand Down

0 comments on commit 2978606

Please sign in to comment.