Skip to content

Commit feb92b6

Browse files
committed
rest api sample
1 parent 42284da commit feb92b6

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from cloudshell.workflow.orchestration.setup.default_setup_orchestrator import DefaultSetupWorkflow
2+
from cloudshell.workflow.orchestration.sandbox import Sandbox
3+
from send_command import send_command
4+
5+
sandbox = Sandbox()
6+
7+
DefaultSetupWorkflow().register(sandbox)
8+
sandbox.workflow.on_configuration_ended(send_command, None)
9+
sandbox.execute_setup()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cloudshell-orch-core>=3.4.0.0,<3.5.0.0
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
from cloudshell.workflow.orchestration.sandbox import Sandbox
2+
from cloudshell.api.cloudshell_api import InputNameValue
3+
4+
5+
LINUX_MODEL = "Linux Server"
6+
COMMAND_STRING = "/root/hello_world.sh"
7+
8+
9+
# ========== Primary Function ==========
10+
def send_command(sandbox, components=None):
11+
"""
12+
Functions passed into orchestration flow MUST have (sandbox, components) signature
13+
:param Sandbox sandbox:
14+
:param components
15+
:return:
16+
"""
17+
api = sandbox.automation_api
18+
res_id = sandbox.id
19+
res_details = api.GetReservationDetails(res_id).ReservationDescription
20+
resources = res_details.Resources
21+
linux_resources = [resource for resource in resources
22+
if resource.ResourceModelName == LINUX_MODEL]
23+
if not linux_resources:
24+
raise Exception("No Linux Server found")
25+
26+
linux_resource = linux_resources[0]
27+
28+
command_inputs = [InputNameValue("command", COMMAND_STRING)]
29+
output = api.ExecuteCommand(reservationId=res_id,
30+
targetName=linux_resource.Name,
31+
targetType="Resource",
32+
commandName="send_custom_command",
33+
commandInputs=command_inputs,
34+
printOutput=True).Output
35+
36+
# validate output
37+
if "hello world" not in output.lower():
38+
raise Exception(f"Command Validation failed. 'hello world' not found.\nOutput received: {output}")

generic-orchestration-samples/setup_call_health_check/__init__.py

Whitespace-only changes.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
TARGET_BLUEPRINT = "<MY_BLUEPRINT_NAME>"
20+
DEPLOYED_SANDBOX_NAME = "My Rest Api Blueprint"
21+
22+
api = SandboxRestApiSession(host=CS_SERVER, username=CS_USER, password=CS_PASSWORD, domain=CS_DOMAIN)
23+
components_response = api.get_sandbox_components(sandbox_id)
24+
print("===== GETTING COMPONENT DETAILS IN SANDBOX =====")
25+
print(f"total components in sandbox: {len(components_response)}")
26+
print(f"components response:\n{json.dumps(components_response, indent=4)}")
27+
28+
linux_server_search = [x for x in components_response if x["component_type"] == "Linux Server"]
29+
if not linux_server_search:
30+
raise ValueError("linux server resource not found")
31+
32+
linux_server = linux_server_search[0]
33+
linux_server_id = linux_server["id"]
34+
35+
print("===== RUNNING COMMAND TO LINUX SERVER =====")
36+
params = [InputParam("command", "ifconfig")]
37+
command_response = api.run_component_command(sandbox_id=sandbox_id,
38+
component_id=linux_server_id,
39+
command_name="send_custom_command",
40+
params=params)
41+
42+
execution_id = command_response["executionId"]
43+
command_completed = False
44+
output = None
45+
while not command_completed:
46+
details = api.get_execution_details(execution_id)
47+
status = details["status"]
48+
if status.lower() in ["completed", "failed"]:
49+
command_completed = True
50+
output = details["output"]
51+
time.sleep(10)
52+
53+
if output:
54+
print(f"command output: {output}")
55+
56+
print("sandbox api commands finished")

0 commit comments

Comments
 (0)