22import os .path as osp
33from .future import DSSFuture
44from dataikuapi .utils import DataikuException
5+ import random , string
56
7+ def random_string (length ):
8+ return '' .join (random .choice (string .ascii_letters ) for _ in range (length ))
69
710class DSSApp (object ):
811 """
@@ -13,27 +16,115 @@ def __init__(self, client, app_id):
1316 self .client = client
1417 self .app_id = app_id
1518
16-
1719 ########################################################
1820 # Instances
1921 ########################################################
2022
23+ def create_instance (self , instance_key , instance_name , wait = True ):
24+ """
25+ Creates a new instance of this app. Each instance. must have a globally unique
26+ instance key, separate from any project key across the whole DSS instance
27+
28+ :return:
29+ """
30+ future_resp = self .client ._perform_json (
31+ "POST" , "/apps/%s/instances" % self .app_id , body = {
32+ "targetProjectKey" : instance_key ,
33+ "targetProjectName" : instance_name
34+ })
35+ future = DSSFuture (self .client , future_resp .get ("jobId" , None ), future_resp )
36+ if wait :
37+ result = future .wait_for_result ()
38+ return DSSAppInstance (self .client , instance_key )
39+ else :
40+ return future
41+
42+ def create_temporary_instance (self ):
43+ """
44+ Creates a new temporary instance of this app.
45+ The return value should be used as a Python context manager. Upon exit, the temporary app
46+ instance is deleted
47+ :return a :class:`TemporaryDSSAppInstance`
48+ """
49+ key = "%s_tmp_%s" % (self .app_id , random_string (10 ))
50+ instance = self .create_instance (key , key , True )
51+ return TemporaryDSSAppInstance (self .client , key )
52+
53+ def list_instance_keys (self ):
54+ """
55+ List the existing instances of this app
56+
57+ :return a list of instance keys, each as a string
58+ """
59+ return [x ["projectKey" ] for x in self .list_instances ()]
60+
2161 def list_instances (self ):
2262 """
23- List the instances in this project
63+ List the existing instances of this app
2464
25- Returns:
26- the list of the instances, each one as a JSON object
65+ :rtype: list of dicts
66+ :return a list of instances, each as a dict containing at least a "projectKey" field
2767 """
2868 return self .client ._perform_json (
2969 "GET" , "/apps/%s/instances/" % self .app_id )
3070
71+ def get_instance (self , instance_key ):
72+ return DSSAppInstance (self .client , instance_key )
73+
74+
75+ def get_manifest (self ):
76+ raw_data = self .client ._perform_json ("GET" , "/apps/%s/" % self .app_id )
77+ return DSSAppManifest (self .client , raw_data )
78+
79+
80+ class DSSAppManifest (object ):
3181
32- class DSSDataikuAppInstance (object ):
82+ def __init__ (self , client , raw_data ):
83+ """The manifest for an app. Do not create this class directly"""
84+ self .client = client
85+ self .raw_data = raw_data
86+
87+ def get_raw (self ):
88+ return self .raw_data
89+
90+ def get_all_actions (self ):
91+ return [x for section in self .raw_data ["homepageSections" ] for x in section ["tiles" ]]
92+
93+ def get_runnable_scenarios (self ):
94+ """Return the scenario identifiers that are declared as actions for this app"""
95+ return [x ["scenarioId" ] for x in self .get_all_actions () if x ["type" ] == "SCENARIO_RUN" ]
96+
97+ class DSSAppInstance (object ):
3398
3499 def __init__ (self , client , project_key ):
35100 self .client = client
36101 self .project_key = project_key
37102
38103 def get_as_project (self ):
39- self
104+ """
105+ Get the :class:`dataikuapi.dss.project DSSProject` corresponding to this app instance
106+ """
107+ return self .client .get_project (self .project_key )
108+
109+ def get_manifest (self ):
110+ """
111+ Get the app manifest for this instance, as a :class:`DSSAppManifest`
112+ """
113+ raw_data = self .client ._perform_json ("GET" , "/projects/%s/app-manifest" % self .project_key )
114+ return DSSAppManifest (self .client , raw_data )
115+
116+ class TemporaryDSSAppInstance (DSSAppInstance ):
117+ """internal class"""
118+
119+ def __init__ (self , client , project_key ):
120+ DSSAppInstance .__init__ (self , client ,project_key )
121+
122+
123+ def close (self ):
124+ self .get_as_project ().delete ()
125+
126+ def __enter__ (self ,):
127+ return self
128+
129+ def __exit__ (self , type , value , traceback ):
130+ self .close ()
0 commit comments