@@ -5,96 +5,160 @@ class Nebula:
55
66 # the nebula class init module serves as the login against the nebula API as it's the only shared thing among the
77 # class functions
8- def __init__ (self , username , password , host , port = 80 , protocol = "http" , request_timeout = 60 ):
8+ def __init__ (self , username = None , password = None , host = "127.0.0.1" , port = 80 , protocol = "http" , request_timeout = 60 ):
99 self .request_timeout = request_timeout
1010 self .username = username
1111 self .password = password
1212 self .protocol = protocol
13- self .protocol = port
13+ self .port = port
1414 self .host = protocol + "://" + host + ":" + str (port )
15- self .basic_auth = base64 .b64encode (username + ":" + password )
1615 self .headers = {
17- 'authorization' : "Basic " + self .basic_auth ,
1816 'content-type' : "application/json" ,
1917 'cache-control' : "no-cache"
2018 }
19+ if username is not None and password is not None :
20+ self .basic_auth = base64 .b64encode (username + ":" + password )
21+ self .headers ["authorization" ] = "Basic " + self .basic_auth
22+ self .API_VERSION = "v2"
2123
2224 # create a new nebula app, requires the app name to create and a complete dict of the config values for it
2325 def create_app (self , app , config ):
24- url = self .host + "/api/apps/" + app
26+ url = self .host + "/api/" + self . API_VERSION + "/ apps/" + app
2527 payload = json .dumps (config )
2628 headers = self .headers
2729 response = requests .request ("POST" , url , data = payload , headers = headers , timeout = self .request_timeout )
28- return response
30+ filtered_response = {"status_code" : response .status_code , "reply" : response .json ()}
31+ return filtered_response
2932
3033 # delete an existing nebula app, no confirmation required in SDK so be careful
3134 def delete_app (self , app ):
32- url = self .host + "/api/apps/" + app
35+ url = self .host + "/api/" + self . API_VERSION + "/ apps/" + app
3336 headers = self .headers
3437 response = requests .request ("DELETE" , url , headers = headers , timeout = self .request_timeout )
35- return response
38+ filtered_response = {"status_code" : response .status_code , "reply" : response .json ()}
39+ return filtered_response
3640
3741 # list all of the apps managed by nebula
3842 def list_apps (self ):
39- url = self .host + "/api/apps"
43+ url = self .host + "/api/" + self . API_VERSION + "/ apps"
4044 headers = self .headers
4145 response = requests .request ("GET" , url , headers = headers , timeout = self .request_timeout )
42- return response
46+ filtered_response = {"status_code" : response .status_code , "reply" : response .json ()}
47+ return filtered_response
4348
4449 # list the config of a nebula app, only requires the app name
4550 def list_app_info (self , app ):
46- url = self .host + "/api/apps/" + app
51+ url = self .host + "/api/" + self . API_VERSION + "/ apps/" + app
4752 headers = self .headers
4853 response = requests .request ("GET" , url , headers = headers , timeout = self .request_timeout )
49- return response
54+ filtered_response = {"status_code" : response .status_code , "reply" : response .json ()}
55+ return filtered_response
5056
5157 # stop a nebula app, only requires the app name
5258 def stop_app (self , app ):
53- url = self .host + "/api/apps/" + app + "/stop"
59+ url = self .host + "/api/" + self . API_VERSION + "/ apps/" + app + "/stop"
5460 headers = self .headers
5561 response = requests .request ("POST" , url , headers = headers , timeout = self .request_timeout )
56- return response
62+ filtered_response = {"status_code" : response .status_code , "reply" : response .json ()}
63+ return filtered_response
5764
5865 # start a nebula app, only requires the app name
5966 def start_app (self , app ):
60- url = self .host + "/api/apps/" + app + "/start"
67+ url = self .host + "/api/" + self . API_VERSION + "/ apps/" + app + "/start"
6168 headers = self .headers
6269 response = requests .request ("POST" , url , headers = headers , timeout = self .request_timeout )
63- return response
70+ filtered_response = {"status_code" : response .status_code , "reply" : response .json ()}
71+ return filtered_response
6472
6573 # restart a nebula app, only requires the app name
6674 def restart_app (self , app ):
67- url = self .host + "/api/apps/" + app + "/restart"
75+ url = self .host + "/api/" + self . API_VERSION + "/ apps/" + app + "/restart"
6876 headers = self .headers
6977 response = requests .request ("POST" , url , headers = headers , timeout = self .request_timeout )
70- return response
78+ filtered_response = {"status_code" : response .status_code , "reply" : response .json ()}
79+ return filtered_response
7180
7281 # update a nebula app, requires the app name and a dict of the config values you want to change, any combination of
7382 # config values is accepted as it keeps the rest unchanged
7483 def update_app (self , app , config ):
75- url = self .host + "/api/apps/" + app + "/update"
84+ url = self .host + "/api/" + self . API_VERSION + "/ apps/" + app + "/update"
7685 payload = json .dumps (config )
7786 headers = self .headers
7887 response = requests .request ("PUT" , url , data = payload , headers = headers , timeout = self .request_timeout )
79- return response
88+ filtered_response = {"status_code" : response .status_code , "reply" : response .json ()}
89+ return filtered_response
8090
81- # rolling restart an app, only requires the app name
82- def roll_app (self , app ):
83- url = self .host + "/api/apps/ " + app + "/roll "
91+ # prune unused images on all devices
92+ def prune_images (self ):
93+ url = self .host + "/api/" + self . API_VERSION + "/prune "
8494 headers = self .headers
8595 response = requests .request ("POST" , url , headers = headers , timeout = self .request_timeout )
86- return response
96+ filtered_response = {"status_code" : response .status_code , "reply" : response .json ()}
97+ return filtered_response
8798
88- # prune unused images on all devices running app on them
89- def prune_images (self , app ):
90- url = self .host + "/api/apps/ " + app + "/prune"
99+ # prune unused images on all devices that are part of the device_group given
100+ def prune__device_group_images (self , device_groups ):
101+ url = self .host + "/api/" + self . API_VERSION + "/device_groups/ " + device_groups + "/prune"
91102 headers = self .headers
92103 response = requests .request ("POST" , url , headers = headers , timeout = self .request_timeout )
93- return response
104+ filtered_response = {"status_code" : response .status_code , "reply" : response .json ()}
105+ return filtered_response
94106
95107 # check that the contacted api is responding as expected
96108 def check_api (self ):
97- url = self .host + "/api/status"
109+ url = self .host + "/api/" + self . API_VERSION + "/ status"
98110 headers = self .headers
99111 response = requests .request ("GET" , url , headers = headers , timeout = self .request_timeout )
100- return response
112+ filtered_response = {"status_code" : response .status_code , "reply" : response .json ()}
113+ return filtered_response
114+
115+ # list the info of all apps and all data of a given device_group
116+ def list_device_group_info (self , device_group ):
117+ url = self .host + "/api/" + self .API_VERSION + "/device_groups/" + device_group + "/info"
118+ headers = self .headers
119+ response = requests .request ("GET" , url , headers = headers , timeout = self .request_timeout )
120+ filtered_response = {"status_code" : response .status_code , "reply" : response .json ()}
121+ return filtered_response
122+
123+ # list all device_groups
124+ def list_device_groups (self ):
125+ url = self .host + "/api/" + self .API_VERSION + "/device_groups"
126+ headers = self .headers
127+ response = requests .request ("GET" , url , headers = headers , timeout = self .request_timeout )
128+ filtered_response = {"status_code" : response .status_code , "reply" : response .json ()}
129+ return filtered_response
130+
131+ # list device_group configuration
132+ def list_device_group (self , device_group ):
133+ url = self .host + "/api/" + self .API_VERSION + "/device_groups/" + device_group
134+ headers = self .headers
135+ response = requests .request ("GET" , url , headers = headers , timeout = self .request_timeout )
136+ filtered_response = {"status_code" : response .status_code , "reply" : response .json ()}
137+ return filtered_response
138+
139+ # create a new nebula delete_device_group, requires the app name to create and a complete dict of the config values
140+ # for it
141+ def create_device_group (self , device_group , config ):
142+ url = self .host + "/api/" + self .API_VERSION + "/device_groups/" + device_group
143+ payload = json .dumps (config )
144+ headers = self .headers
145+ response = requests .request ("POST" , url , data = payload , headers = headers , timeout = self .request_timeout )
146+ filtered_response = {"status_code" : response .status_code , "reply" : response .json ()}
147+ return filtered_response
148+
149+ # delete an existing nebula delete_device_group, no confirmation required in SDK so be careful
150+ def delete_device_group (self , device_group ):
151+ url = self .host + "/api/" + self .API_VERSION + "/device_groups/" + device_group
152+ headers = self .headers
153+ response = requests .request ("DELETE" , url , headers = headers , timeout = self .request_timeout )
154+ filtered_response = {"status_code" : response .status_code , "reply" : response .json ()}
155+ return filtered_response
156+
157+ # update apps in a device group
158+ def update_device_group (self , device_group , config ):
159+ url = self .host + "/api/" + self .API_VERSION + "/device_groups/" + device_group + "/update"
160+ payload = json .dumps (config )
161+ headers = self .headers
162+ response = requests .request ("POST" , url , data = payload , headers = headers , timeout = self .request_timeout )
163+ filtered_response = {"status_code" : response .status_code , "reply" : response .json ()}
164+ return filtered_response
0 commit comments