diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 0c526fb..ab9c4eb 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -26,7 +26,7 @@ repos: - id: isort - repo: https://github.com/myint/autoflake - rev: v2.2.1 + rev: v2.3.0 hooks: - id: autoflake args: diff --git a/requirements.txt b/requirements.txt index 02eb649..5b10d96 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,7 +12,7 @@ pyvim==3.0.3 redis==5.0.1 requests==2.31.0 pyyaml==6.0.1 -pre-commit==3.6.1 +pre-commit==3.6.2 types-PyYAML==6.0.12.12 sympy==1.12 -types-redis==4.6.0.20240106 +types-redis==4.6.0.20240218 diff --git a/simple_vm_client/openstack_connector/openstack_connector.py b/simple_vm_client/openstack_connector/openstack_connector.py index a201193..db8e90a 100644 --- a/simple_vm_client/openstack_connector/openstack_connector.py +++ b/simple_vm_client/openstack_connector/openstack_connector.py @@ -1384,10 +1384,14 @@ def add_research_environment_security_group( self, server_id: str, security_group_name: str ): logger.info(f"Setting up {security_group_name} security group for {server_id}") - server = self.get_server(openstack_id=server_id) - security_group = self.get_research_environment_security_group( + server: Server = self.get_server(openstack_id=server_id) + security_group: SecurityGroup = self.get_research_environment_security_group( security_group_name=security_group_name ) + if self._is_security_group_already_added_to_server( + server=server, security_group_name=security_group.name + ): + return self.openstack_connection.compute.add_security_group_to_server( server=server, security_group=security_group ) @@ -1397,6 +1401,21 @@ def add_metadata_to_server(self, server_id, metadata): self.openstack_connection.compute.set_server_metadata(server, **metadata) + def _is_security_group_already_added_to_server( + self, server: Server, security_group_name: str + ): + server_security_groups = self.openstack_connection.list_server_security_groups( + server + ) + + for sg in server_security_groups: + if sg["name"] == security_group_name: + logger.info( + f" Security group with name {security_group_name} already added to server." + ) + return True + return False + def add_udp_security_group(self, server_id): logger.info(f"Setting up UDP security group for {server_id}") server = self.get_server(openstack_id=server_id) @@ -1404,16 +1423,10 @@ def add_udp_security_group(self, server_id): existing_sec = self.openstack_connection.get_security_group(name_or_id=sec_name) if existing_sec: logger.info(f"UDP Security group with name {sec_name} already exists.") - server_security_groups = ( - self.openstack_connection.list_server_security_groups(server) - ) - - for sg in server_security_groups: - if sg["name"] == sec_name: - logger.info( - f"UDP Security group with name {sec_name} already added to server." - ) - return + if self._is_security_group_already_added_to_server( + server=server, security_group_name=sec_name + ): + return self.openstack_connection.compute.add_security_group_to_server( server=server_id, security_group=existing_sec