Skip to content

Commit 2edc47b

Browse files
committed
connector sample
1 parent e145c0f commit 2edc47b

File tree

5 files changed

+67
-1
lines changed

5 files changed

+67
-1
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ Orchestration-Templates/
66
Bug-exploration/
77
*.pyc
88
credentials.py
9-
cli_credentials.py
9+
cli_credentials.py
10+
__pycache__/
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from port_from_connector import get_port_from_connector_alias
2+
import os
3+
4+
TARGET_ALIAS_PARAM = "target_alias"
5+
6+
7+
target_alias = os.environ.get(TARGET_ALIAS_PARAM)
8+
if not target_alias:
9+
raise ValueError(f"No value received for param '{TARGET_ALIAS_PARAM}'")
10+
11+
get_port_from_connector_alias(target_alias)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from cloudshell.helpers.scripts.cloudshell_dev_helpers import attach_to_cloudshell_as
2+
from port_from_connector import get_port_from_connector_alias
3+
4+
LIVE_SANDBOX_ID = "87df6742-0fd6-43e4-b068-48764b97d7e5"
5+
TARGET_RESOURCE_NAME = "DUT mock 1"
6+
TARGET_ALIAS = "1"
7+
8+
attach_to_cloudshell_as(user="admin",
9+
password="admin",
10+
domain="Global",
11+
reservation_id=LIVE_SANDBOX_ID,
12+
server_address="localhost",
13+
resource_name=TARGET_RESOURCE_NAME)
14+
15+
get_port_from_connector_alias(TARGET_ALIAS)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cloudshell-automation-api

0 commit comments

Comments
 (0)