1
- #!usr/bin/env python
1
+ #!/ usr/bin/env python
2
2
3
3
description = "A python wrapper for SNIC API utilities. It requires a config file with SNIC credentials."
4
4
@@ -56,24 +56,24 @@ def __init__(self, config=None, params=None):
56
56
if params :
57
57
for _key , _val in params .iteritems ():
58
58
setattr (self , _key , _val )
59
-
59
+
60
60
def create_grus_project (self , proj_data = {}):
61
61
"""Create a GRUS delivery project with given info and return info of created project in JSON"""
62
62
pdata = proj_data or getattr (self , 'proj_data' , {})
63
63
create_proj_url = "{}/ngi_delivery/project/create/" .format (self .api_url )
64
64
response = requests .post (create_proj_url , data = json .dumps (pdata ), auth = self .api_cred )
65
65
self ._assert_response (response )
66
66
return response .json ()
67
-
67
+
68
68
def update_grus_project (self , prj_snic_id = None , updata = {}):
69
69
"""Update a GRUS delivery project with given info and return info of updated project in JSON"""
70
70
psnic = prj_snic_id or getattr (self , 'prj_snic_id' , None )
71
- udata = updata or getattr (self , 'updata' , {})
71
+ udata = updata or getattr (self , 'updata' , {})
72
72
update_proj_url = "{}/ngi_delivery/project/{}/update/" .format (self .api_url , psnic )
73
73
response = requests .post (update_proj_url , data = json .dumps (udata ), auth = self .api_cred )
74
74
self ._assert_response (response )
75
75
return response .json ()
76
-
76
+
77
77
def get_user_info (self , user_email = None ):
78
78
"""Search for user in SNIC and return list with matching hits. Each hits is in JSON format"""
79
79
uemail = user_email or getattr (self , 'user_email' )
@@ -83,7 +83,7 @@ def get_user_info(self, user_email=None):
83
83
if len (user_hits ) == 0 :
84
84
logger .info ("No user found with email {}" .format (uemail ))
85
85
return user_hits
86
-
86
+
87
87
def get_project_info (self , grus_project = None ):
88
88
"""Search for delivery project in SNIC and return list with matching hits. Each hits is in JSON format"""
89
89
gproject = grus_project or getattr (self , 'grus_project' )
@@ -93,7 +93,7 @@ def get_project_info(self, grus_project=None):
93
93
if len (project_hits ) == 0 :
94
94
logger .info ("No projects was found with name {}" .format (gproject ))
95
95
return project_hits
96
-
96
+
97
97
def _search_snic (self , search_url , search_params ):
98
98
response = requests .get (search_url , params = search_params , auth = self .api_cred )
99
99
self ._assert_response (response )
@@ -136,7 +136,7 @@ def _create_project(self):
136
136
'end_date' : endday .strftime (supr_date_format ),
137
137
'ngi_ready' : False ,
138
138
'ngi_sensitive_data' : self .sensitive ,
139
- 'member_ids' : mem_snic_ids }
139
+ 'member_ids' : mem_snic_ids }
140
140
question = ("\n A GRUS delivery project will be created with following information, check and confirm\n \n {}\n \n "
141
141
"NOTE: Sensivity for project is my default set to 'True', it can be change dby calling '--no-sensitive'. "
142
142
"Also parameters '--title / --members / --days' can be used to control the defaults, check help\n \n "
@@ -147,13 +147,13 @@ def _create_project(self):
147
147
logger .info ("Created GRUS delivery project with id '{}'" .format (grus_proj_details ["name" ]))
148
148
else :
149
149
logger .warning ("Project will not be created. EXITING" )
150
-
150
+
151
151
def _extend_project (self ):
152
152
ukey = "end_date"
153
153
endday = datetime .date .today () + datetime .timedelta (days = self .days )
154
154
uval = endday .strftime ('%Y-%m-%d' )
155
155
self ._execute_project_update (ukey = ukey , uval = uval )
156
-
156
+
157
157
def _change_pi (self ):
158
158
ukey = "pi_id"
159
159
try :
@@ -164,7 +164,7 @@ def _change_pi(self):
164
164
raise SystemExit
165
165
uval = pi_snic_id
166
166
self ._execute_project_update (ukey = ukey , uval = uval )
167
-
167
+
168
168
def _change_sensitive (self ):
169
169
ukey = "ngi_sensitive_data"
170
170
uval = self .sensitive
@@ -173,11 +173,11 @@ def _change_sensitive(self):
173
173
def _project_info (self ):
174
174
interested_keys = ["name" , "id" , "title" , "ngi_project_name" , "pi" , "members" , "ngi_sensitive_data" , "start_date" , "end_date" ]
175
175
self ._execute_search (exec_func = self .get_project_info , filter_keys = interested_keys )
176
-
176
+
177
177
def _user_info (self ):
178
178
interested_keys = ["first_name" , "last_name" , "id" , "email" , "department" , "organization" ]
179
179
self ._execute_search (exec_func = self .get_user_info , filter_keys = interested_keys )
180
-
180
+
181
181
def _execute_project_update (self , ukey , uval ):
182
182
try :
183
183
prj_info = self .get_project_info (grus_project = self .grus_project )[0 ]
@@ -193,14 +193,14 @@ def _execute_project_update(self, ukey, uval):
193
193
logger .info ("Updated project {}" .format (self .grus_project ))
194
194
else :
195
195
logger .warning ("Project '{}' will not be updated. EXITING" .format (self .grus_project ))
196
-
196
+
197
197
def _execute_search (self , exec_func , filter_keys = [], all_info = False ):
198
198
all_info = all_info or getattr (self , "all_info" , False )
199
199
search_hits = exec_func ()
200
200
for ind , inf in enumerate (search_hits , 1 ):
201
201
oinf = inf if all_info else OrderedDict ((k , inf .get (k )) for k in filter_keys )
202
202
print "Hit {}:\n {}" .format (ind , json .dumps (oinf , indent = 4 ))
203
-
203
+
204
204
205
205
if __name__ == "__main__" :
206
206
# parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter, description=description)
@@ -239,7 +239,7 @@ def _execute_search(self, exec_func, filter_keys=[], all_info=False):
239
239
subparser_change_sensitive .add_argument ("-g" , "--grus-project" , required = True , type = str , metavar = "" , help = "Grus project id, format should be 'deliveryNNNNN'" )
240
240
subparser_change_sensitive .add_argument ("-s" , "--sensitive" , required = True , type = to_bool , metavar = "" ,
241
241
help = "Choose if the project is sensitive or not, (Only 'yes/no' is allowed)" )
242
-
242
+
243
243
params = vars (parser .parse_args ())
244
244
# try loading config file
245
245
try :
0 commit comments