11import json
22from dataclasses import asdict , dataclass
33from typing import List
4+
45import requests
5- from cloudshell .sandbox_rest .exceptions import SandboxRestException , SandboxRestAuthException
6+
7+ from cloudshell .sandbox_rest .exceptions import SandboxRestAuthException , SandboxRestException
68
79
810@dataclass
@@ -22,8 +24,9 @@ class SandboxRestApiSession:
2224 View http://<API_SERVER>/api/v2/explore to see schemas of return json values
2325 """
2426
25- def __init__ (self , host : str , username : str , password = "" , token = "" , domain = "Global" , port = 82 , is_https = False ,
26- api_version = "v2" ):
27+ def __init__ (
28+ self , host : str , username : str , password = "" , token = "" , domain = "Global" , port = 82 , is_https = False , api_version = "v2"
29+ ):
2730 """ login to api and store headers for future requests """
2831 _protocol = "https" if is_https else "http"
2932 self ._base_url = f"{ _protocol } ://{ host } :{ port } /api"
@@ -106,12 +109,12 @@ def delete_token(self, token_id: str) -> None:
106109
107110 # SANDBOX POST REQUESTS
108111 def start_sandbox (
109- self ,
110- blueprint_id : str ,
111- sandbox_name = "" ,
112- duration = "PT2H0M" ,
113- bp_params : List [InputParam ] = None ,
114- permitted_users : List [str ] = None ,
112+ self ,
113+ blueprint_id : str ,
114+ sandbox_name = "" ,
115+ duration = "PT2H0M" ,
116+ bp_params : List [InputParam ] = None ,
117+ permitted_users : List [str ] = None ,
115118 ) -> dict :
116119 """
117120 Create a sandbox from the provided blueprint id
@@ -139,8 +142,7 @@ def start_sandbox(
139142 return response .json ()
140143
141144 def start_persistent_sandbox (
142- self , blueprint_id : str , sandbox_name = "" , bp_params : List [InputParam ] = None ,
143- permitted_users : List [str ] = None
145+ self , blueprint_id : str , sandbox_name = "" , bp_params : List [InputParam ] = None , permitted_users : List [str ] = None
144146 ) -> dict :
145147 """ Create a persistent sandbox from the provided blueprint id """
146148 self ._validate_auth_headers ()
@@ -159,8 +161,9 @@ def start_persistent_sandbox(
159161 raise SandboxRestException (err_msg , response )
160162 return response .json ()
161163
162- def run_sandbox_command (self , sandbox_id : str , command_name : str , params : List [InputParam ] = None ,
163- print_output = True ) -> dict :
164+ def run_sandbox_command (
165+ self , sandbox_id : str , command_name : str , params : List [InputParam ] = None , print_output = True
166+ ) -> dict :
164167 """ Run a sandbox level command """
165168 self ._validate_auth_headers ()
166169 url = f"{ self ._versioned_url } /sandboxes/{ sandbox_id } /commands/{ command_name } /start"
@@ -173,8 +176,7 @@ def run_sandbox_command(self, sandbox_id: str, command_name: str, params: List[I
173176 return response .json ()
174177
175178 def run_component_command (
176- self , sandbox_id : str , component_id : str , command_name : str , params : List [InputParam ] = None ,
177- print_output : bool = True
179+ self , sandbox_id : str , component_id : str , command_name : str , params : List [InputParam ] = None , print_output : bool = True
178180 ) -> dict :
179181 """ Start a command on sandbox component """
180182 self ._validate_auth_headers ()
@@ -231,8 +233,9 @@ def get_sandbox_details(self, sandbox_id: str) -> dict:
231233 raise SandboxRestException (exc_msg , response )
232234 return response .json ()
233235
234- def get_sandbox_activity (self , sandbox_id : str , error_only = False , since = "" , from_event_id : int = None ,
235- tail : int = None ) -> dict :
236+ def get_sandbox_activity (
237+ self , sandbox_id : str , error_only = False , since = "" , from_event_id : int = None , tail : int = None
238+ ) -> dict :
236239 """
237240 Get list of sandbox activity
238241 'since' - format must be a valid 'ISO 8601'. (e.g 'PT23H' or 'PT4H2M')
@@ -298,9 +301,7 @@ def get_sandbox_component_details(self, sandbox_id: str, component_id: str) -> d
298301 )
299302 if not response .ok :
300303 custom_err_msg = (
301- f"Failed to get sandbox component details.\n "
302- f"component id: '{ component_id } '\n "
303- f"sandbox id: '{ sandbox_id } '"
304+ f"Failed to get sandbox component details.\n " f"component id: '{ component_id } '\n " f"sandbox id: '{ sandbox_id } '"
304305 )
305306 raise SandboxRestException (custom_err_msg , response )
306307 return response .json ()
@@ -309,14 +310,12 @@ def get_sandbox_component_commands(self, sandbox_id: str, component_id: str) ->
309310 """ Get list of commands for a particular component in sandbox """
310311 self ._validate_auth_headers ()
311312 response = requests .get (
312- f"{ self ._versioned_url } /sandboxes/{ sandbox_id } /components/{ component_id } /commands" ,
313- headers = self ._auth_headers
313+ f"{ self ._versioned_url } /sandboxes/{ sandbox_id } /components/{ component_id } /commands" , headers = self ._auth_headers
314314 )
315315 if not response .ok :
316316 custom_err_msg = (
317- f"Failed to get component commands.\n "
318- f"component id: '{ component_id } '\n "
319- f"sandbox id: '{ sandbox_id } '" )
317+ f"Failed to get component commands.\n " f"component id: '{ component_id } '\n " f"sandbox id: '{ sandbox_id } '"
318+ )
320319 raise SandboxRestException (custom_err_msg , response )
321320 return response .json ()
322321
@@ -328,24 +327,22 @@ def get_sandbox_component_command_details(self, sandbox_id: str, component_id: s
328327 headers = self ._auth_headers ,
329328 )
330329 if not response .ok :
331- custom_err_msg = (f"Failed to get command details. \n "
332- f"component id: '{ component_id } '\n "
333- f"sandbox id: ' { sandbox_id } '" )
330+ custom_err_msg = (
331+ f"Failed to get command details. \n " f"component id: '{ component_id } '\n " f"sandbox id: ' { sandbox_id } ' "
332+ )
334333 raise SandboxRestException (custom_err_msg , response )
335334 return response .json ()
336335
337336 def get_sandbox_instructions (self , sandbox_id : str ) -> str :
338337 """ pull the instruction text of sandbox """
339338 self ._validate_auth_headers ()
340- response = requests .get (f"{ self ._versioned_url } /sandboxes/{ sandbox_id } /instructions" ,
341- headers = self ._auth_headers )
339+ response = requests .get (f"{ self ._versioned_url } /sandboxes/{ sandbox_id } /instructions" , headers = self ._auth_headers )
342340 if not response .ok :
343341 err_msg = f"Failed to get sandbox instructions for '{ sandbox_id } '"
344342 raise SandboxRestException (err_msg , response )
345343 return response .json ()
346344
347- def get_sandbox_output (self , sandbox_id : str , tail : int = None , from_entry_id : int = None ,
348- since : str = None ) -> dict :
345+ def get_sandbox_output (self , sandbox_id : str , tail : int = None , from_entry_id : int = None , since : str = None ) -> dict :
349346 """ Get list of sandbox output """
350347 self ._validate_auth_headers ()
351348 url = f"{ self ._versioned_url } /sandboxes/{ sandbox_id } /output"
0 commit comments