1 # Copyright 2009 Shikhar Bhushan
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 from ncclient import content
21 "Operations related to configuration editing"
23 class EditConfig(RPC):
28 SPEC = {'tag': 'edit-config', 'subtree': []}
30 def request(self, target=None, target_url=None, config=None,
31 default_operation=None, test_option=None, error_option=None):
32 util.one_of(target, target_url)
33 spec = EditConfig.SPEC.copy()
34 subtree = spec['subtree']
37 'subtree': util.store_or_url(target, target_url, self._assert)
39 subtree.append(content.root_ensured(config, 'config'))
40 if default_operation is not None:
42 'tag': 'default-operation',
43 'text': default_operation
45 if test_option is not None:
46 self._assert(':validate')
51 if error_option is not None:
52 if error_option == 'rollback-on-error':
53 self._assert(':rollback-on-error')
55 'tag': 'error-option',
60 class DeleteConfig(RPC):
65 SPEC = {'tag': 'delete-config', 'subtree': []}
67 def request(self, target=None, target_url=None):
68 spec = DeleteConfig.SPEC.copy()
69 spec['subtree'].append({
71 'subtree': util.store_or_url(target, target_url, self._assert)
73 return self._request(spec)
76 class CopyConfig(RPC):
81 SPEC = {'tag': 'copy-config', 'subtree': []}
83 def request(self, source=None, source_url=None, target=None, target_url=None):
84 spec = CopyConfig.SPEC.copy()
85 spec['subtree'].append({
87 'subtree': util.store_or_url(source, source_url, self._assert)
89 spec['subtree'].append({
91 'subtree': util.store_or_url(target, target_url, self._assert)
93 return self._request(spec)
101 'config attr shd not include <config> root'
103 DEPENDS = [':validate']
105 SPEC = {'tag': 'validate', 'subtree': []}
107 def request(self, source=None, source_url=None, config=None):
108 util.one_of(source, source_url, config)
109 spec = Validate.SPEC.copy()
111 spec['subtree'].append({
113 'subtree': util.store_or_url(source, source_url, self._assert)
116 spec['subtree'].append(content.root_ensured(config, 'config'))
117 return self._request(spec)
125 DEPENDS = [':candidate']
127 SPEC = {'tag': 'commit', 'subtree': []}
129 def _parse_hook(self):
132 def request(self, confirmed=False, timeout=None):
135 self._assert(':confirmed-commit')
136 spec['subtree'].append({'tag': 'confirmed'})
137 if timeout is not None:
138 spec['subtree'].append({
139 'tag': 'confirm-timeout',
142 return self._request(Commit.SPEC)
145 class ConfirmedCommit(Commit):
151 DEPENDS = [':candidate', ':confirmed-commit']
153 def request(self, timeout=None):
154 "Commit changes; requireing that a confirming commit follow"
155 return Commit.request(self, confirmed=True, timeout=timeout)
158 "Make the confirming commit"
159 return Commit.request(self, confirmed=True)
162 class DiscardChanges(RPC):
167 DEPENDS = [':candidate']
169 SPEC = {'tag': 'discard-changes'}