1+ import re
2+
13import qualipy .scripts .cloudshell_scripts_helpers as helpers
2- from qualipy .api .cloudshell_api import InputNameValue
3- from common .utilites .common_utils import first_or_default
4+ from qualipy .api .cloudshell_api import InputNameValue , AttributeNameValue
5+ from common .utilites .command_result import get_result_from_command_output
46
5- from common .model_factory import ResourceModelParser
7+ from common .logger .service import getLogger
8+ _logger = getLogger ('EnvironmentConnector' )
69
10+ VIRTUAL_NETWORK_ATTRIBUTE = 'Virtual Network'
11+ ACCESS_MODE_ATTRIBUTE = 'Access Mode'
712
8- class EnvironmentConnector (object ):
913
14+ class EnvironmentConnector (object ):
1015 def connect_all (self ):
1116 """
1217 Connects all the VLAN Auto services to all the Deployed Apps in the same Environment
@@ -26,26 +31,39 @@ def connect_all(self):
2631
2732 for vlan_service in vlan_services :
2833
29- if not len (vlan_service .Attributes ):
30- raise ValueError ('No attributes on service {0}' .format (vlan_service .ServiceName ))
34+ _logger .debug ('Connecting \' {0}\' ' .format (vlan_service .ServiceName ))
3135
32- access_mode = self ._get_attribute (vlan_service .Attributes , 'Access Mode' )
33- virtual_network = self ._get_attribute (vlan_service .Attributes , 'Virtual Network' )
36+ access_mode = self ._get_attribute (vlan_service .Attributes , ACCESS_MODE_ATTRIBUTE )
37+ virtual_network = self ._get_attribute (vlan_service .Attributes , VIRTUAL_NETWORK_ATTRIBUTE )
3438
3539 # Get Deployed App connected to VLAN Auto service
3640 connected_resources = self ._get_connected_resources (connectors , vlan_service )
3741
3842 if not connected_resources :
3943 continue
4044
45+ if not virtual_network or virtual_network == '' :
46+
47+ _logger .debug ('Executing Auto Resolve Vlan on \' {0}\' ' .format (vlan_service .ServiceName ))
48+
49+ command_result = session .ExecuteCommand (reservation_id , vlan_service .ServiceName , 'Service' ,
50+ 'Auto Resolve Vlan' , [], True )
51+
52+ virtual_network = get_result_from_command_output (command_result .Output )
53+
54+ _logger .debug ('Auto Resolve Vlan returned Virtual Network \' {0}\' ' .format (virtual_network ))
55+
56+ if not virtual_network or virtual_network == '' :
57+ raise ValueError ('Auto Resolve Vlan command did not return Virtual Network' )
58+
4159 for connected_resource in connected_resources :
4260 self ._execute_connect_command_on_connected_resource (access_mode , connected_resource , reservation_id ,
43- session , virtual_network )
61+ session , virtual_network , vlan_service . ServiceName )
4462
4563 @staticmethod
4664 def _get_attribute (attributes , attribute_name ):
4765 attribute = next (item for item in attributes if item .Name == attribute_name )
48- if not attribute or not attribute . Value :
66+ if not attribute :
4967 raise ValueError ('Attribute {0} is missing' .format (attribute_name ))
5068 return attribute .Value
5169
@@ -57,16 +75,32 @@ def _get_connectors(reservation_details):
5775 @staticmethod
5876 def _get_vlan_auto_services (reservation_details ):
5977 vlan_services = [service for service in reservation_details .ReservationDescription .Services
60- if service . ServiceName == 'VLAN Auto' ]
78+ if VIRTUAL_NETWORK_ATTRIBUTE in [ attr . Name for attr in service . Attributes ] ]
6179 return vlan_services
6280
6381 @staticmethod
6482 def _execute_connect_command_on_connected_resource (access_mode , connected_resource , reservation_id , session ,
65- virtual_network ):
66- session .ExecuteCommand (reservation_id , connected_resource , 'Resource' , 'Connect' ,
67- [InputNameValue ('VLAN_ID' , virtual_network ),
68- InputNameValue ('VLAN_SPEC_TYPE' , access_mode )],
69- True )
83+ virtual_network , vlan_service_name ):
84+
85+ _logger .debug ('Executing Connect command on: ' + connected_resource )
86+
87+ command_result = session .ExecuteCommand (reservation_id , connected_resource , 'Resource' , 'Connect' ,
88+ [InputNameValue ('VLAN_ID' , virtual_network ),
89+ InputNameValue ('VLAN_SPEC_TYPE' , access_mode )], True )
90+
91+ result = get_result_from_command_output (command_result .Output )
92+
93+ if not result :
94+ _logger .debug ('Connect command did not return any result' )
95+ return
96+
97+ mac_address = result .replace ('[\' ' , '' ).replace ('\' ]' , '' )
98+ _logger .debug ('Setting Target Interface to: ' + mac_address )
99+
100+ session .SetConnectorAttributes (reservation_id ,
101+ connected_resource ,
102+ vlan_service_name ,
103+ [AttributeNameValue ('Target Interface' , mac_address )])
70104
71105 @staticmethod
72106 def _get_connected_resources (connectors , vlan_service ):
0 commit comments