|
| 1 | +from cloudshell.helpers.scripts.cloudshell_scripts_helpers import get_reservation_context_details, get_api_session, \ |
| 2 | + get_resource_context_details |
| 3 | +from cloudshell.api.cloudshell_api import Connector |
| 4 | + |
| 5 | + |
| 6 | +def _get_connected_resource(connector: Connector, root_resource_name: str): |
| 7 | + if connector.Source.startswith(root_resource_name): |
| 8 | + target_side = connector.Source |
| 9 | + elif connector.Target.startswith(root_resource_name): |
| 10 | + target_side = connector.Target |
| 11 | + else: |
| 12 | + raise ValueError(f"root resource {root_resource_name} not present in Source or Target of connector") |
| 13 | + |
| 14 | + connected_port = target_side.split("/")[-1] |
| 15 | + return connected_port |
| 16 | + |
| 17 | + |
| 18 | +def get_port_from_connector_alias(target_alias: str): |
| 19 | + api = get_api_session() |
| 20 | + sb_details = get_reservation_context_details() |
| 21 | + sb_id = sb_details.id |
| 22 | + |
| 23 | + resource_details = get_resource_context_details() |
| 24 | + resource_name = resource_details.name |
| 25 | + |
| 26 | + api.WriteMessageToReservationOutput(reservationId=sb_id, |
| 27 | + message=f"Searching for port connected to alias '{target_alias}'...") |
| 28 | + |
| 29 | + reservation_details = api.GetReservationDetails(reservationId=sb_id, disableCache=True).ReservationDescription |
| 30 | + connectors = reservation_details.Connectors |
| 31 | + target_connector_search = [x for x in connectors if x.Alias == target_alias] |
| 32 | + if not target_connector_search: |
| 33 | + raise ValueError(f"Connector with Alias {target_alias} not found") |
| 34 | + target_connector = target_connector_search[0] |
| 35 | + connected_resource = _get_connected_resource(target_connector, resource_name) |
| 36 | + |
| 37 | + # printing to std_out will be the return output of resource scripts |
| 38 | + print(connected_resource) |
0 commit comments