This is an automated email from the ASF dual-hosted git repository.

lfrolov pushed a commit to branch DATALAB-2321
in repository https://gitbox.apache.org/repos/asf/incubator-datalab.git

commit 01e3bf9205695a3e83a4ff3aaa32c58d9462cf99
Author: leonidfrolov <[email protected]>
AuthorDate: Tue May 18 10:27:18 2021 +0300

    [DATALAB-2321]: added removal of project subnet from predefined vpc and 
security rule from predefined edge security group during ssn termination
---
 .../src/general/lib/azure/meta_lib.py              | 17 ++++++++++++++-
 .../src/general/scripts/azure/ssn_terminate.py     | 25 ++++++++++++++++------
 2 files changed, 34 insertions(+), 8 deletions(-)

diff --git a/infrastructure-provisioning/src/general/lib/azure/meta_lib.py 
b/infrastructure-provisioning/src/general/lib/azure/meta_lib.py
index 821d304..510e875 100644
--- a/infrastructure-provisioning/src/general/lib/azure/meta_lib.py
+++ b/infrastructure-provisioning/src/general/lib/azure/meta_lib.py
@@ -133,9 +133,24 @@ class AzureMeta:
                                    file=sys.stdout)}))
             traceback.print_exc(file=sys.stdout)
 
+    def list_security_group_rules(self, resource_group_name, sg_name):
+        try:
+            result = 
self.network_client.security_rules.list(resource_group_name, sg_name)
+            return result
+        except AzureExceptions.CloudError as err:
+            if err.status_code == 404:
+                return ''
+        except Exception as err:
+            logging.info(
+                "Unable to get list of security group rules: " + str(err) + 
"\n Traceback: " + traceback.print_exc(file=sys.stdout))
+            append_result(str({"error": "Unable to get list of rules",
+                               "error_message": str(err) + "\n Traceback: " + 
traceback.print_exc(
+                                   file=sys.stdout)}))
+            traceback.print_exc(file=sys.stdout)
+
     def list_subnets(self, resource_group_name, vpc_name):
         try:
-            result = 
self.network_client.virtual_networks.subnets.list(resource_group_name, vpc_name)
+            result = self.network_client.subnets.list(resource_group_name, 
vpc_name)
             return result
         except AzureExceptions.CloudError as err:
             if err.status_code == 404:
diff --git 
a/infrastructure-provisioning/src/general/scripts/azure/ssn_terminate.py 
b/infrastructure-provisioning/src/general/scripts/azure/ssn_terminate.py
index 03aa08b..38ebffc 100644
--- a/infrastructure-provisioning/src/general/scripts/azure/ssn_terminate.py
+++ b/infrastructure-provisioning/src/general/scripts/azure/ssn_terminate.py
@@ -117,17 +117,28 @@ def terminate_ssn_node(resource_group_name, 
service_base_name, vpc_name, region)
     if 'azure_vpc_name' in os.environ:
         print("Removing subnets in predefined VPC")
         try:
-            print('========')
-            print(AzureMeta.list_subnets(resource_group_name, 
os.environ['azure_vpc_name']))
-            print('========')
-            for i in AzureMeta.get_vpc(resource_group_name, 
os.environ['azure_vpc_name']):
-                if service_base_name in i['name']:
-                    AzureActions.remove_subnet(resource_group_name, 
os.environ['azure_vpc_name'], i['name'])
-                    print("Subnet {} has been removed from VPC 
{}".format(i['name'], os.environ['azure_vpc_name']))
+            for subnet in AzureMeta.list_subnets(resource_group_name, 
os.environ['azure_vpc_name']):
+                subnet_name = str(subnet)[str(subnet).find("'name': '") + 9 : 
str(subnet).find("', 'etag':")]
+                if service_base_name in subnet_name:
+                    AzureActions.remove_subnet(resource_group_name, 
os.environ['azure_vpc_name'], subnet_name)
+                    print("Subnet {} has been removed from VPC 
{}".format(subnet_name, os.environ['azure_vpc_name']))
         except Exception as err:
             datalab.fab.append_result("Failed to remove subnets in predefined 
VPC", str(err))
             sys.exit(1)
 
+    print("Removing rules in predefined edge security group")
+    try:
+        if 'azure_edge_security_group_name' in os.environ:
+            for rule in 
AzureMeta.list_security_group_rules(resource_group_name, 
os.environ['azure_edge_security_group_name']):
+                rule_name = str(rule)[str(rule).find("'name': '") + 9 : 
str(rule).find("', 'etag':")]
+                if service_base_name in rule_name:
+                    
AzureActions.remove_security_rules(os.environ['azure_edge_security_group_name'],
+                                               resource_group_name, rule_name)
+                    print("Rule {} is removed".format(rule_name))
+    except Exception as err:
+        datalab.fab.append_result("Failed to remove rules in predefined edge 
security group", str(err))
+        sys.exit(1)
+
     print("Removing VPC")
     try:
         if AzureMeta.get_vpc(resource_group_name, service_base_name + '-vpc'):

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to