-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added datastore option check (#252) #268
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -178,6 +178,8 @@ def copy_config(self, provider, target, source, with_defaults_option=None): | |
self.service_logger.info('Executing copy-config RPC') | ||
|
||
rpc = ietf_netconf.CopyConfigRpc() | ||
_validate_datastore_options(source, 'copy-config:source') | ||
_validate_datastore_options(target, 'copy-config:target') | ||
rpc.input.source = _get_rpc_datastore_object(source, rpc.input.source) | ||
rpc.input.target = _get_rpc_datastore_object(target, rpc.input.target) | ||
rpc.input.with_defaults_option = with_defaults_option | ||
|
@@ -205,6 +207,7 @@ def delete_config(self, provider, target): | |
self.service_logger.info('Executing delete-config RPC') | ||
|
||
rpc = ietf_netconf.DeleteConfigRpc() | ||
_validate_datastore_options(target, 'delete-config:target') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we have some sanity tests for this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
rpc.input.target = _get_rpc_datastore_object(target, rpc.input.target) | ||
|
||
return self.executor.execute_rpc(provider, rpc) | ||
|
@@ -267,6 +270,7 @@ def edit_config(self, provider, target, config, default_operation=None, error_op | |
self.service_logger.info('Executing edit-config RPC') | ||
|
||
rpc = ietf_netconf.EditConfigRpc() | ||
_validate_datastore_options(target, 'edit-config:target') | ||
rpc.input.target = _get_rpc_datastore_object(target, rpc.input.target) | ||
rpc.input.config = config | ||
rpc.input.default_operation = default_operation | ||
|
@@ -304,6 +308,7 @@ def get_config(self, provider, source, get_filter, with_defaults_option=None): | |
|
||
rpc = ietf_netconf.GetConfigRpc() | ||
rpc.input.filter = get_filter | ||
_validate_datastore_options(source, 'get-config:source') | ||
rpc.input.source = _get_rpc_datastore_object(source, rpc.input.source) | ||
rpc.input.with_defaults_option = with_defaults_option | ||
|
||
|
@@ -384,6 +389,7 @@ def lock(self, provider, target): | |
self.service_logger.info('Executing lock RPC') | ||
|
||
rpc = ietf_netconf.LockRpc() | ||
_validate_datastore_options(target, 'lock:target') | ||
rpc.input.target = _get_rpc_datastore_object(target, rpc.input.target) | ||
|
||
return self.executor.execute_rpc(provider, rpc) | ||
|
@@ -410,6 +416,7 @@ def unlock(self, provider, target): | |
self.service_logger.info('Executing unlock RPC') | ||
|
||
rpc = ietf_netconf.UnlockRpc() | ||
_validate_datastore_options(target, 'unlock:target') | ||
rpc.input.target = _get_rpc_datastore_object(target, rpc.input.target) | ||
|
||
return self.executor.execute_rpc(provider, rpc) | ||
|
@@ -437,6 +444,7 @@ def validate(self, provider, source=None, config=None): | |
|
||
rpc = ietf_netconf.ValidateRpc() | ||
if source is not None: | ||
_validate_datastore_options(source, 'validate:source') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Applies to all these changes There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. |
||
rpc.input.source = _get_rpc_datastore_object(source, rpc.input.source) | ||
if config is not None: | ||
rpc.input.source.config = config | ||
|
@@ -467,3 +475,37 @@ def payload_convert(payload): | |
rt = etree.fromstring(payload.encode('utf-8')) | ||
chchs = rt.getchildren()[0].getchildren() | ||
return etree.tostring(chchs[0], pretty_print=True, encoding='utf-8').decode('utf-8') | ||
|
||
|
||
def _validate_datastore_options(datastore, option): | ||
res = True | ||
if option == 'copy-config:target': | ||
res = isinstance(datastore, (str, Datastore)) | ||
elif option == 'copy-config:source': | ||
res = isinstance(datastore, (str, Datastore)) | ||
elif option == 'delete-config:target': | ||
res = isinstance(datastore, str) or datastore == Datastore.startup | ||
elif option == 'edit-config:target': | ||
res = datastore in (Datastore.candidate, Datastore.running) | ||
elif option == 'get-config:source': | ||
res = isinstance(datastore, Datastore) | ||
elif option == 'lock:target': | ||
res = isinstance(datastore, Datastore) | ||
elif option == 'unlock:target': | ||
res = isinstance(datastore, Datastore) | ||
elif option == 'validate:source': | ||
res = isinstance(datastore, (str, Datastore)) | ||
|
||
if not res: | ||
err_msg = _get_datastore_errmsg(option, datastore) | ||
raise YPYServiceError(error_msg=err_msg) | ||
|
||
|
||
def _get_datastore_errmsg(option, datastore): | ||
if isinstance(datastore, Datastore): | ||
pass | ||
elif isinstance(datastore, str): | ||
datastore = 'url' | ||
if ':' in option: | ||
option = option[:option.find(':')] | ||
return "%s datastore is not supported by Netconf %s operation" % (datastore, option) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we have some sanity tests for this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure.