1+ import json
2+ import sys
3+ import time
4+
5+ from cloudshell .sandbox_rest .sandbox_api import SandboxRestApiSession , InputParam
6+
7+ if len (sys .argv ) < 2 :
8+ raise ValueError ("Sandbox Id not passed" )
9+
10+ # sandbox ID is injected into script via command line argument
11+ sandbox_id = sys .argv [1 ]
12+
13+ # pull in api user credentials
14+ CS_SERVER = "localhost"
15+ CS_USER = "admin"
16+ CS_PASSWORD = "admin"
17+ CS_DOMAIN = "Global"
18+
19+
20+ api = SandboxRestApiSession (host = CS_SERVER , username = CS_USER , password = CS_PASSWORD , domain = CS_DOMAIN )
21+ components_response = api .get_sandbox_components (sandbox_id )
22+ print ("===== GETTING COMPONENT DETAILS IN SANDBOX =====" )
23+ print (f"total components in sandbox: { len (components_response )} " )
24+ print (f"components response:\n { json .dumps (components_response , indent = 4 )} " )
25+
26+ resource_search = [x for x in components_response if x ["component_type" ] == "Putshell" ]
27+ if not resource_search :
28+ raise ValueError ("target resource not found" )
29+
30+ resource = resource_search [0 ]
31+ resource_id = resource ["id" ]
32+
33+ print ("===== RUNNING HEALTH CHECK TO SANDBOX RESOURCE =====" )
34+ command_response = api .run_component_command (sandbox_id = sandbox_id ,
35+ component_id = resource_id ,
36+ command_name = "health_check" )
37+
38+ execution_id = command_response ["executionId" ]
39+ command_completed = False
40+ output = None
41+ while not command_completed :
42+ details = api .get_execution_details (execution_id )
43+ status = details ["status" ]
44+ if status .lower () in ["completed" , "failed" ]:
45+ command_completed = True
46+ output = details ["output" ]
47+ time .sleep (10 )
48+
49+ if output :
50+ print (f"command output: { output } " )
51+
52+ print ("sandbox api commands finished" )
0 commit comments