Skip to content

Commit 65885eb

Browse files
committed
Add some argument validation
1 parent 06ef552 commit 65885eb

File tree

1 file changed

+27
-19
lines changed

1 file changed

+27
-19
lines changed

nxos_grpc/client.py

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ def __parse_xpath_to_json(self, xpath, namespace):
181181
"""Parses an XPath to JSON representation, and appends
182182
namespace into the JSON request.
183183
"""
184+
if not namespace:
185+
raise ValueError('Must include namespace if constructing from xpath!')
184186
xpath_dict = {}
185187
xpath_split = xpath.split('/')
186188
first = True
@@ -216,6 +218,13 @@ def __fulfill_request(self, request_method, request_args):
216218
metadata=self.__gen_metadata()
217219
)
218220
)
221+
222+
def __validate_enum_arg(self, name, valid_options, message=None):
223+
r"""Construct error around enumeration validation."""
224+
if name not in valid_options:
225+
if not message:
226+
message = '%s must be one of %s' % (name, ', '.join(valid_options))
227+
raise ValueError(message)
219228

220229
def get_oper(self, yang_path, namespace=None, request_id=0, path_is_payload=False):
221230
r"""Get operational data from device.
@@ -228,7 +237,7 @@ def get_oper(self, yang_path, namespace=None, request_id=0, path_is_payload=Fals
228237
YANG namespace applicable to the specified XPath.
229238
request_id : uint, optional
230239
The request ID to indicate to the device.
231-
path_is_payload : { True, False }, optional
240+
path_is_payload : bool, optional
232241
Indicates that the yang_path parameter contains a preformed JSON
233242
payload and should not be parsed into JSON as an XPath.
234243
@@ -238,8 +247,6 @@ def get_oper(self, yang_path, namespace=None, request_id=0, path_is_payload=Fals
238247
Response wrapper object with ReqID, YangData, and Errors fields.
239248
"""
240249
if not path_is_payload:
241-
if not namespace:
242-
raise Exception('Must include namespace if yang_path is not payload.')
243250
yang_path = self.__parse_xpath_to_json(yang_path, namespace)
244251
request_args = proto.GetOperArgs(ReqID=request_id, YangPath=yang_path)
245252
return self.__fulfill_request(
@@ -258,7 +265,7 @@ def get(self, yang_path, namespace=None, request_id=0, path_is_payload=False):
258265
YANG namespace applicable to the specified XPath.
259266
request_id : uint, optional
260267
The request ID to indicate to the device.
261-
path_is_payload : { True, False }, optional
268+
path_is_payload : bool, optional
262269
Indicates that the yang_path parameter contains a preformed JSON
263270
payload and should not be parsed into JSON as an XPath.
264271
@@ -268,8 +275,6 @@ def get(self, yang_path, namespace=None, request_id=0, path_is_payload=False):
268275
Response wrapper object with ReqID, YangData, and Errors fields.
269276
"""
270277
if not path_is_payload:
271-
if not namespace:
272-
raise Exception('Must include namespace if yangpath is not payload.')
273278
yang_path = self.__parse_xpath_to_json(yang_path, namespace)
274279
request_args = proto.GetArgs(ReqID=request_id, YangPath=yang_path)
275280
return self.__fulfill_request(
@@ -292,7 +297,7 @@ def get_config(self, yang_path, namespace=None, request_id=0,
292297
The request ID to indicate to the device.
293298
source : { 'running', ? }, optional
294299
Source to retrieve configuration from.
295-
path_is_payload : { True, False }, optional
300+
path_is_payload : bool, optional
296301
Indicates that the yang_path parameter contains a preformed JSON
297302
payload and should not be parsed into JSON as an XPath.
298303
@@ -305,14 +310,13 @@ def get_config(self, yang_path, namespace=None, request_id=0,
305310
-----
306311
Need to verify whether source param may be something other than running.
307312
"""
313+
self.__validate_enum_arg(source, {'running'})
308314
if not path_is_payload:
309-
if not namespace:
310-
raise Exception('Must include namespace if yang_path is not payload.')
311315
yang_path = self.__parse_xpath_to_json(yang_path, namespace)
312316
request_args = proto.GetConfigArgs(ReqID=request_id, Source=source, YangPath=yang_path)
313317
return self.__fulfill_request(
314-
self.__client.GetConfig,
315-
request_args
318+
request_method=self.__client.GetConfig,
319+
request_args=request_args
316320
)
317321

318322
def edit_config(self, yang_path,
@@ -344,14 +348,18 @@ def edit_config(self, yang_path,
344348
gRPCResponse
345349
Response wrapper object with ReqID, YangData, and Errors fields.
346350
"""
351+
self.__validate_enum_arg(operation, {'merge', 'create', 'replace', 'delete', 'remove'})
352+
self.__validate_enum_arg(default_operation, {'merge', 'replace', 'none'})
353+
self.__validate_enum_arg(target, {'running'})
354+
self.__validate_enum_arg(error_operation, {'roll-back', 'stop', 'continue'})
347355
request_args = proto.EditConfigArgs(
348356
YangPath=yang_path, Operation=operation, SessionID=session_id,
349357
ReqID=request_id, Target=target, DefOp=default_operation,
350358
ErrorOp=error_operation
351359
)
352360
return self.__fulfill_request(
353-
self.__client.EditConfig,
354-
request_args
361+
request_method=self.__client.EditConfig,
362+
request_args=request_args
355363
)
356364

357365
def start_session(self, request_id=0):
@@ -369,8 +377,8 @@ def start_session(self, request_id=0):
369377
"""
370378
request_args = proto.SessionArgs(ReqID=request_id)
371379
return self.__fulfill_request(
372-
self.__client.StartSession,
373-
request_args
380+
request_method=self.__client.StartSession,
381+
request_args=request_args
374382
)
375383

376384
def close_session(self, session_id, request_id=0):
@@ -390,8 +398,8 @@ def close_session(self, session_id, request_id=0):
390398
"""
391399
request_args = proto.CloseSessionArgs(ReqID=request_id, SessionID=session_id)
392400
return self.__fulfill_request(
393-
self.__client.CloseSession,
394-
request_args
401+
request_method=self.__client.CloseSession,
402+
request_args=request_args
395403
)
396404

397405
def kill_session(self, session_id, session_id_to_kill, request_id=0):
@@ -421,6 +429,6 @@ def kill_session(self, session_id, session_id_to_kill, request_id=0):
421429
SessionIDToKill=session_id_to_kill
422430
)
423431
return self.__fulfill_request(
424-
self.__client.KillSession,
425-
request_args
432+
request_method=self.__client.KillSession,
433+
request_args=request_args
426434
)

0 commit comments

Comments
 (0)